"""Base Command class, and related routines"""
from __future__ import absolute_import
from pip import cmdoptions
from pip.index import PackageFinder
from pip.locations import running_under_virtualenv
from pip.download import PipSession
from pip.exceptions import (BadCommand, InstallationError, UninstallationError,
CommandError, PreviousBuildDirError)
from pip.compat import logging_dictConfig
from pip.baseparser import ConfigOptionParser, UpdatingDefaultsHelpFormatter
from pip.req import InstallRequirement, parse_requirements
from pip.status_codes import (
SUCCESS, ERROR, UNKNOWN_ERROR, VIRTUALENV_NOT_FOUND,
PREVIOUS_BUILD_DIR_ERROR,
from pip.utils import deprecation, get_prog, normalize_path
from pip.utils.logging import IndentingFormatter
from pip.utils.outdated import pip_version_check
logger = logging.getLogger(__name__)
log_streams = ("ext://sys.stdout", "ext://sys.stderr")
def __init__(self, isolated=False):
'prog': '%s %s' % (get_prog(), self.name),
'formatter': UpdatingDefaultsHelpFormatter(),
'add_help_option': False,
'description': self.__doc__,
self.parser = ConfigOptionParser(**parser_kw)
# Commands should add options to this option group
optgroup_name = '%s Options' % self.name.capitalize()
self.cmd_opts = optparse.OptionGroup(self.parser, optgroup_name)
# Add the general options
gen_opts = cmdoptions.make_option_group(
cmdoptions.general_group,
self.parser.add_option_group(gen_opts)
def _build_session(self, options, retries=None, timeout=None):
normalize_path(os.path.join(options.cache_dir, "http"))
if options.cache_dir else None
retries=retries if retries is not None else options.retries,
insecure_hosts=options.trusted_hosts,
# Handle custom ca-bundles from the user
session.verify = options.cert
# Handle SSL client certificate
session.cert = options.client_cert
if options.timeout or timeout:
timeout if timeout is not None else options.timeout
# Handle configured proxies
# Determine if we can prompt the user for authentication or not
session.auth.prompting = not options.no_input
def parse_args(self, args):
# factored out for testability
return self.parser.parse_args(args)
options, args = self.parse_args(args)
# The root logger should match the "console" level *unless* we
# specified "--log" to send debug logs to a file.
"disable_existing_loggers": False,
"()": "pip.utils.logging.MaxLevelFilter",
"level": logging.WARNING,
"()": IndentingFormatter,
"class": "pip.utils.logging.ColorizedStreamHandler",
"stream": self.log_streams[0],
"filters": ["exclude_warnings"],
"class": "pip.utils.logging.ColorizedStreamHandler",
"stream": self.log_streams[1],
"class": "pip.utils.logging.BetterRotatingFileHandler",
"filename": options.log or "/dev/null",
"handlers": list(filter(None, [
"user_log" if options.log else None,
# Disable any logging besides WARNING unless we have DEBUG level
# logging enabled. These use both pip._vendor and the bare names
# for the case where someone unbundles our libraries.
if level in ["INFO", "ERROR"]
for name in ["pip._vendor", "distlib", "requests", "urllib3"]
if sys.version_info[:2] == (2, 6):
"Python 2.6 is no longer supported by the Python core team, "
"please upgrade your Python. A future version of pip will "
"drop support for Python 2.6",
deprecation.Python26DeprecationWarning
# TODO: try to get these passing down from the command?
# without resorting to os.environ to hold these.
os.environ['PIP_NO_INPUT'] = '1'
if options.exists_action:
os.environ['PIP_EXISTS_ACTION'] = ' '.join(options.exists_action)
# If a venv is required check if it can really be found
if not running_under_virtualenv():
'Could not find an activated virtualenv (required).'
sys.exit(VIRTUALENV_NOT_FOUND)
status = self.run(options, args)
# FIXME: all commands should return an exit status
# and when it is done, isinstance is not needed anymore
if isinstance(status, int):
except PreviousBuildDirError as exc:
logger.critical(str(exc))
logger.debug('Exception information:', exc_info=True)
return PREVIOUS_BUILD_DIR_ERROR
except (InstallationError, UninstallationError, BadCommand) as exc:
logger.critical(str(exc))
logger.debug('Exception information:', exc_info=True)
except CommandError as exc:
logger.critical('ERROR: %s', exc)
logger.debug('Exception information:', exc_info=True)
except KeyboardInterrupt:
logger.critical('Operation cancelled by user')
logger.debug('Exception information:', exc_info=True)
logger.critical('Exception:', exc_info=True)
# Check if we're using the latest version of pip available
if (not options.disable_pip_version_check and not
getattr(options, "no_index", False)):
with self._build_session(
timeout=min(5, options.timeout)) as session:
pip_version_check(session)
class RequirementCommand(Command):
def populate_requirement_set(requirement_set, args, options, finder,
session, name, wheel_cache):
Marshal cmd line args into a requirement set.
for filename in options.constraints:
for req in parse_requirements(
constraint=True, finder=finder, options=options,
session=session, wheel_cache=wheel_cache):
requirement_set.add_requirement(req)
requirement_set.add_requirement(
InstallRequirement.from_line(
req, None, isolated=options.isolated_mode,
for req in options.editables:
requirement_set.add_requirement(
InstallRequirement.from_editable(
default_vcs=options.default_vcs,
isolated=options.isolated_mode,
found_req_in_file = False
for filename in options.requirements:
for req in parse_requirements(
finder=finder, options=options, session=session,
wheel_cache=wheel_cache):
requirement_set.add_requirement(req)
# If --require-hashes was a line in a requirements file, tell
# RequirementSet about it:
requirement_set.require_hashes = options.require_hashes
if not (args or options.editables or found_req_in_file):
msg = ('You must give at least one requirement to '
'%(name)s (maybe you meant "pip %(name)s '
dict(opts, links=' '.join(options.find_links)))
msg = ('You must give at least one requirement '
'to %(name)s (see "pip help %(name)s")' % opts)
def _build_package_finder(self, options, session,
platform=None, python_versions=None,
abi=None, implementation=None):
Create a package finder appropriate to this requirement command.
index_urls = [options.index_url] + options.extra_index_urls
logger.debug('Ignoring indexes: %s', ','.join(index_urls))
find_links=options.find_links,
format_control=options.format_control,
trusted_hosts=options.trusted_hosts,
allow_all_prereleases=options.pre,
process_dependency_links=options.process_dependency_links,
versions=python_versions,
implementation=implementation,