<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import time <EOL> import fnmatch <EOL> import tempfile <EOL> import tarfile <EOL> from distutils import log <EOL> try : <EOL> from site import USER_SITE <EOL> except ImportError : <EOL> USER_SITE = None <EOL> try : <EOL> import subprocess <EOL> def _python_cmd ( * args ) : <EOL> args = ( sys . executable , ) + args <EOL> return subprocess . call ( args ) == <NUM_LIT:0> <EOL> except ImportError : <EOL> def _python_cmd ( * args ) : <EOL> args = ( sys . executable , ) + args <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> def quote ( arg ) : <EOL> if '<STR_LIT:U+0020>' in arg : <EOL> return '<STR_LIT>' % arg <EOL> return arg <EOL> args = [ quote ( arg ) for arg in args ] <EOL> return os . spawnl ( os . P_WAIT , sys . executable , * args ) == <NUM_LIT:0> <EOL> DEFAULT_VERSION = "<STR_LIT>" <EOL> DEFAULT_URL = "<STR_LIT>" <EOL> SETUPTOOLS_FAKED_VERSION = "<STR_LIT>" <EOL> SETUPTOOLS_PKG_INFO = """<STR_LIT>""" % SETUPTOOLS_FAKED_VERSION <EOL> def _install ( tarball , install_args = ( ) ) : <EOL> tmpdir = tempfile . mkdtemp ( ) <EOL> log . warn ( '<STR_LIT>' , tmpdir ) <EOL> old_wd = os . getcwd ( ) <EOL> try : <EOL> os . chdir ( tmpdir ) <EOL> tar = tarfile . open ( tarball ) <EOL> _extractall ( tar ) <EOL> tar . close ( ) <EOL> subdir = os . path . join ( tmpdir , os . listdir ( tmpdir ) [ <NUM_LIT:0> ] ) <EOL> os . chdir ( subdir ) <EOL> log . warn ( '<STR_LIT>' , subdir ) <EOL> log . warn ( '<STR_LIT>' ) <EOL> if not _python_cmd ( '<STR_LIT>' , '<STR_LIT>' , * install_args ) : <EOL> log . warn ( '<STR_LIT>' ) <EOL> log . warn ( '<STR_LIT>' ) <EOL> finally : <EOL> os . chdir ( old_wd ) <EOL> def _build_egg ( egg , tarball , to_dir ) : <EOL> tmpdir = tempfile . mkdtemp ( ) <EOL> log . warn ( '<STR_LIT>' , tmpdir ) <EOL> old_wd = os . getcwd ( ) <EOL> try : <EOL> os . chdir ( tmpdir ) <EOL> tar = tarfile . open ( tarball ) <EOL> _extractall ( tar ) <EOL> tar . close ( ) <EOL> subdir = os . path . join ( tmpdir , os . listdir ( tmpdir ) [ <NUM_LIT:0> ] ) <EOL> os . chdir ( subdir ) <EOL> log . warn ( '<STR_LIT>' , subdir ) <EOL> log . warn ( '<STR_LIT>' , to_dir ) <EOL> _python_cmd ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , to_dir ) <EOL> finally : <EOL> os . chdir ( old_wd ) <EOL> log . warn ( egg ) <EOL> if not os . path . exists ( egg ) : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> def _do_download ( version , download_base , to_dir , download_delay ) : <EOL> egg = os . path . join ( to_dir , '<STR_LIT>' <EOL> % ( version , sys . version_info [ <NUM_LIT:0> ] , sys . version_info [ <NUM_LIT:1> ] ) ) <EOL> if not os . path . exists ( egg ) : <EOL> tarball = download_setuptools ( version , download_base , <EOL> to_dir , download_delay ) <EOL> _build_egg ( egg , tarball , to_dir ) <EOL> sys . path . insert ( <NUM_LIT:0> , egg ) <EOL> import setuptools <EOL> setuptools . bootstrap_install_from = egg <EOL> def use_setuptools ( version = DEFAULT_VERSION , download_base = DEFAULT_URL , <EOL> to_dir = os . curdir , download_delay = <NUM_LIT:15> , no_fake = True ) : <EOL> to_dir = os . path . abspath ( to_dir ) <EOL> was_imported = '<STR_LIT>' in sys . modules or '<STR_LIT>' in sys . modules <EOL> try : <EOL> try : <EOL> import pkg_resources <EOL> if not hasattr ( pkg_resources , '<STR_LIT>' ) : <EOL> if not no_fake : <EOL> _fake_setuptools ( ) <EOL> raise ImportError <EOL> except ImportError : <EOL> return _do_download ( version , download_base , to_dir , download_delay ) <EOL> try : <EOL> pkg_resources . require ( "<STR_LIT>" + version ) <EOL> return <EOL> except pkg_resources . VersionConflict : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if was_imported : <EOL> sys . stderr . write ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( version , e . args [ <NUM_LIT:0> ] ) ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> else : <EOL> del pkg_resources , sys . modules [ '<STR_LIT>' ] <EOL> return _do_download ( version , download_base , to_dir , <EOL> download_delay ) <EOL> except pkg_resources . DistributionNotFound : <EOL> return _do_download ( version , download_base , to_dir , <EOL> download_delay ) <EOL> finally : <EOL> if not no_fake : <EOL> _create_fake_setuptools_pkg_info ( to_dir ) <EOL> def download_setuptools ( version = DEFAULT_VERSION , download_base = DEFAULT_URL , <EOL> to_dir = os . curdir , delay = <NUM_LIT:15> ) : <EOL> """<STR_LIT>""" <EOL> to_dir = os . path . abspath ( to_dir ) <EOL> try : <EOL> from urllib . request import urlopen <EOL> except ImportError : <EOL> from urllib2 import urlopen <EOL> tgz_name = "<STR_LIT>" % version <EOL> url = download_base + tgz_name <EOL> saveto = os . path . join ( to_dir , tgz_name ) <EOL> src = dst = None <EOL> if not os . path . exists ( saveto ) : <EOL> try : <EOL> log . warn ( "<STR_LIT>" , url ) <EOL> src = urlopen ( url ) <EOL> data = src . read ( ) <EOL> dst = open ( saveto , "<STR_LIT:wb>" ) <EOL> dst . write ( data ) <EOL> finally : <EOL> if src : <EOL> src . close ( ) <EOL> if dst : <EOL> dst . close ( ) <EOL> return os . path . realpath ( saveto ) <EOL> def _no_sandbox ( function ) : <EOL> def __no_sandbox ( * args , ** kw ) : <EOL> try : <EOL> from setuptools . sandbox import DirectorySandbox <EOL> if not hasattr ( DirectorySandbox , '<STR_LIT>' ) : <EOL> def violation ( * args ) : <EOL> pass <EOL> DirectorySandbox . _old = DirectorySandbox . _violation <EOL> DirectorySandbox . _violation = violation <EOL> patched = True <EOL> else : <EOL> patched = False <EOL> except ImportError : <EOL> patched = False <EOL> try : <EOL> return function ( * args , ** kw ) <EOL> finally : <EOL> if patched : <EOL> DirectorySandbox . _violation = DirectorySandbox . _old <EOL> del DirectorySandbox . _old <EOL> return __no_sandbox <EOL> def _patch_file ( path , content ) : <EOL> """<STR_LIT>""" <EOL> existing_content = open ( path ) . read ( ) <EOL> if existing_content == content : <EOL> log . warn ( '<STR_LIT>' ) <EOL> return False <EOL> log . warn ( '<STR_LIT>' ) <EOL> _rename_path ( path ) <EOL> f = open ( path , '<STR_LIT:w>' ) <EOL> try : <EOL> f . write ( content ) <EOL> finally : <EOL> f . close ( ) <EOL> return True <EOL> _patch_file = _no_sandbox ( _patch_file ) <EOL> def _same_content ( path , content ) : <EOL> return open ( path ) . read ( ) == content <EOL> def _rename_path ( path ) : <EOL> new_name = path + '<STR_LIT>' % time . time ( ) <EOL> log . warn ( '<STR_LIT>' , path , new_name ) <EOL> os . rename ( path , new_name ) <EOL> return new_name <EOL> def _remove_flat_installation ( placeholder ) : <EOL> if not os . path . isdir ( placeholder ) : <EOL> log . warn ( '<STR_LIT>' , placeholder ) <EOL> return False <EOL> found = False <EOL> for file in os . listdir ( placeholder ) : <EOL> if fnmatch . fnmatch ( file , '<STR_LIT>' ) : <EOL> found = True <EOL> break <EOL> if not found : <EOL> log . warn ( '<STR_LIT>' ) <EOL> return <EOL> log . warn ( '<STR_LIT>' ) <EOL> pkg_info = os . path . join ( placeholder , file ) <EOL> if os . path . isdir ( pkg_info ) : <EOL> patched = _patch_egg_dir ( pkg_info ) <EOL> else : <EOL> patched = _patch_file ( pkg_info , SETUPTOOLS_PKG_INFO ) <EOL> if not patched : <EOL> log . warn ( '<STR_LIT>' , pkg_info ) <EOL> return False <EOL> for element in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> element = os . path . join ( placeholder , element ) <EOL> if os . path . exists ( element ) : <EOL> _rename_path ( element ) <EOL> else : <EOL> log . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' , element ) <EOL> return True <EOL> _remove_flat_installation = _no_sandbox ( _remove_flat_installation ) <EOL> def _after_install ( dist ) : <EOL> log . warn ( '<STR_LIT>' ) <EOL> placeholder = dist . get_command_obj ( '<STR_LIT>' ) . install_purelib <EOL> _create_fake_setuptools_pkg_info ( placeholder ) <EOL> def _create_fake_setuptools_pkg_info ( placeholder ) : <EOL> if not placeholder or not os . path . exists ( placeholder ) : <EOL> log . warn ( '<STR_LIT>' ) <EOL> return <EOL> pyver = '<STR_LIT>' % ( sys . version_info [ <NUM_LIT:0> ] , sys . version_info [ <NUM_LIT:1> ] ) <EOL> setuptools_file = '<STR_LIT>' % ( SETUPTOOLS_FAKED_VERSION , pyver ) <EOL> pkg_info = os . path . join ( placeholder , setuptools_file ) <EOL> if os . path . exists ( pkg_info ) : <EOL> log . warn ( '<STR_LIT>' , pkg_info ) <EOL> return <EOL> if not os . access ( pkg_info , os . W_OK ) : <EOL> log . warn ( "<STR_LIT>" , pkg_info ) <EOL> log . warn ( '<STR_LIT>' , pkg_info ) <EOL> f = open ( pkg_info , '<STR_LIT:w>' ) <EOL> try : <EOL> f . write ( SETUPTOOLS_PKG_INFO ) <EOL> finally : <EOL> f . close ( ) <EOL> pth_file = os . path . join ( placeholder , '<STR_LIT>' ) <EOL> log . warn ( '<STR_LIT>' , pth_file ) <EOL> f = open ( pth_file , '<STR_LIT:w>' ) <EOL> try : <EOL> f . write ( os . path . join ( os . curdir , setuptools_file ) ) <EOL> finally : <EOL> f . close ( ) <EOL> _create_fake_setuptools_pkg_info = _no_sandbox ( <EOL> _create_fake_setuptools_pkg_info <EOL> ) <EOL> def _patch_egg_dir ( path ) : <EOL> pkg_info = os . path . join ( path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if os . path . exists ( pkg_info ) : <EOL> if _same_content ( pkg_info , SETUPTOOLS_PKG_INFO ) : <EOL> log . warn ( '<STR_LIT>' , pkg_info ) <EOL> return False <EOL> _rename_path ( path ) <EOL> os . mkdir ( path ) <EOL> os . mkdir ( os . path . join ( path , '<STR_LIT>' ) ) <EOL> pkg_info = os . path . join ( path , '<STR_LIT>' , '<STR_LIT>' ) <EOL> f = open ( pkg_info , '<STR_LIT:w>' ) <EOL> try : <EOL> f . write ( SETUPTOOLS_PKG_INFO ) <EOL> finally : <EOL> f . close ( ) <EOL> return True <EOL> _patch_egg_dir = _no_sandbox ( _patch_egg_dir ) <EOL> def _before_install ( ) : <EOL> log . warn ( '<STR_LIT>' ) <EOL> _fake_setuptools ( ) <EOL> def _under_prefix ( location ) : <EOL> if '<STR_LIT>' not in sys . argv : <EOL> return True <EOL> args = sys . argv [ sys . argv . index ( '<STR_LIT>' ) + <NUM_LIT:1> : ] <EOL> for index , arg in enumerate ( args ) : <EOL> for option in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if arg . startswith ( '<STR_LIT>' % option ) : <EOL> top_dir = arg . split ( '<STR_LIT>' ) [ - <NUM_LIT:1> ] <EOL> return location . startswith ( top_dir ) <EOL> elif arg == option : <EOL> if len ( args ) > index : <EOL> top_dir = args [ index + <NUM_LIT:1> ] <EOL> return location . startswith ( top_dir ) <EOL> if arg == '<STR_LIT>' and USER_SITE is not None : <EOL> return location . startswith ( USER_SITE ) <EOL> return True <EOL> def _fake_setuptools ( ) : <EOL> log . warn ( '<STR_LIT>' ) <EOL> try : <EOL> import pkg_resources <EOL> except ImportError : <EOL> log . warn ( '<STR_LIT>' ) <EOL> return <EOL> ws = pkg_resources . working_set <EOL> try : <EOL> setuptools_dist = ws . find ( <EOL> pkg_resources . Requirement . parse ( '<STR_LIT>' , replacement = False ) <EOL> ) <EOL> except TypeError : <EOL> setuptools_dist = ws . find ( <EOL> pkg_resources . Requirement . parse ( '<STR_LIT>' ) <EOL> ) <EOL> if setuptools_dist is None : <EOL> log . warn ( '<STR_LIT>' ) <EOL> return <EOL> setuptools_location = setuptools_dist . location <EOL> log . warn ( '<STR_LIT>' , setuptools_location ) <EOL> if not _under_prefix ( setuptools_location ) : <EOL> log . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return <EOL> if not setuptools_location . endswith ( '<STR_LIT>' ) : <EOL> log . warn ( '<STR_LIT>' ) <EOL> res = _remove_flat_installation ( setuptools_location ) <EOL> if not res : <EOL> return <EOL> else : <EOL> log . warn ( '<STR_LIT>' ) <EOL> pkg_info = os . path . join ( setuptools_location , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if ( os . path . exists ( pkg_info ) and <EOL> _same_content ( pkg_info , SETUPTOOLS_PKG_INFO ) ) : <EOL> log . warn ( '<STR_LIT>' ) <EOL> return <EOL> log . warn ( '<STR_LIT>' ) <EOL> res = _patch_egg_dir ( setuptools_location ) <EOL> if not res : <EOL> return <EOL> log . warn ( '<STR_LIT>' ) <EOL> _relaunch ( ) <EOL> def _relaunch ( ) : <EOL> log . warn ( '<STR_LIT>' ) <EOL> _cmd = [ '<STR_LIT:-c>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if sys . argv [ : <NUM_LIT:3> ] == _cmd : <EOL> sys . argv [ <NUM_LIT:0> ] = '<STR_LIT>' <EOL> args = [ sys . executable ] + sys . argv <EOL> sys . exit ( subprocess . call ( args ) ) <EOL> def _extractall ( self , path = "<STR_LIT:.>" , members = None ) : <EOL> """<STR_LIT>""" <EOL> import copy <EOL> import operator <EOL> from tarfile import ExtractError <EOL> directories = [ ] <EOL> if members is None : <EOL> members = self <EOL> for tarinfo in members : <EOL> if tarinfo . isdir ( ) : <EOL> directories . append ( tarinfo ) <EOL> tarinfo = copy . copy ( tarinfo ) <EOL> tarinfo . mode = <NUM_LIT> <EOL> self . extract ( tarinfo , path ) <EOL> if sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:4> ) : <EOL> def sorter ( dir1 , dir2 ) : <EOL> return cmp ( dir1 . name , dir2 . name ) <EOL> directories . sort ( sorter ) <EOL> directories . reverse ( ) <EOL> else : <EOL> directories . sort ( key = operator . attrgetter ( '<STR_LIT:name>' ) , reverse = True ) <EOL> for tarinfo in directories : <EOL> dirpath = os . path . join ( path , tarinfo . name ) <EOL> try : <EOL> self . chown ( tarinfo , dirpath ) <EOL> self . utime ( tarinfo , dirpath ) <EOL> self . chmod ( tarinfo , dirpath ) <EOL> except ExtractError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if self . errorlevel > <NUM_LIT:1> : <EOL> raise <EOL> else : <EOL> self . _dbg ( <NUM_LIT:1> , "<STR_LIT>" % e ) <EOL> def _build_install_args ( argv ) : <EOL> install_args = [ ] <EOL> user_install = '<STR_LIT>' in argv <EOL> if user_install and sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:6> ) : <EOL> log . warn ( "<STR_LIT>" ) <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> if user_install : <EOL> install_args . append ( '<STR_LIT>' ) <EOL> return install_args <EOL> def main ( argv , version = DEFAULT_VERSION ) : <EOL> """<STR_LIT>""" <EOL> tarball = download_setuptools ( ) <EOL> _install ( tarball , _build_install_args ( argv ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( sys . argv [ <NUM_LIT:1> : ] ) </s>
