cmdoptions.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 parse's 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 copy
  10. from optparse import OptionGroup, SUPPRESS_HELP, Option
  11. from pip.index import PyPI
  12. from pip.locations import CA_BUNDLE_PATH, USER_CACHE_DIR, src_prefix
  13. def make_option_group(group, parser):
  14. """
  15. Return an OptionGroup object
  16. group -- assumed to be dict with 'name' and 'options' keys
  17. parser -- an optparse Parser
  18. """
  19. option_group = OptionGroup(parser, group['name'])
  20. for option in group['options']:
  21. option_group.add_option(option.make())
  22. return option_group
  23. class OptionMaker(object):
  24. """Class that stores the args/kwargs that would be used to make an Option,
  25. for making them later, and uses deepcopy's to reset state."""
  26. def __init__(self, *args, **kwargs):
  27. self.args = args
  28. self.kwargs = kwargs
  29. def make(self):
  30. args_copy = copy.deepcopy(self.args)
  31. kwargs_copy = copy.deepcopy(self.kwargs)
  32. return Option(*args_copy, **kwargs_copy)
  33. ###########
  34. # options #
  35. ###########
  36. help_ = OptionMaker(
  37. '-h', '--help',
  38. dest='help',
  39. action='help',
  40. help='Show help.')
  41. isolated_mode = OptionMaker(
  42. "--isolated",
  43. dest="isolated_mode",
  44. action="store_true",
  45. default=False,
  46. help=(
  47. "Run pip in an isolated mode, ignoring environment variables and user "
  48. "configuration."
  49. ),
  50. )
  51. require_virtualenv = OptionMaker(
  52. # Run only if inside a virtualenv, bail if not.
  53. '--require-virtualenv', '--require-venv',
  54. dest='require_venv',
  55. action='store_true',
  56. default=False,
  57. help=SUPPRESS_HELP)
  58. verbose = OptionMaker(
  59. '-v', '--verbose',
  60. dest='verbose',
  61. action='count',
  62. default=0,
  63. help='Give more output. Option is additive, and can be used up to 3 times.'
  64. )
  65. version = OptionMaker(
  66. '-V', '--version',
  67. dest='version',
  68. action='store_true',
  69. help='Show version and exit.')
  70. quiet = OptionMaker(
  71. '-q', '--quiet',
  72. dest='quiet',
  73. action='count',
  74. default=0,
  75. help='Give less output.')
  76. log = OptionMaker(
  77. "--log", "--log-file", "--local-log",
  78. dest="log",
  79. metavar="path",
  80. help="Path to a verbose appending log."
  81. )
  82. log_explicit_levels = OptionMaker(
  83. # Writes the log levels explicitely to the log'
  84. '--log-explicit-levels',
  85. dest='log_explicit_levels',
  86. action='store_true',
  87. default=False,
  88. help=SUPPRESS_HELP)
  89. no_input = OptionMaker(
  90. # Don't ask for input
  91. '--no-input',
  92. dest='no_input',
  93. action='store_true',
  94. default=False,
  95. help=SUPPRESS_HELP)
  96. proxy = OptionMaker(
  97. '--proxy',
  98. dest='proxy',
  99. type='str',
  100. default='',
  101. help="Specify a proxy in the form [user:passwd@]proxy.server:port.")
  102. retries = OptionMaker(
  103. '--retries',
  104. dest='retries',
  105. type='int',
  106. default=5,
  107. help="Maximum number of retries each connection should attempt "
  108. "(default %default times).")
  109. timeout = OptionMaker(
  110. '--timeout', '--default-timeout',
  111. metavar='sec',
  112. dest='timeout',
  113. type='float',
  114. default=15,
  115. help='Set the socket timeout (default %default seconds).')
  116. default_vcs = OptionMaker(
  117. # The default version control system for editables, e.g. 'svn'
  118. '--default-vcs',
  119. dest='default_vcs',
  120. type='str',
  121. default='',
  122. help=SUPPRESS_HELP)
  123. skip_requirements_regex = OptionMaker(
  124. # A regex to be used to skip requirements
  125. '--skip-requirements-regex',
  126. dest='skip_requirements_regex',
  127. type='str',
  128. default='',
  129. help=SUPPRESS_HELP)
  130. exists_action = OptionMaker(
  131. # Option when path already exist
  132. '--exists-action',
  133. dest='exists_action',
  134. type='choice',
  135. choices=['s', 'i', 'w', 'b'],
  136. default=[],
  137. action='append',
  138. metavar='action',
  139. help="Default action when a path already exists: "
  140. "(s)witch, (i)gnore, (w)ipe, (b)ackup.")
  141. cert = OptionMaker(
  142. '--cert',
  143. dest='cert',
  144. type='str',
  145. default=CA_BUNDLE_PATH,
  146. metavar='path',
  147. help="Path to alternate CA bundle.")
  148. client_cert = OptionMaker(
  149. '--client-cert',
  150. dest='client_cert',
  151. type='str',
  152. default=None,
  153. metavar='path',
  154. help="Path to SSL client certificate, a single file containing the "
  155. "private key and the certificate in PEM format.")
  156. index_url = OptionMaker(
  157. '-i', '--index-url', '--pypi-url',
  158. dest='index_url',
  159. metavar='URL',
  160. default=PyPI.simple_url,
  161. help='Base URL of Python Package Index (default %default).')
  162. extra_index_url = OptionMaker(
  163. '--extra-index-url',
  164. dest='extra_index_urls',
  165. metavar='URL',
  166. action='append',
  167. default=[],
  168. help='Extra URLs of package indexes to use in addition to --index-url.')
  169. no_index = OptionMaker(
  170. '--no-index',
  171. dest='no_index',
  172. action='store_true',
  173. default=False,
  174. help='Ignore package index (only looking at --find-links URLs instead).')
  175. find_links = OptionMaker(
  176. '-f', '--find-links',
  177. dest='find_links',
  178. action='append',
  179. default=[],
  180. metavar='url',
  181. help="If a url or path to an html file, then parse for links to archives. "
  182. "If a local path or file:// url that's a directory, then look for "
  183. "archives in the directory listing.")
  184. # TODO: Remove after 6.0
  185. use_mirrors = OptionMaker(
  186. '-M', '--use-mirrors',
  187. dest='use_mirrors',
  188. action='store_true',
  189. default=False,
  190. help=SUPPRESS_HELP)
  191. # TODO: Remove after 6.0
  192. mirrors = OptionMaker(
  193. '--mirrors',
  194. dest='mirrors',
  195. metavar='URL',
  196. action='append',
  197. default=[],
  198. help=SUPPRESS_HELP)
  199. allow_external = OptionMaker(
  200. "--allow-external",
  201. dest="allow_external",
  202. action="append",
  203. default=[],
  204. metavar="PACKAGE",
  205. help="Allow the installation of a package even if it is externally hosted",
  206. )
  207. allow_all_external = OptionMaker(
  208. "--allow-all-external",
  209. dest="allow_all_external",
  210. action="store_true",
  211. default=False,
  212. help="Allow the installation of all packages that are externally hosted",
  213. )
  214. trusted_host = OptionMaker(
  215. "--trusted-host",
  216. dest="trusted_hosts",
  217. action="append",
  218. metavar="HOSTNAME",
  219. default=[],
  220. help="Mark this host as trusted, even though it does not have valid or "
  221. "any HTTPS.",
  222. )
  223. # Remove after 7.0
  224. no_allow_external = OptionMaker(
  225. "--no-allow-external",
  226. dest="allow_all_external",
  227. action="store_false",
  228. default=False,
  229. help=SUPPRESS_HELP,
  230. )
  231. # Remove --allow-insecure after 7.0
  232. allow_unsafe = OptionMaker(
  233. "--allow-unverified", "--allow-insecure",
  234. dest="allow_unverified",
  235. action="append",
  236. default=[],
  237. metavar="PACKAGE",
  238. help="Allow the installation of a package even if it is hosted "
  239. "in an insecure and unverifiable way",
  240. )
  241. # Remove after 7.0
  242. no_allow_unsafe = OptionMaker(
  243. "--no-allow-insecure",
  244. dest="allow_all_insecure",
  245. action="store_false",
  246. default=False,
  247. help=SUPPRESS_HELP
  248. )
  249. # Remove after 1.5
  250. process_dependency_links = OptionMaker(
  251. "--process-dependency-links",
  252. dest="process_dependency_links",
  253. action="store_true",
  254. default=False,
  255. help="Enable the processing of dependency links.",
  256. )
  257. requirements = OptionMaker(
  258. '-r', '--requirement',
  259. dest='requirements',
  260. action='append',
  261. default=[],
  262. metavar='file',
  263. help='Install from the given requirements file. '
  264. 'This option can be used multiple times.')
  265. editable = OptionMaker(
  266. '-e', '--editable',
  267. dest='editables',
  268. action='append',
  269. default=[],
  270. metavar='path/url',
  271. help=('Install a project in editable mode (i.e. setuptools '
  272. '"develop mode") from a local project path or a VCS url.'),
  273. )
  274. src = OptionMaker(
  275. '--src', '--source', '--source-dir', '--source-directory',
  276. dest='src_dir',
  277. metavar='dir',
  278. default=src_prefix,
  279. help='Directory to check out editable projects into. '
  280. 'The default in a virtualenv is "<venv path>/src". '
  281. 'The default for global installs is "<current dir>/src".'
  282. )
  283. use_wheel = OptionMaker(
  284. '--use-wheel',
  285. dest='use_wheel',
  286. action='store_true',
  287. help=SUPPRESS_HELP,
  288. )
  289. no_use_wheel = OptionMaker(
  290. '--no-use-wheel',
  291. dest='use_wheel',
  292. action='store_false',
  293. default=True,
  294. help=('Do not Find and prefer wheel archives when searching indexes and '
  295. 'find-links locations.'),
  296. )
  297. cache_dir = OptionMaker(
  298. "--cache-dir",
  299. dest="cache_dir",
  300. default=USER_CACHE_DIR,
  301. metavar="dir",
  302. help="Store the cache data in <dir>."
  303. )
  304. no_cache = OptionMaker(
  305. "--no-cache-dir",
  306. dest="cache_dir",
  307. action="store_false",
  308. help="Disable the cache.",
  309. )
  310. download_cache = OptionMaker(
  311. '--download-cache',
  312. dest='download_cache',
  313. default=None,
  314. help=SUPPRESS_HELP)
  315. no_deps = OptionMaker(
  316. '--no-deps', '--no-dependencies',
  317. dest='ignore_dependencies',
  318. action='store_true',
  319. default=False,
  320. help="Don't install package dependencies.")
  321. build_dir = OptionMaker(
  322. '-b', '--build', '--build-dir', '--build-directory',
  323. dest='build_dir',
  324. metavar='dir',
  325. help='Directory to unpack packages into and build in.'
  326. )
  327. install_options = OptionMaker(
  328. '--install-option',
  329. dest='install_options',
  330. action='append',
  331. metavar='options',
  332. help="Extra arguments to be supplied to the setup.py install "
  333. "command (use like --install-option=\"--install-scripts=/usr/local/"
  334. "bin\"). Use multiple --install-option options to pass multiple "
  335. "options to setup.py install. If you are using an option with a "
  336. "directory path, be sure to use absolute path.")
  337. global_options = OptionMaker(
  338. '--global-option',
  339. dest='global_options',
  340. action='append',
  341. metavar='options',
  342. help="Extra global options to be supplied to the setup.py "
  343. "call before the install command.")
  344. no_clean = OptionMaker(
  345. '--no-clean',
  346. action='store_true',
  347. default=False,
  348. help="Don't clean up build directories.")
  349. disable_pip_version_check = OptionMaker(
  350. "--disable-pip-version-check",
  351. dest="disable_pip_version_check",
  352. action="store_true",
  353. default=False,
  354. help="Don't periodically check PyPI to determine whether a new version "
  355. "of pip is available for download. Implied with --no-index.")
  356. ##########
  357. # groups #
  358. ##########
  359. general_group = {
  360. 'name': 'General Options',
  361. 'options': [
  362. help_,
  363. isolated_mode,
  364. require_virtualenv,
  365. verbose,
  366. version,
  367. quiet,
  368. log,
  369. log_explicit_levels,
  370. no_input,
  371. proxy,
  372. retries,
  373. timeout,
  374. default_vcs,
  375. skip_requirements_regex,
  376. exists_action,
  377. trusted_host,
  378. cert,
  379. client_cert,
  380. cache_dir,
  381. no_cache,
  382. disable_pip_version_check,
  383. ]
  384. }
  385. index_group = {
  386. 'name': 'Package Index Options',
  387. 'options': [
  388. index_url,
  389. extra_index_url,
  390. no_index,
  391. find_links,
  392. use_mirrors,
  393. mirrors,
  394. allow_external,
  395. allow_all_external,
  396. no_allow_external,
  397. allow_unsafe,
  398. no_allow_unsafe,
  399. process_dependency_links,
  400. ]
  401. }