<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> from os import curdir <EOL> from os . path import join as opj , abspath , expanduser , expandvars , isdir <EOL> import re <EOL> from . base import Interface <EOL> from datalad . support . param import Parameter <EOL> from datalad . support . constraints import EnsureStr , EnsureNone <EOL> from datalad . support . collectionrepo import CollectionRepo <EOL> from datalad . support . collection_backends import CollectionRepoBackend <EOL> from datalad . support . collection import Collection <EOL> from datalad . cmd import CommandError <EOL> from datalad . log import lgr <EOL> from datalad . cmdline . helpers import get_datalad_master <EOL> class RegisterCollection ( Interface ) : <EOL> """<STR_LIT>""" <EOL> _params_ = dict ( <EOL> url = Parameter ( <EOL> doc = "<STR_LIT>" , <EOL> constraints = EnsureStr ( ) ) , <EOL> name = Parameter ( <EOL> args = ( '<STR_LIT:name>' , ) , <EOL> nargs = '<STR_LIT:?>' , <EOL> doc = "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> constraints = EnsureStr ( ) | EnsureNone ( ) ) ) <EOL> def __call__ ( self , url , name = None ) : <EOL> """<STR_LIT>""" <EOL> local_master = get_datalad_master ( ) <EOL> if isdir ( abspath ( expandvars ( expanduser ( url ) ) ) ) : <EOL> url = abspath ( expandvars ( expanduser ( url ) ) ) <EOL> repo = CollectionRepo ( url , create = False ) <EOL> if name is None : <EOL> name = repo . name <EOL> else : <EOL> url += '<STR_LIT:/>' if not url . endswith ( '<STR_LIT:/>' ) else '<STR_LIT>' <EOL> url_completions = [ url , <EOL> url + '<STR_LIT>' , <EOL> url + url . rstrip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] + <EOL> '<STR_LIT>' ] <EOL> url_ok = False <EOL> for address in url_completions : <EOL> try : <EOL> local_master . git_ls_remote ( address , "<STR_LIT>" ) <EOL> url = address <EOL> url_ok = True <EOL> break <EOL> except CommandError as e : <EOL> if re . match ( "<STR_LIT>" % url , e . stderr ) : <EOL> continue <EOL> else : <EOL> lgr . error ( "<STR_LIT>" % e ) <EOL> return <EOL> if not url_ok : <EOL> lgr . error ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return <EOL> if name is None : <EOL> parts = url . rstrip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' ) <EOL> if parts [ - <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> name = parts [ - <NUM_LIT:2> ] <EOL> elif parts [ - <NUM_LIT:1> ] . endswith ( '<STR_LIT>' ) : <EOL> name = parts [ - <NUM_LIT:1> ] [ <NUM_LIT:0> : - <NUM_LIT:4> ] <EOL> elif parts [ - <NUM_LIT:1> ] . endswith ( '<STR_LIT>' ) : <EOL> name = parts [ - <NUM_LIT:1> ] [ <NUM_LIT:0> : - <NUM_LIT> ] <EOL> else : <EOL> name = parts [ - <NUM_LIT:1> ] <EOL> local_master . git_remote_add ( name , url ) <EOL> local_master . git_fetch ( name ) <EOL> if not self . cmdline : <EOL> return CollectionRepoBackend ( local_master , name + "<STR_LIT>" ) </s>
