<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> import errno <EOL> import os <EOL> import sys <EOL> from os . path import exists , join as opj , realpath , dirname , lexists <EOL> from traceback import format_exc <EOL> from six . moves import range <EOL> from six . moves . urllib . parse import urlparse , urlunparse <EOL> from . . cmd import Runner <EOL> from . . support . exceptions import CommandError <EOL> from . . support . protocol import ProtocolInterface <EOL> from . . support . annexrepo import AnnexRepo <EOL> from . . support . cache import DictCache <EOL> from . . cmdline . helpers import get_repo_instance <EOL> import logging <EOL> lgr = logging . getLogger ( '<STR_LIT>' ) <EOL> URI_PREFIX = "<STR_LIT>" <EOL> SUPPORTED_PROTOCOL = <NUM_LIT:1> <EOL> DEFAULT_COST = <NUM_LIT:100> <EOL> DEFAULT_AVAILABILITY = "<STR_LIT>" <EOL> class AnnexRemoteQuit ( Exception ) : <EOL> pass <EOL> class AnnexExchangeProtocol ( ProtocolInterface ) : <EOL> """<STR_LIT>""" <EOL> HEADER = r"""<STR_LIT>""" <EOL> def __init__ ( self , repopath , custom_remote_name = None ) : <EOL> super ( AnnexExchangeProtocol , self ) . __init__ ( ) <EOL> self . repopath = repopath <EOL> self . custom_remote_name = custom_remote_name <EOL> self . _file = None <EOL> self . _initiated = False <EOL> def initiate ( self ) : <EOL> if self . _initiated : <EOL> return <EOL> self . _initiated = True <EOL> d = opj ( self . repopath , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not exists ( d ) : <EOL> os . makedirs ( d ) <EOL> suf = '<STR_LIT:->' + self . custom_remote_name . rstrip ( '<STR_LIT::>' ) if self . custom_remote_name else '<STR_LIT>' <EOL> self . _file = _file = opj ( d , '<STR_LIT>' + suf ) <EOL> if exists ( _file ) : <EOL> lgr . debug ( "<STR_LIT>" ) <EOL> with open ( _file ) as f : <EOL> entries = f . readlines ( ) <EOL> for i in range ( len ( self . HEADER . split ( os . linesep ) ) , len ( entries ) ) : <EOL> e = entries [ i ] <EOL> if e . startswith ( '<STR_LIT>' ) or e . startswith ( '<STR_LIT>' ) : <EOL> entries [ i ] = '<STR_LIT:#>' + e <EOL> with open ( _file , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' . join ( entries ) ) <EOL> return <EOL> lgr . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( realpath ( self . repopath ) , _file [ len ( self . repopath ) + <NUM_LIT:1> : ] ) ) <EOL> with open ( _file , '<STR_LIT:a>' ) as f : <EOL> f . write ( self . HEADER ) <EOL> os . chmod ( _file , <NUM_LIT> ) <EOL> def write_section ( self , cmd ) : <EOL> self . initiate ( ) <EOL> with open ( self . _file , '<STR_LIT:a>' ) as f : <EOL> f . write ( '<STR_LIT>' % ( os . linesep , cmd , os . linesep ) ) <EOL> lgr . info ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( realpath ( self . repopath ) , <EOL> dirname ( self . _file ) , <EOL> cmd ) ) <EOL> def write_entries ( self , entries ) : <EOL> self . initiate ( ) <EOL> with open ( self . _file , '<STR_LIT:a>' ) as f : <EOL> f . write ( os . linesep . join ( entries + [ '<STR_LIT>' ] ) ) <EOL> def __iadd__ ( self , entry ) : <EOL> self . initiate ( ) <EOL> with open ( self . _file , '<STR_LIT:a>' ) as f : <EOL> f . write ( entry + os . linesep ) <EOL> return self <EOL> def start_section ( self , cmd ) : <EOL> self . _sections . append ( { '<STR_LIT>' : cmd } ) <EOL> self . write_section ( cmd ) <EOL> return len ( self . _sections ) - <NUM_LIT:1> <EOL> def end_section ( self , id_ , exception ) : <EOL> self . _sections . __getitem__ ( id_ ) <EOL> def add_section ( self , cmd , exception ) : <EOL> self . start_section ( cmd ) <EOL> @ property <EOL> def records_callables ( self ) : <EOL> return False <EOL> @ property <EOL> def records_ext_commands ( self ) : <EOL> return True <EOL> @ property <EOL> def do_execute_ext_commands ( self ) : <EOL> return True <EOL> @ property <EOL> def do_execute_callables ( self ) : <EOL> return True <EOL> class AnnexCustomRemote ( object ) : <EOL> """<STR_LIT>""" <EOL> CUSTOM_REMOTE_NAME = None <EOL> AVAILABILITY = DEFAULT_AVAILABILITY <EOL> def __init__ ( self , path = None , cost = DEFAULT_COST ) : <EOL> """<STR_LIT>""" <EOL> self . runner = Runner ( ) <EOL> self . fin = sys . stdin <EOL> self . fout = sys . stdout <EOL> self . repo = get_repo_instance ( class_ = AnnexRepo ) if not path else AnnexRepo ( path , create = False , init = False ) <EOL> self . path = self . repo . path <EOL> self . _progress = <NUM_LIT:0> <EOL> self . cost = cost <EOL> assert ( self . AVAILABILITY . upper ( ) in ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . _in_the_loop = False <EOL> self . _protocol = AnnexExchangeProtocol ( self . path , self . CUSTOM_REMOTE_NAME ) if os . environ . get ( '<STR_LIT>' ) else None <EOL> self . _contentlocations = DictCache ( size_limit = <NUM_LIT:100> ) <EOL> @ classmethod <EOL> def _get_custom_scheme ( cls , prefix ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( URI_PREFIX , prefix ) <EOL> def get_contentlocation ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if key not in self . _contentlocations : <EOL> fpath = self . repo . get_contentlocation ( key , batch = True ) <EOL> if fpath : <EOL> self . _contentlocations [ key ] = fpath <EOL> else : <EOL> fpath = self . _contentlocations [ key ] <EOL> if not lexists ( opj ( self . path , fpath ) ) : <EOL> del self . _contentlocations [ key ] <EOL> fpath = '<STR_LIT>' <EOL> return fpath <EOL> def send ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> msg = "<STR_LIT:U+0020>" . join ( map ( str , args ) ) <EOL> if not self . _in_the_loop : <EOL> lgr . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" % msg . encode ( ) ) <EOL> return <EOL> try : <EOL> self . heavydebug ( "<STR_LIT>" % msg ) <EOL> self . fout . write ( msg + "<STR_LIT:\n>" ) <EOL> self . fout . flush ( ) <EOL> if self . _protocol is not None : <EOL> self . _protocol += "<STR_LIT>" % msg <EOL> except IOError as exc : <EOL> lgr . debug ( "<STR_LIT>" % str ( exc ) ) <EOL> if exc . errno == errno . EPIPE : <EOL> self . stop ( ) <EOL> else : <EOL> raise exc <EOL> def send_unsupported ( self ) : <EOL> self . send ( "<STR_LIT>" ) <EOL> def read ( self , req = None , n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> l = self . fin . readline ( ) . rstrip ( os . linesep ) <EOL> if self . _protocol is not None : <EOL> self . _protocol += "<STR_LIT>" % l <EOL> msg = l . split ( None , n ) <EOL> if req and ( req != msg [ <NUM_LIT:0> ] ) : <EOL> self . error ( "<STR_LIT>" % ( resp , msg [ <NUM_LIT:0> ] ) ) <EOL> return None <EOL> self . heavydebug ( "<STR_LIT>" % ( msg , ) ) <EOL> return msg <EOL> def heavydebug ( self , msg ) : <EOL> lgr . log ( <NUM_LIT:4> , msg ) <EOL> def debug ( self , msg ) : <EOL> lgr . debug ( msg ) <EOL> self . send ( "<STR_LIT>" , msg ) <EOL> def error ( self , msg , annex_err = "<STR_LIT>" ) : <EOL> lgr . error ( msg ) <EOL> self . send ( annex_err , msg ) <EOL> def progress ( self , perc ) : <EOL> perc = int ( perc ) <EOL> if self . _progress != perc : <EOL> self . send ( "<STR_LIT>" , perc ) <EOL> def main ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _in_the_loop = True <EOL> self . _loop ( ) <EOL> except AnnexRemoteQuit : <EOL> pass <EOL> except KeyboardInterrupt : <EOL> self . stop ( "<STR_LIT>" ) <EOL> except Exception as e : <EOL> self . stop ( str ( e ) ) <EOL> finally : <EOL> self . _in_the_loop = False <EOL> def stop ( self , msg = None ) : <EOL> lgr . info ( "<STR_LIT>" % <EOL> ( self , "<STR_LIT>" % msg if msg else "<STR_LIT>" ) ) <EOL> raise AnnexRemoteQuit ( msg ) <EOL> def _loop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . send ( "<STR_LIT>" , SUPPORTED_PROTOCOL ) <EOL> while True : <EOL> l = self . read ( n = - <NUM_LIT:1> ) <EOL> if l is not None and not l : <EOL> self . stop ( ) <EOL> return <EOL> req , req_load = l [ <NUM_LIT:0> ] , l [ <NUM_LIT:1> : ] <EOL> method = getattr ( self , "<STR_LIT>" % req , None ) <EOL> if not method : <EOL> self . error ( "<STR_LIT>" <EOL> % ( req , l ) ) <EOL> self . send ( "<STR_LIT>" ) <EOL> continue <EOL> try : <EOL> method ( * req_load ) <EOL> except Exception as e : <EOL> self . error ( "<STR_LIT>" <EOL> % ( req , req_load , e ) ) <EOL> lgr . error ( "<STR_LIT>" % format_exc ( ) ) <EOL> def req_INITREMOTE ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _initremote ( * args ) <EOL> except Exception as e : <EOL> self . error ( "<STR_LIT>" % ( self , e ) , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . send ( "<STR_LIT>" ) <EOL> def req_PREPARE ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _prepare ( * args ) <EOL> except Exception as e : <EOL> self . error ( "<STR_LIT>" % ( self , e ) , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . send ( "<STR_LIT>" ) <EOL> def req_GETCOST ( self ) : <EOL> self . send ( "<STR_LIT>" , self . cost ) <EOL> def req_GETAVAILABILITY ( self ) : <EOL> self . send ( "<STR_LIT>" , self . AVAILABILITY . upper ( ) ) <EOL> def req_CLAIMURL ( self , url ) : <EOL> scheme = urlparse ( url ) . scheme <EOL> if scheme in self . SUPPORTED_SCHEMES : <EOL> self . debug ( "<STR_LIT>" % url ) <EOL> self . send ( "<STR_LIT>" ) <EOL> else : <EOL> self . debug ( "<STR_LIT>" % url ) <EOL> self . send ( "<STR_LIT>" ) <EOL> def req_TRANSFER ( self , cmd , key , file ) : <EOL> if cmd in ( "<STR_LIT>" , ) : <EOL> lgr . info ( "<STR_LIT>" % ( cmd , key , file ) ) <EOL> self . _transfer ( cmd , key , file ) <EOL> else : <EOL> self . error ( "<STR_LIT>" % cmd ) <EOL> self . send_unsupported ( ) <EOL> def req_CHECKURL ( self , url ) : <EOL> """<STR_LIT>""" <EOL> self . send_unsupported ( ) <EOL> def req_CHECKPRESENT ( self , key ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def req_REMOVE ( self , key ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def req_WHEREIS ( self , key ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def _transfer ( self , cmd , key , file ) : <EOL> raise NotImplementedError ( ) <EOL> def _initremote ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _prepare ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_DIRHASH ( self , key , full = False ) : <EOL> """<STR_LIT>""" <EOL> self . send ( "<STR_LIT>" , key ) <EOL> val = self . read ( "<STR_LIT>" , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> if full : <EOL> return opj ( self . path , val , key ) <EOL> else : <EOL> return val <EOL> def get_URLS ( self , key ) : <EOL> """<STR_LIT>""" <EOL> urls = [ ] <EOL> for scheme in self . SUPPORTED_SCHEMES : <EOL> scheme_ = scheme + "<STR_LIT::>" <EOL> self . send ( "<STR_LIT>" , key , scheme_ ) <EOL> while True : <EOL> url = self . read ( "<STR_LIT>" , <NUM_LIT:1> ) [ <NUM_LIT:1> : ] <EOL> if url : <EOL> assert ( len ( url ) == <NUM_LIT:1> ) <EOL> urls . append ( url [ <NUM_LIT:0> ] ) <EOL> else : <EOL> break <EOL> self . heavydebug ( "<STR_LIT>" % urls ) <EOL> if not urls : <EOL> raise ValueError ( "<STR_LIT>" % key ) <EOL> return urls <EOL> def _get_key_path ( self , key ) : <EOL> """<STR_LIT>""" <EOL> ( out , err ) = self . runner ( [ '<STR_LIT>' , '<STR_LIT>' , key ] , cwd = self . path ) <EOL> return opj ( self . path , out . rstrip ( os . linesep ) ) </s>
