2shared options and groups
4The principle here is to define options once, but *not* instantiate them
5globally. One reason being that options with action='append' can carry state
6between parses. pip parses general options twice internally, and shouldn't
7pass on state. To be consistent, all options will follow this design.
17from functools
import partial
18from optparse
import SUPPRESS_HELP, Option, OptionGroup, OptionParser, Values
19from textwrap
import dedent
20from typing
import Any, Callable, Dict, Optional, Tuple
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}"
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"]:
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(
78 sdist_dependencies_allowed = (
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 "
97 "Can not use any platform or abi specific options unless "
98 "installing via '--target'"
107 return canonicalize_name(value)
113 TYPE_CHECKER[
"package_name"] = _package_name_option_check
114 TYPE_CHECKER[
"path"] = _path_option_check
121help_: Callable[..., Option] = partial(
130debug_mode: Callable[..., Option] = partial(
137 "Let unhandled exceptions propagate outside the main subroutine, "
138 "instead of logging them to stderr."
142isolated_mode: Callable[..., Option] = partial(
145 dest=
"isolated_mode",
149 "Run pip in an isolated mode, ignoring environment variables and user "
154require_virtualenv: Callable[..., Option] = partial(
156 "--require-virtualenv",
162 "Allow pip to only run in a virtual environment; "
163 "exit with an error otherwise."
167override_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",
175python: Callable[..., Option] = partial(
179 help=
"Run pip with the specified Python interpreter.",
182verbose: Callable[..., Option] = partial(
189 help=
"Give more output. Option is additive, and can be used up to 3 times.",
192no_color: Callable[..., Option] = partial(
198 help=
"Suppress colored output.",
201version: Callable[..., Option] = partial(
207 help=
"Show version and exit.",
210quiet: 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"
224progress_bar: Callable[..., Option] = partial(
229 choices=[
"on",
"off"],
231 help=
"Specify whether the progress bar should be used [on, off] (default: on)",
234log: Callable[..., Option] = partial(
242 help=
"Path to a verbose appending log.",
245no_input: Callable[..., Option] = partial(
252 help=
"Disable prompting for input.",
255keyring_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)"
268proxy: Callable[..., Option] = partial(
274 help=
"Specify a proxy in the form scheme://[user:passwd@]proxy.server:port.",
277retries: Callable[..., Option] = partial(
283 help=
"Maximum number of retries each connection should attempt "
284 "(default %default times).",
287timeout: Callable[..., Option] = partial(
295 help=
"Set the socket timeout (default %default seconds).",
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.",
314cert: 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."
328client_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.",
339index_url: Callable[..., Option] = partial(
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.",
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 "
367no_index: Callable[..., Option] = partial(
373 help=
"Ignore package index (only looking at --find-links URLs instead).",
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.",
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.",
413 help=
"Constrain versions using the given constraints file. "
414 "This option can be used multiple times.",
426 help=
"Install from the given requirements file. "
427 "This option can be used multiple times.",
440 "Install a project in editable mode (i.e. setuptools "
441 '"develop mode") from a local project path or a VCS url.'
446def _handle_src(option: Option, opt_str: str, value: str, parser: OptionParser) ->
None:
451src: 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".',
470 """Get a format_control object."""
475 option: Option, opt_str: str, value: str, parser: OptionParser
486 option: Option, opt_str: str, value: str, parser: OptionParser
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.",
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.",
532platforms: 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."
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.
561 return ((),
"at most three version parts are allowed")
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)
578 option: Option, opt_str: str, value: str, parser: OptionParser
581 Handle a provided --python-version value.
584 if error_msg
is not None:
585 msg =
"invalid --python-version value: {!r}: {}".format(
594python_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).
615implementation: 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."
631abis: 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 "
656def make_target_python(options: Values) -> TargetPython:
670 dest=
"prefer_binary",
673 help=
"Prefer older binary packages over newer source packages.",
677cache_dir: Callable[..., Option] = partial(
681 default=USER_CACHE_DIR,
684 help=
"Store the cache data in <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.
700 if value
is not None:
704 except ValueError
as exc:
717no_cache: Callable[..., Option] = partial(
722 callback=_handle_no_cache_dir,
723 help=
"Disable the cache.",
726no_deps: Callable[..., Option] = partial(
730 dest=
"ignore_dependencies",
733 help=
"Don't install package dependencies.",
736ignore_requires_python: Callable[..., Option] = partial(
738 "--ignore-requires-python",
739 dest=
"ignore_requires_python",
741 help=
"Ignore the Requires-Python information.",
744no_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.",
755check_build_deps: Callable[..., Option] = partial(
757 "--check-build-dependencies",
758 dest=
"check_build_deps",
761 help=
"Check the build dependencies when PEP517 is used.",
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.
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.
788 packages = (
"setuptools",
"wheel")
791 f
"It is not possible to use --no-use-pep517 "
792 f
"without {' and '.join(packages)} installed."
800use_pep517: Any = partial(
806 help=
"Use PEP 517 for building source distributions "
807 "(use --no-use-pep517 to force legacy behaviour).",
810no_use_pep517: Any = partial(
815 callback=_handle_no_use_pep517,
822 option: Option, opt_str: str, value: str, parser: OptionParser
826 parser.error(f
"Arguments to {opt_str} must be of the form KEY=VAL")
833 dest[key].append(val)
835 dest[key] = [dest[key], val]
840config_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.",
854build_options: Callable[..., Option] = partial(
857 dest=
"build_options",
860 help=
"Extra arguments to be supplied to 'setup.py bdist_wheel'.",
863global_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.",
873no_clean: Callable[..., Option] = partial(
878 help=
"Don't clean up build directories.",
881pre: Callable[..., Option] = partial(
886 help=
"Include pre-release and development versions. By default, "
887 "pip only finds stable versions.",
890disable_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.",
900root_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.",
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."""
921 "Arguments to {} must be a hash name "
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(
928 opt_str,
", ".join(STRONG_HASHES)
934hash: Callable[..., Option] = partial(
941 callback=_handle_merge_hash,
943 help=
"Verify that the package's archive matches this "
944 "hash before installing. Example: --hash=sha256:abcdef...",
948require_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.",
960list_path: Callable[..., Option] = partial(
966 help=
"Restrict to the specified installation path for listing "
967 "packages (can be used multiple times).",
973 raise CommandError(
"Cannot combine '--path' with '--user' or '--local'")
976list_exclude: Callable[..., Option] = partial(
983 help=
"Exclude specified package from the output",
987no_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.",
998ALWAYS_ENABLED_FEATURES = [
999 "no-binary-enable-wheel-cache",
1002use_new_feature: Callable[..., Option] = partial(
1005 dest=
"features_enabled",
1013 + ALWAYS_ENABLED_FEATURES,
1014 help=
"Enable new functionality, that may be backward incompatible.",
1017use_deprecated_feature: Callable[..., Option] = partial(
1020 dest=
"deprecated_features_enabled",
1027 help=(
"Enable deprecated functionality, that will be removed in the future."),
1035general_group: Dict[str, Any] = {
1036 "name":
"General Options",
1058 disable_pip_version_check,
1060 no_python_version_warning,
1062 use_deprecated_feature,
1066index_group: Dict[str, Any] = {
1067 "name":
"Package Index Options",
OptionGroup make_option_group(Dict[str, Any] group, ConfigOptionParser parser)
Any _get_format_control(Values values, Option option)
None _handle_merge_hash(Option option, str opt_str, str value, OptionParser parser)
None _handle_no_use_pep517(Option option, str opt, str value, OptionParser parser)
None raise_option_error(OptionParser parser, Option option, str msg)
None check_list_path_option(Values options)
str _path_option_check(Option option, str opt, str value)
None check_dist_restriction(Values options, bool check_target=False)
None _handle_config_settings(Option option, str opt_str, str value, OptionParser parser)
None _handle_no_binary(Option option, str opt_str, str value, OptionParser parser)
None _handle_src(Option option, str opt_str, str value, OptionParser parser)
str _package_name_option_check(Option option, str opt, str value)
None _handle_no_cache_dir(Option option, str opt, str value, OptionParser parser)
None _handle_only_binary(Option option, str opt_str, str value, OptionParser parser)
None add_target_python_options(OptionGroup cmd_opts)
Tuple[Tuple[int,...], Optional[str]] _convert_python_version(str value)
None _handle_python_version(Option option, str opt_str, str value, OptionParser parser)