<s> """<STR_LIT>""" <EOL> import os <EOL> from pkgutil import walk_packages <EOL> import socket <EOL> import sys <EOL> import tempfile <EOL> import traceback <EOL> import time <EOL> from pip import commands <EOL> from pip . log import logger <EOL> from pip . baseparser import parser , ConfigOptionParser , UpdatingDefaultsHelpFormatter <EOL> from pip . download import urlopen <EOL> from pip . exceptions import ( BadCommand , InstallationError , UninstallationError , <EOL> CommandError ) <EOL> from pip . backwardcompat import StringIO <EOL> from pip . status_codes import SUCCESS , ERROR , UNKNOWN_ERROR , VIRTUALENV_NOT_FOUND <EOL> from pip . util import get_prog <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> command_dict = { } <EOL> get_proxy = urlopen . get_proxy <EOL> class Command ( object ) : <EOL> name = None <EOL> usage = None <EOL> hidden = False <EOL> def __init__ ( self ) : <EOL> assert self . name <EOL> self . parser = ConfigOptionParser ( <EOL> usage = self . usage , <EOL> prog = '<STR_LIT>' % ( get_prog ( ) , self . name ) , <EOL> version = parser . version , <EOL> formatter = UpdatingDefaultsHelpFormatter ( ) , <EOL> name = self . name ) <EOL> for option in parser . option_list : <EOL> if not option . dest or option . dest == '<STR_LIT>' : <EOL> continue <EOL> self . parser . add_option ( option ) <EOL> command_dict [ self . name ] = self <EOL> def merge_options ( self , initial_options , options ) : <EOL> for attr in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] : <EOL> setattr ( options , attr , getattr ( initial_options , attr ) or getattr ( options , attr ) ) <EOL> options . quiet += initial_options . quiet <EOL> options . verbose += initial_options . verbose <EOL> def setup_logging ( self ) : <EOL> pass <EOL> def main ( self , args , initial_options ) : <EOL> options , args = self . parser . parse_args ( args ) <EOL> self . merge_options ( initial_options , options ) <EOL> level = <NUM_LIT:1> <EOL> level += options . verbose <EOL> level -= options . quiet <EOL> level = logger . level_for_integer ( <NUM_LIT:4> - level ) <EOL> complete_log = [ ] <EOL> logger . consumers . extend ( <EOL> [ ( level , sys . stdout ) , <EOL> ( logger . DEBUG , complete_log . append ) ] ) <EOL> if options . log_explicit_levels : <EOL> logger . explicit_levels = True <EOL> self . setup_logging ( ) <EOL> if options . no_input : <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> if options . exists_action : <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' . join ( options . exists_action ) <EOL> if options . require_venv : <EOL> if not os . environ . get ( '<STR_LIT>' ) : <EOL> logger . fatal ( '<STR_LIT>' ) <EOL> sys . exit ( VIRTUALENV_NOT_FOUND ) <EOL> if options . log : <EOL> log_fp = open_logfile ( options . log , '<STR_LIT:a>' ) <EOL> logger . consumers . append ( ( logger . DEBUG , log_fp ) ) <EOL> else : <EOL> log_fp = None <EOL> socket . setdefaulttimeout ( options . timeout or None ) <EOL> urlopen . setup ( proxystr = options . proxy , prompting = not options . no_input ) <EOL> exit = SUCCESS <EOL> store_log = False <EOL> try : <EOL> status = self . run ( options , args ) <EOL> if isinstance ( status , int ) : <EOL> exit = status <EOL> except ( InstallationError , UninstallationError ) : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> logger . fatal ( str ( e ) ) <EOL> logger . info ( '<STR_LIT>' % format_exc ( ) ) <EOL> store_log = True <EOL> exit = ERROR <EOL> except BadCommand : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> logger . fatal ( str ( e ) ) <EOL> logger . info ( '<STR_LIT>' % format_exc ( ) ) <EOL> store_log = True <EOL> exit = ERROR <EOL> except CommandError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> logger . fatal ( '<STR_LIT>' % e ) <EOL> logger . info ( '<STR_LIT>' % format_exc ( ) ) <EOL> exit = ERROR <EOL> except KeyboardInterrupt : <EOL> logger . fatal ( '<STR_LIT>' ) <EOL> logger . info ( '<STR_LIT>' % format_exc ( ) ) <EOL> store_log = True <EOL> exit = ERROR <EOL> except : <EOL> logger . fatal ( '<STR_LIT>' % format_exc ( ) ) <EOL> store_log = True <EOL> exit = UNKNOWN_ERROR <EOL> if log_fp is not None : <EOL> log_fp . close ( ) <EOL> if store_log : <EOL> log_fn = options . log_file <EOL> text = '<STR_LIT:\n>' . join ( complete_log ) <EOL> try : <EOL> log_fp = open_logfile ( log_fn , '<STR_LIT:w>' ) <EOL> except IOError : <EOL> temp = tempfile . NamedTemporaryFile ( delete = False ) <EOL> log_fn = temp . name <EOL> log_fp = open_logfile ( log_fn , '<STR_LIT:w>' ) <EOL> logger . fatal ( '<STR_LIT>' % log_fn ) <EOL> log_fp . write ( text ) <EOL> log_fp . close ( ) <EOL> return exit <EOL> def format_exc ( exc_info = None ) : <EOL> if exc_info is None : <EOL> exc_info = sys . exc_info ( ) <EOL> out = StringIO ( ) <EOL> traceback . print_exception ( * exc_info , ** dict ( file = out ) ) <EOL> return out . getvalue ( ) <EOL> def open_logfile ( filename , mode = '<STR_LIT:a>' ) : <EOL> """<STR_LIT>""" <EOL> filename = os . path . expanduser ( filename ) <EOL> filename = os . path . abspath ( filename ) <EOL> dirname = os . path . dirname ( filename ) <EOL> if not os . path . exists ( dirname ) : <EOL> os . makedirs ( dirname ) <EOL> exists = os . path . exists ( filename ) <EOL> log_fp = open ( filename , mode ) <EOL> if exists : <EOL> log_fp . write ( '<STR_LIT>' % ( '<STR_LIT:->' * <NUM_LIT> ) ) <EOL> log_fp . write ( '<STR_LIT>' % ( sys . argv [ <NUM_LIT:0> ] , time . strftime ( '<STR_LIT>' ) ) ) <EOL> return log_fp <EOL> def load_command ( name ) : <EOL> full_name = '<STR_LIT>' % name <EOL> if full_name in sys . modules : <EOL> return <EOL> try : <EOL> __import__ ( full_name ) <EOL> except ImportError : <EOL> pass <EOL> def load_all_commands ( ) : <EOL> for name in command_names ( ) : <EOL> load_command ( name ) <EOL> def command_names ( ) : <EOL> names = set ( ( pkg [ <NUM_LIT:1> ] for pkg in walk_packages ( path = commands . __path__ ) ) ) <EOL> return list ( names ) </s>
