2008-10-16 00:02:57 +02:00
|
|
|
#!/usr/bin/env python
|
2014-08-31 01:52:28 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import logging
|
2009-02-13 16:07:43 +01:00
|
|
|
import os
|
2008-10-16 00:02:57 +02:00
|
|
|
import optparse
|
2014-08-31 01:52:28 +02:00
|
|
|
import warnings
|
2010-07-26 18:53:59 +02:00
|
|
|
|
2010-07-26 04:54:16 +02:00
|
|
|
import sys
|
2008-10-16 00:02:57 +02:00
|
|
|
import re
|
2010-07-26 04:54:16 +02:00
|
|
|
|
2012-11-10 05:05:38 +01:00
|
|
|
from pip.exceptions import InstallationError, CommandError, PipError
|
2014-08-31 01:52:28 +02:00
|
|
|
from pip.utils import get_installed_distributions, get_prog
|
|
|
|
from pip.utils import deprecation
|
2012-09-06 10:30:13 +02:00
|
|
|
from pip.vcs import git, mercurial, subversion, bazaar # noqa
|
2013-09-18 07:00:22 +02:00
|
|
|
from pip.baseparser import ConfigOptionParser, UpdatingDefaultsHelpFormatter
|
2014-12-02 16:47:36 +01:00
|
|
|
from pip.commands import get_summaries, get_similar_commands
|
|
|
|
from pip.commands import commands_dict
|
2014-12-20 23:27:11 +01:00
|
|
|
from pip._vendor.requests.packages.urllib3.exceptions import (
|
|
|
|
InsecureRequestWarning,
|
|
|
|
)
|
2014-12-02 16:47:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
# assignment for flake8 to be happy
|
2010-06-03 04:25:26 +02:00
|
|
|
|
2013-10-21 12:48:47 +02:00
|
|
|
# This fixes a peculiarity when importing via __import__ - as we are
|
|
|
|
# initialising the pip module, "from pip import cmdoptions" is recursive
|
|
|
|
# and appears not to work properly in that situation.
|
|
|
|
import pip.cmdoptions
|
|
|
|
cmdoptions = pip.cmdoptions
|
|
|
|
|
2012-09-01 19:33:52 +02:00
|
|
|
# The version as used in the setup.py and the docs conf.py
|
2015-05-23 01:32:02 +02:00
|
|
|
__version__ = "7.0.1.dev0"
|
2012-09-01 19:33:52 +02:00
|
|
|
|
|
|
|
|
2014-08-31 01:52:28 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-12-20 23:27:11 +01:00
|
|
|
# Hide the InsecureRequestWArning from urllib3
|
|
|
|
warnings.filterwarnings("ignore", category=InsecureRequestWarning)
|
|
|
|
|
2014-08-31 01:52:28 +02:00
|
|
|
|
2009-11-07 04:46:30 +01:00
|
|
|
def autocomplete():
|
|
|
|
"""Command and option completion for the main option parser (and options)
|
|
|
|
and its subcommands (and options).
|
|
|
|
|
|
|
|
Enable by sourcing one of the completion shell scripts (bash or zsh).
|
|
|
|
"""
|
|
|
|
# Don't complete if user hasn't sourced bash_completion file.
|
2010-06-03 04:25:26 +02:00
|
|
|
if 'PIP_AUTO_COMPLETE' not in os.environ:
|
2009-11-07 04:46:30 +01:00
|
|
|
return
|
|
|
|
cwords = os.environ['COMP_WORDS'].split()[1:]
|
|
|
|
cword = int(os.environ['COMP_CWORD'])
|
|
|
|
try:
|
2012-09-01 19:33:52 +02:00
|
|
|
current = cwords[cword - 1]
|
2009-11-07 04:46:30 +01:00
|
|
|
except IndexError:
|
|
|
|
current = ''
|
2012-11-10 05:05:38 +01:00
|
|
|
|
2012-12-25 06:58:44 +01:00
|
|
|
subcommands = [cmd for cmd, summary in get_summaries()]
|
2009-11-07 04:46:30 +01:00
|
|
|
options = []
|
|
|
|
# subcommand
|
2010-03-09 22:30:04 +01:00
|
|
|
try:
|
|
|
|
subcommand_name = [w for w in cwords if w in subcommands][0]
|
|
|
|
except IndexError:
|
|
|
|
subcommand_name = None
|
2012-12-14 14:35:00 +01:00
|
|
|
|
|
|
|
parser = create_main_parser()
|
2009-11-07 04:46:30 +01:00
|
|
|
# subcommand options
|
2010-03-09 22:30:04 +01:00
|
|
|
if subcommand_name:
|
|
|
|
# special case: 'help' subcommand has no options
|
|
|
|
if subcommand_name == 'help':
|
|
|
|
sys.exit(1)
|
2010-03-09 20:39:48 +01:00
|
|
|
# special case: list locally installed dists for uninstall command
|
2010-03-09 22:30:04 +01:00
|
|
|
if subcommand_name == 'uninstall' and not current.startswith('-'):
|
2010-03-09 20:39:48 +01:00
|
|
|
installed = []
|
|
|
|
lc = current.lower()
|
|
|
|
for dist in get_installed_distributions(local_only=True):
|
|
|
|
if dist.key.startswith(lc) and dist.key not in cwords[1:]:
|
|
|
|
installed.append(dist.key)
|
|
|
|
# if there are no dists installed, fall back to option completion
|
|
|
|
if installed:
|
|
|
|
for dist in installed:
|
2011-03-15 20:49:48 +01:00
|
|
|
print(dist)
|
2010-03-09 20:39:48 +01:00
|
|
|
sys.exit(1)
|
2012-12-14 14:35:00 +01:00
|
|
|
|
2014-12-02 16:47:36 +01:00
|
|
|
subcommand = commands_dict[subcommand_name]()
|
2009-11-07 04:46:30 +01:00
|
|
|
options += [(opt.get_opt_string(), opt.nargs)
|
2012-12-14 14:35:00 +01:00
|
|
|
for opt in subcommand.parser.option_list_all
|
2009-11-07 04:46:30 +01:00
|
|
|
if opt.help != optparse.SUPPRESS_HELP]
|
2012-12-14 14:35:00 +01:00
|
|
|
|
2009-11-07 04:46:30 +01:00
|
|
|
# filter out previously specified options from available options
|
2012-09-01 19:33:52 +02:00
|
|
|
prev_opts = [x.split('=')[0] for x in cwords[1:cword - 1]]
|
2011-03-15 20:49:48 +01:00
|
|
|
options = [(x, v) for (x, v) in options if x not in prev_opts]
|
2009-11-07 04:46:30 +01:00
|
|
|
# filter options by current input
|
|
|
|
options = [(k, v) for k, v in options if k.startswith(current)]
|
|
|
|
for option in options:
|
|
|
|
opt_label = option[0]
|
|
|
|
# append '=' to options which require args
|
|
|
|
if option[1]:
|
|
|
|
opt_label += '='
|
2011-03-15 20:49:48 +01:00
|
|
|
print(opt_label)
|
2010-03-09 22:30:04 +01:00
|
|
|
else:
|
2012-11-10 05:17:58 +01:00
|
|
|
# show main parser options only when necessary
|
2010-03-09 22:30:04 +01:00
|
|
|
if current.startswith('-') or current.startswith('--'):
|
2012-11-10 05:17:58 +01:00
|
|
|
opts = [i.option_list for i in parser.option_groups]
|
|
|
|
opts.append(parser.option_list)
|
2012-12-06 15:56:46 +01:00
|
|
|
opts = (o for it in opts for o in it)
|
2012-11-10 05:17:58 +01:00
|
|
|
|
|
|
|
subcommands += [i.get_opt_string() for i in opts
|
|
|
|
if i.help != optparse.SUPPRESS_HELP]
|
|
|
|
|
2011-03-15 20:49:48 +01:00
|
|
|
print(' '.join([x for x in subcommands if x.startswith(current)]))
|
2009-11-07 04:46:30 +01:00
|
|
|
sys.exit(1)
|
2008-10-28 09:29:07 +01:00
|
|
|
|
2010-07-29 18:56:03 +02:00
|
|
|
|
2013-09-11 02:55:08 +02:00
|
|
|
def create_main_parser():
|
|
|
|
parser_kw = {
|
|
|
|
'usage': '\n%prog <command> [options]',
|
|
|
|
'add_help_option': False,
|
|
|
|
'formatter': UpdatingDefaultsHelpFormatter(),
|
|
|
|
'name': 'global',
|
|
|
|
'prog': get_prog(),
|
|
|
|
}
|
|
|
|
|
|
|
|
parser = ConfigOptionParser(**parser_kw)
|
|
|
|
parser.disable_interspersed_args()
|
|
|
|
|
2013-09-18 07:00:22 +02:00
|
|
|
pip_pkg_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
parser.version = 'pip %s from %s (python %s)' % (
|
2014-02-24 22:52:23 +01:00
|
|
|
__version__, pip_pkg_dir, sys.version[:3])
|
2013-09-11 02:55:08 +02:00
|
|
|
|
2013-09-17 07:21:15 +02:00
|
|
|
# add the general options
|
|
|
|
gen_opts = cmdoptions.make_option_group(cmdoptions.general_group, parser)
|
|
|
|
parser.add_option_group(gen_opts)
|
2013-09-11 02:55:08 +02:00
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
parser.main = True # so the help formatter knows
|
2012-11-10 05:17:58 +01:00
|
|
|
|
2013-09-11 02:55:08 +02:00
|
|
|
# create command listing for description
|
2012-11-10 05:17:58 +01:00
|
|
|
command_summaries = get_summaries()
|
2013-01-18 22:25:15 +01:00
|
|
|
description = [''] + ['%-27s %s' % (i, j) for i, j in command_summaries]
|
2012-11-10 05:17:58 +01:00
|
|
|
parser.description = '\n'.join(description)
|
|
|
|
|
2013-09-11 02:55:08 +02:00
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
def parseopts(args):
|
|
|
|
parser = create_main_parser()
|
2012-11-10 05:17:58 +01:00
|
|
|
|
2014-01-27 15:07:10 +01:00
|
|
|
# Note: parser calls disable_interspersed_args(), so the result of this
|
|
|
|
# call is to split the initial args into the general options before the
|
2013-09-11 02:55:08 +02:00
|
|
|
# subcommand and everything else.
|
|
|
|
# For example:
|
|
|
|
# args: ['--timeout=5', 'install', '--user', 'INITools']
|
|
|
|
# general_options: ['--timeout==5']
|
|
|
|
# args_else: ['install', '--user', 'INITools']
|
|
|
|
general_options, args_else = parser.parse_args(args)
|
|
|
|
|
|
|
|
# --version
|
|
|
|
if general_options.version:
|
2012-11-10 05:17:58 +01:00
|
|
|
sys.stdout.write(parser.version)
|
|
|
|
sys.stdout.write(os.linesep)
|
|
|
|
sys.exit()
|
|
|
|
|
2013-09-11 02:55:08 +02:00
|
|
|
# pip || pip help -> print_help()
|
|
|
|
if not args_else or (args_else[0] == 'help' and len(args_else) == 1):
|
2012-11-10 05:17:58 +01:00
|
|
|
parser.print_help()
|
|
|
|
sys.exit()
|
|
|
|
|
2013-09-11 02:55:08 +02:00
|
|
|
# the subcommand name
|
2014-04-15 23:47:15 +02:00
|
|
|
cmd_name = args_else[0]
|
2012-11-10 05:17:58 +01:00
|
|
|
|
2014-12-02 16:47:36 +01:00
|
|
|
if cmd_name not in commands_dict:
|
2013-09-11 02:55:08 +02:00
|
|
|
guess = get_similar_commands(cmd_name)
|
2012-11-10 05:17:58 +01:00
|
|
|
|
2013-09-11 02:55:08 +02:00
|
|
|
msg = ['unknown command "%s"' % cmd_name]
|
2012-11-10 05:17:58 +01:00
|
|
|
if guess:
|
2012-12-06 15:56:46 +01:00
|
|
|
msg.append('maybe you meant "%s"' % guess)
|
2012-11-10 05:17:58 +01:00
|
|
|
|
2012-12-06 16:22:04 +01:00
|
|
|
raise CommandError(' - '.join(msg))
|
2012-11-10 05:17:58 +01:00
|
|
|
|
2014-04-27 17:24:22 +02:00
|
|
|
# all the args without the subcommand
|
|
|
|
cmd_args = args[:]
|
|
|
|
cmd_args.remove(cmd_name)
|
|
|
|
|
2013-09-17 07:21:15 +02:00
|
|
|
return cmd_name, cmd_args
|
2012-11-10 05:17:58 +01:00
|
|
|
|
|
|
|
|
2014-11-24 03:07:53 +01:00
|
|
|
def check_isolated(args):
|
|
|
|
isolated = False
|
|
|
|
|
|
|
|
if "--isolated" in args:
|
|
|
|
isolated = True
|
|
|
|
|
2014-12-11 15:44:53 +01:00
|
|
|
return isolated
|
2014-11-24 03:07:53 +01:00
|
|
|
|
|
|
|
|
2014-02-27 12:22:08 +01:00
|
|
|
def main(args=None):
|
|
|
|
if args is None:
|
|
|
|
args = sys.argv[1:]
|
2012-11-10 05:17:58 +01:00
|
|
|
|
2014-08-31 01:52:28 +02:00
|
|
|
# Enable our Deprecation Warnings
|
|
|
|
for deprecation_warning in deprecation.DEPRECATIONS:
|
|
|
|
warnings.simplefilter("default", deprecation_warning)
|
|
|
|
|
|
|
|
# Configure our deprecation warnings to be sent through loggers
|
|
|
|
deprecation.install_warning_logger()
|
|
|
|
|
2009-11-07 04:46:30 +01:00
|
|
|
autocomplete()
|
2012-11-10 05:17:58 +01:00
|
|
|
|
|
|
|
try:
|
2014-02-27 12:22:08 +01:00
|
|
|
cmd_name, cmd_args = parseopts(args)
|
2014-01-27 14:02:10 +01:00
|
|
|
except PipError as exc:
|
|
|
|
sys.stderr.write("ERROR: %s" % exc)
|
2012-11-10 05:17:58 +01:00
|
|
|
sys.stderr.write(os.linesep)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2014-12-02 16:47:36 +01:00
|
|
|
command = commands_dict[cmd_name](isolated=check_isolated(cmd_args))
|
2013-09-10 05:56:46 +02:00
|
|
|
return command.main(cmd_args)
|
2008-10-18 00:01:58 +02:00
|
|
|
|
2011-05-04 09:44:02 +02:00
|
|
|
|
2014-03-26 23:24:19 +01:00
|
|
|
# ###########################################################
|
|
|
|
# # Writing freeze files
|
2008-10-16 00:02:57 +02:00
|
|
|
|
|
|
|
class FrozenRequirement(object):
|
|
|
|
|
|
|
|
def __init__(self, name, req, editable, comments=()):
|
|
|
|
self.name = name
|
|
|
|
self.req = req
|
|
|
|
self.editable = editable
|
|
|
|
self.comments = comments
|
|
|
|
|
|
|
|
_rev_re = re.compile(r'-r(\d+)$')
|
|
|
|
_date_re = re.compile(r'-(20\d\d\d\d\d\d)$')
|
|
|
|
|
|
|
|
@classmethod
|
2014-08-01 22:20:23 +02:00
|
|
|
def from_dist(cls, dist, dependency_links, find_tags=False):
|
2008-10-16 00:02:57 +02:00
|
|
|
location = os.path.normcase(os.path.abspath(dist.location))
|
|
|
|
comments = []
|
2010-07-26 04:54:16 +02:00
|
|
|
from pip.vcs import vcs, get_src_requirement
|
2009-01-20 14:40:19 +01:00
|
|
|
if vcs.get_backend_name(location):
|
2008-10-16 00:02:57 +02:00
|
|
|
editable = True
|
2012-03-19 20:55:39 +01:00
|
|
|
try:
|
|
|
|
req = get_src_requirement(dist, location, find_tags)
|
2014-01-27 14:02:10 +01:00
|
|
|
except InstallationError as exc:
|
2014-08-31 01:52:28 +02:00
|
|
|
logger.warning(
|
2014-01-27 15:07:10 +01:00
|
|
|
"Error when trying to get requirement for VCS system %s, "
|
2014-08-31 01:52:28 +02:00
|
|
|
"falling back to uneditable format", exc
|
2014-01-27 15:07:10 +01:00
|
|
|
)
|
2012-03-19 20:55:39 +01:00
|
|
|
req = None
|
2008-10-16 00:02:57 +02:00
|
|
|
if req is None:
|
2014-08-31 01:52:28 +02:00
|
|
|
logger.warning(
|
|
|
|
'Could not determine repository location of %s', location
|
2014-01-27 15:07:10 +01:00
|
|
|
)
|
|
|
|
comments.append(
|
|
|
|
'## !! Could not determine repository location'
|
|
|
|
)
|
2008-10-16 00:02:57 +02:00
|
|
|
req = dist.as_requirement()
|
|
|
|
editable = False
|
|
|
|
else:
|
|
|
|
editable = False
|
|
|
|
req = dist.as_requirement()
|
|
|
|
specs = req.specs
|
2015-03-09 13:49:05 +01:00
|
|
|
assert len(specs) == 1 and specs[0][0] in ["==", "==="], \
|
|
|
|
'Expected 1 spec with == or ===; specs = %r; dist = %r' % \
|
|
|
|
(specs, dist)
|
2014-08-01 22:20:23 +02:00
|
|
|
version = specs[0][1]
|
|
|
|
ver_match = cls._rev_re.search(version)
|
|
|
|
date_match = cls._date_re.search(version)
|
|
|
|
if ver_match or date_match:
|
|
|
|
svn_backend = vcs.get_backend('svn')
|
|
|
|
if svn_backend:
|
|
|
|
svn_location = svn_backend().get_location(
|
|
|
|
dist,
|
|
|
|
dependency_links,
|
|
|
|
)
|
|
|
|
if not svn_location:
|
2014-08-31 01:52:28 +02:00
|
|
|
logger.warning(
|
|
|
|
'Warning: cannot find svn location for %s', req)
|
2014-08-01 22:20:23 +02:00
|
|
|
comments.append(
|
|
|
|
'## FIXME: could not find svn URL in dependency_links '
|
|
|
|
'for this package:'
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
comments.append(
|
|
|
|
'# Installing as editable to satisfy requirement %s:' %
|
|
|
|
req
|
|
|
|
)
|
|
|
|
if ver_match:
|
|
|
|
rev = ver_match.group(1)
|
|
|
|
else:
|
|
|
|
rev = '{%s}' % date_match.group(1)
|
|
|
|
editable = True
|
|
|
|
req = '%s@%s#egg=%s' % (
|
|
|
|
svn_location,
|
|
|
|
rev,
|
|
|
|
cls.egg_name(dist)
|
|
|
|
)
|
2008-10-16 00:02:57 +02:00
|
|
|
return cls(dist.project_name, req, editable, comments)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def egg_name(dist):
|
|
|
|
name = dist.egg_name()
|
|
|
|
match = re.search(r'-py\d\.\d$', name)
|
|
|
|
if match:
|
|
|
|
name = name[:match.start()]
|
|
|
|
return name
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
req = self.req
|
|
|
|
if self.editable:
|
|
|
|
req = '-e %s' % req
|
2012-09-01 19:33:52 +02:00
|
|
|
return '\n'.join(list(self.comments) + [str(req)]) + '\n'
|
2008-10-16 00:02:57 +02:00
|
|
|
|
2009-11-20 22:03:40 +01:00
|
|
|
|
2008-10-16 00:02:57 +02:00
|
|
|
if __name__ == '__main__':
|
2014-02-27 12:22:08 +01:00
|
|
|
sys.exit(main())
|