<s> """<STR_LIT>""" <EOL> import sys <EOL> import errno <EOL> try : <EOL> ESKY_CONTROL_DIR <EOL> except NameError : <EOL> ESKY_CONTROL_DIR = "<STR_LIT>" <EOL> try : <EOL> ESKY_APPDATA_DIR <EOL> except NameError : <EOL> ESKY_APPDATA_DIR = "<STR_LIT>" <EOL> try : <EOL> __esky_name__ <EOL> except NameError : <EOL> __esky_name__ = "<STR_LIT>" <EOL> try : <EOL> __rpython__ <EOL> except NameError : <EOL> __rpython__ = False <EOL> _exit_code = [ <NUM_LIT:0> ] <EOL> if "<STR_LIT>" in sys . builtin_module_names : <EOL> import fcntl <EOL> from posix import listdir , stat , unlink , rename , execv , getcwd , environ <EOL> from posix import open as os_open <EOL> from posix import close as os_close <EOL> SEP = "<STR_LIT:/>" <EOL> def isabs ( path ) : <EOL> return ( path . startswith ( SEP ) ) <EOL> def abspath ( path ) : <EOL> path = pathjoin ( getcwd ( ) , path ) <EOL> components_in = path . split ( SEP ) <EOL> components = [ components_in [ <NUM_LIT:0> ] ] <EOL> for comp in components_in [ <NUM_LIT:1> : ] : <EOL> if not comp or comp == "<STR_LIT:.>" : <EOL> pass <EOL> elif comp == "<STR_LIT:..>" : <EOL> components . pop ( ) <EOL> else : <EOL> components . append ( comp ) <EOL> return SEP . join ( components ) <EOL> elif "<STR_LIT>" in sys . builtin_module_names : <EOL> fcntl = None <EOL> import nt <EOL> from nt import listdir , stat , unlink , rename , spawnv <EOL> from nt import getcwd , P_WAIT , environ <EOL> from nt import open as os_open <EOL> from nt import close as os_close <EOL> SEP = "<STR_LIT:\\>" <EOL> def isabs ( path ) : <EOL> if path . startswith ( SEP ) : <EOL> return True <EOL> if len ( path ) >= <NUM_LIT:2> : <EOL> if path [ <NUM_LIT:0> ] . isalpha ( ) and path [ <NUM_LIT:1> ] == "<STR_LIT::>" : <EOL> return True <EOL> return False <EOL> def abspath ( path ) : <EOL> path = pathjoin ( getcwd ( ) , path ) <EOL> components_in = path . split ( SEP ) <EOL> components = [ components_in [ <NUM_LIT:0> ] ] <EOL> for comp in components_in [ <NUM_LIT:1> : ] : <EOL> if not comp or comp == "<STR_LIT:.>" : <EOL> pass <EOL> elif comp == "<STR_LIT:..>" : <EOL> components . pop ( ) <EOL> else : <EOL> components . append ( comp ) <EOL> if path . startswith ( SEP + SEP ) : <EOL> components . insert ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> return SEP . join ( components ) <EOL> def execv ( filename , args ) : <EOL> try : <EOL> tdir = environ [ "<STR_LIT>" ] <EOL> except : <EOL> tdir = None <EOL> if tdir : <EOL> try : <EOL> nt . mkdir ( pathjoin ( tdir , "<STR_LIT>" ) , <NUM_LIT:0> <NUM_LIT> ) <EOL> except EnvironmentError : <EOL> pass <EOL> if exists ( pathjoin ( tdir , "<STR_LIT>" ) ) : <EOL> flags = nt . O_CREAT | nt . O_EXCL | nt . O_TEMPORARY | nt . O_NOINHERIT <EOL> for i in xrange ( <NUM_LIT:10> ) : <EOL> tfilenm = "<STR_LIT>" % ( nt . getpid ( ) , i , ) <EOL> tfilenm = pathjoin ( tdir , "<STR_LIT>" , tfilenm ) <EOL> try : <EOL> os_open ( tfilenm , flags , <NUM_LIT:0> <NUM_LIT> ) <EOL> args . insert ( <NUM_LIT:1> , tfilenm ) <EOL> args . insert ( <NUM_LIT:1> , "<STR_LIT>" ) <EOL> break <EOL> except EnvironmentError : <EOL> pass <EOL> for i , arg in enumerate ( args ) : <EOL> if arg [ <NUM_LIT:0> ] != "<STR_LIT>" and args [ - <NUM_LIT:1> ] != "<STR_LIT>" : <EOL> args [ i ] = "<STR_LIT>" . format ( arg ) <EOL> res = spawnv ( P_WAIT , filename , args ) <EOL> _exit_code [ <NUM_LIT:0> ] = res <EOL> raise SystemExit ( res ) <EOL> class fcntl : <EOL> LOCK_SH = <NUM_LIT:0> <EOL> def flock ( self , fd , mode ) : <EOL> pass <EOL> def __nonzero__ ( self ) : <EOL> return False <EOL> fcntl = fcntl ( ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" + sys . platform ) <EOL> if __rpython__ : <EOL> from os import environ <EOL> _sys = sys <EOL> class sys : <EOL> platform = _sys . platform <EOL> executable = _sys . executable <EOL> argv = _sys . argv <EOL> version_info = _sys . version_info <EOL> modules = { } <EOL> builtin_module_names = _sys . builtin_module_names <EOL> def exit ( self , code ) : <EOL> _exit_code [ <NUM_LIT:0> ] = code <EOL> raise SystemExit ( code ) <EOL> def exc_info ( self ) : <EOL> return None , None , None <EOL> sys = sys ( ) <EOL> sys . modules [ "<STR_LIT>" ] = sys <EOL> def _list_gt ( l1 , l2 ) : <EOL> i = <NUM_LIT:0> <EOL> while i < len ( l1 ) and i < len ( l2 ) : <EOL> if l1 [ i ] > l2 [ i ] : <EOL> return True <EOL> if l1 [ i ] < l2 [ i ] : <EOL> return False <EOL> i += <NUM_LIT:1> <EOL> if len ( l1 ) > len ( l2 ) : <EOL> return True <EOL> return False <EOL> def sorted ( lst , reverse = False ) : <EOL> slst = [ ] <EOL> if reverse : <EOL> for item in lst : <EOL> for j in xrange ( len ( slst ) ) : <EOL> if not _list_gt ( slst [ j ] [ <NUM_LIT:0> ] , item [ <NUM_LIT:0> ] ) : <EOL> slst . insert ( j , item ) <EOL> break <EOL> else : <EOL> slst . append ( item ) <EOL> else : <EOL> for item in lst : <EOL> for j in xrange ( len ( slst ) ) : <EOL> if _list_gt ( slst [ j ] [ <NUM_LIT:0> ] , item [ <NUM_LIT:0> ] ) : <EOL> slst . insert ( j , item ) <EOL> break <EOL> else : <EOL> slst . append ( item ) <EOL> return slst <EOL> def zfill ( str , n ) : <EOL> while len ( str ) < n : <EOL> str = "<STR_LIT:0>" + str <EOL> return str <EOL> def isalnum ( str ) : <EOL> for c in str : <EOL> if not c . isalnum ( ) : <EOL> return False <EOL> return True <EOL> if fcntl : <EOL> class fcntl : <EOL> LOCK_SH = fcntl . LOCK_SH <EOL> def flock ( self , fd , mode ) : <EOL> pass <EOL> fcntl = fcntl ( ) <EOL> else : <EOL> def zfill ( str , n ) : <EOL> return str . zfill ( n ) <EOL> def isalnum ( str ) : <EOL> return str . isalnum ( ) <EOL> def pathjoin ( * args ) : <EOL> """<STR_LIT>""" <EOL> path = args [ <NUM_LIT:0> ] <EOL> for arg in list ( args [ <NUM_LIT:1> : ] ) : <EOL> if isabs ( arg ) : <EOL> path = arg <EOL> else : <EOL> while path . endswith ( SEP ) : <EOL> path = path [ : - <NUM_LIT:1> ] <EOL> path = path + SEP + arg <EOL> return path <EOL> def basename ( p ) : <EOL> """<STR_LIT>""" <EOL> return p . split ( SEP ) [ - <NUM_LIT:1> ] <EOL> def dirname ( p ) : <EOL> """<STR_LIT>""" <EOL> return SEP . join ( p . split ( SEP ) [ : - <NUM_LIT:1> ] ) <EOL> def exists ( path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> stat ( path ) <EOL> except EnvironmentError , e : <EOL> if not __rpython__ : <EOL> if e . errno not in ( errno . ENOENT , errno . ENOTDIR , errno . ESRCH , ) : <EOL> raise <EOL> return False <EOL> else : <EOL> return True <EOL> def appdir_from_executable ( exepath ) : <EOL> """<STR_LIT>""" <EOL> appdir = dirname ( exepath ) <EOL> if sys . platform == "<STR_LIT>" and basename ( appdir ) == "<STR_LIT>" : <EOL> appdir = dirname ( appdir ) <EOL> if basename ( appdir ) == "<STR_LIT>" : <EOL> appdir = dirname ( appdir ) <EOL> else : <EOL> appdir = dirname ( exepath ) <EOL> return appdir <EOL> def bootstrap ( ) : <EOL> """<STR_LIT>""" <EOL> sys . executable = abspath ( sys . executable ) <EOL> appdir = appdir_from_executable ( sys . executable ) <EOL> vsdir = pathjoin ( appdir , ESKY_APPDATA_DIR ) <EOL> best_version = None <EOL> try : <EOL> if __esky_name__ : <EOL> best_version = get_best_version ( vsdir , appname = __esky_name__ ) <EOL> if best_version is None : <EOL> best_version = get_best_version ( vsdir ) <EOL> if best_version is None : <EOL> if exists ( vsdir ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> else : <EOL> raise EnvironmentError <EOL> except EnvironmentError : <EOL> if exists ( vsdir ) : <EOL> raise <EOL> vsdir = appdir <EOL> if __esky_name__ : <EOL> best_version = get_best_version ( vsdir , appname = __esky_name__ ) <EOL> if best_version is None : <EOL> best_version = get_best_version ( vsdir ) <EOL> if best_version is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return chainload ( pathjoin ( vsdir , best_version ) ) <EOL> def chainload ( target_dir ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> lock_version_dir ( target_dir ) <EOL> except EnvironmentError : <EOL> if exists ( dirname ( target_dir ) ) : <EOL> bsfile = pathjoin ( target_dir , ESKY_CONTROL_DIR ) <EOL> bsfile = pathjoin ( bsfile , "<STR_LIT>" ) <EOL> if not exists ( bsfile ) : <EOL> execv ( sys . executable , list ( sys . argv ) ) <EOL> return <EOL> raise <EOL> else : <EOL> _chainload ( target_dir ) <EOL> def get_exe_locations ( target_dir ) : <EOL> """<STR_LIT>""" <EOL> locs = [ ] <EOL> appdir = appdir_from_executable ( sys . executable ) <EOL> if sys . platform == "<STR_LIT>" : <EOL> if basename ( dirname ( sys . executable ) ) == "<STR_LIT>" : <EOL> if __esky_name__ : <EOL> locs . append ( pathjoin ( target_dir , <EOL> __esky_name__ + "<STR_LIT>" , <EOL> sys . executable [ len ( appdir ) + <NUM_LIT:1> : ] ) ) <EOL> else : <EOL> for nm in listdir ( target_dir ) : <EOL> if nm . endswith ( "<STR_LIT>" ) : <EOL> locs . append ( pathjoin ( target_dir , <EOL> nm , <EOL> sys . executable [ len ( appdir ) + <NUM_LIT:1> : ] ) ) <EOL> locs . append ( target_dir + sys . executable [ len ( appdir ) : ] ) <EOL> orig_exe = get_original_filename ( sys . executable ) <EOL> if orig_exe is not None : <EOL> locs . append ( target_dir + orig_exe [ len ( appdir ) : ] ) <EOL> return locs <EOL> def verify ( target_file ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _chainload ( target_dir ) : <EOL> """<STR_LIT>""" <EOL> exc_type , exc_value , traceback = None , None , None <EOL> for target_exe in get_exe_locations ( target_dir ) : <EOL> verify ( target_exe ) <EOL> try : <EOL> execv ( target_exe , [ target_exe ] + sys . argv [ <NUM_LIT:1> : ] ) <EOL> return <EOL> except EnvironmentError , exc_value : <EOL> exc_type , _ , traceback = sys . exc_info ( ) <EOL> if not __rpython__ : <EOL> if exc_value . errno != errno . ENOENT : <EOL> raise <EOL> else : <EOL> if exists ( target_exe ) : <EOL> raise <EOL> else : <EOL> if exc_value is not None : <EOL> if exc_type is not None : <EOL> raise exc_type , exc_value , traceback <EOL> else : <EOL> raise exc_value <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def get_best_version ( appdir , include_partial_installs = False , appname = None ) : <EOL> """<STR_LIT>""" <EOL> candidates = [ ] <EOL> for nm in listdir ( appdir ) : <EOL> ( appnm , ver , platform ) = split_app_version ( nm ) <EOL> if ver and platform : <EOL> if appname is not None and appnm != appname : <EOL> continue <EOL> if is_version_dir ( pathjoin ( appdir , nm ) ) : <EOL> ver = parse_version ( ver ) <EOL> candidates . append ( ( ver , nm ) ) <EOL> candidates = [ c [ <NUM_LIT:1> ] for c in sorted ( candidates , reverse = True ) ] <EOL> if not candidates : <EOL> return None <EOL> if len ( candidates ) == <NUM_LIT:1> : <EOL> return candidates [ <NUM_LIT:0> ] <EOL> if include_partial_installs : <EOL> return candidates [ <NUM_LIT:0> ] <EOL> while candidates : <EOL> nm = candidates . pop ( <NUM_LIT:0> ) <EOL> if is_installed_version_dir ( pathjoin ( appdir , nm ) ) : <EOL> return nm <EOL> return None <EOL> def get_all_versions ( appdir , include_partial_installs = False ) : <EOL> """<STR_LIT>""" <EOL> candidates = [ ] <EOL> for nm in listdir ( appdir ) : <EOL> ( _ , ver , platform ) = split_app_version ( nm ) <EOL> if ver and platform : <EOL> if is_version_dir ( pathjoin ( appdir , nm ) ) : <EOL> ver = parse_version ( ver ) <EOL> candidates . append ( ( ver , nm ) ) <EOL> candidates = [ c [ <NUM_LIT:1> ] for c in sorted ( candidates , reverse = True ) ] <EOL> if not include_partial_installs : <EOL> i = <NUM_LIT:0> <EOL> while i < len ( candidates ) : <EOL> if not is_installed_version_dir ( pathjoin ( appdir , candidates [ i ] ) ) : <EOL> del candidates [ i ] <EOL> else : <EOL> i += <NUM_LIT:1> <EOL> return candidates <EOL> def is_version_dir ( vdir ) : <EOL> """<STR_LIT>""" <EOL> if exists ( pathjoin ( vdir , ESKY_CONTROL_DIR , "<STR_LIT>" ) ) : <EOL> return True <EOL> return False <EOL> def is_installed_version_dir ( vdir ) : <EOL> """<STR_LIT>""" <EOL> if not exists ( pathjoin ( vdir , ESKY_CONTROL_DIR , "<STR_LIT>" ) ) : <EOL> return True <EOL> return False <EOL> def is_uninstalled_version_dir ( vdir ) : <EOL> """<STR_LIT>""" <EOL> if exists ( pathjoin ( vdir , ESKY_CONTROL_DIR , "<STR_LIT>" ) ) : <EOL> return True <EOL> return False <EOL> def split_app_version ( s ) : <EOL> """<STR_LIT>""" <EOL> bits = s . split ( "<STR_LIT:->" ) <EOL> idx = <NUM_LIT:1> <EOL> while idx < len ( bits ) : <EOL> if bits [ idx ] : <EOL> if not bits [ idx ] [ <NUM_LIT:0> ] . isalpha ( ) or not isalnum ( bits [ idx ] ) : <EOL> break <EOL> idx += <NUM_LIT:1> <EOL> appname = "<STR_LIT:->" . join ( bits [ : idx ] ) <EOL> bits = "<STR_LIT:->" . join ( bits [ idx : ] ) . split ( "<STR_LIT:.>" ) <EOL> version = "<STR_LIT:.>" . join ( bits [ : - <NUM_LIT:1> ] ) <EOL> platform = bits [ - <NUM_LIT:1> ] <EOL> return ( appname , version , platform ) <EOL> def join_app_version ( appname , version , platform ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( appname , version , platform , ) <EOL> def parse_version ( s ) : <EOL> """<STR_LIT>""" <EOL> parts = [ ] <EOL> for part in _parse_version_parts ( s . lower ( ) ) : <EOL> if part . startswith ( '<STR_LIT:*>' ) : <EOL> if part < '<STR_LIT>' : <EOL> while parts and parts [ - <NUM_LIT:1> ] == '<STR_LIT>' : parts . pop ( ) <EOL> while parts and parts [ - <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> parts . pop ( ) <EOL> parts . append ( part ) <EOL> return parts <EOL> _replace_p = { '<STR_LIT>' : '<STR_LIT:c>' , '<STR_LIT>' : '<STR_LIT:c>' , '<STR_LIT:->' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:c>' , '<STR_LIT>' : '<STR_LIT:@>' } . get <EOL> def _parse_version_parts ( s ) : <EOL> parts = [ ] <EOL> for part in _split_version_components ( s ) : <EOL> part = _replace_p ( part , part ) <EOL> if not part or part == '<STR_LIT:.>' : <EOL> continue <EOL> if part [ : <NUM_LIT:1> ] in '<STR_LIT>' : <EOL> parts . append ( zfill ( part , <NUM_LIT:8> ) ) <EOL> else : <EOL> parts . append ( '<STR_LIT:*>' + part ) <EOL> parts . append ( '<STR_LIT>' ) <EOL> return parts <EOL> def _split_version_components ( s ) : <EOL> """<STR_LIT>""" <EOL> comps = [ ] <EOL> start = <NUM_LIT:0> <EOL> while start < len ( s ) : <EOL> end = start + <NUM_LIT:1> <EOL> if s [ start ] . isdigit ( ) : <EOL> while end < len ( s ) and s [ end ] . isdigit ( ) : <EOL> end += <NUM_LIT:1> <EOL> elif s [ start ] . isalpha ( ) : <EOL> while end < len ( s ) and s [ end ] . isalpha ( ) : <EOL> end += <NUM_LIT:1> <EOL> elif s [ start ] in ( "<STR_LIT:.>" , "<STR_LIT:->" ) : <EOL> pass <EOL> else : <EOL> while end < len ( s ) and not ( s [ end ] . isdigit ( ) or s [ end ] . isalpha ( ) or s [ end ] in ( "<STR_LIT:.>" , "<STR_LIT:->" ) ) : <EOL> end += <NUM_LIT:1> <EOL> comps . append ( s [ start : end ] ) <EOL> start = end <EOL> return comps <EOL> def get_original_filename ( backname ) : <EOL> """<STR_LIT>""" <EOL> filtered = "<STR_LIT:.>" . join ( [ n for n in backname . split ( "<STR_LIT:.>" ) if n != "<STR_LIT>" ] ) <EOL> for nm in listdir ( dirname ( backname ) ) : <EOL> if nm == backname : <EOL> continue <EOL> if filtered == "<STR_LIT:.>" . join ( [ n for n in nm . split ( "<STR_LIT:.>" ) if n != "<STR_LIT>" ] ) : <EOL> return pathjoin ( dirname ( backname ) , nm ) <EOL> return None <EOL> _locked_version_dirs = { } <EOL> def lock_version_dir ( vdir ) : <EOL> """<STR_LIT>""" <EOL> if sys . platform == "<STR_LIT:win32>" : <EOL> lockfile = pathjoin ( vdir , ESKY_CONTROL_DIR , "<STR_LIT>" ) <EOL> _locked_version_dirs . setdefault ( vdir , [ ] ) . append ( os_open ( lockfile , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> else : <EOL> lockfile = pathjoin ( vdir , ESKY_CONTROL_DIR , "<STR_LIT>" ) <EOL> f = os_open ( lockfile , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> _locked_version_dirs . setdefault ( vdir , [ ] ) . append ( f ) <EOL> fcntl . flock ( f , fcntl . LOCK_SH ) <EOL> def unlock_version_dir ( vdir ) : <EOL> """<STR_LIT>""" <EOL> os_close ( _locked_version_dirs [ vdir ] . pop ( ) ) <EOL> if __rpython__ : <EOL> def main ( ) : <EOL> bootstrap ( ) <EOL> def target ( driver , args ) : <EOL> """<STR_LIT>""" <EOL> def entry_point ( argv ) : <EOL> exit_code = <NUM_LIT:0> <EOL> sys . executable = abspath ( pathjoin ( getcwd ( ) , argv [ <NUM_LIT:0> ] ) ) <EOL> sys . argv = argv <EOL> try : <EOL> main ( ) <EOL> except SystemExit , e : <EOL> exit_code = _exit_code [ <NUM_LIT:0> ] <EOL> return exit_code <EOL> return entry_point , None </s>
