constructors.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. """Backing implementation for InstallRequirement's various constructors
  2. The idea here is that these formed a major chunk of InstallRequirement's size
  3. so, moving them and support code dedicated to them outside of that class
  4. helps creates for better understandability for the rest of the code.
  5. These are meant to be used elsewhere within pip to create instances of
  6. InstallRequirement.
  7. """
  8. import logging
  9. import os
  10. import re
  11. from typing import Any, Dict, Optional, Set, Tuple, Union
  12. from pip._vendor.packaging.markers import Marker
  13. from pip._vendor.packaging.requirements import InvalidRequirement, Requirement
  14. from pip._vendor.packaging.specifiers import Specifier
  15. from pip._vendor.pkg_resources import RequirementParseError, parse_requirements
  16. from pip._internal.exceptions import InstallationError
  17. from pip._internal.models.index import PyPI, TestPyPI
  18. from pip._internal.models.link import Link
  19. from pip._internal.models.wheel import Wheel
  20. from pip._internal.pyproject import make_pyproject_path
  21. from pip._internal.req.req_file import ParsedRequirement
  22. from pip._internal.req.req_install import InstallRequirement
  23. from pip._internal.utils.filetypes import is_archive_file
  24. from pip._internal.utils.misc import is_installable_dir
  25. from pip._internal.utils.urls import path_to_url
  26. from pip._internal.vcs import is_url, vcs
  27. __all__ = [
  28. "install_req_from_editable", "install_req_from_line",
  29. "parse_editable"
  30. ]
  31. logger = logging.getLogger(__name__)
  32. operators = Specifier._operators.keys()
  33. def _strip_extras(path: str) -> Tuple[str, Optional[str]]:
  34. m = re.match(r'^(.+)(\[[^\]]+\])$', path)
  35. extras = None
  36. if m:
  37. path_no_extras = m.group(1)
  38. extras = m.group(2)
  39. else:
  40. path_no_extras = path
  41. return path_no_extras, extras
  42. def convert_extras(extras: Optional[str]) -> Set[str]:
  43. if not extras:
  44. return set()
  45. return Requirement("placeholder" + extras.lower()).extras
  46. def parse_editable(editable_req: str) -> Tuple[Optional[str], str, Set[str]]:
  47. """Parses an editable requirement into:
  48. - a requirement name
  49. - an URL
  50. - extras
  51. - editable options
  52. Accepted requirements:
  53. svn+http://blahblah@rev#egg=Foobar[baz]&subdirectory=version_subdir
  54. .[some_extra]
  55. """
  56. url = editable_req
  57. # If a file path is specified with extras, strip off the extras.
  58. url_no_extras, extras = _strip_extras(url)
  59. if os.path.isdir(url_no_extras):
  60. setup_py = os.path.join(url_no_extras, 'setup.py')
  61. setup_cfg = os.path.join(url_no_extras, 'setup.cfg')
  62. if not os.path.exists(setup_py) and not os.path.exists(setup_cfg):
  63. msg = (
  64. 'File "setup.py" or "setup.cfg" not found. Directory cannot be '
  65. 'installed in editable mode: {}'
  66. .format(os.path.abspath(url_no_extras))
  67. )
  68. pyproject_path = make_pyproject_path(url_no_extras)
  69. if os.path.isfile(pyproject_path):
  70. msg += (
  71. '\n(A "pyproject.toml" file was found, but editable '
  72. 'mode currently requires a setuptools-based build.)'
  73. )
  74. raise InstallationError(msg)
  75. # Treating it as code that has already been checked out
  76. url_no_extras = path_to_url(url_no_extras)
  77. if url_no_extras.lower().startswith('file:'):
  78. package_name = Link(url_no_extras).egg_fragment
  79. if extras:
  80. return (
  81. package_name,
  82. url_no_extras,
  83. Requirement("placeholder" + extras.lower()).extras,
  84. )
  85. else:
  86. return package_name, url_no_extras, set()
  87. for version_control in vcs:
  88. if url.lower().startswith(f'{version_control}:'):
  89. url = f'{version_control}+{url}'
  90. break
  91. link = Link(url)
  92. if not link.is_vcs:
  93. backends = ", ".join(vcs.all_schemes)
  94. raise InstallationError(
  95. f'{editable_req} is not a valid editable requirement. '
  96. f'It should either be a path to a local project or a VCS URL '
  97. f'(beginning with {backends}).'
  98. )
  99. package_name = link.egg_fragment
  100. if not package_name:
  101. raise InstallationError(
  102. "Could not detect requirement name for '{}', please specify one "
  103. "with #egg=your_package_name".format(editable_req)
  104. )
  105. return package_name, url, set()
  106. def deduce_helpful_msg(req: str) -> str:
  107. """Returns helpful msg in case requirements file does not exist,
  108. or cannot be parsed.
  109. :params req: Requirements file path
  110. """
  111. msg = ""
  112. if os.path.exists(req):
  113. msg = " The path does exist. "
  114. # Try to parse and check if it is a requirements file.
  115. try:
  116. with open(req) as fp:
  117. # parse first line only
  118. next(parse_requirements(fp.read()))
  119. msg += (
  120. "The argument you provided "
  121. "({}) appears to be a"
  122. " requirements file. If that is the"
  123. " case, use the '-r' flag to install"
  124. " the packages specified within it."
  125. ).format(req)
  126. except RequirementParseError:
  127. logger.debug(
  128. "Cannot parse '%s' as requirements file", req, exc_info=True
  129. )
  130. else:
  131. msg += f" File '{req}' does not exist."
  132. return msg
  133. class RequirementParts:
  134. def __init__(
  135. self,
  136. requirement: Optional[Requirement],
  137. link: Optional[Link],
  138. markers: Optional[Marker],
  139. extras: Set[str],
  140. ):
  141. self.requirement = requirement
  142. self.link = link
  143. self.markers = markers
  144. self.extras = extras
  145. def parse_req_from_editable(editable_req: str) -> RequirementParts:
  146. name, url, extras_override = parse_editable(editable_req)
  147. if name is not None:
  148. try:
  149. req: Optional[Requirement] = Requirement(name)
  150. except InvalidRequirement:
  151. raise InstallationError(f"Invalid requirement: '{name}'")
  152. else:
  153. req = None
  154. link = Link(url)
  155. return RequirementParts(req, link, None, extras_override)
  156. # ---- The actual constructors follow ----
  157. def install_req_from_editable(
  158. editable_req: str,
  159. comes_from: Optional[Union[InstallRequirement, str]] = None,
  160. use_pep517: Optional[bool] = None,
  161. isolated: bool = False,
  162. options: Optional[Dict[str, Any]] = None,
  163. constraint: bool = False,
  164. user_supplied: bool = False,
  165. ) -> InstallRequirement:
  166. parts = parse_req_from_editable(editable_req)
  167. return InstallRequirement(
  168. parts.requirement,
  169. comes_from=comes_from,
  170. user_supplied=user_supplied,
  171. editable=True,
  172. link=parts.link,
  173. constraint=constraint,
  174. use_pep517=use_pep517,
  175. isolated=isolated,
  176. install_options=options.get("install_options", []) if options else [],
  177. global_options=options.get("global_options", []) if options else [],
  178. hash_options=options.get("hashes", {}) if options else {},
  179. extras=parts.extras,
  180. )
  181. def _looks_like_path(name: str) -> bool:
  182. """Checks whether the string "looks like" a path on the filesystem.
  183. This does not check whether the target actually exists, only judge from the
  184. appearance.
  185. Returns true if any of the following conditions is true:
  186. * a path separator is found (either os.path.sep or os.path.altsep);
  187. * a dot is found (which represents the current directory).
  188. """
  189. if os.path.sep in name:
  190. return True
  191. if os.path.altsep is not None and os.path.altsep in name:
  192. return True
  193. if name.startswith("."):
  194. return True
  195. return False
  196. def _get_url_from_path(path: str, name: str) -> Optional[str]:
  197. """
  198. First, it checks whether a provided path is an installable directory. If it
  199. is, returns the path.
  200. If false, check if the path is an archive file (such as a .whl).
  201. The function checks if the path is a file. If false, if the path has
  202. an @, it will treat it as a PEP 440 URL requirement and return the path.
  203. """
  204. if _looks_like_path(name) and os.path.isdir(path):
  205. if is_installable_dir(path):
  206. return path_to_url(path)
  207. raise InstallationError(
  208. f"Directory {name!r} is not installable. Neither 'setup.py' "
  209. "nor 'pyproject.toml' found."
  210. )
  211. if not is_archive_file(path):
  212. return None
  213. if os.path.isfile(path):
  214. return path_to_url(path)
  215. urlreq_parts = name.split('@', 1)
  216. if len(urlreq_parts) >= 2 and not _looks_like_path(urlreq_parts[0]):
  217. # If the path contains '@' and the part before it does not look
  218. # like a path, try to treat it as a PEP 440 URL req instead.
  219. return None
  220. logger.warning(
  221. 'Requirement %r looks like a filename, but the '
  222. 'file does not exist',
  223. name
  224. )
  225. return path_to_url(path)
  226. def parse_req_from_line(name: str, line_source: Optional[str]) -> RequirementParts:
  227. if is_url(name):
  228. marker_sep = '; '
  229. else:
  230. marker_sep = ';'
  231. if marker_sep in name:
  232. name, markers_as_string = name.split(marker_sep, 1)
  233. markers_as_string = markers_as_string.strip()
  234. if not markers_as_string:
  235. markers = None
  236. else:
  237. markers = Marker(markers_as_string)
  238. else:
  239. markers = None
  240. name = name.strip()
  241. req_as_string = None
  242. path = os.path.normpath(os.path.abspath(name))
  243. link = None
  244. extras_as_string = None
  245. if is_url(name):
  246. link = Link(name)
  247. else:
  248. p, extras_as_string = _strip_extras(path)
  249. url = _get_url_from_path(p, name)
  250. if url is not None:
  251. link = Link(url)
  252. # it's a local file, dir, or url
  253. if link:
  254. # Handle relative file URLs
  255. if link.scheme == 'file' and re.search(r'\.\./', link.url):
  256. link = Link(
  257. path_to_url(os.path.normpath(os.path.abspath(link.path))))
  258. # wheel file
  259. if link.is_wheel:
  260. wheel = Wheel(link.filename) # can raise InvalidWheelFilename
  261. req_as_string = f"{wheel.name}=={wheel.version}"
  262. else:
  263. # set the req to the egg fragment. when it's not there, this
  264. # will become an 'unnamed' requirement
  265. req_as_string = link.egg_fragment
  266. # a requirement specifier
  267. else:
  268. req_as_string = name
  269. extras = convert_extras(extras_as_string)
  270. def with_source(text: str) -> str:
  271. if not line_source:
  272. return text
  273. return f'{text} (from {line_source})'
  274. def _parse_req_string(req_as_string: str) -> Requirement:
  275. try:
  276. req = Requirement(req_as_string)
  277. except InvalidRequirement:
  278. if os.path.sep in req_as_string:
  279. add_msg = "It looks like a path."
  280. add_msg += deduce_helpful_msg(req_as_string)
  281. elif ('=' in req_as_string and
  282. not any(op in req_as_string for op in operators)):
  283. add_msg = "= is not a valid operator. Did you mean == ?"
  284. else:
  285. add_msg = ''
  286. msg = with_source(
  287. f'Invalid requirement: {req_as_string!r}'
  288. )
  289. if add_msg:
  290. msg += f'\nHint: {add_msg}'
  291. raise InstallationError(msg)
  292. else:
  293. # Deprecate extras after specifiers: "name>=1.0[extras]"
  294. # This currently works by accident because _strip_extras() parses
  295. # any extras in the end of the string and those are saved in
  296. # RequirementParts
  297. for spec in req.specifier:
  298. spec_str = str(spec)
  299. if spec_str.endswith(']'):
  300. msg = f"Extras after version '{spec_str}'."
  301. raise InstallationError(msg)
  302. return req
  303. if req_as_string is not None:
  304. req: Optional[Requirement] = _parse_req_string(req_as_string)
  305. else:
  306. req = None
  307. return RequirementParts(req, link, markers, extras)
  308. def install_req_from_line(
  309. name: str,
  310. comes_from: Optional[Union[str, InstallRequirement]] = None,
  311. use_pep517: Optional[bool] = None,
  312. isolated: bool = False,
  313. options: Optional[Dict[str, Any]] = None,
  314. constraint: bool = False,
  315. line_source: Optional[str] = None,
  316. user_supplied: bool = False,
  317. ) -> InstallRequirement:
  318. """Creates an InstallRequirement from a name, which might be a
  319. requirement, directory containing 'setup.py', filename, or URL.
  320. :param line_source: An optional string describing where the line is from,
  321. for logging purposes in case of an error.
  322. """
  323. parts = parse_req_from_line(name, line_source)
  324. return InstallRequirement(
  325. parts.requirement, comes_from, link=parts.link, markers=parts.markers,
  326. use_pep517=use_pep517, isolated=isolated,
  327. install_options=options.get("install_options", []) if options else [],
  328. global_options=options.get("global_options", []) if options else [],
  329. hash_options=options.get("hashes", {}) if options else {},
  330. constraint=constraint,
  331. extras=parts.extras,
  332. user_supplied=user_supplied,
  333. )
  334. def install_req_from_req_string(
  335. req_string: str,
  336. comes_from: Optional[InstallRequirement] = None,
  337. isolated: bool = False,
  338. use_pep517: Optional[bool] = None,
  339. user_supplied: bool = False,
  340. ) -> InstallRequirement:
  341. try:
  342. req = Requirement(req_string)
  343. except InvalidRequirement:
  344. raise InstallationError(f"Invalid requirement: '{req_string}'")
  345. domains_not_allowed = [
  346. PyPI.file_storage_domain,
  347. TestPyPI.file_storage_domain,
  348. ]
  349. if (req.url and comes_from and comes_from.link and
  350. comes_from.link.netloc in domains_not_allowed):
  351. # Explicitly disallow pypi packages that depend on external urls
  352. raise InstallationError(
  353. "Packages installed from PyPI cannot depend on packages "
  354. "which are not also hosted on PyPI.\n"
  355. "{} depends on {} ".format(comes_from.name, req)
  356. )
  357. return InstallRequirement(
  358. req,
  359. comes_from,
  360. isolated=isolated,
  361. use_pep517=use_pep517,
  362. user_supplied=user_supplied,
  363. )
  364. def install_req_from_parsed_requirement(
  365. parsed_req: ParsedRequirement,
  366. isolated: bool = False,
  367. use_pep517: Optional[bool] = None,
  368. user_supplied: bool = False,
  369. ) -> InstallRequirement:
  370. if parsed_req.is_editable:
  371. req = install_req_from_editable(
  372. parsed_req.requirement,
  373. comes_from=parsed_req.comes_from,
  374. use_pep517=use_pep517,
  375. constraint=parsed_req.constraint,
  376. isolated=isolated,
  377. user_supplied=user_supplied,
  378. )
  379. else:
  380. req = install_req_from_line(
  381. parsed_req.requirement,
  382. comes_from=parsed_req.comes_from,
  383. use_pep517=use_pep517,
  384. isolated=isolated,
  385. options=parsed_req.options,
  386. constraint=parsed_req.constraint,
  387. line_source=parsed_req.line_source,
  388. user_supplied=user_supplied,
  389. )
  390. return req
  391. def install_req_from_link_and_ireq(
  392. link: Link, ireq: InstallRequirement
  393. ) -> InstallRequirement:
  394. return InstallRequirement(
  395. req=ireq.req,
  396. comes_from=ireq.comes_from,
  397. editable=ireq.editable,
  398. link=link,
  399. markers=ireq.markers,
  400. use_pep517=ireq.use_pep517,
  401. isolated=ireq.isolated,
  402. install_options=ireq.install_options,
  403. global_options=ireq.global_options,
  404. hash_options=ireq.hash_options,
  405. )