<s> import os <EOL> import optparse <EOL> import sys <EOL> import re <EOL> import difflib <EOL> from pip . basecommand import command_dict , load_command , load_all_commands , command_names <EOL> from pip . baseparser import parser <EOL> from pip . exceptions import InstallationError <EOL> from pip . log import logger <EOL> from pip . util import get_installed_distributions , get_prog <EOL> from pip . vcs import git , mercurial , subversion , bazaar <EOL> __version__ = "<STR_LIT>" <EOL> def autocomplete ( ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in os . environ : <EOL> return <EOL> cwords = os . environ [ '<STR_LIT>' ] . split ( ) [ <NUM_LIT:1> : ] <EOL> cword = int ( os . environ [ '<STR_LIT>' ] ) <EOL> try : <EOL> current = cwords [ cword - <NUM_LIT:1> ] <EOL> except IndexError : <EOL> current = '<STR_LIT>' <EOL> load_all_commands ( ) <EOL> subcommands = [ cmd for cmd , cls in command_dict . items ( ) if not cls . hidden ] <EOL> options = [ ] <EOL> try : <EOL> subcommand_name = [ w for w in cwords if w in subcommands ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> subcommand_name = None <EOL> if subcommand_name : <EOL> if subcommand_name == '<STR_LIT>' : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if subcommand_name == '<STR_LIT>' and not current . startswith ( '<STR_LIT:->' ) : <EOL> installed = [ ] <EOL> lc = current . lower ( ) <EOL> for dist in get_installed_distributions ( local_only = True ) : <EOL> if dist . key . startswith ( lc ) and dist . key not in cwords [ <NUM_LIT:1> : ] : <EOL> installed . append ( dist . key ) <EOL> if installed : <EOL> for dist in installed : <EOL> print ( dist ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> subcommand = command_dict . get ( subcommand_name ) <EOL> options += [ ( opt . get_opt_string ( ) , opt . nargs ) <EOL> for opt in subcommand . parser . option_list <EOL> if opt . help != optparse . SUPPRESS_HELP ] <EOL> prev_opts = [ x . split ( '<STR_LIT:=>' ) [ <NUM_LIT:0> ] for x in cwords [ <NUM_LIT:1> : cword - <NUM_LIT:1> ] ] <EOL> options = [ ( x , v ) for ( x , v ) in options if x not in prev_opts ] <EOL> options = [ ( k , v ) for k , v in options if k . startswith ( current ) ] <EOL> for option in options : <EOL> opt_label = option [ <NUM_LIT:0> ] <EOL> if option [ <NUM_LIT:1> ] : <EOL> opt_label += '<STR_LIT:=>' <EOL> print ( opt_label ) <EOL> else : <EOL> if current . startswith ( '<STR_LIT:->' ) or current . startswith ( '<STR_LIT>' ) : <EOL> subcommands += [ opt . get_opt_string ( ) <EOL> for opt in parser . option_list <EOL> if opt . help != optparse . SUPPRESS_HELP ] <EOL> print ( '<STR_LIT:U+0020>' . join ( [ x for x in subcommands if x . startswith ( current ) ] ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def main ( initial_args = None ) : <EOL> if initial_args is None : <EOL> initial_args = sys . argv [ <NUM_LIT:1> : ] <EOL> autocomplete ( ) <EOL> options , args = parser . parse_args ( initial_args ) <EOL> if options . help and not args : <EOL> args = [ '<STR_LIT>' ] <EOL> if not args : <EOL> parser . error ( '<STR_LIT>' <EOL> '<STR_LIT>' % get_prog ( ) ) <EOL> command = args [ <NUM_LIT:0> ] . lower ( ) <EOL> load_command ( command ) <EOL> if command not in command_dict : <EOL> close_commands = difflib . get_close_matches ( command , command_names ( ) ) <EOL> if close_commands : <EOL> guess = close_commands [ <NUM_LIT:0> ] <EOL> if args [ <NUM_LIT:1> : ] : <EOL> guess = "<STR_LIT>" % ( guess , "<STR_LIT:U+0020>" . join ( args [ <NUM_LIT:1> : ] ) ) <EOL> else : <EOL> guess = '<STR_LIT>' % command <EOL> error_dict = { '<STR_LIT>' : command , '<STR_LIT>' : guess , <EOL> '<STR_LIT>' : os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) } <EOL> parser . error ( '<STR_LIT>' <EOL> '<STR_LIT>' % error_dict ) <EOL> command = command_dict [ command ] <EOL> return command . main ( args [ <NUM_LIT:1> : ] , options ) <EOL> def bootstrap ( ) : <EOL> """<STR_LIT>""" <EOL> return main ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class FrozenRequirement ( object ) : <EOL> def __init__ ( self , name , req , editable , comments = ( ) ) : <EOL> self . name = name <EOL> self . req = req <EOL> self . editable = editable <EOL> self . comments = comments <EOL> _rev_re = re . compile ( r'<STR_LIT>' ) <EOL> _date_re = re . compile ( r'<STR_LIT>' ) <EOL> @ classmethod <EOL> def from_dist ( cls , dist , dependency_links , find_tags = False ) : <EOL> location = os . path . normcase ( os . path . abspath ( dist . location ) ) <EOL> comments = [ ] <EOL> from pip . vcs import vcs , get_src_requirement <EOL> if vcs . get_backend_name ( location ) : <EOL> editable = True <EOL> try : <EOL> req = get_src_requirement ( dist , location , find_tags ) <EOL> except InstallationError : <EOL> ex = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> logger . warn ( "<STR_LIT>" % ex ) <EOL> req = None <EOL> if req is None : <EOL> logger . warn ( '<STR_LIT>' % location ) <EOL> comments . append ( '<STR_LIT>' ) <EOL> req = dist . as_requirement ( ) <EOL> editable = False <EOL> else : <EOL> editable = False <EOL> req = dist . as_requirement ( ) <EOL> specs = req . specs <EOL> assert len ( specs ) == <NUM_LIT:1> and specs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> version = specs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> ver_match = cls . _rev_re . search ( version ) <EOL> date_match = cls . _date_re . search ( version ) <EOL> if ver_match or date_match : <EOL> svn_backend = vcs . get_backend ( '<STR_LIT>' ) <EOL> if svn_backend : <EOL> svn_location = svn_backend ( <EOL> ) . get_location ( dist , dependency_links ) <EOL> if not svn_location : <EOL> logger . warn ( <EOL> '<STR_LIT>' % req ) <EOL> comments . append ( '<STR_LIT>' ) <EOL> else : <EOL> comments . append ( '<STR_LIT>' % req ) <EOL> if ver_match : <EOL> rev = ver_match . group ( <NUM_LIT:1> ) <EOL> else : <EOL> rev = '<STR_LIT>' % date_match . group ( <NUM_LIT:1> ) <EOL> editable = True <EOL> req = '<STR_LIT>' % ( svn_location , rev , cls . egg_name ( dist ) ) <EOL> return cls ( dist . project_name , req , editable , comments ) <EOL> @ staticmethod <EOL> def egg_name ( dist ) : <EOL> name = dist . egg_name ( ) <EOL> match = re . search ( r'<STR_LIT>' , name ) <EOL> if match : <EOL> name = name [ : match . start ( ) ] <EOL> return name <EOL> def __str__ ( self ) : <EOL> req = self . req <EOL> if self . editable : <EOL> req = '<STR_LIT>' % req <EOL> return '<STR_LIT:\n>' . join ( list ( self . comments ) + [ str ( req ) ] ) + '<STR_LIT:\n>' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> exit = main ( ) <EOL> if exit : <EOL> sys . exit ( exit ) </s>
