<s> """<STR_LIT>""" <EOL> import os <EOL> import shutil <EOL> import sys <EOL> import tempfile <EOL> import zipfile <EOL> import optparse <EOL> import subprocess <EOL> import platform <EOL> import textwrap <EOL> import contextlib <EOL> import warnings <EOL> from distutils import log <EOL> try : <EOL> from urllib . request import urlopen <EOL> except ImportError : <EOL> from urllib2 import urlopen <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> DEFAULT_SAVE_DIR = os . curdir <EOL> def _python_cmd ( * args ) : <EOL> """<STR_LIT>""" <EOL> args = ( sys . executable , ) + args <EOL> return subprocess . call ( args ) == <NUM_LIT:0> <EOL> def _install ( archive_filename , install_args = ( ) ) : <EOL> """<STR_LIT>""" <EOL> with archive_context ( archive_filename ) : <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> def _build_egg ( egg , archive_filename , to_dir ) : <EOL> """<STR_LIT>""" <EOL> with archive_context ( archive_filename ) : <EOL> log . warn ( '<STR_LIT>' , to_dir ) <EOL> _python_cmd ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , to_dir ) <EOL> log . warn ( egg ) <EOL> if not os . path . exists ( egg ) : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> class ContextualZipFile ( zipfile . ZipFile ) : <EOL> """<STR_LIT>""" <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> self . close ( ) <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( zipfile . ZipFile , '<STR_LIT>' ) : <EOL> return zipfile . ZipFile ( * args , ** kwargs ) <EOL> return super ( ContextualZipFile , cls ) . __new__ ( cls ) <EOL> @ contextlib . contextmanager <EOL> def archive_context ( filename ) : <EOL> """<STR_LIT>""" <EOL> tmpdir = tempfile . mkdtemp ( ) <EOL> log . warn ( '<STR_LIT>' , tmpdir ) <EOL> old_wd = os . getcwd ( ) <EOL> try : <EOL> os . chdir ( tmpdir ) <EOL> with ContextualZipFile ( filename ) as archive : <EOL> archive . extractall ( ) <EOL> subdir = os . path . join ( tmpdir , os . listdir ( tmpdir ) [ <NUM_LIT:0> ] ) <EOL> os . chdir ( subdir ) <EOL> log . warn ( '<STR_LIT>' , subdir ) <EOL> yield <EOL> finally : <EOL> os . chdir ( old_wd ) <EOL> shutil . rmtree ( tmpdir ) <EOL> def _do_download ( version , download_base , to_dir , download_delay ) : <EOL> """<STR_LIT>""" <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> archive = download_setuptools ( version , download_base , <EOL> to_dir , download_delay ) <EOL> _build_egg ( egg , archive , 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 ( <EOL> version = DEFAULT_VERSION , download_base = DEFAULT_URL , <EOL> to_dir = DEFAULT_SAVE_DIR , download_delay = <NUM_LIT:15> ) : <EOL> """<STR_LIT>""" <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> pkg_resources . require ( "<STR_LIT>" + version ) <EOL> return <EOL> except ImportError : <EOL> pass <EOL> except pkg_resources . DistributionNotFound : <EOL> pass <EOL> except pkg_resources . VersionConflict as VC_err : <EOL> if imported : <EOL> _conflict_bail ( VC_err , version ) <EOL> del pkg_resources <EOL> _unload_pkg_resources ( ) <EOL> return _do_download ( version , download_base , to_dir , download_delay ) <EOL> def _conflict_bail ( VC_err , version ) : <EOL> """<STR_LIT>""" <EOL> conflict_tmpl = textwrap . dedent ( """<STR_LIT>""" ) <EOL> msg = conflict_tmpl . format ( ** locals ( ) ) <EOL> sys . stderr . write ( msg ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> def _unload_pkg_resources ( ) : <EOL> del_modules = [ <EOL> name for name in sys . modules <EOL> if name . startswith ( '<STR_LIT>' ) <EOL> ] <EOL> for mod_name in del_modules : <EOL> del sys . modules [ mod_name ] <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> ps_cmd = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % vars ( ) <EOL> ) <EOL> cmd = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ps_cmd , <EOL> ] <EOL> _clean_check ( cmd , target ) <EOL> def has_powershell ( ) : <EOL> """<STR_LIT>""" <EOL> if platform . system ( ) != '<STR_LIT>' : <EOL> return False <EOL> cmd = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> with open ( os . path . devnull , '<STR_LIT:wb>' ) as devnull : <EOL> try : <EOL> subprocess . check_call ( cmd , stdout = devnull , stderr = devnull ) <EOL> except Exception : <EOL> return False <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> with open ( os . path . devnull , '<STR_LIT:wb>' ) as devnull : <EOL> try : <EOL> subprocess . check_call ( cmd , stdout = devnull , stderr = devnull ) <EOL> except Exception : <EOL> return False <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> with open ( os . path . devnull , '<STR_LIT:wb>' ) as devnull : <EOL> try : <EOL> subprocess . check_call ( cmd , stdout = devnull , stderr = devnull ) <EOL> except Exception : <EOL> return False <EOL> return True <EOL> download_file_wget . viable = has_wget <EOL> def download_file_insecure ( url , target ) : <EOL> """<STR_LIT>""" <EOL> src = urlopen ( url ) <EOL> try : <EOL> data = src . read ( ) <EOL> finally : <EOL> src . close ( ) <EOL> with open ( target , "<STR_LIT:wb>" ) as dst : <EOL> dst . write ( data ) <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> viable_downloaders = ( dl for dl in downloaders if dl . viable ( ) ) <EOL> return next ( viable_downloaders , None ) <EOL> def download_setuptools ( <EOL> version = DEFAULT_VERSION , download_base = DEFAULT_URL , <EOL> to_dir = DEFAULT_SAVE_DIR , delay = <NUM_LIT:15> , <EOL> downloader_factory = get_best_downloader ) : <EOL> """<STR_LIT>""" <EOL> to_dir = os . path . abspath ( to_dir ) <EOL> zip_name = "<STR_LIT>" % version <EOL> url = download_base + zip_name <EOL> saveto = os . path . join ( to_dir , zip_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 _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> parser . add_option ( <EOL> '<STR_LIT>' , help = "<STR_LIT>" , <EOL> default = DEFAULT_VERSION , <EOL> ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , <EOL> help = "<STR_LIT>" , <EOL> default = DEFAULT_SAVE_DIR , <EOL> ) <EOL> options , args = parser . parse_args ( ) <EOL> return options <EOL> def _download_args ( options ) : <EOL> """<STR_LIT>""" <EOL> return dict ( <EOL> version = options . version , <EOL> download_base = options . download_base , <EOL> downloader_factory = options . downloader_factory , <EOL> to_dir = options . to_dir , <EOL> ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> options = _parse_args ( ) <EOL> archive = download_setuptools ( ** _download_args ( options ) ) <EOL> return _install ( archive , _build_install_args ( options ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
