Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmdoptions.py 19KB

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