cmdoptions.py 17 KB

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