2 shared options and groups
4 The principle here is to define options once, but *not* instantiate them
5 globally. One reason being that options with action='append' can carry state
6 between parses. pip parses general options twice internally, and shouldn't
7 pass on state. To be consistent, all options will follow this design.
10 # The following comment should be removed at some point in the future.
11 # mypy: strict-optional=False
17 from functools
import partial
18 from optparse
import SUPPRESS_HELP
, Option
, OptionGroup
, OptionParser
, Values
19 from textwrap
import dedent
20 from typing
import Any
, Callable
, Dict
, Optional
, Tuple
22 from pip
._vendor
.packaging
.utils
import canonicalize_name
24 from pip
._internal
.cli
.parser
import ConfigOptionParser
25 from pip
._internal
.exceptions
import CommandError
26 from pip
._internal
.locations
import USER_CACHE_DIR
, get_src_prefix
27 from pip
._internal
.models
.format_control
import FormatControl
28 from pip
._internal
.models
.index
import PyPI
29 from pip
._internal
.models
.target_python
import TargetPython
30 from pip
._internal
.utils
.hashes
import STRONG_HASHES
31 from pip
._internal
.utils
.misc
import strtobool
33 logger
= logging
.getLogger(__name__
)
36 def raise_option_error(parser
: OptionParser
, option
: Option
, msg
: str) -> None:
38 Raise an option parsing error using parser.error().
41 parser: an OptionParser instance.
42 option: an Option instance.
45 msg
= f
"{option} error: {msg}"
46 msg
= textwrap
.fill(" ".join(msg
.split()))
50 def make_option_group(group
: Dict
[str, Any
], parser
: ConfigOptionParser
) -> OptionGroup
:
52 Return an OptionGroup object
53 group -- assumed to be dict with 'name' and 'options' keys
54 parser -- an optparse Parser
56 option_group
= OptionGroup(parser
, group
["name"])
57 for option
in group
["options"]:
58 option_group
.add_option(option())
62 def check_dist_restriction(options
: Values
, check_target
: bool = False) -> None:
63 """Function for determining if custom platform options are allowed.
65 :param options: The OptionParser options.
66 :param check_target: Whether or not to check if --target is being used.
68 dist_restriction_set
= any(
70 options
.python_version
,
73 options
.implementation
,
77 binary_only
= FormatControl(set(), {":all:"}
)
78 sdist_dependencies_allowed
= (
79 options
.format_control
!= binary_only
and not options
.ignore_dependencies
82 # Installations or downloads using dist restrictions must not combine
83 # source distributions and dist-specific wheels, as they are not
84 # guaranteed to be locally compatible.
85 if dist_restriction_set
and sdist_dependencies_allowed
:
87 "When restricting platform and interpreter constraints using "
88 "--python-version, --platform, --abi, or --implementation, "
89 "either --no-deps must be set, or --only-binary=:all: must be "
90 "set and --no-binary must not be set (or must be set to "
95 if dist_restriction_set
and not options
.target_dir
:
97 "Can not use any platform or abi specific options unless "
98 "installing via '--target'"
102 def _path_option_check(option
: Option
, opt
: str, value
: str) -> str:
103 return os
.path
.expanduser(value
)
106 def _package_name_option_check(option
: Option
, opt
: str, value
: str) -> str:
107 return canonicalize_name(value
)
110 class PipOption(Option
):
111 TYPES
= Option
.TYPES
+ ("path", "package_name")
112 TYPE_CHECKER
= Option
.TYPE_CHECKER
.copy()
113 TYPE_CHECKER
["package_name"] = _package_name_option_check
114 TYPE_CHECKER
["path"] = _path_option_check
121 help_
: Callable
[..., Option
] = partial(
130 debug_mode
: Callable
[..., Option
] = partial(
137 "Let unhandled exceptions propagate outside the main subroutine, "
138 "instead of logging them to stderr."
142 isolated_mode
: Callable
[..., Option
] = partial(
145 dest
="isolated_mode",
149 "Run pip in an isolated mode, ignoring environment variables and user "
154 require_virtualenv
: Callable
[..., Option
] = partial(
156 "--require-virtualenv",
162 "Allow pip to only run in a virtual environment; "
163 "exit with an error otherwise."
167 override_externally_managed
: Callable
[..., Option
] = partial(
169 "--break-system-packages",
170 dest
="override_externally_managed",
172 help="Allow pip to modify an EXTERNALLY-MANAGED Python installation",
175 python
: Callable
[..., Option
] = partial(
179 help="Run pip with the specified Python interpreter.",
182 verbose
: Callable
[..., Option
] = partial(
189 help="Give more output. Option is additive, and can be used up to 3 times.",
192 no_color
: Callable
[..., Option
] = partial(
198 help="Suppress colored output.",
201 version
: Callable
[..., Option
] = partial(
207 help="Show version and exit.",
210 quiet
: Callable
[..., Option
] = partial(
218 "Give less output. Option is additive, and can be used up to 3"
219 " times (corresponding to WARNING, ERROR, and CRITICAL logging"
224 progress_bar
: Callable
[..., Option
] = partial(
229 choices
=["on", "off"],
231 help="Specify whether the progress bar should be used [on, off] (default: on)",
234 log
: Callable
[..., Option
] = partial(
242 help="Path to a verbose appending log.",
245 no_input
: Callable
[..., Option
] = partial(
247 # Don't ask for input
252 help="Disable prompting for input.",
255 keyring_provider
: Callable
[..., Option
] = partial(
257 "--keyring-provider",
258 dest
="keyring_provider",
259 choices
=["auto", "disabled", "import", "subprocess"],
262 "Enable the credential lookup via the keyring library if user input is allowed."
263 " Specify which mechanism to use [disabled, import, subprocess]."
264 " (default: disabled)"
268 proxy
: Callable
[..., Option
] = partial(
274 help="Specify a proxy in the form scheme://[user:passwd@]proxy.server:port.",
277 retries
: Callable
[..., Option
] = partial(
283 help="Maximum number of retries each connection should attempt "
284 "(default %default times).",
287 timeout
: Callable
[..., Option
] = partial(
295 help="Set the socket timeout (default %default seconds).",
299 def exists_action() -> Option
:
301 # Option when path already exist
303 dest
="exists_action",
305 choices
=["s", "i", "w", "b", "a"],
309 help="Default action when a path already exists: "
310 "(s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.",
314 cert
: Callable
[..., Option
] = partial(
321 "Path to PEM-encoded CA certificate bundle. "
322 "If provided, overrides the default. "
323 "See 'SSL Certificate Verification' in pip documentation "
324 "for more information."
328 client_cert
: Callable
[..., Option
] = partial(
335 help="Path to SSL client certificate, a single file containing the "
336 "private key and the certificate in PEM format.",
339 index_url
: Callable
[..., Option
] = partial(
346 default
=PyPI
.simple_url
,
347 help="Base URL of the Python Package Index (default %default). "
348 "This should point to a repository compliant with PEP 503 "
349 "(the simple repository API) or a local directory laid out "
350 "in the same format.",
354 def extra_index_url() -> Option
:
357 dest
="extra_index_urls",
361 help="Extra URLs of package indexes to use in addition to "
362 "--index-url. Should follow the same rules as "
367 no_index
: Callable
[..., Option
] = partial(
373 help="Ignore package index (only looking at --find-links URLs instead).",
377 def find_links() -> Option
:
385 help="If a URL or path to an html file, then parse for links to "
386 "archives such as sdist (.tar.gz) or wheel (.whl) files. "
387 "If a local path or file:// URL that's a directory, "
388 "then look for archives in the directory listing. "
389 "Links to VCS project URLs are not supported.",
393 def trusted_host() -> Option
:
396 dest
="trusted_hosts",
400 help="Mark this host or host:port pair as trusted, even though it "
401 "does not have valid or any HTTPS.",
405 def constraints() -> Option
:
413 help="Constrain versions using the given constraints file. "
414 "This option can be used multiple times.",
418 def requirements() -> Option
:
426 help="Install from the given requirements file. "
427 "This option can be used multiple times.",
431 def editable() -> Option
:
440 "Install a project in editable mode (i.e. setuptools "
441 '"develop mode") from a local project path or a VCS url.'
446 def _handle_src(option
: Option
, opt_str
: str, value
: str, parser
: OptionParser
) -> None:
447 value
= os
.path
.abspath(value
)
448 setattr(parser
.values
, option
.dest
, value
)
451 src
: Callable
[..., Option
] = partial(
456 "--source-directory",
460 default
=get_src_prefix(),
462 callback
=_handle_src
,
463 help="Directory to check out editable projects into. "
464 'The default in a virtualenv is "<venv path>/src". '
465 'The default for global installs is "<current dir>/src".',
469 def _get_format_control(values
: Values
, option
: Option
) -> Any
:
470 """Get a format_control object."""
471 return getattr(values
, option
.dest
)
474 def _handle_no_binary(
475 option
: Option
, opt_str
: str, value
: str, parser
: OptionParser
477 existing
= _get_format_control(parser
.values
, option
)
478 FormatControl
.handle_mutual_excludes(
481 existing
.only_binary
,
485 def _handle_only_binary(
486 option
: Option
, opt_str
: str, value
: str, parser
: OptionParser
488 existing
= _get_format_control(parser
.values
, option
)
489 FormatControl
.handle_mutual_excludes(
491 existing
.only_binary
,
496 def no_binary() -> Option
:
497 format_control
= FormatControl(set(), set())
500 dest
="format_control",
502 callback
=_handle_no_binary
,
504 default
=format_control
,
505 help="Do not use binary packages. Can be supplied multiple times, and "
506 'each time adds to the existing value. Accepts either ":all:" to '
507 'disable all binary packages, ":none:" to empty the set (notice '
508 "the colons), or one or more package names with commas between "
509 "them (no colons). Note that some packages are tricky to compile "
510 "and may fail to install when this option is used on them.",
514 def only_binary() -> Option
:
515 format_control
= FormatControl(set(), set())
518 dest
="format_control",
520 callback
=_handle_only_binary
,
522 default
=format_control
,
523 help="Do not use source packages. Can be supplied multiple times, and "
524 'each time adds to the existing value. Accepts either ":all:" to '
525 'disable all source packages, ":none:" to empty the set, or one '
526 "or more package names with commas between them. Packages "
527 "without binary distributions will fail to install when this "
528 "option is used on them.",
532 platforms
: Callable
[..., Option
] = partial(
540 "Only use wheels compatible with <platform>. Defaults to the "
541 "platform of the running system. Use this option multiple times to "
542 "specify multiple platforms supported by the target interpreter."
547 # This was made a separate function for unit-testing purposes.
548 def _convert_python_version(value
: str) -> Tuple
[Tuple
[int, ...], Optional
[str]]:
550 Convert a version string like "3", "37", or "3.7.3" into a tuple of ints.
552 :return: A 2-tuple (version_info, error_msg), where `error_msg` is
553 non-None if and only if there was a parsing error.
556 # The empty string is the same as not providing a value.
559 parts
= value
.split(".")
561 return ((), "at most three version parts are allowed")
564 # Then we are in the case of "3" or "37".
567 parts
= [value
[0], value
[1:]]
570 version_info
= tuple(int(part
) for part
in parts
)
572 return ((), "each version part must be an integer")
574 return (version_info
, None)
577 def _handle_python_version(
578 option
: Option
, opt_str
: str, value
: str, parser
: OptionParser
581 Handle a provided --python-version value.
583 version_info
, error_msg
= _convert_python_version(value
)
584 if error_msg
is not None:
585 msg
= "invalid --python-version value: {!r}: {}".format(
589 raise_option_error(parser
, option
=option
, msg
=msg
)
591 parser
.values
.python_version
= version_info
594 python_version
: Callable
[..., Option
] = partial(
597 dest
="python_version",
598 metavar
="python_version",
600 callback
=_handle_python_version
,
605 The Python interpreter version to use for wheel and "Requires-Python"
606 compatibility checks. Defaults to a version derived from the running
607 interpreter. The version can be specified using up to three dot-separated
608 integers (e.g. "3" for 3.0.0, "3.7" for 3.7.0, or "3.7.3"). A major-minor
609 version can also be given as a string without dots (e.g. "37" for 3.7.0).
615 implementation
: Callable
[..., Option
] = partial(
618 dest
="implementation",
619 metavar
="implementation",
622 "Only use wheels compatible with Python "
623 "implementation <implementation>, e.g. 'pp', 'jy', 'cp', "
624 " or 'ip'. If not specified, then the current "
625 "interpreter implementation is used. Use 'py' to force "
626 "implementation-agnostic wheels."
631 abis
: Callable
[..., Option
] = partial(
639 "Only use wheels compatible with Python abi <abi>, e.g. 'pypy_41'. "
640 "If not specified, then the current interpreter abi tag is used. "
641 "Use this option multiple times to specify multiple abis supported "
642 "by the target interpreter. Generally you will need to specify "
643 "--implementation, --platform, and --python-version when using this "
649 def add_target_python_options(cmd_opts
: OptionGroup
) -> None:
650 cmd_opts
.add_option(platforms())
651 cmd_opts
.add_option(python_version())
652 cmd_opts
.add_option(implementation())
653 cmd_opts
.add_option(abis())
656 def make_target_python(options
: Values
) -> TargetPython
:
657 target_python
= TargetPython(
658 platforms
=options
.platforms
,
659 py_version_info
=options
.python_version
,
661 implementation
=options
.implementation
,
667 def prefer_binary() -> Option
:
670 dest
="prefer_binary",
673 help="Prefer older binary packages over newer source packages.",
677 cache_dir
: Callable
[..., Option
] = partial(
681 default
=USER_CACHE_DIR
,
684 help="Store the cache data in <dir>.",
688 def _handle_no_cache_dir(
689 option
: Option
, opt
: str, value
: str, parser
: OptionParser
692 Process a value provided for the --no-cache-dir option.
694 This is an optparse.Option callback for the --no-cache-dir option.
696 # The value argument will be None if --no-cache-dir is passed via the
697 # command-line, since the option doesn't accept arguments. However,
698 # the value can be non-None if the option is triggered e.g. by an
699 # environment variable, like PIP_NO_CACHE_DIR=true.
700 if value
is not None:
701 # Then parse the string value to get argument error-checking.
704 except ValueError as exc
:
705 raise_option_error(parser
, option
=option
, msg
=str(exc
))
707 # Originally, setting PIP_NO_CACHE_DIR to a value that strtobool()
708 # converted to 0 (like "false" or "no") caused cache_dir to be disabled
709 # rather than enabled (logic would say the latter). Thus, we disable
710 # the cache directory not just on values that parse to True, but (for
711 # backwards compatibility reasons) also on values that parse to False.
712 # In other words, always set it to False if the option is provided in
714 parser
.values
.cache_dir
= False
717 no_cache
: Callable
[..., Option
] = partial(
722 callback
=_handle_no_cache_dir
,
723 help="Disable the cache.",
726 no_deps
: Callable
[..., Option
] = partial(
730 dest
="ignore_dependencies",
733 help="Don't install package dependencies.",
736 ignore_requires_python
: Callable
[..., Option
] = partial(
738 "--ignore-requires-python",
739 dest
="ignore_requires_python",
741 help="Ignore the Requires-Python information.",
744 no_build_isolation
: Callable
[..., Option
] = partial(
746 "--no-build-isolation",
747 dest
="build_isolation",
748 action
="store_false",
750 help="Disable isolation when building a modern source distribution. "
751 "Build dependencies specified by PEP 518 must be already installed "
752 "if this option is used.",
755 check_build_deps
: Callable
[..., Option
] = partial(
757 "--check-build-dependencies",
758 dest
="check_build_deps",
761 help="Check the build dependencies when PEP517 is used.",
765 def _handle_no_use_pep517(
766 option
: Option
, opt
: str, value
: str, parser
: OptionParser
769 Process a value provided for the --no-use-pep517 option.
771 This is an optparse.Option callback for the no_use_pep517 option.
773 # Since --no-use-pep517 doesn't accept arguments, the value argument
774 # will be None if --no-use-pep517 is passed via the command-line.
775 # However, the value can be non-None if the option is triggered e.g.
776 # by an environment variable, for example "PIP_NO_USE_PEP517=true".
777 if value
is not None:
778 msg
= """A value was passed for --no-use-pep517,
779 probably using either the PIP_NO_USE_PEP517 environment variable
780 or the "no-use-pep517" config file option. Use an appropriate value
781 of the PIP_USE_PEP517 environment variable or the "use-pep517"
782 config file option instead.
784 raise_option_error(parser
, option
=option
, msg
=msg
)
786 # If user doesn't wish to use pep517, we check if setuptools and wheel are installed
787 # and raise error if it is not.
788 packages
= ("setuptools", "wheel")
789 if not all(importlib
.util
.find_spec(package
) for package
in packages
):
791 f
"It is not possible to use --no-use-pep517 "
792 f
"without {' and '.join(packages)} installed."
794 raise_option_error(parser
, option
=option
, msg
=msg
)
796 # Otherwise, --no-use-pep517 was passed via the command-line.
797 parser
.values
.use_pep517
= False
800 use_pep517
: Any
= partial(
806 help="Use PEP 517 for building source distributions "
807 "(use --no-use-pep517 to force legacy behaviour).",
810 no_use_pep517
: Any
= partial(
815 callback
=_handle_no_use_pep517
,
821 def _handle_config_settings(
822 option
: Option
, opt_str
: str, value
: str, parser
: OptionParser
824 key
, sep
, val
= value
.partition("=")
826 parser
.error(f
"Arguments to {opt_str} must be of the form KEY=VAL") # noqa
827 dest
= getattr(parser
.values
, option
.dest
)
830 setattr(parser
.values
, option
.dest
, dest
)
832 if isinstance(dest
[key
], list):
833 dest
[key
].append(val
)
835 dest
[key
] = [dest
[key
], val
]
840 config_settings
: Callable
[..., Option
] = partial(
844 dest
="config_settings",
847 callback
=_handle_config_settings
,
849 help="Configuration settings to be passed to the PEP 517 build backend. "
850 "Settings take the form KEY=VALUE. Use multiple --config-settings options "
851 "to pass multiple keys to the backend.",
854 build_options
: Callable
[..., Option
] = partial(
857 dest
="build_options",
860 help="Extra arguments to be supplied to 'setup.py bdist_wheel'.",
863 global_options
: Callable
[..., Option
] = partial(
866 dest
="global_options",
869 help="Extra global options to be supplied to the setup.py "
870 "call before the install or bdist_wheel command.",
873 no_clean
: Callable
[..., Option
] = partial(
878 help="Don't clean up build directories.",
881 pre
: Callable
[..., Option
] = partial(
886 help="Include pre-release and development versions. By default, "
887 "pip only finds stable versions.",
890 disable_pip_version_check
: Callable
[..., Option
] = partial(
892 "--disable-pip-version-check",
893 dest
="disable_pip_version_check",
896 help="Don't periodically check PyPI to determine whether a new version "
897 "of pip is available for download. Implied with --no-index.",
900 root_user_action
: Callable
[..., Option
] = partial(
902 "--root-user-action",
903 dest
="root_user_action",
905 choices
=["warn", "ignore"],
906 help="Action if pip is run as a root user. By default, a warning message is shown.",
910 def _handle_merge_hash(
911 option
: Option
, opt_str
: str, value
: str, parser
: OptionParser
913 """Given a value spelled "algo:digest", append the digest to a list
914 pointed to in a dict by the algo name."""
915 if not parser
.values
.hashes
:
916 parser
.values
.hashes
= {}
918 algo
, digest
= value
.split(":", 1)
921 "Arguments to {} must be a hash name " # noqa
922 "followed by a value, like --hash=sha256:"
923 "abcde...".format(opt_str
)
925 if algo
not in STRONG_HASHES
:
927 "Allowed hash algorithms for {} are {}.".format( # noqa
928 opt_str
, ", ".join(STRONG_HASHES
)
931 parser
.values
.hashes
.setdefault(algo
, []).append(digest
)
934 hash: Callable
[..., Option
] = partial(
937 # Hash values eventually end up in InstallRequirement.hashes due to
938 # __dict__ copying in process_line().
941 callback
=_handle_merge_hash
,
943 help="Verify that the package's archive matches this "
944 "hash before installing. Example: --hash=sha256:abcdef...",
948 require_hashes
: Callable
[..., Option
] = partial(
951 dest
="require_hashes",
954 help="Require a hash to check each requirement against, for "
955 "repeatable installs. This option is implied when any package in a "
956 "requirements file has a --hash option.",
960 list_path
: Callable
[..., Option
] = partial(
966 help="Restrict to the specified installation path for listing "
967 "packages (can be used multiple times).",
971 def check_list_path_option(options
: Values
) -> None:
972 if options
.path
and (options
.user
or options
.local
):
973 raise CommandError("Cannot combine '--path' with '--user' or '--local'")
976 list_exclude
: Callable
[..., Option
] = partial(
983 help="Exclude specified package from the output",
987 no_python_version_warning
: Callable
[..., Option
] = partial(
989 "--no-python-version-warning",
990 dest
="no_python_version_warning",
993 help="Silence deprecation warnings for upcoming unsupported Pythons.",
997 # Features that are now always on. A warning is printed if they are used.
998 ALWAYS_ENABLED_FEATURES
= [
999 "no-binary-enable-wheel-cache", # always on since 23.1
1002 use_new_feature
: Callable
[..., Option
] = partial(
1005 dest
="features_enabled",
1013 + ALWAYS_ENABLED_FEATURES
,
1014 help="Enable new functionality, that may be backward incompatible.",
1017 use_deprecated_feature
: Callable
[..., Option
] = partial(
1020 dest
="deprecated_features_enabled",
1027 help=("Enable deprecated functionality, that will be removed in the future."),
1035 general_group
: Dict
[str, Any
] = {
1036 "name": "General Options",
1058 disable_pip_version_check
,
1060 no_python_version_warning
,
1062 use_deprecated_feature
,
1066 index_group
: Dict
[str, Any
] = {
1067 "name": "Package Index Options",