<s> import sys <EOL> import shutil <EOL> import os <EOL> import stat <EOL> import re <EOL> import posixpath <EOL> import pkg_resources <EOL> import zipfile <EOL> import tarfile <EOL> import subprocess <EOL> from pip . exceptions import InstallationError , BadCommand <EOL> from pip . backwardcompat import WindowsError , string_types , raw_input , console_to_str , user_site <EOL> from pip . locations import site_packages , running_under_virtualenv , virtualenv_no_global <EOL> from pip . log import logger <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def get_prog ( ) : <EOL> try : <EOL> if os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) in ( '<STR_LIT>' , '<STR_LIT:-c>' ) : <EOL> return "<STR_LIT>" % sys . executable <EOL> except ( AttributeError , TypeError , IndexError ) : <EOL> pass <EOL> return '<STR_LIT>' <EOL> def rmtree ( dir , ignore_errors = False ) : <EOL> shutil . rmtree ( dir , ignore_errors = ignore_errors , <EOL> onerror = rmtree_errorhandler ) <EOL> def rmtree_errorhandler ( func , path , exc_info ) : <EOL> """<STR_LIT>""" <EOL> exctype , value = exc_info [ : <NUM_LIT:2> ] <EOL> if not ( ( exctype is WindowsError and value . args [ <NUM_LIT:0> ] == <NUM_LIT:5> ) or <EOL> ( exctype is OSError and value . args [ <NUM_LIT:0> ] == <NUM_LIT> ) ) : <EOL> raise <EOL> if ( ( os . stat ( path ) . st_mode & stat . S_IREAD ) != stat . S_IREAD ) : <EOL> raise <EOL> os . chmod ( path , stat . S_IWRITE ) <EOL> func ( path ) <EOL> def display_path ( path ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . normcase ( os . path . abspath ( path ) ) <EOL> if path . startswith ( os . getcwd ( ) + os . path . sep ) : <EOL> path = '<STR_LIT:.>' + path [ len ( os . getcwd ( ) ) : ] <EOL> return path <EOL> def backup_dir ( dir , ext = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> n = <NUM_LIT:1> <EOL> extension = ext <EOL> while os . path . exists ( dir + extension ) : <EOL> n += <NUM_LIT:1> <EOL> extension = ext + str ( n ) <EOL> return dir + extension <EOL> def find_command ( cmd , paths = None , pathext = None ) : <EOL> """<STR_LIT>""" <EOL> if paths is None : <EOL> paths = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( os . pathsep ) <EOL> if isinstance ( paths , string_types ) : <EOL> paths = [ paths ] <EOL> if pathext is None : <EOL> pathext = get_pathext ( ) <EOL> pathext = [ ext for ext in pathext . lower ( ) . split ( os . pathsep ) ] <EOL> if os . path . splitext ( cmd ) [ <NUM_LIT:1> ] . lower ( ) in pathext : <EOL> pathext = [ '<STR_LIT>' ] <EOL> for path in paths : <EOL> cmd_path = os . path . join ( path , cmd ) <EOL> for ext in pathext : <EOL> cmd_path_ext = cmd_path + ext <EOL> if os . path . isfile ( cmd_path_ext ) : <EOL> return cmd_path_ext <EOL> if os . path . isfile ( cmd_path ) : <EOL> return cmd_path <EOL> raise BadCommand ( '<STR_LIT>' % cmd ) <EOL> def get_pathext ( default_pathext = None ) : <EOL> """<STR_LIT>""" <EOL> if default_pathext is None : <EOL> default_pathext = os . pathsep . join ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> pathext = os . environ . get ( '<STR_LIT>' , default_pathext ) <EOL> return pathext <EOL> def ask_path_exists ( message , options ) : <EOL> for action in os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if action in options : <EOL> return action <EOL> return ask ( message , options ) <EOL> def ask ( message , options ) : <EOL> """<STR_LIT>""" <EOL> while <NUM_LIT:1> : <EOL> if os . environ . get ( '<STR_LIT>' ) : <EOL> raise Exception ( '<STR_LIT>' % message ) <EOL> response = raw_input ( message ) <EOL> response = response . strip ( ) . lower ( ) <EOL> if response not in options : <EOL> print ( '<STR_LIT>' % ( <EOL> response , '<STR_LIT:U+002CU+0020>' . join ( options ) ) ) <EOL> else : <EOL> return response <EOL> class _Inf ( object ) : <EOL> """<STR_LIT>""" <EOL> def __cmp__ ( self , a ) : <EOL> if self is a : <EOL> return <NUM_LIT:0> <EOL> return <NUM_LIT:1> <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> Inf = _Inf ( ) <EOL> del _Inf <EOL> _normalize_re = re . compile ( r'<STR_LIT>' , re . I ) <EOL> def normalize_name ( name ) : <EOL> return _normalize_re . sub ( '<STR_LIT:->' , name . lower ( ) ) <EOL> def format_size ( bytes ) : <EOL> if bytes > <NUM_LIT:1000> * <NUM_LIT:1000> : <EOL> return '<STR_LIT>' % ( bytes / <NUM_LIT> / <NUM_LIT:1000> ) <EOL> elif bytes > <NUM_LIT:10> * <NUM_LIT:1000> : <EOL> return '<STR_LIT>' % ( bytes / <NUM_LIT:1000> ) <EOL> elif bytes > <NUM_LIT:1000> : <EOL> return '<STR_LIT>' % ( bytes / <NUM_LIT> ) <EOL> else : <EOL> return '<STR_LIT>' % bytes <EOL> def is_installable_dir ( path ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . isdir ( path ) : <EOL> return False <EOL> setup_py = os . path . join ( path , '<STR_LIT>' ) <EOL> if os . path . isfile ( setup_py ) : <EOL> return True <EOL> return False <EOL> def is_svn_page ( html ) : <EOL> """<STR_LIT>""" <EOL> return ( re . search ( r'<STR_LIT>' , html ) <EOL> and re . search ( r'<STR_LIT>' , html , re . I ) ) <EOL> def file_contents ( filename ) : <EOL> fp = open ( filename , '<STR_LIT:rb>' ) <EOL> try : <EOL> return fp . read ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> finally : <EOL> fp . close ( ) <EOL> def split_leading_dir ( path ) : <EOL> path = str ( path ) <EOL> path = path . lstrip ( '<STR_LIT:/>' ) . lstrip ( '<STR_LIT:\\>' ) <EOL> if '<STR_LIT:/>' in path and ( ( '<STR_LIT:\\>' in path and path . find ( '<STR_LIT:/>' ) < path . find ( '<STR_LIT:\\>' ) ) <EOL> or '<STR_LIT:\\>' not in path ) : <EOL> return path . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> elif '<STR_LIT:\\>' in path : <EOL> return path . split ( '<STR_LIT:\\>' , <NUM_LIT:1> ) <EOL> else : <EOL> return path , '<STR_LIT>' <EOL> def has_leading_dir ( paths ) : <EOL> """<STR_LIT>""" <EOL> common_prefix = None <EOL> for path in paths : <EOL> prefix , rest = split_leading_dir ( path ) <EOL> if not prefix : <EOL> return False <EOL> elif common_prefix is None : <EOL> common_prefix = prefix <EOL> elif prefix != common_prefix : <EOL> return False <EOL> return True <EOL> def make_path_relative ( path , rel_to ) : <EOL> """<STR_LIT>""" <EOL> path_filename = os . path . basename ( path ) <EOL> path = os . path . dirname ( path ) <EOL> path = os . path . normpath ( os . path . abspath ( path ) ) <EOL> rel_to = os . path . normpath ( os . path . abspath ( rel_to ) ) <EOL> path_parts = path . strip ( os . path . sep ) . split ( os . path . sep ) <EOL> rel_to_parts = rel_to . strip ( os . path . sep ) . split ( os . path . sep ) <EOL> while path_parts and rel_to_parts and path_parts [ <NUM_LIT:0> ] == rel_to_parts [ <NUM_LIT:0> ] : <EOL> path_parts . pop ( <NUM_LIT:0> ) <EOL> rel_to_parts . pop ( <NUM_LIT:0> ) <EOL> full_parts = [ '<STR_LIT:..>' ] * len ( rel_to_parts ) + path_parts + [ path_filename ] <EOL> if full_parts == [ '<STR_LIT>' ] : <EOL> return '<STR_LIT:.>' + os . path . sep <EOL> return os . path . sep . join ( full_parts ) <EOL> def normalize_path ( path ) : <EOL> """<STR_LIT>""" <EOL> return os . path . normcase ( os . path . realpath ( path ) ) <EOL> def splitext ( path ) : <EOL> """<STR_LIT>""" <EOL> base , ext = posixpath . splitext ( path ) <EOL> if base . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> ext = base [ - <NUM_LIT:4> : ] + ext <EOL> base = base [ : - <NUM_LIT:4> ] <EOL> return base , ext <EOL> def renames ( old , new ) : <EOL> """<STR_LIT>""" <EOL> head , tail = os . path . split ( new ) <EOL> if head and tail and not os . path . exists ( head ) : <EOL> os . makedirs ( head ) <EOL> shutil . move ( old , new ) <EOL> head , tail = os . path . split ( old ) <EOL> if head and tail : <EOL> try : <EOL> os . removedirs ( head ) <EOL> except OSError : <EOL> pass <EOL> def is_local ( path ) : <EOL> """<STR_LIT>""" <EOL> if not running_under_virtualenv ( ) : <EOL> return True <EOL> return normalize_path ( path ) . startswith ( normalize_path ( sys . prefix ) ) <EOL> def dist_is_local ( dist ) : <EOL> """<STR_LIT>""" <EOL> return is_local ( dist_location ( dist ) ) <EOL> def dist_in_usersite ( dist ) : <EOL> """<STR_LIT>""" <EOL> if user_site : <EOL> return normalize_path ( dist_location ( dist ) ) . startswith ( normalize_path ( user_site ) ) <EOL> else : <EOL> return False <EOL> def dist_in_site_packages ( dist ) : <EOL> """<STR_LIT>""" <EOL> return normalize_path ( dist_location ( dist ) ) . startswith ( normalize_path ( site_packages ) ) <EOL> def get_installed_distributions ( local_only = True , skip = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> """<STR_LIT>""" <EOL> if local_only : <EOL> local_test = dist_is_local <EOL> else : <EOL> local_test = lambda d : True <EOL> return [ d for d in pkg_resources . working_set if local_test ( d ) and d . key not in skip ] <EOL> def egg_link_path ( dist ) : <EOL> """<STR_LIT>""" <EOL> sites = [ ] <EOL> if running_under_virtualenv ( ) : <EOL> if virtualenv_no_global ( ) : <EOL> sites . append ( site_packages ) <EOL> else : <EOL> sites . append ( site_packages ) <EOL> if user_site : <EOL> sites . append ( user_site ) <EOL> else : <EOL> if user_site : <EOL> sites . append ( user_site ) <EOL> sites . append ( site_packages ) <EOL> for site in sites : <EOL> egglink = os . path . join ( site , dist . project_name ) + '<STR_LIT>' <EOL> if os . path . isfile ( egglink ) : <EOL> return egglink <EOL> def dist_location ( dist ) : <EOL> """<STR_LIT>""" <EOL> egg_link = egg_link_path ( dist ) <EOL> if egg_link : <EOL> return egg_link <EOL> return dist . location <EOL> def get_terminal_size ( ) : <EOL> """<STR_LIT>""" <EOL> def ioctl_GWINSZ ( fd ) : <EOL> try : <EOL> import fcntl <EOL> import termios <EOL> import struct <EOL> cr = struct . unpack ( '<STR_LIT>' , fcntl . ioctl ( fd , termios . TIOCGWINSZ , <EOL> '<STR_LIT>' ) ) <EOL> except : <EOL> return None <EOL> if cr == ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> return None <EOL> if cr == ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> return None <EOL> return cr <EOL> cr = ioctl_GWINSZ ( <NUM_LIT:0> ) or ioctl_GWINSZ ( <NUM_LIT:1> ) or ioctl_GWINSZ ( <NUM_LIT:2> ) <EOL> if not cr : <EOL> try : <EOL> fd = os . open ( os . ctermid ( ) , os . O_RDONLY ) <EOL> cr = ioctl_GWINSZ ( fd ) <EOL> os . close ( fd ) <EOL> except : <EOL> pass <EOL> if not cr : <EOL> cr = ( os . environ . get ( '<STR_LIT>' , <NUM_LIT> ) , os . environ . get ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> return int ( cr [ <NUM_LIT:1> ] ) , int ( cr [ <NUM_LIT:0> ] ) <EOL> def unzip_file ( filename , location , flatten = True ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( location ) : <EOL> os . makedirs ( location ) <EOL> zipfp = open ( filename , '<STR_LIT:rb>' ) <EOL> try : <EOL> zip = zipfile . ZipFile ( zipfp ) <EOL> leading = has_leading_dir ( zip . namelist ( ) ) and flatten <EOL> for name in zip . namelist ( ) : <EOL> data = zip . read ( name ) <EOL> fn = name <EOL> if leading : <EOL> fn = split_leading_dir ( name ) [ <NUM_LIT:1> ] <EOL> fn = os . path . join ( location , fn ) <EOL> dir = os . path . dirname ( fn ) <EOL> if not os . path . exists ( dir ) : <EOL> os . makedirs ( dir ) <EOL> if fn . endswith ( '<STR_LIT:/>' ) or fn . endswith ( '<STR_LIT:\\>' ) : <EOL> if not os . path . exists ( fn ) : <EOL> os . makedirs ( fn ) <EOL> else : <EOL> fp = open ( fn , '<STR_LIT:wb>' ) <EOL> try : <EOL> fp . write ( data ) <EOL> finally : <EOL> fp . close ( ) <EOL> finally : <EOL> zipfp . close ( ) <EOL> def untar_file ( filename , location ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( location ) : <EOL> os . makedirs ( location ) <EOL> if filename . lower ( ) . endswith ( '<STR_LIT>' ) or filename . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> mode = '<STR_LIT>' <EOL> elif filename . lower ( ) . endswith ( '<STR_LIT>' ) or filename . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> mode = '<STR_LIT>' <EOL> elif filename . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> mode = '<STR_LIT:r>' <EOL> else : <EOL> logger . warn ( '<STR_LIT>' % filename ) <EOL> mode = '<STR_LIT>' <EOL> tar = tarfile . open ( filename , mode ) <EOL> try : <EOL> leading = has_leading_dir ( [ <EOL> member . name for member in tar . getmembers ( ) <EOL> if member . name != '<STR_LIT>' <EOL> ] ) <EOL> for member in tar . getmembers ( ) : <EOL> fn = member . name <EOL> if fn == '<STR_LIT>' : <EOL> continue <EOL> if leading : <EOL> fn = split_leading_dir ( fn ) [ <NUM_LIT:1> ] <EOL> path = os . path . join ( location , fn ) <EOL> if member . isdir ( ) : <EOL> if not os . path . exists ( path ) : <EOL> os . makedirs ( path ) <EOL> elif member . issym ( ) : <EOL> try : <EOL> tar . _extract_member ( member , path ) <EOL> except : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> logger . warn ( <EOL> '<STR_LIT>' <EOL> % ( filename , member . name , e ) ) <EOL> continue <EOL> else : <EOL> try : <EOL> fp = tar . extractfile ( member ) <EOL> except ( KeyError , AttributeError ) : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> logger . warn ( <EOL> '<STR_LIT>' <EOL> % ( filename , member . name , e ) ) <EOL> continue <EOL> if not os . path . exists ( os . path . dirname ( path ) ) : <EOL> os . makedirs ( os . path . dirname ( path ) ) <EOL> destfp = open ( path , '<STR_LIT:wb>' ) <EOL> try : <EOL> shutil . copyfileobj ( fp , destfp ) <EOL> finally : <EOL> destfp . close ( ) <EOL> fp . close ( ) <EOL> finally : <EOL> tar . close ( ) <EOL> def create_download_cache_folder ( folder ) : <EOL> logger . indent -= <NUM_LIT:2> <EOL> logger . notify ( '<STR_LIT>' % folder ) <EOL> logger . indent += <NUM_LIT:2> <EOL> os . makedirs ( folder ) <EOL> def cache_download ( target_file , temp_location , content_type ) : <EOL> logger . notify ( '<STR_LIT>' % display_path ( target_file ) ) <EOL> shutil . copyfile ( temp_location , target_file ) <EOL> fp = open ( target_file + '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> fp . write ( content_type ) <EOL> fp . close ( ) <EOL> os . unlink ( temp_location ) <EOL> def unpack_file ( filename , location , content_type , link ) : <EOL> if ( content_type == '<STR_LIT>' <EOL> or filename . endswith ( '<STR_LIT>' ) <EOL> or filename . endswith ( '<STR_LIT>' ) <EOL> or zipfile . is_zipfile ( filename ) ) : <EOL> unzip_file ( filename , location , flatten = not filename . endswith ( '<STR_LIT>' ) ) <EOL> elif ( content_type == '<STR_LIT>' <EOL> or tarfile . is_tarfile ( filename ) <EOL> or splitext ( filename ) [ <NUM_LIT:1> ] . lower ( ) in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> untar_file ( filename , location ) <EOL> elif ( content_type and content_type . startswith ( '<STR_LIT>' ) <EOL> and is_svn_page ( file_contents ( filename ) ) ) : <EOL> from pip . vcs . subversion import Subversion <EOL> Subversion ( '<STR_LIT>' + link . url ) . unpack ( location ) <EOL> else : <EOL> logger . fatal ( '<STR_LIT>' <EOL> % ( filename , location , content_type ) ) <EOL> raise InstallationError ( '<STR_LIT>' % location ) <EOL> def call_subprocess ( cmd , show_stdout = True , <EOL> filter_stdout = None , cwd = None , <EOL> raise_on_returncode = True , <EOL> command_level = logger . DEBUG , command_desc = None , <EOL> extra_environ = None ) : <EOL> if command_desc is None : <EOL> cmd_parts = [ ] <EOL> for part in cmd : <EOL> if '<STR_LIT:U+0020>' in part or '<STR_LIT:\n>' in part or '<STR_LIT:">' in part or "<STR_LIT:'>" in part : <EOL> part = '<STR_LIT>' % part . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> cmd_parts . append ( part ) <EOL> command_desc = '<STR_LIT:U+0020>' . join ( cmd_parts ) <EOL> if show_stdout : <EOL> stdout = None <EOL> else : <EOL> stdout = subprocess . PIPE <EOL> logger . log ( command_level , "<STR_LIT>" % command_desc ) <EOL> env = os . environ . copy ( ) <EOL> if extra_environ : <EOL> env . update ( extra_environ ) <EOL> try : <EOL> proc = subprocess . Popen ( <EOL> cmd , stderr = subprocess . STDOUT , stdin = None , stdout = stdout , <EOL> cwd = cwd , env = env ) <EOL> except Exception : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> logger . fatal ( <EOL> "<STR_LIT>" % ( e , command_desc ) ) <EOL> raise <EOL> all_output = [ ] <EOL> if stdout is not None : <EOL> stdout = proc . stdout <EOL> while <NUM_LIT:1> : <EOL> line = console_to_str ( stdout . readline ( ) ) <EOL> if not line : <EOL> break <EOL> line = line . rstrip ( ) <EOL> all_output . append ( line + '<STR_LIT:\n>' ) <EOL> if filter_stdout : <EOL> level = filter_stdout ( line ) <EOL> if isinstance ( level , tuple ) : <EOL> level , line = level <EOL> logger . log ( level , line ) <EOL> if not logger . stdout_level_matches ( level ) : <EOL> logger . show_progress ( ) <EOL> else : <EOL> logger . info ( line ) <EOL> else : <EOL> returned_stdout , returned_stderr = proc . communicate ( ) <EOL> all_output = [ returned_stdout or '<STR_LIT>' ] <EOL> proc . wait ( ) <EOL> if proc . returncode : <EOL> if raise_on_returncode : <EOL> if all_output : <EOL> logger . notify ( '<STR_LIT>' % command_desc ) <EOL> logger . notify ( '<STR_LIT:\n>' . join ( all_output ) + '<STR_LIT>' ) <EOL> raise InstallationError ( <EOL> "<STR_LIT>" <EOL> % ( command_desc , proc . returncode , cwd ) ) <EOL> else : <EOL> logger . warn ( <EOL> "<STR_LIT>" <EOL> % ( command_desc , proc . returncode , cwd ) ) <EOL> if stdout is not None : <EOL> return '<STR_LIT>' . join ( all_output ) </s>
