<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' ] <EOL> try : <EOL> import distutils . version <EOL> except ImportError : <EOL> distutils = None <EOL> import os <EOL> import sys <EOL> server_software = os . getenv ( '<STR_LIT>' ) <EOL> USING_SDK = not server_software or server_software . startswith ( '<STR_LIT>' ) <EOL> del server_software <EOL> if not USING_SDK : <EOL> import google <EOL> this_version = os . path . dirname ( os . path . dirname ( google . __file__ ) ) <EOL> versions = os . path . dirname ( this_version ) <EOL> PYTHON_LIB = os . path . dirname ( versions ) <EOL> del google , this_version , versions <EOL> else : <EOL> PYTHON_LIB = '<STR_LIT>' <EOL> installed = { } <EOL> def SetAllowedModule ( _ ) : <EOL> pass <EOL> class UnacceptableVersionError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def DjangoVersion ( ) : <EOL> """<STR_LIT>""" <EOL> import django <EOL> return distutils . version . LooseVersion ( '<STR_LIT:.>' . join ( map ( str , django . VERSION ) ) ) <EOL> def PylonsVersion ( ) : <EOL> """<STR_LIT>""" <EOL> import pylons <EOL> return distutils . version . LooseVersion ( pylons . __version__ ) <EOL> PACKAGES = { <EOL> '<STR_LIT>' : ( DjangoVersion , <EOL> { '<STR_LIT>' : None , <EOL> '<STR_LIT:1.0>' : None , <EOL> '<STR_LIT>' : None , <EOL> } ) , <EOL> '<STR_LIT>' : ( lambda : distutils . version . LooseVersion ( '<STR_LIT:1.0>' ) , { '<STR_LIT:1.0>' : None } ) , <EOL> '<STR_LIT>' : ( lambda : distutils . version . LooseVersion ( '<STR_LIT:1.0>' ) , <EOL> { '<STR_LIT:1.0>' : set ( [ ( '<STR_LIT>' , '<STR_LIT:1.0>' ) ] ) } ) , <EOL> } <EOL> def EqualVersions ( version , baseline ) : <EOL> """<STR_LIT>""" <EOL> baseline_tuple = baseline . version <EOL> truncated_tuple = version . version [ : len ( baseline_tuple ) ] <EOL> if truncated_tuple == baseline_tuple : <EOL> return True <EOL> else : <EOL> return False <EOL> def AllowInstalledLibrary ( name , desired ) : <EOL> """<STR_LIT>""" <EOL> if name == '<STR_LIT>' and desired != '<STR_LIT>' : <EOL> tail = os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> sys . path [ : ] = [ dirname <EOL> for dirname in sys . path <EOL> if not dirname . endswith ( tail ) ] <EOL> CallSetAllowedModule ( name , desired ) <EOL> dependencies = PACKAGES [ name ] [ <NUM_LIT:1> ] [ desired ] <EOL> if dependencies : <EOL> for dep_name , dep_version in dependencies : <EOL> AllowInstalledLibrary ( dep_name , dep_version ) <EOL> installed [ name ] = desired , False <EOL> def CheckInstalledLibrary ( name , desired ) : <EOL> """<STR_LIT>""" <EOL> dependencies = PACKAGES [ name ] [ <NUM_LIT:1> ] [ desired ] <EOL> if dependencies : <EOL> for dep_name , dep_version in dependencies : <EOL> AllowInstalledLibrary ( dep_name , dep_version ) <EOL> CheckInstalledVersion ( name , desired , explicit = True ) <EOL> def CheckInstalledVersion ( name , desired , explicit ) : <EOL> """<STR_LIT>""" <EOL> CallSetAllowedModule ( name , desired ) <EOL> find_version = PACKAGES [ name ] [ <NUM_LIT:0> ] <EOL> installed_version = find_version ( ) <EOL> desired_version = distutils . version . LooseVersion ( desired ) <EOL> if not EqualVersions ( installed_version , desired_version ) : <EOL> raise UnacceptableVersionError ( <EOL> '<STR_LIT>' % <EOL> ( name , desired_version , installed_version ) ) <EOL> installed [ name ] = desired , explicit <EOL> def CallSetAllowedModule ( name , desired ) : <EOL> """<STR_LIT>""" <EOL> if name == '<STR_LIT>' and desired != '<STR_LIT>' : <EOL> tail = os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> sys . path [ : ] = [ dirname <EOL> for dirname in sys . path <EOL> if not dirname . endswith ( tail ) ] <EOL> SetAllowedModule ( name ) <EOL> def CreatePath ( name , version ) : <EOL> """<STR_LIT>""" <EOL> package_dir = '<STR_LIT>' % ( name , version ) <EOL> return os . path . join ( PYTHON_LIB , '<STR_LIT>' , '<STR_LIT>' , package_dir ) <EOL> def RemoveLibrary ( name ) : <EOL> """<STR_LIT>""" <EOL> installed_version , _ = installed [ name ] <EOL> path = CreatePath ( name , installed_version ) <EOL> try : <EOL> sys . path . remove ( path ) <EOL> except ValueError : <EOL> pass <EOL> del installed [ name ] <EOL> def AddLibrary ( name , version , explicit ) : <EOL> """<STR_LIT>""" <EOL> sys . path . insert ( <NUM_LIT:1> , CreatePath ( name , version ) ) <EOL> installed [ name ] = version , explicit <EOL> def InstallLibrary ( name , version , explicit = True ) : <EOL> """<STR_LIT>""" <EOL> installed_version , explicitly_installed = installed . get ( name , [ None ] * <NUM_LIT:2> ) <EOL> if name in sys . modules : <EOL> if explicit : <EOL> CheckInstalledVersion ( name , version , explicit = True ) <EOL> return <EOL> elif installed_version : <EOL> if version == installed_version : <EOL> return <EOL> if explicit : <EOL> if explicitly_installed : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( name , version , installed_version ) ) <EOL> RemoveLibrary ( name ) <EOL> else : <EOL> version_ob = distutils . version . LooseVersion ( version ) <EOL> installed_ob = distutils . version . LooseVersion ( installed_version ) <EOL> if version_ob <= installed_ob : <EOL> return <EOL> else : <EOL> RemoveLibrary ( name ) <EOL> AddLibrary ( name , version , explicit ) <EOL> dep_details = PACKAGES [ name ] [ <NUM_LIT:1> ] [ version ] <EOL> if not dep_details : <EOL> return <EOL> for dep_name , dep_version in dep_details : <EOL> InstallLibrary ( dep_name , dep_version , explicit = False ) <EOL> def use_library ( name , version ) : <EOL> """<STR_LIT>""" <EOL> if name not in PACKAGES : <EOL> raise ValueError ( '<STR_LIT>' % name ) <EOL> versions = PACKAGES [ name ] [ <NUM_LIT:1> ] . keys ( ) <EOL> if version not in versions : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( version , name , versions ) ) <EOL> if USING_SDK : <EOL> CheckInstalledLibrary ( name , version ) <EOL> else : <EOL> InstallLibrary ( name , version , explicit = True ) <EOL> if not USING_SDK : <EOL> InstallLibrary ( '<STR_LIT>' , '<STR_LIT>' , explicit = False ) </s>
