cmdoptions.py 28 KB

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