<s> """<STR_LIT>""" <EOL> import os <EOL> import shutil <EOL> import sys <EOL> import tempfile <EOL> import tarfile <EOL> import optparse <EOL> import subprocess <EOL> import platform <EOL> import textwrap <EOL> from distutils import log <EOL> try : <EOL> from site import USER_SITE <EOL> except ImportError : <EOL> USER_SITE = None <EOL> DEFAULT_VERSION = "<STR_LIT>" <EOL> DEFAULT_URL = "<STR_LIT>" <EOL> def _python_cmd ( * args ) : <EOL> """<STR_LIT>""" <EOL> args = ( sys . executable , ) + args <EOL> return subprocess . call ( args ) == <NUM_LIT:0> <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> return <NUM_LIT:2> <EOL> finally : <EOL> os . chdir ( old_wd ) <EOL> shutil . rmtree ( tmpdir ) <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> shutil . rmtree ( tmpdir ) <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> if '<STR_LIT>' in sys . modules : <EOL> del sys . modules [ '<STR_LIT>' ] <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> ) : <EOL> to_dir = os . path . abspath ( to_dir ) <EOL> rep_modules = '<STR_LIT>' , '<STR_LIT>' <EOL> imported = set ( sys . modules ) . intersection ( rep_modules ) <EOL> try : <EOL> import pkg_resources <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 . DistributionNotFound : <EOL> return _do_download ( version , download_base , to_dir , download_delay ) <EOL> except pkg_resources . VersionConflict as VC_err : <EOL> if imported : <EOL> msg = textwrap . dedent ( """<STR_LIT>""" ) . format ( VC_err = VC_err , version = version ) <EOL> sys . stderr . write ( msg ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> del pkg_resources , sys . modules [ '<STR_LIT>' ] <EOL> return _do_download ( version , download_base , to_dir , download_delay ) <EOL> def _clean_check ( cmd , target ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> subprocess . check_call ( cmd ) <EOL> except subprocess . CalledProcessError : <EOL> if os . access ( target , os . F_OK ) : <EOL> os . unlink ( target ) <EOL> raise <EOL> def download_file_powershell ( url , target ) : <EOL> """<STR_LIT>""" <EOL> target = os . path . abspath ( target ) <EOL> cmd = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" % vars ( ) , <EOL> ] <EOL> _clean_check ( cmd , target ) <EOL> def has_powershell ( ) : <EOL> if platform . system ( ) != '<STR_LIT>' : <EOL> return False <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> devnull = open ( os . path . devnull , '<STR_LIT:wb>' ) <EOL> try : <EOL> try : <EOL> subprocess . check_call ( cmd , stdout = devnull , stderr = devnull ) <EOL> except : <EOL> return False <EOL> finally : <EOL> devnull . close ( ) <EOL> return True <EOL> download_file_powershell . viable = has_powershell <EOL> def download_file_curl ( url , target ) : <EOL> cmd = [ '<STR_LIT>' , url , '<STR_LIT>' , '<STR_LIT>' , target ] <EOL> _clean_check ( cmd , target ) <EOL> def has_curl ( ) : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> devnull = open ( os . path . devnull , '<STR_LIT:wb>' ) <EOL> try : <EOL> try : <EOL> subprocess . check_call ( cmd , stdout = devnull , stderr = devnull ) <EOL> except : <EOL> return False <EOL> finally : <EOL> devnull . close ( ) <EOL> return True <EOL> download_file_curl . viable = has_curl <EOL> def download_file_wget ( url , target ) : <EOL> cmd = [ '<STR_LIT>' , url , '<STR_LIT>' , '<STR_LIT>' , target ] <EOL> _clean_check ( cmd , target ) <EOL> def has_wget ( ) : <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> devnull = open ( os . path . devnull , '<STR_LIT:wb>' ) <EOL> try : <EOL> try : <EOL> subprocess . check_call ( cmd , stdout = devnull , stderr = devnull ) <EOL> except : <EOL> return False <EOL> finally : <EOL> devnull . close ( ) <EOL> return True <EOL> download_file_wget . viable = has_wget <EOL> def download_file_insecure ( url , target ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> from urllib . request import urlopen <EOL> except ImportError : <EOL> from urllib2 import urlopen <EOL> src = dst = None <EOL> try : <EOL> src = urlopen ( url ) <EOL> data = src . read ( ) <EOL> dst = open ( target , "<STR_LIT:wb>" ) <EOL> dst . write ( data ) <EOL> finally : <EOL> if src : <EOL> src . close ( ) <EOL> if dst : <EOL> dst . close ( ) <EOL> download_file_insecure . viable = lambda : True <EOL> def get_best_downloader ( ) : <EOL> downloaders = [ <EOL> download_file_powershell , <EOL> download_file_curl , <EOL> download_file_wget , <EOL> download_file_insecure , <EOL> ] <EOL> for dl in downloaders : <EOL> if dl . viable ( ) : <EOL> return dl <EOL> def download_setuptools ( version = DEFAULT_VERSION , download_base = DEFAULT_URL , <EOL> to_dir = os . curdir , delay = <NUM_LIT:15> , <EOL> downloader_factory = get_best_downloader ) : <EOL> """<STR_LIT>""" <EOL> to_dir = os . path . abspath ( to_dir ) <EOL> tgz_name = "<STR_LIT>" % version <EOL> url = download_base + tgz_name <EOL> saveto = os . path . join ( to_dir , tgz_name ) <EOL> if not os . path . exists ( saveto ) : <EOL> log . warn ( "<STR_LIT>" , url ) <EOL> downloader = downloader_factory ( ) <EOL> downloader ( url , saveto ) <EOL> return os . path . realpath ( saveto ) <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> 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 as e : <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 ( options ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT>' ] if options . user_install else [ ] <EOL> def _parse_args ( ) : <EOL> """<STR_LIT>""" <EOL> parser = optparse . OptionParser ( ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , metavar = "<STR_LIT>" , <EOL> default = DEFAULT_URL , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> const = lambda : download_file_insecure , default = get_best_downloader , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> options , args = parser . parse_args ( ) <EOL> return options <EOL> def main ( version = DEFAULT_VERSION ) : <EOL> """<STR_LIT>""" <EOL> options = _parse_args ( ) <EOL> tarball = download_setuptools ( download_base = options . download_base , <EOL> downloader_factory = options . downloader_factory ) <EOL> return _install ( tarball , _build_install_args ( options ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
