Path: blob/master/venv/Lib/site-packages/pip/_internal/cli/cmdoptions.py
811 views
"""1shared options and groups23The principle here is to define options once, but *not* instantiate them4globally. One reason being that options with action='append' can carry state5between parses. pip parses general options twice internally, and shouldn't6pass on state. To be consistent, all options will follow this design.7"""89# The following comment should be removed at some point in the future.10# mypy: strict-optional=False1112from __future__ import absolute_import1314import logging15import os16import textwrap17import warnings18from distutils.util import strtobool19from functools import partial20from optparse import SUPPRESS_HELP, Option, OptionGroup21from textwrap import dedent2223from pip._internal.cli.progress_bars import BAR_TYPES24from pip._internal.exceptions import CommandError25from pip._internal.locations import USER_CACHE_DIR, get_src_prefix26from pip._internal.models.format_control import FormatControl27from pip._internal.models.index import PyPI28from pip._internal.models.target_python import TargetPython29from pip._internal.utils.hashes import STRONG_HASHES30from pip._internal.utils.typing import MYPY_CHECK_RUNNING3132if MYPY_CHECK_RUNNING:33from typing import Any, Callable, Dict, Optional, Tuple34from optparse import OptionParser, Values35from pip._internal.cli.parser import ConfigOptionParser3637logger = logging.getLogger(__name__)383940def raise_option_error(parser, option, msg):41# type: (OptionParser, Option, str) -> None42"""43Raise an option parsing error using parser.error().4445Args:46parser: an OptionParser instance.47option: an Option instance.48msg: the error text.49"""50msg = '{} error: {}'.format(option, msg)51msg = textwrap.fill(' '.join(msg.split()))52parser.error(msg)535455def make_option_group(group, parser):56# type: (Dict[str, Any], ConfigOptionParser) -> OptionGroup57"""58Return an OptionGroup object59group -- assumed to be dict with 'name' and 'options' keys60parser -- an optparse Parser61"""62option_group = OptionGroup(parser, group['name'])63for option in group['options']:64option_group.add_option(option())65return option_group666768def check_install_build_global(options, check_options=None):69# type: (Values, Optional[Values]) -> None70"""Disable wheels if per-setup.py call options are set.7172:param options: The OptionParser options to update.73:param check_options: The options to check, if not supplied defaults to74options.75"""76if check_options is None:77check_options = options7879def getname(n):80# type: (str) -> Optional[Any]81return getattr(check_options, n, None)82names = ["build_options", "global_options", "install_options"]83if any(map(getname, names)):84control = options.format_control85control.disallow_binaries()86warnings.warn(87'Disabling all use of wheels due to the use of --build-option '88'/ --global-option / --install-option.', stacklevel=2,89)909192def check_dist_restriction(options, check_target=False):93# type: (Values, bool) -> None94"""Function for determining if custom platform options are allowed.9596:param options: The OptionParser options.97:param check_target: Whether or not to check if --target is being used.98"""99dist_restriction_set = any([100options.python_version,101options.platform,102options.abi,103options.implementation,104])105106binary_only = FormatControl(set(), {':all:'})107sdist_dependencies_allowed = (108options.format_control != binary_only and109not options.ignore_dependencies110)111112# Installations or downloads using dist restrictions must not combine113# source distributions and dist-specific wheels, as they are not114# guaranteed to be locally compatible.115if dist_restriction_set and sdist_dependencies_allowed:116raise CommandError(117"When restricting platform and interpreter constraints using "118"--python-version, --platform, --abi, or --implementation, "119"either --no-deps must be set, or --only-binary=:all: must be "120"set and --no-binary must not be set (or must be set to "121":none:)."122)123124if check_target:125if dist_restriction_set and not options.target_dir:126raise CommandError(127"Can not use any platform or abi specific options unless "128"installing via '--target'"129)130131132def _path_option_check(option, opt, value):133# type: (Option, str, str) -> str134return os.path.expanduser(value)135136137class PipOption(Option):138TYPES = Option.TYPES + ("path",)139TYPE_CHECKER = Option.TYPE_CHECKER.copy()140TYPE_CHECKER["path"] = _path_option_check141142143###########144# options #145###########146147help_ = partial(148Option,149'-h', '--help',150dest='help',151action='help',152help='Show help.',153) # type: Callable[..., Option]154155isolated_mode = partial(156Option,157"--isolated",158dest="isolated_mode",159action="store_true",160default=False,161help=(162"Run pip in an isolated mode, ignoring environment variables and user "163"configuration."164),165) # type: Callable[..., Option]166167require_virtualenv = partial(168Option,169# Run only if inside a virtualenv, bail if not.170'--require-virtualenv', '--require-venv',171dest='require_venv',172action='store_true',173default=False,174help=SUPPRESS_HELP175) # type: Callable[..., Option]176177verbose = partial(178Option,179'-v', '--verbose',180dest='verbose',181action='count',182default=0,183help='Give more output. Option is additive, and can be used up to 3 times.'184) # type: Callable[..., Option]185186no_color = partial(187Option,188'--no-color',189dest='no_color',190action='store_true',191default=False,192help="Suppress colored output",193) # type: Callable[..., Option]194195version = partial(196Option,197'-V', '--version',198dest='version',199action='store_true',200help='Show version and exit.',201) # type: Callable[..., Option]202203quiet = partial(204Option,205'-q', '--quiet',206dest='quiet',207action='count',208default=0,209help=(210'Give less output. Option is additive, and can be used up to 3'211' times (corresponding to WARNING, ERROR, and CRITICAL logging'212' levels).'213),214) # type: Callable[..., Option]215216progress_bar = partial(217Option,218'--progress-bar',219dest='progress_bar',220type='choice',221choices=list(BAR_TYPES.keys()),222default='on',223help=(224'Specify type of progress to be displayed [' +225'|'.join(BAR_TYPES.keys()) + '] (default: %default)'226),227) # type: Callable[..., Option]228229log = partial(230PipOption,231"--log", "--log-file", "--local-log",232dest="log",233metavar="path",234type="path",235help="Path to a verbose appending log."236) # type: Callable[..., Option]237238no_input = partial(239Option,240# Don't ask for input241'--no-input',242dest='no_input',243action='store_true',244default=False,245help=SUPPRESS_HELP246) # type: Callable[..., Option]247248proxy = partial(249Option,250'--proxy',251dest='proxy',252type='str',253default='',254help="Specify a proxy in the form [user:passwd@]proxy.server:port."255) # type: Callable[..., Option]256257retries = partial(258Option,259'--retries',260dest='retries',261type='int',262default=5,263help="Maximum number of retries each connection should attempt "264"(default %default times).",265) # type: Callable[..., Option]266267timeout = partial(268Option,269'--timeout', '--default-timeout',270metavar='sec',271dest='timeout',272type='float',273default=15,274help='Set the socket timeout (default %default seconds).',275) # type: Callable[..., Option]276277278def exists_action():279# type: () -> Option280return Option(281# Option when path already exist282'--exists-action',283dest='exists_action',284type='choice',285choices=['s', 'i', 'w', 'b', 'a'],286default=[],287action='append',288metavar='action',289help="Default action when a path already exists: "290"(s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.",291)292293294cert = partial(295PipOption,296'--cert',297dest='cert',298type='path',299metavar='path',300help="Path to alternate CA bundle.",301) # type: Callable[..., Option]302303client_cert = partial(304PipOption,305'--client-cert',306dest='client_cert',307type='path',308default=None,309metavar='path',310help="Path to SSL client certificate, a single file containing the "311"private key and the certificate in PEM format.",312) # type: Callable[..., Option]313314index_url = partial(315Option,316'-i', '--index-url', '--pypi-url',317dest='index_url',318metavar='URL',319default=PyPI.simple_url,320help="Base URL of the Python Package Index (default %default). "321"This should point to a repository compliant with PEP 503 "322"(the simple repository API) or a local directory laid out "323"in the same format.",324) # type: Callable[..., Option]325326327def extra_index_url():328# type: () -> Option329return Option(330'--extra-index-url',331dest='extra_index_urls',332metavar='URL',333action='append',334default=[],335help="Extra URLs of package indexes to use in addition to "336"--index-url. Should follow the same rules as "337"--index-url.",338)339340341no_index = partial(342Option,343'--no-index',344dest='no_index',345action='store_true',346default=False,347help='Ignore package index (only looking at --find-links URLs instead).',348) # type: Callable[..., Option]349350351def find_links():352# type: () -> Option353return Option(354'-f', '--find-links',355dest='find_links',356action='append',357default=[],358metavar='url',359help="If a URL or path to an html file, then parse for links to "360"archives such as sdist (.tar.gz) or wheel (.whl) files. "361"If a local path or file:// URL that's a directory, "362"then look for archives in the directory listing. "363"Links to VCS project URLs are not supported.",364)365366367def trusted_host():368# type: () -> Option369return Option(370"--trusted-host",371dest="trusted_hosts",372action="append",373metavar="HOSTNAME",374default=[],375help="Mark this host or host:port pair as trusted, even though it "376"does not have valid or any HTTPS.",377)378379380def constraints():381# type: () -> Option382return Option(383'-c', '--constraint',384dest='constraints',385action='append',386default=[],387metavar='file',388help='Constrain versions using the given constraints file. '389'This option can be used multiple times.'390)391392393def requirements():394# type: () -> Option395return Option(396'-r', '--requirement',397dest='requirements',398action='append',399default=[],400metavar='file',401help='Install from the given requirements file. '402'This option can be used multiple times.'403)404405406def editable():407# type: () -> Option408return Option(409'-e', '--editable',410dest='editables',411action='append',412default=[],413metavar='path/url',414help=('Install a project in editable mode (i.e. setuptools '415'"develop mode") from a local project path or a VCS url.'),416)417418419def _handle_src(option, opt_str, value, parser):420# type: (Option, str, str, OptionParser) -> None421value = os.path.abspath(value)422setattr(parser.values, option.dest, value)423424425src = partial(426PipOption,427'--src', '--source', '--source-dir', '--source-directory',428dest='src_dir',429type='path',430metavar='dir',431default=get_src_prefix(),432action='callback',433callback=_handle_src,434help='Directory to check out editable projects into. '435'The default in a virtualenv is "<venv path>/src". '436'The default for global installs is "<current dir>/src".'437) # type: Callable[..., Option]438439440def _get_format_control(values, option):441# type: (Values, Option) -> Any442"""Get a format_control object."""443return getattr(values, option.dest)444445446def _handle_no_binary(option, opt_str, value, parser):447# type: (Option, str, str, OptionParser) -> None448existing = _get_format_control(parser.values, option)449FormatControl.handle_mutual_excludes(450value, existing.no_binary, existing.only_binary,451)452453454def _handle_only_binary(option, opt_str, value, parser):455# type: (Option, str, str, OptionParser) -> None456existing = _get_format_control(parser.values, option)457FormatControl.handle_mutual_excludes(458value, existing.only_binary, existing.no_binary,459)460461462def no_binary():463# type: () -> Option464format_control = FormatControl(set(), set())465return Option(466"--no-binary", dest="format_control", action="callback",467callback=_handle_no_binary, type="str",468default=format_control,469help='Do not use binary packages. Can be supplied multiple times, and '470'each time adds to the existing value. Accepts either ":all:" to '471'disable all binary packages, ":none:" to empty the set (notice '472'the colons), or one or more package names with commas between '473'them (no colons). Note that some packages are tricky to compile '474'and may fail to install when this option is used on them.',475)476477478def only_binary():479# type: () -> Option480format_control = FormatControl(set(), set())481return Option(482"--only-binary", dest="format_control", action="callback",483callback=_handle_only_binary, type="str",484default=format_control,485help='Do not use source packages. Can be supplied multiple times, and '486'each time adds to the existing value. Accepts either ":all:" to '487'disable all source packages, ":none:" to empty the set, or one '488'or more package names with commas between them. Packages '489'without binary distributions will fail to install when this '490'option is used on them.',491)492493494platform = partial(495Option,496'--platform',497dest='platform',498metavar='platform',499default=None,500help=("Only use wheels compatible with <platform>. "501"Defaults to the platform of the running system."),502) # type: Callable[..., Option]503504505# This was made a separate function for unit-testing purposes.506def _convert_python_version(value):507# type: (str) -> Tuple[Tuple[int, ...], Optional[str]]508"""509Convert a version string like "3", "37", or "3.7.3" into a tuple of ints.510511:return: A 2-tuple (version_info, error_msg), where `error_msg` is512non-None if and only if there was a parsing error.513"""514if not value:515# The empty string is the same as not providing a value.516return (None, None)517518parts = value.split('.')519if len(parts) > 3:520return ((), 'at most three version parts are allowed')521522if len(parts) == 1:523# Then we are in the case of "3" or "37".524value = parts[0]525if len(value) > 1:526parts = [value[0], value[1:]]527528try:529version_info = tuple(int(part) for part in parts)530except ValueError:531return ((), 'each version part must be an integer')532533return (version_info, None)534535536def _handle_python_version(option, opt_str, value, parser):537# type: (Option, str, str, OptionParser) -> None538"""539Handle a provided --python-version value.540"""541version_info, error_msg = _convert_python_version(value)542if error_msg is not None:543msg = (544'invalid --python-version value: {!r}: {}'.format(545value, error_msg,546)547)548raise_option_error(parser, option=option, msg=msg)549550parser.values.python_version = version_info551552553python_version = partial(554Option,555'--python-version',556dest='python_version',557metavar='python_version',558action='callback',559callback=_handle_python_version, type='str',560default=None,561help=dedent("""\562The Python interpreter version to use for wheel and "Requires-Python"563compatibility checks. Defaults to a version derived from the running564interpreter. The version can be specified using up to three dot-separated565integers (e.g. "3" for 3.0.0, "3.7" for 3.7.0, or "3.7.3"). A major-minor566version can also be given as a string without dots (e.g. "37" for 3.7.0).567"""),568) # type: Callable[..., Option]569570571implementation = partial(572Option,573'--implementation',574dest='implementation',575metavar='implementation',576default=None,577help=("Only use wheels compatible with Python "578"implementation <implementation>, e.g. 'pp', 'jy', 'cp', "579" or 'ip'. If not specified, then the current "580"interpreter implementation is used. Use 'py' to force "581"implementation-agnostic wheels."),582) # type: Callable[..., Option]583584585abi = partial(586Option,587'--abi',588dest='abi',589metavar='abi',590default=None,591help=("Only use wheels compatible with Python "592"abi <abi>, e.g. 'pypy_41'. If not specified, then the "593"current interpreter abi tag is used. Generally "594"you will need to specify --implementation, "595"--platform, and --python-version when using "596"this option."),597) # type: Callable[..., Option]598599600def add_target_python_options(cmd_opts):601# type: (OptionGroup) -> None602cmd_opts.add_option(platform())603cmd_opts.add_option(python_version())604cmd_opts.add_option(implementation())605cmd_opts.add_option(abi())606607608def make_target_python(options):609# type: (Values) -> TargetPython610target_python = TargetPython(611platform=options.platform,612py_version_info=options.python_version,613abi=options.abi,614implementation=options.implementation,615)616617return target_python618619620def prefer_binary():621# type: () -> Option622return Option(623"--prefer-binary",624dest="prefer_binary",625action="store_true",626default=False,627help="Prefer older binary packages over newer source packages."628)629630631cache_dir = partial(632PipOption,633"--cache-dir",634dest="cache_dir",635default=USER_CACHE_DIR,636metavar="dir",637type='path',638help="Store the cache data in <dir>."639) # type: Callable[..., Option]640641642def _handle_no_cache_dir(option, opt, value, parser):643# type: (Option, str, str, OptionParser) -> None644"""645Process a value provided for the --no-cache-dir option.646647This is an optparse.Option callback for the --no-cache-dir option.648"""649# The value argument will be None if --no-cache-dir is passed via the650# command-line, since the option doesn't accept arguments. However,651# the value can be non-None if the option is triggered e.g. by an652# environment variable, like PIP_NO_CACHE_DIR=true.653if value is not None:654# Then parse the string value to get argument error-checking.655try:656strtobool(value)657except ValueError as exc:658raise_option_error(parser, option=option, msg=str(exc))659660# Originally, setting PIP_NO_CACHE_DIR to a value that strtobool()661# converted to 0 (like "false" or "no") caused cache_dir to be disabled662# rather than enabled (logic would say the latter). Thus, we disable663# the cache directory not just on values that parse to True, but (for664# backwards compatibility reasons) also on values that parse to False.665# In other words, always set it to False if the option is provided in666# some (valid) form.667parser.values.cache_dir = False668669670no_cache = partial(671Option,672"--no-cache-dir",673dest="cache_dir",674action="callback",675callback=_handle_no_cache_dir,676help="Disable the cache.",677) # type: Callable[..., Option]678679no_deps = partial(680Option,681'--no-deps', '--no-dependencies',682dest='ignore_dependencies',683action='store_true',684default=False,685help="Don't install package dependencies.",686) # type: Callable[..., Option]687688689def _handle_build_dir(option, opt, value, parser):690# type: (Option, str, str, OptionParser) -> None691if value:692value = os.path.abspath(value)693setattr(parser.values, option.dest, value)694695696build_dir = partial(697PipOption,698'-b', '--build', '--build-dir', '--build-directory',699dest='build_dir',700type='path',701metavar='dir',702action='callback',703callback=_handle_build_dir,704help='Directory to unpack packages into and build in. Note that '705'an initial build still takes place in a temporary directory. '706'The location of temporary directories can be controlled by setting '707'the TMPDIR environment variable (TEMP on Windows) appropriately. '708'When passed, build directories are not cleaned in case of failures.'709) # type: Callable[..., Option]710711ignore_requires_python = partial(712Option,713'--ignore-requires-python',714dest='ignore_requires_python',715action='store_true',716help='Ignore the Requires-Python information.'717) # type: Callable[..., Option]718719no_build_isolation = partial(720Option,721'--no-build-isolation',722dest='build_isolation',723action='store_false',724default=True,725help='Disable isolation when building a modern source distribution. '726'Build dependencies specified by PEP 518 must be already installed '727'if this option is used.'728) # type: Callable[..., Option]729730731def _handle_no_use_pep517(option, opt, value, parser):732# type: (Option, str, str, OptionParser) -> None733"""734Process a value provided for the --no-use-pep517 option.735736This is an optparse.Option callback for the no_use_pep517 option.737"""738# Since --no-use-pep517 doesn't accept arguments, the value argument739# will be None if --no-use-pep517 is passed via the command-line.740# However, the value can be non-None if the option is triggered e.g.741# by an environment variable, for example "PIP_NO_USE_PEP517=true".742if value is not None:743msg = """A value was passed for --no-use-pep517,744probably using either the PIP_NO_USE_PEP517 environment variable745or the "no-use-pep517" config file option. Use an appropriate value746of the PIP_USE_PEP517 environment variable or the "use-pep517"747config file option instead.748"""749raise_option_error(parser, option=option, msg=msg)750751# Otherwise, --no-use-pep517 was passed via the command-line.752parser.values.use_pep517 = False753754755use_pep517 = partial(756Option,757'--use-pep517',758dest='use_pep517',759action='store_true',760default=None,761help='Use PEP 517 for building source distributions '762'(use --no-use-pep517 to force legacy behaviour).'763) # type: Any764765no_use_pep517 = partial(766Option,767'--no-use-pep517',768dest='use_pep517',769action='callback',770callback=_handle_no_use_pep517,771default=None,772help=SUPPRESS_HELP773) # type: Any774775install_options = partial(776Option,777'--install-option',778dest='install_options',779action='append',780metavar='options',781help="Extra arguments to be supplied to the setup.py install "782"command (use like --install-option=\"--install-scripts=/usr/local/"783"bin\"). Use multiple --install-option options to pass multiple "784"options to setup.py install. If you are using an option with a "785"directory path, be sure to use absolute path.",786) # type: Callable[..., Option]787788global_options = partial(789Option,790'--global-option',791dest='global_options',792action='append',793metavar='options',794help="Extra global options to be supplied to the setup.py "795"call before the install command.",796) # type: Callable[..., Option]797798no_clean = partial(799Option,800'--no-clean',801action='store_true',802default=False,803help="Don't clean up build directories."804) # type: Callable[..., Option]805806pre = partial(807Option,808'--pre',809action='store_true',810default=False,811help="Include pre-release and development versions. By default, "812"pip only finds stable versions.",813) # type: Callable[..., Option]814815disable_pip_version_check = partial(816Option,817"--disable-pip-version-check",818dest="disable_pip_version_check",819action="store_true",820default=False,821help="Don't periodically check PyPI to determine whether a new version "822"of pip is available for download. Implied with --no-index.",823) # type: Callable[..., Option]824825826# Deprecated, Remove later827always_unzip = partial(828Option,829'-Z', '--always-unzip',830dest='always_unzip',831action='store_true',832help=SUPPRESS_HELP,833) # type: Callable[..., Option]834835836def _handle_merge_hash(option, opt_str, value, parser):837# type: (Option, str, str, OptionParser) -> None838"""Given a value spelled "algo:digest", append the digest to a list839pointed to in a dict by the algo name."""840if not parser.values.hashes:841parser.values.hashes = {}842try:843algo, digest = value.split(':', 1)844except ValueError:845parser.error('Arguments to {} must be a hash name '846'followed by a value, like --hash=sha256:'847'abcde...'.format(opt_str))848if algo not in STRONG_HASHES:849parser.error('Allowed hash algorithms for {} are {}.'.format(850opt_str, ', '.join(STRONG_HASHES)))851parser.values.hashes.setdefault(algo, []).append(digest)852853854hash = partial(855Option,856'--hash',857# Hash values eventually end up in InstallRequirement.hashes due to858# __dict__ copying in process_line().859dest='hashes',860action='callback',861callback=_handle_merge_hash,862type='string',863help="Verify that the package's archive matches this "864'hash before installing. Example: --hash=sha256:abcdef...',865) # type: Callable[..., Option]866867868require_hashes = partial(869Option,870'--require-hashes',871dest='require_hashes',872action='store_true',873default=False,874help='Require a hash to check each requirement against, for '875'repeatable installs. This option is implied when any package in a '876'requirements file has a --hash option.',877) # type: Callable[..., Option]878879880list_path = partial(881PipOption,882'--path',883dest='path',884type='path',885action='append',886help='Restrict to the specified installation path for listing '887'packages (can be used multiple times).'888) # type: Callable[..., Option]889890891def check_list_path_option(options):892# type: (Values) -> None893if options.path and (options.user or options.local):894raise CommandError(895"Cannot combine '--path' with '--user' or '--local'"896)897898899no_python_version_warning = partial(900Option,901'--no-python-version-warning',902dest='no_python_version_warning',903action='store_true',904default=False,905help='Silence deprecation warnings for upcoming unsupported Pythons.',906) # type: Callable[..., Option]907908909unstable_feature = partial(910Option,911'--unstable-feature',912dest='unstable_features',913metavar='feature',914action='append',915default=[],916choices=['resolver'],917help=SUPPRESS_HELP, # TODO: Enable this when the resolver actually works.918# help='Enable unstable feature(s) that may be backward incompatible.',919) # type: Callable[..., Option]920921922##########923# groups #924##########925926general_group = {927'name': 'General Options',928'options': [929help_,930isolated_mode,931require_virtualenv,932verbose,933version,934quiet,935log,936no_input,937proxy,938retries,939timeout,940exists_action,941trusted_host,942cert,943client_cert,944cache_dir,945no_cache,946disable_pip_version_check,947no_color,948no_python_version_warning,949unstable_feature,950]951} # type: Dict[str, Any]952953index_group = {954'name': 'Package Index Options',955'options': [956index_url,957extra_index_url,958no_index,959find_links,960]961} # type: Dict[str, Any]962963964