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 14 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. PyPI, FormatControl, fmt_ctl_handle_mutual_exclude, fmt_ctl_no_binary,
  14. fmt_ctl_no_use_wheel)
  15. from pip.locations import CA_BUNDLE_PATH, USER_CACHE_DIR, src_prefix
  16. def make_option_group(group, parser):
  17. """
  18. Return an OptionGroup object
  19. group -- assumed to be dict with 'name' and 'options' keys
  20. parser -- an optparse Parser
  21. """
  22. option_group = OptionGroup(parser, group['name'])
  23. for option in group['options']:
  24. option_group.add_option(option())
  25. return option_group
  26. def resolve_wheel_no_use_binary(options):
  27. if not options.use_wheel:
  28. control = options.format_control
  29. fmt_ctl_no_use_wheel(control)
  30. def check_install_build_global(options, check_options=None):
  31. """Disable wheels if per-setup.py call options are set.
  32. :param options: The OptionParser options to update.
  33. :param check_options: The options to check, if not supplied defaults to
  34. options.
  35. """
  36. if check_options is None:
  37. check_options = options
  38. def getname(n):
  39. return getattr(check_options, n, None)
  40. names = ["build_options", "global_options", "install_options"]
  41. if any(map(getname, names)):
  42. control = options.format_control
  43. fmt_ctl_no_binary(control)
  44. warnings.warn(
  45. 'Disabling all use of wheels due to the use of --build-options '
  46. '/ --global-options / --install-options.', stacklevel=2)
  47. ###########
  48. # options #
  49. ###########
  50. help_ = partial(
  51. Option,
  52. '-h', '--help',
  53. dest='help',
  54. action='help',
  55. help='Show help.')
  56. isolated_mode = partial(
  57. Option,
  58. "--isolated",
  59. dest="isolated_mode",
  60. action="store_true",
  61. default=False,
  62. help=(
  63. "Run pip in an isolated mode, ignoring environment variables and user "
  64. "configuration."
  65. ),
  66. )
  67. require_virtualenv = partial(
  68. Option,
  69. # Run only if inside a virtualenv, bail if not.
  70. '--require-virtualenv', '--require-venv',
  71. dest='require_venv',
  72. action='store_true',
  73. default=False,
  74. help=SUPPRESS_HELP)
  75. verbose = partial(
  76. Option,
  77. '-v', '--verbose',
  78. dest='verbose',
  79. action='count',
  80. default=0,
  81. help='Give more output. Option is additive, and can be used up to 3 times.'
  82. )
  83. version = partial(
  84. Option,
  85. '-V', '--version',
  86. dest='version',
  87. action='store_true',
  88. help='Show version and exit.')
  89. quiet = partial(
  90. Option,
  91. '-q', '--quiet',
  92. dest='quiet',
  93. action='count',
  94. default=0,
  95. help='Give less output.')
  96. log = partial(
  97. Option,
  98. "--log", "--log-file", "--local-log",
  99. dest="log",
  100. metavar="path",
  101. help="Path to a verbose appending log."
  102. )
  103. log_explicit_levels = partial(
  104. Option,
  105. # Writes the log levels explicitely to the log'
  106. '--log-explicit-levels',
  107. dest='log_explicit_levels',
  108. action='store_true',
  109. default=False,
  110. help=SUPPRESS_HELP)
  111. no_input = partial(
  112. Option,
  113. # Don't ask for input
  114. '--no-input',
  115. dest='no_input',
  116. action='store_true',
  117. default=False,
  118. help=SUPPRESS_HELP)
  119. proxy = partial(
  120. Option,
  121. '--proxy',
  122. dest='proxy',
  123. type='str',
  124. default='',
  125. help="Specify a proxy in the form [user:passwd@]proxy.server:port.")
  126. retries = partial(
  127. Option,
  128. '--retries',
  129. dest='retries',
  130. type='int',
  131. default=5,
  132. help="Maximum number of retries each connection should attempt "
  133. "(default %default times).")
  134. timeout = partial(
  135. Option,
  136. '--timeout', '--default-timeout',
  137. metavar='sec',
  138. dest='timeout',
  139. type='float',
  140. default=15,
  141. help='Set the socket timeout (default %default seconds).')
  142. default_vcs = partial(
  143. Option,
  144. # The default version control system for editables, e.g. 'svn'
  145. '--default-vcs',
  146. dest='default_vcs',
  147. type='str',
  148. default='',
  149. help=SUPPRESS_HELP)
  150. skip_requirements_regex = partial(
  151. Option,
  152. # A regex to be used to skip requirements
  153. '--skip-requirements-regex',
  154. dest='skip_requirements_regex',
  155. type='str',
  156. default='',
  157. help=SUPPRESS_HELP)
  158. def exists_action():
  159. return Option(
  160. # Option when path already exist
  161. '--exists-action',
  162. dest='exists_action',
  163. type='choice',
  164. choices=['s', 'i', 'w', 'b'],
  165. default=[],
  166. action='append',
  167. metavar='action',
  168. help="Default action when a path already exists: "
  169. "(s)witch, (i)gnore, (w)ipe, (b)ackup.")
  170. cert = partial(
  171. Option,
  172. '--cert',
  173. dest='cert',
  174. type='str',
  175. default=CA_BUNDLE_PATH,
  176. metavar='path',
  177. help="Path to alternate CA bundle.")
  178. client_cert = partial(
  179. Option,
  180. '--client-cert',
  181. dest='client_cert',
  182. type='str',
  183. default=None,
  184. metavar='path',
  185. help="Path to SSL client certificate, a single file containing the "
  186. "private key and the certificate in PEM format.")
  187. index_url = partial(
  188. Option,
  189. '-i', '--index-url', '--pypi-url',
  190. dest='index_url',
  191. metavar='URL',
  192. default=PyPI.simple_url,
  193. help='Base URL of Python Package Index (default %default).')
  194. def extra_index_url():
  195. return Option(
  196. '--extra-index-url',
  197. dest='extra_index_urls',
  198. metavar='URL',
  199. action='append',
  200. default=[],
  201. help='Extra URLs of package indexes to use in addition to --index-url.'
  202. )
  203. no_index = partial(
  204. Option,
  205. '--no-index',
  206. dest='no_index',
  207. action='store_true',
  208. default=False,
  209. help='Ignore package index (only looking at --find-links URLs instead).')
  210. def find_links():
  211. return Option(
  212. '-f', '--find-links',
  213. dest='find_links',
  214. action='append',
  215. default=[],
  216. metavar='url',
  217. help="If a url or path to an html file, then parse for links to "
  218. "archives. If a local path or file:// url that's a directory,"
  219. "then look for archives in the directory listing.")
  220. def allow_external():
  221. return Option(
  222. "--allow-external",
  223. dest="allow_external",
  224. action="append",
  225. default=[],
  226. metavar="PACKAGE",
  227. help="Allow the installation of a package even if it is externally "
  228. "hosted",
  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="Allow the installation of all packages that are externally hosted",
  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="Allow the installation of a package even if it is hosted "
  266. "in an insecure and unverifiable way",
  267. )
  268. # Remove after 7.0
  269. no_allow_unsafe = partial(
  270. Option,
  271. "--no-allow-insecure",
  272. dest="allow_all_insecure",
  273. action="store_false",
  274. default=False,
  275. help=SUPPRESS_HELP
  276. )
  277. # Remove after 1.5
  278. process_dependency_links = partial(
  279. Option,
  280. "--process-dependency-links",
  281. dest="process_dependency_links",
  282. action="store_true",
  283. default=False,
  284. help="Enable the processing of dependency links.",
  285. )
  286. def constraints():
  287. return Option(
  288. '-c', '--constraint',
  289. dest='constraints',
  290. action='append',
  291. default=[],
  292. metavar='file',
  293. help='Constrain versions using the given constraints file. '
  294. 'This option can be used multiple times.')
  295. def requirements():
  296. return Option(
  297. '-r', '--requirement',
  298. dest='requirements',
  299. action='append',
  300. default=[],
  301. metavar='file',
  302. help='Install from the given requirements file. '
  303. 'This option can be used multiple times.')
  304. def editable():
  305. return Option(
  306. '-e', '--editable',
  307. dest='editables',
  308. action='append',
  309. default=[],
  310. metavar='path/url',
  311. help=('Install a project in editable mode (i.e. setuptools '
  312. '"develop mode") from a local project path or a VCS url.'),
  313. )
  314. src = partial(
  315. Option,
  316. '--src', '--source', '--source-dir', '--source-directory',
  317. dest='src_dir',
  318. metavar='dir',
  319. default=src_prefix,
  320. help='Directory to check out editable projects into. '
  321. 'The default in a virtualenv is "<venv path>/src". '
  322. 'The default for global installs is "<current dir>/src".'
  323. )
  324. # XXX: deprecated, remove in 9.0
  325. use_wheel = partial(
  326. Option,
  327. '--use-wheel',
  328. dest='use_wheel',
  329. action='store_true',
  330. default=True,
  331. help=SUPPRESS_HELP,
  332. )
  333. # XXX: deprecated, remove in 9.0
  334. no_use_wheel = partial(
  335. Option,
  336. '--no-use-wheel',
  337. dest='use_wheel',
  338. action='store_false',
  339. default=True,
  340. help=('Do not Find and prefer wheel archives when searching indexes and '
  341. 'find-links locations. DEPRECATED in favour of --no-binary.'),
  342. )
  343. def _get_format_control(values, option):
  344. """Get a format_control object."""
  345. return getattr(values, option.dest)
  346. def _handle_no_binary(option, opt_str, value, parser):
  347. existing = getattr(parser.values, option.dest)
  348. fmt_ctl_handle_mutual_exclude(
  349. value, existing.no_binary, existing.only_binary)
  350. def _handle_only_binary(option, opt_str, value, parser):
  351. existing = getattr(parser.values, option.dest)
  352. fmt_ctl_handle_mutual_exclude(
  353. value, existing.only_binary, existing.no_binary)
  354. def no_binary():
  355. return Option(
  356. "--no-binary", dest="format_control", action="callback",
  357. callback=_handle_no_binary, type="str",
  358. default=FormatControl(set(), set()),
  359. help="Do not use binary packages. Can be supplied multiple times, and "
  360. "each time adds to the existing value. Accepts either :all: to "
  361. "disable all binary packages, :none: to empty the set, or one or "
  362. "more package names with commas between them. Note that some "
  363. "packages are tricky to compile and may fail to install when "
  364. "this option is used on them.")
  365. def only_binary():
  366. return Option(
  367. "--only-binary", dest="format_control", action="callback",
  368. callback=_handle_only_binary, type="str",
  369. default=FormatControl(set(), set()),
  370. help="Do not use source packages. Can be supplied multiple times, and "
  371. "each time adds to the existing value. Accepts either :all: to "
  372. "disable all source packages, :none: to empty the set, or one or "
  373. "more package names with commas between them. Packages without "
  374. "binary distributions will fail to install when this option is "
  375. "used on them.")
  376. cache_dir = partial(
  377. Option,
  378. "--cache-dir",
  379. dest="cache_dir",
  380. default=USER_CACHE_DIR,
  381. metavar="dir",
  382. help="Store the cache data in <dir>."
  383. )
  384. no_cache = partial(
  385. Option,
  386. "--no-cache-dir",
  387. dest="cache_dir",
  388. action="store_false",
  389. help="Disable the cache.",
  390. )
  391. download_cache = partial(
  392. Option,
  393. '--download-cache',
  394. dest='download_cache',
  395. default=None,
  396. help=SUPPRESS_HELP)
  397. no_deps = partial(
  398. Option,
  399. '--no-deps', '--no-dependencies',
  400. dest='ignore_dependencies',
  401. action='store_true',
  402. default=False,
  403. help="Don't install package dependencies.")
  404. build_dir = partial(
  405. Option,
  406. '-b', '--build', '--build-dir', '--build-directory',
  407. dest='build_dir',
  408. metavar='dir',
  409. help='Directory to unpack packages into and build in.'
  410. )
  411. install_options = partial(
  412. Option,
  413. '--install-option',
  414. dest='install_options',
  415. action='append',
  416. metavar='options',
  417. help="Extra arguments to be supplied to the setup.py install "
  418. "command (use like --install-option=\"--install-scripts=/usr/local/"
  419. "bin\"). Use multiple --install-option options to pass multiple "
  420. "options to setup.py install. If you are using an option with a "
  421. "directory path, be sure to use absolute path.")
  422. global_options = partial(
  423. Option,
  424. '--global-option',
  425. dest='global_options',
  426. action='append',
  427. metavar='options',
  428. help="Extra global options to be supplied to the setup.py "
  429. "call before the install command.")
  430. no_clean = partial(
  431. Option,
  432. '--no-clean',
  433. action='store_true',
  434. default=False,
  435. help="Don't clean up build directories.")
  436. disable_pip_version_check = partial(
  437. Option,
  438. "--disable-pip-version-check",
  439. dest="disable_pip_version_check",
  440. action="store_true",
  441. default=False,
  442. help="Don't periodically check PyPI to determine whether a new version "
  443. "of pip is available for download. Implied with --no-index.")
  444. # Deprecated, Remove later
  445. always_unzip = partial(
  446. Option,
  447. '-Z', '--always-unzip',
  448. dest='always_unzip',
  449. action='store_true',
  450. help=SUPPRESS_HELP,
  451. )
  452. ##########
  453. # groups #
  454. ##########
  455. general_group = {
  456. 'name': 'General Options',
  457. 'options': [
  458. help_,
  459. isolated_mode,
  460. require_virtualenv,
  461. verbose,
  462. version,
  463. quiet,
  464. log,
  465. log_explicit_levels,
  466. no_input,
  467. proxy,
  468. retries,
  469. timeout,
  470. default_vcs,
  471. skip_requirements_regex,
  472. exists_action,
  473. trusted_host,
  474. cert,
  475. client_cert,
  476. cache_dir,
  477. no_cache,
  478. disable_pip_version_check,
  479. ]
  480. }
  481. index_group = {
  482. 'name': 'Package Index Options',
  483. 'options': [
  484. index_url,
  485. extra_index_url,
  486. no_index,
  487. find_links,
  488. allow_external,
  489. allow_all_external,
  490. no_allow_external,
  491. allow_unsafe,
  492. no_allow_unsafe,
  493. process_dependency_links,
  494. ]
  495. }