cmdoptions.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. from __future__ import absolute_import
  9. import warnings
  10. from functools import partial
  11. from optparse import SUPPRESS_HELP, Option, OptionGroup
  12. from pip._internal.index import (
  13. FormatControl, fmt_ctl_handle_mutual_exclude, fmt_ctl_no_binary,
  14. )
  15. from pip._internal.locations import USER_CACHE_DIR, src_prefix
  16. from pip._internal.models import PyPI
  17. from pip._internal.utils.hashes import STRONG_HASHES
  18. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  19. from pip._internal.utils.ui import BAR_TYPES
  20. if MYPY_CHECK_RUNNING:
  21. from typing import Any
  22. def make_option_group(group, parser):
  23. """
  24. Return an OptionGroup object
  25. group -- assumed to be dict with 'name' and 'options' keys
  26. parser -- an optparse Parser
  27. """
  28. option_group = OptionGroup(parser, group['name'])
  29. for option in group['options']:
  30. option_group.add_option(option())
  31. return option_group
  32. def check_install_build_global(options, check_options=None):
  33. """Disable wheels if per-setup.py call options are set.
  34. :param options: The OptionParser options to update.
  35. :param check_options: The options to check, if not supplied defaults to
  36. options.
  37. """
  38. if check_options is None:
  39. check_options = options
  40. def getname(n):
  41. return getattr(check_options, n, None)
  42. names = ["build_options", "global_options", "install_options"]
  43. if any(map(getname, names)):
  44. control = options.format_control
  45. fmt_ctl_no_binary(control)
  46. warnings.warn(
  47. 'Disabling all use of wheels due to the use of --build-options '
  48. '/ --global-options / --install-options.', stacklevel=2,
  49. )
  50. ###########
  51. # options #
  52. ###########
  53. help_ = partial(
  54. Option,
  55. '-h', '--help',
  56. dest='help',
  57. action='help',
  58. help='Show help.',
  59. ) # type: Any
  60. isolated_mode = partial(
  61. Option,
  62. "--isolated",
  63. dest="isolated_mode",
  64. action="store_true",
  65. default=False,
  66. help=(
  67. "Run pip in an isolated mode, ignoring environment variables and user "
  68. "configuration."
  69. ),
  70. )
  71. require_virtualenv = partial(
  72. Option,
  73. # Run only if inside a virtualenv, bail if not.
  74. '--require-virtualenv', '--require-venv',
  75. dest='require_venv',
  76. action='store_true',
  77. default=False,
  78. help=SUPPRESS_HELP
  79. ) # type: Any
  80. verbose = partial(
  81. Option,
  82. '-v', '--verbose',
  83. dest='verbose',
  84. action='count',
  85. default=0,
  86. help='Give more output. Option is additive, and can be used up to 3 times.'
  87. )
  88. no_color = partial(
  89. Option,
  90. '--no-color',
  91. dest='no_color',
  92. action='store_true',
  93. default=False,
  94. help="Suppress colored output",
  95. )
  96. version = partial(
  97. Option,
  98. '-V', '--version',
  99. dest='version',
  100. action='store_true',
  101. help='Show version and exit.',
  102. ) # type: Any
  103. quiet = partial(
  104. Option,
  105. '-q', '--quiet',
  106. dest='quiet',
  107. action='count',
  108. default=0,
  109. help=(
  110. 'Give less output. Option is additive, and can be used up to 3'
  111. ' times (corresponding to WARNING, ERROR, and CRITICAL logging'
  112. ' levels).'
  113. ),
  114. ) # type: Any
  115. progress_bar = partial(
  116. Option,
  117. '--progress-bar',
  118. dest='progress_bar',
  119. type='choice',
  120. choices=list(BAR_TYPES.keys()),
  121. default='on',
  122. help=(
  123. 'Specify type of progress to be displayed [' +
  124. '|'.join(BAR_TYPES.keys()) + '] (default: %default)'
  125. ),
  126. ) # type: Any
  127. log = partial(
  128. Option,
  129. "--log", "--log-file", "--local-log",
  130. dest="log",
  131. metavar="path",
  132. help="Path to a verbose appending log."
  133. ) # type: Any
  134. no_input = partial(
  135. Option,
  136. # Don't ask for input
  137. '--no-input',
  138. dest='no_input',
  139. action='store_true',
  140. default=False,
  141. help=SUPPRESS_HELP
  142. ) # type: Any
  143. proxy = partial(
  144. Option,
  145. '--proxy',
  146. dest='proxy',
  147. type='str',
  148. default='',
  149. help="Specify a proxy in the form [user:passwd@]proxy.server:port."
  150. ) # type: Any
  151. retries = partial(
  152. Option,
  153. '--retries',
  154. dest='retries',
  155. type='int',
  156. default=5,
  157. help="Maximum number of retries each connection should attempt "
  158. "(default %default times).",
  159. ) # type: Any
  160. timeout = partial(
  161. Option,
  162. '--timeout', '--default-timeout',
  163. metavar='sec',
  164. dest='timeout',
  165. type='float',
  166. default=15,
  167. help='Set the socket timeout (default %default seconds).',
  168. ) # type: Any
  169. skip_requirements_regex = partial(
  170. Option,
  171. # A regex to be used to skip requirements
  172. '--skip-requirements-regex',
  173. dest='skip_requirements_regex',
  174. type='str',
  175. default='',
  176. help=SUPPRESS_HELP,
  177. ) # type: Any
  178. def exists_action():
  179. return Option(
  180. # Option when path already exist
  181. '--exists-action',
  182. dest='exists_action',
  183. type='choice',
  184. choices=['s', 'i', 'w', 'b', 'a'],
  185. default=[],
  186. action='append',
  187. metavar='action',
  188. help="Default action when a path already exists: "
  189. "(s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort).",
  190. )
  191. cert = partial(
  192. Option,
  193. '--cert',
  194. dest='cert',
  195. type='str',
  196. metavar='path',
  197. help="Path to alternate CA bundle.",
  198. ) # type: Any
  199. client_cert = partial(
  200. Option,
  201. '--client-cert',
  202. dest='client_cert',
  203. type='str',
  204. default=None,
  205. metavar='path',
  206. help="Path to SSL client certificate, a single file containing the "
  207. "private key and the certificate in PEM format.",
  208. ) # type: Any
  209. index_url = partial(
  210. Option,
  211. '-i', '--index-url', '--pypi-url',
  212. dest='index_url',
  213. metavar='URL',
  214. default=PyPI.simple_url,
  215. help="Base URL of Python Package Index (default %default). "
  216. "This should point to a repository compliant with PEP 503 "
  217. "(the simple repository API) or a local directory laid out "
  218. "in the same format.",
  219. ) # type: Any
  220. def extra_index_url():
  221. return Option(
  222. '--extra-index-url',
  223. dest='extra_index_urls',
  224. metavar='URL',
  225. action='append',
  226. default=[],
  227. help="Extra URLs of package indexes to use in addition to "
  228. "--index-url. Should follow the same rules as "
  229. "--index-url.",
  230. )
  231. no_index = partial(
  232. Option,
  233. '--no-index',
  234. dest='no_index',
  235. action='store_true',
  236. default=False,
  237. help='Ignore package index (only looking at --find-links URLs instead).',
  238. ) # type: Any
  239. def find_links():
  240. return Option(
  241. '-f', '--find-links',
  242. dest='find_links',
  243. action='append',
  244. default=[],
  245. metavar='url',
  246. help="If a url or path to an html file, then parse for links to "
  247. "archives. If a local path or file:// url that's a directory, "
  248. "then look for archives in the directory listing.",
  249. )
  250. def trusted_host():
  251. return Option(
  252. "--trusted-host",
  253. dest="trusted_hosts",
  254. action="append",
  255. metavar="HOSTNAME",
  256. default=[],
  257. help="Mark this host as trusted, even though it does not have valid "
  258. "or any HTTPS.",
  259. )
  260. # Remove after 1.5
  261. process_dependency_links = partial(
  262. Option,
  263. "--process-dependency-links",
  264. dest="process_dependency_links",
  265. action="store_true",
  266. default=False,
  267. help="Enable the processing of dependency links.",
  268. ) # type: Any
  269. def constraints():
  270. return Option(
  271. '-c', '--constraint',
  272. dest='constraints',
  273. action='append',
  274. default=[],
  275. metavar='file',
  276. help='Constrain versions using the given constraints file. '
  277. 'This option can be used multiple times.'
  278. )
  279. def requirements():
  280. return Option(
  281. '-r', '--requirement',
  282. dest='requirements',
  283. action='append',
  284. default=[],
  285. metavar='file',
  286. help='Install from the given requirements file. '
  287. 'This option can be used multiple times.'
  288. )
  289. def editable():
  290. return Option(
  291. '-e', '--editable',
  292. dest='editables',
  293. action='append',
  294. default=[],
  295. metavar='path/url',
  296. help=('Install a project in editable mode (i.e. setuptools '
  297. '"develop mode") from a local project path or a VCS url.'),
  298. )
  299. src = partial(
  300. Option,
  301. '--src', '--source', '--source-dir', '--source-directory',
  302. dest='src_dir',
  303. metavar='dir',
  304. default=src_prefix,
  305. help='Directory to check out editable projects into. '
  306. 'The default in a virtualenv is "<venv path>/src". '
  307. 'The default for global installs is "<current dir>/src".'
  308. ) # type: Any
  309. def _get_format_control(values, option):
  310. """Get a format_control object."""
  311. return getattr(values, option.dest)
  312. def _handle_no_binary(option, opt_str, value, parser):
  313. existing = getattr(parser.values, option.dest)
  314. fmt_ctl_handle_mutual_exclude(
  315. value, existing.no_binary, existing.only_binary,
  316. )
  317. def _handle_only_binary(option, opt_str, value, parser):
  318. existing = getattr(parser.values, option.dest)
  319. fmt_ctl_handle_mutual_exclude(
  320. value, existing.only_binary, existing.no_binary,
  321. )
  322. def no_binary():
  323. return Option(
  324. "--no-binary", dest="format_control", action="callback",
  325. callback=_handle_no_binary, type="str",
  326. default=FormatControl(set(), set()),
  327. help="Do not use binary packages. Can be supplied multiple times, and "
  328. "each time adds to the existing value. Accepts either :all: to "
  329. "disable all binary packages, :none: to empty the set, or one or "
  330. "more package names with commas between them. Note that some "
  331. "packages are tricky to compile and may fail to install when "
  332. "this option is used on them.",
  333. )
  334. def only_binary():
  335. return Option(
  336. "--only-binary", dest="format_control", action="callback",
  337. callback=_handle_only_binary, type="str",
  338. default=FormatControl(set(), set()),
  339. help="Do not use source packages. Can be supplied multiple times, and "
  340. "each time adds to the existing value. Accepts either :all: to "
  341. "disable all source packages, :none: to empty the set, or one or "
  342. "more package names with commas between them. Packages without "
  343. "binary distributions will fail to install when this option is "
  344. "used on them.",
  345. )
  346. cache_dir = partial(
  347. Option,
  348. "--cache-dir",
  349. dest="cache_dir",
  350. default=USER_CACHE_DIR,
  351. metavar="dir",
  352. help="Store the cache data in <dir>."
  353. )
  354. no_cache = partial(
  355. Option,
  356. "--no-cache-dir",
  357. dest="cache_dir",
  358. action="store_false",
  359. help="Disable the cache.",
  360. )
  361. no_deps = partial(
  362. Option,
  363. '--no-deps', '--no-dependencies',
  364. dest='ignore_dependencies',
  365. action='store_true',
  366. default=False,
  367. help="Don't install package dependencies.",
  368. ) # type: Any
  369. build_dir = partial(
  370. Option,
  371. '-b', '--build', '--build-dir', '--build-directory',
  372. dest='build_dir',
  373. metavar='dir',
  374. help='Directory to unpack packages into and build in. Note that '
  375. 'an initial build still takes place in a temporary directory. '
  376. 'The location of temporary directories can be controlled by setting '
  377. 'the TMPDIR environment variable (TEMP on Windows) appropriately. '
  378. 'When passed, build directories are not cleaned in case of failures.'
  379. ) # type: Any
  380. ignore_requires_python = partial(
  381. Option,
  382. '--ignore-requires-python',
  383. dest='ignore_requires_python',
  384. action='store_true',
  385. help='Ignore the Requires-Python information.'
  386. ) # type: Any
  387. no_build_isolation = partial(
  388. Option,
  389. '--no-build-isolation',
  390. dest='build_isolation',
  391. action='store_false',
  392. default=True,
  393. help='Disable isolation when building a modern source distribution. '
  394. 'Build dependencies specified by PEP 518 must be already installed '
  395. 'if this option is used.'
  396. ) # type: Any
  397. install_options = partial(
  398. Option,
  399. '--install-option',
  400. dest='install_options',
  401. action='append',
  402. metavar='options',
  403. help="Extra arguments to be supplied to the setup.py install "
  404. "command (use like --install-option=\"--install-scripts=/usr/local/"
  405. "bin\"). Use multiple --install-option options to pass multiple "
  406. "options to setup.py install. If you are using an option with a "
  407. "directory path, be sure to use absolute path.",
  408. ) # type: Any
  409. global_options = partial(
  410. Option,
  411. '--global-option',
  412. dest='global_options',
  413. action='append',
  414. metavar='options',
  415. help="Extra global options to be supplied to the setup.py "
  416. "call before the install command.",
  417. ) # type: Any
  418. no_clean = partial(
  419. Option,
  420. '--no-clean',
  421. action='store_true',
  422. default=False,
  423. help="Don't clean up build directories)."
  424. ) # type: Any
  425. pre = partial(
  426. Option,
  427. '--pre',
  428. action='store_true',
  429. default=False,
  430. help="Include pre-release and development versions. By default, "
  431. "pip only finds stable versions.",
  432. ) # type: Any
  433. disable_pip_version_check = partial(
  434. Option,
  435. "--disable-pip-version-check",
  436. dest="disable_pip_version_check",
  437. action="store_true",
  438. default=False,
  439. help="Don't periodically check PyPI to determine whether a new version "
  440. "of pip is available for download. Implied with --no-index.",
  441. ) # type: Any
  442. # Deprecated, Remove later
  443. always_unzip = partial(
  444. Option,
  445. '-Z', '--always-unzip',
  446. dest='always_unzip',
  447. action='store_true',
  448. help=SUPPRESS_HELP,
  449. ) # type: Any
  450. def _merge_hash(option, opt_str, value, parser):
  451. """Given a value spelled "algo:digest", append the digest to a list
  452. pointed to in a dict by the algo name."""
  453. if not parser.values.hashes:
  454. parser.values.hashes = {}
  455. try:
  456. algo, digest = value.split(':', 1)
  457. except ValueError:
  458. parser.error('Arguments to %s must be a hash name '
  459. 'followed by a value, like --hash=sha256:abcde...' %
  460. opt_str)
  461. if algo not in STRONG_HASHES:
  462. parser.error('Allowed hash algorithms for %s are %s.' %
  463. (opt_str, ', '.join(STRONG_HASHES)))
  464. parser.values.hashes.setdefault(algo, []).append(digest)
  465. hash = partial(
  466. Option,
  467. '--hash',
  468. # Hash values eventually end up in InstallRequirement.hashes due to
  469. # __dict__ copying in process_line().
  470. dest='hashes',
  471. action='callback',
  472. callback=_merge_hash,
  473. type='string',
  474. help="Verify that the package's archive matches this "
  475. 'hash before installing. Example: --hash=sha256:abcdef...',
  476. ) # type: Any
  477. require_hashes = partial(
  478. Option,
  479. '--require-hashes',
  480. dest='require_hashes',
  481. action='store_true',
  482. default=False,
  483. help='Require a hash to check each requirement against, for '
  484. 'repeatable installs. This option is implied when any package in a '
  485. 'requirements file has a --hash option.',
  486. ) # type: Any
  487. ##########
  488. # groups #
  489. ##########
  490. general_group = {
  491. 'name': 'General Options',
  492. 'options': [
  493. help_,
  494. isolated_mode,
  495. require_virtualenv,
  496. verbose,
  497. version,
  498. quiet,
  499. log,
  500. no_input,
  501. proxy,
  502. retries,
  503. timeout,
  504. skip_requirements_regex,
  505. exists_action,
  506. trusted_host,
  507. cert,
  508. client_cert,
  509. cache_dir,
  510. no_cache,
  511. disable_pip_version_check,
  512. no_color,
  513. ]
  514. }
  515. index_group = {
  516. 'name': 'Package Index Options',
  517. 'options': [
  518. index_url,
  519. extra_index_url,
  520. no_index,
  521. find_links,
  522. process_dependency_links,
  523. ]
  524. }