cmdoptions.py 28 KB

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