cmdoptions.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. """
  2. shared options and groups
  3. The principle here is to define options once, but *not* instantiate them
  4. globally. One reason being that options with action='append' can carry state
  5. between parses. pip parses general options twice internally, and shouldn't
  6. pass on state. To be consistent, all options will follow this design.
  7. """
  8. # The following comment should be removed at some point in the future.
  9. # mypy: strict-optional=False
  10. from __future__ import annotations
  11. import logging
  12. import os
  13. import pathlib
  14. import textwrap
  15. from functools import partial
  16. from optparse import SUPPRESS_HELP, Option, OptionGroup, OptionParser, Values
  17. from textwrap import dedent
  18. from typing import Any, Callable
  19. from pip._vendor.packaging.utils import canonicalize_name
  20. from pip._internal.cli.parser import ConfigOptionParser
  21. from pip._internal.exceptions import CommandError
  22. from pip._internal.locations import USER_CACHE_DIR, get_src_prefix
  23. from pip._internal.models.format_control import FormatControl
  24. from pip._internal.models.index import PyPI
  25. from pip._internal.models.target_python import TargetPython
  26. from pip._internal.utils.hashes import STRONG_HASHES
  27. from pip._internal.utils.misc import strtobool
  28. logger = logging.getLogger(__name__)
  29. def raise_option_error(parser: OptionParser, option: Option, msg: str) -> None:
  30. """
  31. Raise an option parsing error using parser.error().
  32. Args:
  33. parser: an OptionParser instance.
  34. option: an Option instance.
  35. msg: the error text.
  36. """
  37. msg = f"{option} error: {msg}"
  38. msg = textwrap.fill(" ".join(msg.split()))
  39. parser.error(msg)
  40. def make_option_group(group: dict[str, Any], parser: ConfigOptionParser) -> OptionGroup:
  41. """
  42. Return an OptionGroup object
  43. group -- assumed to be dict with 'name' and 'options' keys
  44. parser -- an optparse Parser
  45. """
  46. option_group = OptionGroup(parser, group["name"])
  47. for option in group["options"]:
  48. option_group.add_option(option())
  49. return option_group
  50. def check_dist_restriction(options: Values, check_target: bool = False) -> None:
  51. """Function for determining if custom platform options are allowed.
  52. :param options: The OptionParser options.
  53. :param check_target: Whether or not to check if --target is being used.
  54. """
  55. dist_restriction_set = any(
  56. [
  57. options.python_version,
  58. options.platforms,
  59. options.abis,
  60. options.implementation,
  61. ]
  62. )
  63. binary_only = FormatControl(set(), {":all:"})
  64. sdist_dependencies_allowed = (
  65. options.format_control != binary_only and not options.ignore_dependencies
  66. )
  67. # Installations or downloads using dist restrictions must not combine
  68. # source distributions and dist-specific wheels, as they are not
  69. # guaranteed to be locally compatible.
  70. if dist_restriction_set and sdist_dependencies_allowed:
  71. raise CommandError(
  72. "When restricting platform and interpreter constraints using "
  73. "--python-version, --platform, --abi, or --implementation, "
  74. "either --no-deps must be set, or --only-binary=:all: must be "
  75. "set and --no-binary must not be set (or must be set to "
  76. ":none:)."
  77. )
  78. if check_target:
  79. if not options.dry_run and dist_restriction_set and not options.target_dir:
  80. raise CommandError(
  81. "Can not use any platform or abi specific options unless "
  82. "installing via '--target' or using '--dry-run'"
  83. )
  84. def check_build_constraints(options: Values) -> None:
  85. """Function for validating build constraints options.
  86. :param options: The OptionParser options.
  87. """
  88. if hasattr(options, "build_constraints") and options.build_constraints:
  89. if not options.build_isolation:
  90. raise CommandError(
  91. "--build-constraint cannot be used with --no-build-isolation."
  92. )
  93. # Import here to avoid circular imports
  94. from pip._internal.network.session import PipSession
  95. from pip._internal.req.req_file import get_file_content
  96. # Eagerly check build constraints file contents
  97. # is valid so that we don't fail in when trying
  98. # to check constraints in isolated build process
  99. with PipSession() as session:
  100. for constraint_file in options.build_constraints:
  101. get_file_content(constraint_file, session)
  102. def _path_option_check(option: Option, opt: str, value: str) -> str:
  103. return os.path.expanduser(value)
  104. def _package_name_option_check(option: Option, opt: str, value: str) -> str:
  105. return canonicalize_name(value)
  106. class PipOption(Option):
  107. TYPES = Option.TYPES + ("path", "package_name")
  108. TYPE_CHECKER = Option.TYPE_CHECKER.copy()
  109. TYPE_CHECKER["package_name"] = _package_name_option_check
  110. TYPE_CHECKER["path"] = _path_option_check
  111. ###########
  112. # options #
  113. ###########
  114. help_: Callable[..., Option] = partial(
  115. Option,
  116. "-h",
  117. "--help",
  118. dest="help",
  119. action="help",
  120. help="Show help.",
  121. )
  122. debug_mode: Callable[..., Option] = partial(
  123. Option,
  124. "--debug",
  125. dest="debug_mode",
  126. action="store_true",
  127. default=False,
  128. help=(
  129. "Let unhandled exceptions propagate outside the main subroutine, "
  130. "instead of logging them to stderr."
  131. ),
  132. )
  133. isolated_mode: Callable[..., Option] = partial(
  134. Option,
  135. "--isolated",
  136. dest="isolated_mode",
  137. action="store_true",
  138. default=False,
  139. help=(
  140. "Run pip in an isolated mode, ignoring environment variables and user "
  141. "configuration."
  142. ),
  143. )
  144. require_virtualenv: Callable[..., Option] = partial(
  145. Option,
  146. "--require-virtualenv",
  147. "--require-venv",
  148. dest="require_venv",
  149. action="store_true",
  150. default=False,
  151. help=(
  152. "Allow pip to only run in a virtual environment; exit with an error otherwise."
  153. ),
  154. )
  155. override_externally_managed: Callable[..., Option] = partial(
  156. Option,
  157. "--break-system-packages",
  158. dest="override_externally_managed",
  159. action="store_true",
  160. help="Allow pip to modify an EXTERNALLY-MANAGED Python installation",
  161. )
  162. python: Callable[..., Option] = partial(
  163. Option,
  164. "--python",
  165. dest="python",
  166. help="Run pip with the specified Python interpreter.",
  167. )
  168. verbose: Callable[..., Option] = partial(
  169. Option,
  170. "-v",
  171. "--verbose",
  172. dest="verbose",
  173. action="count",
  174. default=0,
  175. help="Give more output. Option is additive, and can be used up to 3 times.",
  176. )
  177. no_color: Callable[..., Option] = partial(
  178. Option,
  179. "--no-color",
  180. dest="no_color",
  181. action="store_true",
  182. default=False,
  183. help="Suppress colored output.",
  184. )
  185. version: Callable[..., Option] = partial(
  186. Option,
  187. "-V",
  188. "--version",
  189. dest="version",
  190. action="store_true",
  191. help="Show version and exit.",
  192. )
  193. quiet: Callable[..., Option] = partial(
  194. Option,
  195. "-q",
  196. "--quiet",
  197. dest="quiet",
  198. action="count",
  199. default=0,
  200. help=(
  201. "Give less output. Option is additive, and can be used up to 3"
  202. " times (corresponding to WARNING, ERROR, and CRITICAL logging"
  203. " levels)."
  204. ),
  205. )
  206. progress_bar: Callable[..., Option] = partial(
  207. Option,
  208. "--progress-bar",
  209. dest="progress_bar",
  210. type="choice",
  211. choices=["auto", "on", "off", "raw"],
  212. default="auto",
  213. help=(
  214. "Specify whether the progress bar should be used. In 'auto'"
  215. " mode, --quiet will suppress all progress bars."
  216. " [auto, on, off, raw] (default: auto)"
  217. ),
  218. )
  219. log: Callable[..., Option] = partial(
  220. PipOption,
  221. "--log",
  222. "--log-file",
  223. "--local-log",
  224. dest="log",
  225. metavar="path",
  226. type="path",
  227. help="Path to a verbose appending log.",
  228. )
  229. no_input: Callable[..., Option] = partial(
  230. Option,
  231. # Don't ask for input
  232. "--no-input",
  233. dest="no_input",
  234. action="store_true",
  235. default=False,
  236. help="Disable prompting for input.",
  237. )
  238. keyring_provider: Callable[..., Option] = partial(
  239. Option,
  240. "--keyring-provider",
  241. dest="keyring_provider",
  242. choices=["auto", "disabled", "import", "subprocess"],
  243. default="auto",
  244. help=(
  245. "Enable the credential lookup via the keyring library if user input is allowed."
  246. " Specify which mechanism to use [auto, disabled, import, subprocess]."
  247. " (default: %default)"
  248. ),
  249. )
  250. proxy: Callable[..., Option] = partial(
  251. Option,
  252. "--proxy",
  253. dest="proxy",
  254. type="str",
  255. default="",
  256. help="Specify a proxy in the form scheme://[user:passwd@]proxy.server:port.",
  257. )
  258. retries: Callable[..., Option] = partial(
  259. Option,
  260. "--retries",
  261. dest="retries",
  262. type="int",
  263. default=5,
  264. help="Maximum attempts to establish a new HTTP connection. (default: %default)",
  265. )
  266. resume_retries: Callable[..., Option] = partial(
  267. Option,
  268. "--resume-retries",
  269. dest="resume_retries",
  270. type="int",
  271. default=5,
  272. help="Maximum attempts to resume or restart an incomplete download. "
  273. "(default: %default)",
  274. )
  275. timeout: Callable[..., Option] = partial(
  276. Option,
  277. "--timeout",
  278. "--default-timeout",
  279. metavar="sec",
  280. dest="timeout",
  281. type="float",
  282. default=15,
  283. help="Set the socket timeout (default %default seconds).",
  284. )
  285. def exists_action() -> Option:
  286. return Option(
  287. # Option when path already exist
  288. "--exists-action",
  289. dest="exists_action",
  290. type="choice",
  291. choices=["s", "i", "w", "b", "a"],
  292. default=[],
  293. action="append",
  294. metavar="action",
  295. help="Default action when a path already exists: "
  296. "(s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.",
  297. )
  298. cert: Callable[..., Option] = partial(
  299. PipOption,
  300. "--cert",
  301. dest="cert",
  302. type="path",
  303. metavar="path",
  304. help=(
  305. "Path to PEM-encoded CA certificate bundle. "
  306. "If provided, overrides the default. "
  307. "See 'SSL Certificate Verification' in pip documentation "
  308. "for more information."
  309. ),
  310. )
  311. client_cert: Callable[..., Option] = partial(
  312. PipOption,
  313. "--client-cert",
  314. dest="client_cert",
  315. type="path",
  316. default=None,
  317. metavar="path",
  318. help="Path to SSL client certificate, a single file containing the "
  319. "private key and the certificate in PEM format.",
  320. )
  321. index_url: Callable[..., Option] = partial(
  322. Option,
  323. "-i",
  324. "--index-url",
  325. "--pypi-url",
  326. dest="index_url",
  327. metavar="URL",
  328. default=PyPI.simple_url,
  329. help="Base URL of the Python Package Index (default %default). "
  330. "This should point to a repository compliant with PEP 503 "
  331. "(the simple repository API) or a local directory laid out "
  332. "in the same format.",
  333. )
  334. def extra_index_url() -> Option:
  335. return Option(
  336. "--extra-index-url",
  337. dest="extra_index_urls",
  338. metavar="URL",
  339. action="append",
  340. default=[],
  341. help="Extra URLs of package indexes to use in addition to "
  342. "--index-url. Should follow the same rules as "
  343. "--index-url.",
  344. )
  345. no_index: Callable[..., Option] = partial(
  346. Option,
  347. "--no-index",
  348. dest="no_index",
  349. action="store_true",
  350. default=False,
  351. help="Ignore package index (only looking at --find-links URLs instead).",
  352. )
  353. def find_links() -> Option:
  354. return Option(
  355. "-f",
  356. "--find-links",
  357. dest="find_links",
  358. action="append",
  359. default=[],
  360. metavar="url",
  361. help="If a URL or path to an html file, then parse for links to "
  362. "archives such as sdist (.tar.gz) or wheel (.whl) files. "
  363. "If a local path or file:// URL that's a directory, "
  364. "then look for archives in the directory listing. "
  365. "Links to VCS project URLs are not supported.",
  366. )
  367. def trusted_host() -> Option:
  368. return Option(
  369. "--trusted-host",
  370. dest="trusted_hosts",
  371. action="append",
  372. metavar="HOSTNAME",
  373. default=[],
  374. help="Mark this host or host:port pair as trusted, even though it "
  375. "does not have valid or any HTTPS.",
  376. )
  377. def constraints() -> Option:
  378. return Option(
  379. "-c",
  380. "--constraint",
  381. dest="constraints",
  382. action="append",
  383. default=[],
  384. metavar="file",
  385. help="Constrain versions using the given constraints file. "
  386. "This option can be used multiple times.",
  387. )
  388. def build_constraints() -> Option:
  389. return Option(
  390. "--build-constraint",
  391. dest="build_constraints",
  392. action="append",
  393. type="str",
  394. default=[],
  395. metavar="file",
  396. help=(
  397. "Constrain build dependencies using the given constraints file. "
  398. "This option can be used multiple times."
  399. ),
  400. )
  401. def requirements() -> Option:
  402. return Option(
  403. "-r",
  404. "--requirement",
  405. dest="requirements",
  406. action="append",
  407. default=[],
  408. metavar="file",
  409. help="Install from the given requirements file. "
  410. "This option can be used multiple times.",
  411. )
  412. def editable() -> Option:
  413. return Option(
  414. "-e",
  415. "--editable",
  416. dest="editables",
  417. action="append",
  418. default=[],
  419. metavar="path/url",
  420. help=(
  421. "Install a project in editable mode (i.e. setuptools "
  422. '"develop mode") from a local project path or a VCS url.'
  423. ),
  424. )
  425. def _handle_src(option: Option, opt_str: str, value: str, parser: OptionParser) -> None:
  426. value = os.path.abspath(value)
  427. setattr(parser.values, option.dest, value)
  428. src: Callable[..., Option] = partial(
  429. PipOption,
  430. "--src",
  431. "--source",
  432. "--source-dir",
  433. "--source-directory",
  434. dest="src_dir",
  435. type="path",
  436. metavar="dir",
  437. default=get_src_prefix(),
  438. action="callback",
  439. callback=_handle_src,
  440. help="Directory to check out editable projects into. "
  441. 'The default in a virtualenv is "<venv path>/src". '
  442. 'The default for global installs is "<current dir>/src".',
  443. )
  444. def _get_format_control(values: Values, option: Option) -> Any:
  445. """Get a format_control object."""
  446. return getattr(values, option.dest)
  447. def _handle_no_binary(
  448. option: Option, opt_str: str, value: str, parser: OptionParser
  449. ) -> None:
  450. existing = _get_format_control(parser.values, option)
  451. FormatControl.handle_mutual_excludes(
  452. value,
  453. existing.no_binary,
  454. existing.only_binary,
  455. )
  456. def _handle_only_binary(
  457. option: Option, opt_str: str, value: str, parser: OptionParser
  458. ) -> None:
  459. existing = _get_format_control(parser.values, option)
  460. FormatControl.handle_mutual_excludes(
  461. value,
  462. existing.only_binary,
  463. existing.no_binary,
  464. )
  465. def no_binary() -> Option:
  466. format_control = FormatControl(set(), set())
  467. return Option(
  468. "--no-binary",
  469. dest="format_control",
  470. action="callback",
  471. callback=_handle_no_binary,
  472. type="str",
  473. default=format_control,
  474. help="Do not use binary packages. Can be supplied multiple times, and "
  475. 'each time adds to the existing value. Accepts either ":all:" to '
  476. 'disable all binary packages, ":none:" to empty the set (notice '
  477. "the colons), or one or more package names with commas between "
  478. "them (no colons). Note that some packages are tricky to compile "
  479. "and may fail to install when this option is used on them.",
  480. )
  481. def only_binary() -> Option:
  482. format_control = FormatControl(set(), set())
  483. return Option(
  484. "--only-binary",
  485. dest="format_control",
  486. action="callback",
  487. callback=_handle_only_binary,
  488. type="str",
  489. default=format_control,
  490. help="Do not use source packages. Can be supplied multiple times, and "
  491. 'each time adds to the existing value. Accepts either ":all:" to '
  492. 'disable all source packages, ":none:" to empty the set, or one '
  493. "or more package names with commas between them. Packages "
  494. "without binary distributions will fail to install when this "
  495. "option is used on them.",
  496. )
  497. platforms: Callable[..., Option] = partial(
  498. Option,
  499. "--platform",
  500. dest="platforms",
  501. metavar="platform",
  502. action="append",
  503. default=None,
  504. help=(
  505. "Only use wheels compatible with <platform>. Defaults to the "
  506. "platform of the running system. Use this option multiple times to "
  507. "specify multiple platforms supported by the target interpreter."
  508. ),
  509. )
  510. # This was made a separate function for unit-testing purposes.
  511. def _convert_python_version(value: str) -> tuple[tuple[int, ...], str | None]:
  512. """
  513. Convert a version string like "3", "37", or "3.7.3" into a tuple of ints.
  514. :return: A 2-tuple (version_info, error_msg), where `error_msg` is
  515. non-None if and only if there was a parsing error.
  516. """
  517. if not value:
  518. # The empty string is the same as not providing a value.
  519. return (None, None)
  520. parts = value.split(".")
  521. if len(parts) > 3:
  522. return ((), "at most three version parts are allowed")
  523. if len(parts) == 1:
  524. # Then we are in the case of "3" or "37".
  525. value = parts[0]
  526. if len(value) > 1:
  527. parts = [value[0], value[1:]]
  528. try:
  529. version_info = tuple(int(part) for part in parts)
  530. except ValueError:
  531. return ((), "each version part must be an integer")
  532. return (version_info, None)
  533. def _handle_python_version(
  534. option: Option, opt_str: str, value: str, parser: OptionParser
  535. ) -> None:
  536. """
  537. Handle a provided --python-version value.
  538. """
  539. version_info, error_msg = _convert_python_version(value)
  540. if error_msg is not None:
  541. msg = f"invalid --python-version value: {value!r}: {error_msg}"
  542. raise_option_error(parser, option=option, msg=msg)
  543. parser.values.python_version = version_info
  544. python_version: Callable[..., Option] = partial(
  545. Option,
  546. "--python-version",
  547. dest="python_version",
  548. metavar="python_version",
  549. action="callback",
  550. callback=_handle_python_version,
  551. type="str",
  552. default=None,
  553. help=dedent(
  554. """\
  555. The Python interpreter version to use for wheel and "Requires-Python"
  556. compatibility checks. Defaults to a version derived from the running
  557. interpreter. The version can be specified using up to three dot-separated
  558. integers (e.g. "3" for 3.0.0, "3.7" for 3.7.0, or "3.7.3"). A major-minor
  559. version can also be given as a string without dots (e.g. "37" for 3.7.0).
  560. """
  561. ),
  562. )
  563. implementation: Callable[..., Option] = partial(
  564. Option,
  565. "--implementation",
  566. dest="implementation",
  567. metavar="implementation",
  568. default=None,
  569. help=(
  570. "Only use wheels compatible with Python "
  571. "implementation <implementation>, e.g. 'pp', 'jy', 'cp', "
  572. " or 'ip'. If not specified, then the current "
  573. "interpreter implementation is used. Use 'py' to force "
  574. "implementation-agnostic wheels."
  575. ),
  576. )
  577. abis: Callable[..., Option] = partial(
  578. Option,
  579. "--abi",
  580. dest="abis",
  581. metavar="abi",
  582. action="append",
  583. default=None,
  584. help=(
  585. "Only use wheels compatible with Python abi <abi>, e.g. 'pypy_41'. "
  586. "If not specified, then the current interpreter abi tag is used. "
  587. "Use this option multiple times to specify multiple abis supported "
  588. "by the target interpreter. Generally you will need to specify "
  589. "--implementation, --platform, and --python-version when using this "
  590. "option."
  591. ),
  592. )
  593. def add_target_python_options(cmd_opts: OptionGroup) -> None:
  594. cmd_opts.add_option(platforms())
  595. cmd_opts.add_option(python_version())
  596. cmd_opts.add_option(implementation())
  597. cmd_opts.add_option(abis())
  598. def make_target_python(options: Values) -> TargetPython:
  599. target_python = TargetPython(
  600. platforms=options.platforms,
  601. py_version_info=options.python_version,
  602. abis=options.abis,
  603. implementation=options.implementation,
  604. )
  605. return target_python
  606. def prefer_binary() -> Option:
  607. return Option(
  608. "--prefer-binary",
  609. dest="prefer_binary",
  610. action="store_true",
  611. default=False,
  612. help=(
  613. "Prefer binary packages over source packages, even if the "
  614. "source packages are newer."
  615. ),
  616. )
  617. cache_dir: Callable[..., Option] = partial(
  618. PipOption,
  619. "--cache-dir",
  620. dest="cache_dir",
  621. default=USER_CACHE_DIR,
  622. metavar="dir",
  623. type="path",
  624. help="Store the cache data in <dir>.",
  625. )
  626. def _handle_no_cache_dir(
  627. option: Option, opt: str, value: str, parser: OptionParser
  628. ) -> None:
  629. """
  630. Process a value provided for the --no-cache-dir option.
  631. This is an optparse.Option callback for the --no-cache-dir option.
  632. """
  633. # The value argument will be None if --no-cache-dir is passed via the
  634. # command-line, since the option doesn't accept arguments. However,
  635. # the value can be non-None if the option is triggered e.g. by an
  636. # environment variable, like PIP_NO_CACHE_DIR=true.
  637. if value is not None:
  638. # Then parse the string value to get argument error-checking.
  639. try:
  640. strtobool(value)
  641. except ValueError as exc:
  642. raise_option_error(parser, option=option, msg=str(exc))
  643. # Originally, setting PIP_NO_CACHE_DIR to a value that strtobool()
  644. # converted to 0 (like "false" or "no") caused cache_dir to be disabled
  645. # rather than enabled (logic would say the latter). Thus, we disable
  646. # the cache directory not just on values that parse to True, but (for
  647. # backwards compatibility reasons) also on values that parse to False.
  648. # In other words, always set it to False if the option is provided in
  649. # some (valid) form.
  650. parser.values.cache_dir = False
  651. no_cache: Callable[..., Option] = partial(
  652. Option,
  653. "--no-cache-dir",
  654. dest="cache_dir",
  655. action="callback",
  656. callback=_handle_no_cache_dir,
  657. help="Disable the cache.",
  658. )
  659. no_deps: Callable[..., Option] = partial(
  660. Option,
  661. "--no-deps",
  662. "--no-dependencies",
  663. dest="ignore_dependencies",
  664. action="store_true",
  665. default=False,
  666. help="Don't install package dependencies.",
  667. )
  668. def _handle_dependency_group(
  669. option: Option, opt: str, value: str, parser: OptionParser
  670. ) -> None:
  671. """
  672. Process a value provided for the --group option.
  673. Splits on the rightmost ":", and validates that the path (if present) ends
  674. in `pyproject.toml`. Defaults the path to `pyproject.toml` when one is not given.
  675. `:` cannot appear in dependency group names, so this is a safe and simple parse.
  676. This is an optparse.Option callback for the dependency_groups option.
  677. """
  678. path, sep, groupname = value.rpartition(":")
  679. if not sep:
  680. path = "pyproject.toml"
  681. else:
  682. # check for 'pyproject.toml' filenames using pathlib
  683. if pathlib.PurePath(path).name != "pyproject.toml":
  684. msg = "group paths use 'pyproject.toml' filenames"
  685. raise_option_error(parser, option=option, msg=msg)
  686. parser.values.dependency_groups.append((path, groupname))
  687. dependency_groups: Callable[..., Option] = partial(
  688. Option,
  689. "--group",
  690. dest="dependency_groups",
  691. default=[],
  692. type=str,
  693. action="callback",
  694. callback=_handle_dependency_group,
  695. metavar="[path:]group",
  696. help='Install a named dependency-group from a "pyproject.toml" file. '
  697. 'If a path is given, the name of the file must be "pyproject.toml". '
  698. 'Defaults to using "pyproject.toml" in the current directory.',
  699. )
  700. ignore_requires_python: Callable[..., Option] = partial(
  701. Option,
  702. "--ignore-requires-python",
  703. dest="ignore_requires_python",
  704. action="store_true",
  705. help="Ignore the Requires-Python information.",
  706. )
  707. no_build_isolation: Callable[..., Option] = partial(
  708. Option,
  709. "--no-build-isolation",
  710. dest="build_isolation",
  711. action="store_false",
  712. default=True,
  713. help="Disable isolation when building a modern source distribution. "
  714. "Build dependencies specified by PEP 518 must be already installed "
  715. "if this option is used.",
  716. )
  717. check_build_deps: Callable[..., Option] = partial(
  718. Option,
  719. "--check-build-dependencies",
  720. dest="check_build_deps",
  721. action="store_true",
  722. default=False,
  723. help="Check the build dependencies.",
  724. )
  725. use_pep517: Any = partial(
  726. Option,
  727. "--use-pep517",
  728. dest="use_pep517",
  729. action="store_true",
  730. default=True,
  731. help=SUPPRESS_HELP,
  732. )
  733. def _handle_config_settings(
  734. option: Option, opt_str: str, value: str, parser: OptionParser
  735. ) -> None:
  736. key, sep, val = value.partition("=")
  737. if sep != "=":
  738. parser.error(f"Arguments to {opt_str} must be of the form KEY=VAL")
  739. dest = getattr(parser.values, option.dest)
  740. if dest is None:
  741. dest = {}
  742. setattr(parser.values, option.dest, dest)
  743. if key in dest:
  744. if isinstance(dest[key], list):
  745. dest[key].append(val)
  746. else:
  747. dest[key] = [dest[key], val]
  748. else:
  749. dest[key] = val
  750. config_settings: Callable[..., Option] = partial(
  751. Option,
  752. "-C",
  753. "--config-settings",
  754. dest="config_settings",
  755. type=str,
  756. action="callback",
  757. callback=_handle_config_settings,
  758. metavar="settings",
  759. help="Configuration settings to be passed to the build backend. "
  760. "Settings take the form KEY=VALUE. Use multiple --config-settings options "
  761. "to pass multiple keys to the backend.",
  762. )
  763. no_clean: Callable[..., Option] = partial(
  764. Option,
  765. "--no-clean",
  766. action="store_true",
  767. default=False,
  768. help="Don't clean up build directories.",
  769. )
  770. pre: Callable[..., Option] = partial(
  771. Option,
  772. "--pre",
  773. action="store_true",
  774. default=False,
  775. help="Include pre-release and development versions. By default, "
  776. "pip only finds stable versions.",
  777. )
  778. json: Callable[..., Option] = partial(
  779. Option,
  780. "--json",
  781. action="store_true",
  782. default=False,
  783. help="Output data in a machine-readable JSON format.",
  784. )
  785. disable_pip_version_check: Callable[..., Option] = partial(
  786. Option,
  787. "--disable-pip-version-check",
  788. dest="disable_pip_version_check",
  789. action="store_true",
  790. default=False,
  791. help="Don't periodically check PyPI to determine whether a new version "
  792. "of pip is available for download. Implied with --no-index.",
  793. )
  794. root_user_action: Callable[..., Option] = partial(
  795. Option,
  796. "--root-user-action",
  797. dest="root_user_action",
  798. default="warn",
  799. choices=["warn", "ignore"],
  800. help="Action if pip is run as a root user [warn, ignore] (default: warn)",
  801. )
  802. def _handle_merge_hash(
  803. option: Option, opt_str: str, value: str, parser: OptionParser
  804. ) -> None:
  805. """Given a value spelled "algo:digest", append the digest to a list
  806. pointed to in a dict by the algo name."""
  807. if not parser.values.hashes:
  808. parser.values.hashes = {}
  809. try:
  810. algo, digest = value.split(":", 1)
  811. except ValueError:
  812. parser.error(
  813. f"Arguments to {opt_str} must be a hash name "
  814. "followed by a value, like --hash=sha256:"
  815. "abcde..."
  816. )
  817. if algo not in STRONG_HASHES:
  818. parser.error(
  819. "Allowed hash algorithms for {} are {}.".format(
  820. opt_str, ", ".join(STRONG_HASHES)
  821. )
  822. )
  823. parser.values.hashes.setdefault(algo, []).append(digest)
  824. hash: Callable[..., Option] = partial(
  825. Option,
  826. "--hash",
  827. # Hash values eventually end up in InstallRequirement.hashes due to
  828. # __dict__ copying in process_line().
  829. dest="hashes",
  830. action="callback",
  831. callback=_handle_merge_hash,
  832. type="string",
  833. help="Verify that the package's archive matches this "
  834. "hash before installing. Example: --hash=sha256:abcdef...",
  835. )
  836. require_hashes: Callable[..., Option] = partial(
  837. Option,
  838. "--require-hashes",
  839. dest="require_hashes",
  840. action="store_true",
  841. default=False,
  842. help="Require a hash to check each requirement against, for "
  843. "repeatable installs. This option is implied when any package in a "
  844. "requirements file has a --hash option.",
  845. )
  846. list_path: Callable[..., Option] = partial(
  847. PipOption,
  848. "--path",
  849. dest="path",
  850. type="path",
  851. action="append",
  852. help="Restrict to the specified installation path for listing "
  853. "packages (can be used multiple times).",
  854. )
  855. def check_list_path_option(options: Values) -> None:
  856. if options.path and (options.user or options.local):
  857. raise CommandError("Cannot combine '--path' with '--user' or '--local'")
  858. list_exclude: Callable[..., Option] = partial(
  859. PipOption,
  860. "--exclude",
  861. dest="excludes",
  862. action="append",
  863. metavar="package",
  864. type="package_name",
  865. help="Exclude specified package from the output",
  866. )
  867. no_python_version_warning: Callable[..., Option] = partial(
  868. Option,
  869. "--no-python-version-warning",
  870. dest="no_python_version_warning",
  871. action="store_true",
  872. default=False,
  873. help=SUPPRESS_HELP, # No-op, a hold-over from the Python 2->3 transition.
  874. )
  875. # Features that are now always on. A warning is printed if they are used.
  876. ALWAYS_ENABLED_FEATURES = [
  877. "truststore", # always on since 24.2
  878. "no-binary-enable-wheel-cache", # always on since 23.1
  879. ]
  880. use_new_feature: Callable[..., Option] = partial(
  881. Option,
  882. "--use-feature",
  883. dest="features_enabled",
  884. metavar="feature",
  885. action="append",
  886. default=[],
  887. choices=[
  888. "fast-deps",
  889. "build-constraint",
  890. ]
  891. + ALWAYS_ENABLED_FEATURES,
  892. help="Enable new functionality, that may be backward incompatible.",
  893. )
  894. use_deprecated_feature: Callable[..., Option] = partial(
  895. Option,
  896. "--use-deprecated",
  897. dest="deprecated_features_enabled",
  898. metavar="feature",
  899. action="append",
  900. default=[],
  901. choices=[
  902. "legacy-resolver",
  903. "legacy-certs",
  904. ],
  905. help=("Enable deprecated functionality, that will be removed in the future."),
  906. )
  907. ##########
  908. # groups #
  909. ##########
  910. general_group: dict[str, Any] = {
  911. "name": "General Options",
  912. "options": [
  913. help_,
  914. debug_mode,
  915. isolated_mode,
  916. require_virtualenv,
  917. python,
  918. verbose,
  919. version,
  920. quiet,
  921. log,
  922. no_input,
  923. keyring_provider,
  924. proxy,
  925. retries,
  926. timeout,
  927. exists_action,
  928. trusted_host,
  929. cert,
  930. client_cert,
  931. cache_dir,
  932. no_cache,
  933. disable_pip_version_check,
  934. no_color,
  935. no_python_version_warning,
  936. use_new_feature,
  937. use_deprecated_feature,
  938. resume_retries,
  939. ],
  940. }
  941. index_group: dict[str, Any] = {
  942. "name": "Package Index Options",
  943. "options": [
  944. index_url,
  945. extra_index_url,
  946. no_index,
  947. find_links,
  948. ],
  949. }