constructors.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. from __future__ import annotations
  9. import copy
  10. import logging
  11. import os
  12. import re
  13. from collections.abc import Collection
  14. from dataclasses import dataclass
  15. from pip._vendor.packaging.markers import Marker
  16. from pip._vendor.packaging.requirements import InvalidRequirement, Requirement
  17. from pip._vendor.packaging.specifiers import Specifier
  18. from pip._internal.exceptions import InstallationError
  19. from pip._internal.models.index import PyPI, TestPyPI
  20. from pip._internal.models.link import Link
  21. from pip._internal.models.wheel import Wheel
  22. from pip._internal.req.req_file import ParsedRequirement
  23. from pip._internal.req.req_install import InstallRequirement
  24. from pip._internal.utils.filetypes import is_archive_file
  25. from pip._internal.utils.misc import is_installable_dir
  26. from pip._internal.utils.packaging import get_requirement
  27. from pip._internal.utils.urls import path_to_url
  28. from pip._internal.vcs import is_url, vcs
  29. __all__ = [
  30. "install_req_from_editable",
  31. "install_req_from_line",
  32. "parse_editable",
  33. ]
  34. logger = logging.getLogger(__name__)
  35. operators = Specifier._operators.keys()
  36. def _strip_extras(path: str) -> tuple[str, str | None]:
  37. m = re.match(r"^(.+)(\[[^\]]+\])$", path)
  38. extras = None
  39. if m:
  40. path_no_extras = m.group(1).rstrip()
  41. extras = m.group(2)
  42. else:
  43. path_no_extras = path
  44. return path_no_extras, extras
  45. def convert_extras(extras: str | None) -> set[str]:
  46. if not extras:
  47. return set()
  48. return get_requirement("placeholder" + extras.lower()).extras
  49. def _set_requirement_extras(req: Requirement, new_extras: set[str]) -> Requirement:
  50. """
  51. Returns a new requirement based on the given one, with the supplied extras. If the
  52. given requirement already has extras those are replaced (or dropped if no new extras
  53. are given).
  54. """
  55. match: re.Match[str] | None = re.fullmatch(
  56. # see https://peps.python.org/pep-0508/#complete-grammar
  57. r"([\w\t .-]+)(\[[^\]]*\])?(.*)",
  58. str(req),
  59. flags=re.ASCII,
  60. )
  61. # ireq.req is a valid requirement so the regex should always match
  62. assert (
  63. match is not None
  64. ), f"regex match on requirement {req} failed, this should never happen"
  65. pre: str | None = match.group(1)
  66. post: str | None = match.group(3)
  67. assert (
  68. pre is not None and post is not None
  69. ), f"regex group selection for requirement {req} failed, this should never happen"
  70. extras: str = "[{}]".format(",".join(sorted(new_extras)) if new_extras else "")
  71. return get_requirement(f"{pre}{extras}{post}")
  72. def _parse_direct_url_editable(editable_req: str) -> tuple[str | None, str, set[str]]:
  73. try:
  74. req = Requirement(editable_req)
  75. except InvalidRequirement:
  76. pass
  77. else:
  78. if req.url:
  79. # Join the marker back into the name part. This will be parsed out
  80. # later into a Requirement again.
  81. if req.marker:
  82. name = f"{req.name} ; {req.marker}"
  83. else:
  84. name = req.name
  85. return (name, req.url, req.extras)
  86. raise ValueError
  87. def _parse_pip_syntax_editable(editable_req: str) -> tuple[str | None, str, set[str]]:
  88. url = editable_req
  89. # If a file path is specified with extras, strip off the extras.
  90. url_no_extras, extras = _strip_extras(url)
  91. if os.path.isdir(url_no_extras):
  92. # Treating it as code that has already been checked out
  93. url_no_extras = path_to_url(url_no_extras)
  94. if url_no_extras.lower().startswith("file:"):
  95. package_name = Link(url_no_extras).egg_fragment
  96. if extras:
  97. return (
  98. package_name,
  99. url_no_extras,
  100. get_requirement("placeholder" + extras.lower()).extras,
  101. )
  102. else:
  103. return package_name, url_no_extras, set()
  104. for version_control in vcs:
  105. if url.lower().startswith(f"{version_control}:"):
  106. url = f"{version_control}+{url}"
  107. break
  108. return Link(url).egg_fragment, url, set()
  109. def parse_editable(editable_req: str) -> tuple[str | None, str, set[str]]:
  110. """Parses an editable requirement into:
  111. - a requirement name with environment markers
  112. - an URL
  113. - extras
  114. Accepted requirements:
  115. - svn+http://blahblah@rev#egg=Foobar[baz]&subdirectory=version_subdir
  116. - local_path[some_extra]
  117. - Foobar[extra] @ svn+http://blahblah@rev#subdirectory=subdir ; markers
  118. """
  119. try:
  120. package_name, url, extras = _parse_direct_url_editable(editable_req)
  121. except ValueError:
  122. package_name, url, extras = _parse_pip_syntax_editable(editable_req)
  123. link = Link(url)
  124. if not link.is_vcs and not link.url.startswith("file:"):
  125. backends = ", ".join(vcs.all_schemes)
  126. raise InstallationError(
  127. f"{editable_req} is not a valid editable requirement. "
  128. f"It should either be a path to a local project or a VCS URL "
  129. f"(beginning with {backends})."
  130. )
  131. # The project name can be inferred from local file URIs easily.
  132. if not package_name and not link.url.startswith("file:"):
  133. raise InstallationError(
  134. f"Could not detect requirement name for '{editable_req}', "
  135. "please specify one with your_package_name @ URL"
  136. )
  137. return package_name, url, extras
  138. def check_first_requirement_in_file(filename: str) -> None:
  139. """Check if file is parsable as a requirements file.
  140. This is heavily based on ``pkg_resources.parse_requirements``, but
  141. simplified to just check the first meaningful line.
  142. :raises InvalidRequirement: If the first meaningful line cannot be parsed
  143. as an requirement.
  144. """
  145. with open(filename, encoding="utf-8", errors="ignore") as f:
  146. # Create a steppable iterator, so we can handle \-continuations.
  147. lines = (
  148. line
  149. for line in (line.strip() for line in f)
  150. if line and not line.startswith("#") # Skip blank lines/comments.
  151. )
  152. for line in lines:
  153. # Drop comments -- a hash without a space may be in a URL.
  154. if " #" in line:
  155. line = line[: line.find(" #")]
  156. # If there is a line continuation, drop it, and append the next line.
  157. if line.endswith("\\"):
  158. line = line[:-2].strip() + next(lines, "")
  159. get_requirement(line)
  160. return
  161. def deduce_helpful_msg(req: str) -> str:
  162. """Returns helpful msg in case requirements file does not exist,
  163. or cannot be parsed.
  164. :params req: Requirements file path
  165. """
  166. if not os.path.exists(req):
  167. return f" File '{req}' does not exist."
  168. msg = " The path does exist. "
  169. # Try to parse and check if it is a requirements file.
  170. try:
  171. check_first_requirement_in_file(req)
  172. except InvalidRequirement:
  173. logger.debug("Cannot parse '%s' as requirements file", req)
  174. else:
  175. msg += (
  176. f"The argument you provided "
  177. f"({req}) appears to be a"
  178. f" requirements file. If that is the"
  179. f" case, use the '-r' flag to install"
  180. f" the packages specified within it."
  181. )
  182. return msg
  183. @dataclass(frozen=True)
  184. class RequirementParts:
  185. requirement: Requirement | None
  186. link: Link | None
  187. markers: Marker | None
  188. extras: set[str]
  189. def parse_req_from_editable(editable_req: str) -> RequirementParts:
  190. name, url, extras_override = parse_editable(editable_req)
  191. if name is not None:
  192. try:
  193. req: Requirement | None = get_requirement(name)
  194. except InvalidRequirement as exc:
  195. raise InstallationError(f"Invalid requirement: {name!r}: {exc}")
  196. else:
  197. req = None
  198. link = Link(url)
  199. return RequirementParts(req, link, None, extras_override)
  200. # ---- The actual constructors follow ----
  201. def install_req_from_editable(
  202. editable_req: str,
  203. comes_from: InstallRequirement | str | None = None,
  204. *,
  205. isolated: bool = False,
  206. hash_options: dict[str, list[str]] | None = None,
  207. constraint: bool = False,
  208. user_supplied: bool = False,
  209. permit_editable_wheels: bool = False,
  210. config_settings: dict[str, str | list[str]] | None = None,
  211. ) -> InstallRequirement:
  212. if constraint:
  213. raise InstallationError("Editable requirements are not allowed as constraints")
  214. parts = parse_req_from_editable(editable_req)
  215. return InstallRequirement(
  216. parts.requirement,
  217. comes_from=comes_from,
  218. user_supplied=user_supplied,
  219. editable=True,
  220. permit_editable_wheels=permit_editable_wheels,
  221. link=parts.link,
  222. constraint=constraint,
  223. isolated=isolated,
  224. hash_options=hash_options,
  225. config_settings=config_settings,
  226. extras=parts.extras,
  227. )
  228. def _looks_like_path(name: str) -> bool:
  229. """Checks whether the string "looks like" a path on the filesystem.
  230. This does not check whether the target actually exists, only judge from the
  231. appearance.
  232. Returns true if any of the following conditions is true:
  233. * a path separator is found (either os.path.sep or os.path.altsep);
  234. * a dot is found (which represents the current directory).
  235. """
  236. if os.path.sep in name:
  237. return True
  238. if os.path.altsep is not None and os.path.altsep in name:
  239. return True
  240. if name.startswith("."):
  241. return True
  242. return False
  243. def _get_url_from_path(path: str, name: str) -> str | None:
  244. """
  245. First, it checks whether a provided path is an installable directory. If it
  246. is, returns the path.
  247. If false, check if the path is an archive file (such as a .whl).
  248. The function checks if the path is a file. If false, if the path has
  249. an @, it will treat it as a PEP 440 URL requirement and return the path.
  250. """
  251. if _looks_like_path(name) and os.path.isdir(path):
  252. if is_installable_dir(path):
  253. return path_to_url(path)
  254. # TODO: The is_installable_dir test here might not be necessary
  255. # now that it is done in load_pyproject_toml too.
  256. raise InstallationError(
  257. f"Directory {name!r} is not installable. Neither 'setup.py' "
  258. "nor 'pyproject.toml' found."
  259. )
  260. if not is_archive_file(path):
  261. return None
  262. if os.path.isfile(path):
  263. return path_to_url(path)
  264. urlreq_parts = name.split("@", 1)
  265. if len(urlreq_parts) >= 2 and not _looks_like_path(urlreq_parts[0]):
  266. # If the path contains '@' and the part before it does not look
  267. # like a path, try to treat it as a PEP 440 URL req instead.
  268. return None
  269. logger.warning(
  270. "Requirement %r looks like a filename, but the file does not exist",
  271. name,
  272. )
  273. return path_to_url(path)
  274. def parse_req_from_line(name: str, line_source: str | None) -> RequirementParts:
  275. if is_url(name):
  276. marker_sep = "; "
  277. else:
  278. marker_sep = ";"
  279. if marker_sep in name:
  280. name, markers_as_string = name.split(marker_sep, 1)
  281. markers_as_string = markers_as_string.strip()
  282. if not markers_as_string:
  283. markers = None
  284. else:
  285. markers = Marker(markers_as_string)
  286. else:
  287. markers = None
  288. name = name.strip()
  289. req_as_string = None
  290. path = os.path.normpath(os.path.abspath(name))
  291. link = None
  292. extras_as_string = None
  293. if is_url(name):
  294. link = Link(name)
  295. else:
  296. p, extras_as_string = _strip_extras(path)
  297. url = _get_url_from_path(p, name)
  298. if url is not None:
  299. link = Link(url)
  300. # it's a local file, dir, or url
  301. if link:
  302. # Handle relative file URLs
  303. if link.scheme == "file" and re.search(r"\.\./", link.url):
  304. link = Link(path_to_url(os.path.normpath(os.path.abspath(link.path))))
  305. # wheel file
  306. if link.is_wheel:
  307. wheel = Wheel(link.filename) # can raise InvalidWheelFilename
  308. req_as_string = f"{wheel.name}=={wheel.version}"
  309. else:
  310. # set the req to the egg fragment. when it's not there, this
  311. # will become an 'unnamed' requirement
  312. req_as_string = link.egg_fragment
  313. # a requirement specifier
  314. else:
  315. req_as_string = name
  316. extras = convert_extras(extras_as_string)
  317. def with_source(text: str) -> str:
  318. if not line_source:
  319. return text
  320. return f"{text} (from {line_source})"
  321. def _parse_req_string(req_as_string: str) -> Requirement:
  322. try:
  323. return get_requirement(req_as_string)
  324. except InvalidRequirement as exc:
  325. if os.path.sep in req_as_string:
  326. add_msg = "It looks like a path."
  327. add_msg += deduce_helpful_msg(req_as_string)
  328. elif "=" in req_as_string and not any(
  329. op in req_as_string for op in operators
  330. ):
  331. add_msg = "= is not a valid operator. Did you mean == ?"
  332. else:
  333. add_msg = ""
  334. msg = with_source(f"Invalid requirement: {req_as_string!r}: {exc}")
  335. if add_msg:
  336. msg += f"\nHint: {add_msg}"
  337. raise InstallationError(msg)
  338. if req_as_string is not None:
  339. req: Requirement | None = _parse_req_string(req_as_string)
  340. else:
  341. req = None
  342. return RequirementParts(req, link, markers, extras)
  343. def install_req_from_line(
  344. name: str,
  345. comes_from: str | InstallRequirement | None = None,
  346. *,
  347. isolated: bool = False,
  348. hash_options: dict[str, list[str]] | None = None,
  349. constraint: bool = False,
  350. line_source: str | None = None,
  351. user_supplied: bool = False,
  352. config_settings: dict[str, str | list[str]] | None = None,
  353. ) -> InstallRequirement:
  354. """Creates an InstallRequirement from a name, which might be a
  355. requirement, directory containing 'setup.py', filename, or URL.
  356. :param line_source: An optional string describing where the line is from,
  357. for logging purposes in case of an error.
  358. """
  359. parts = parse_req_from_line(name, line_source)
  360. return InstallRequirement(
  361. parts.requirement,
  362. comes_from,
  363. link=parts.link,
  364. markers=parts.markers,
  365. isolated=isolated,
  366. hash_options=hash_options,
  367. config_settings=config_settings,
  368. constraint=constraint,
  369. extras=parts.extras,
  370. user_supplied=user_supplied,
  371. )
  372. def install_req_from_req_string(
  373. req_string: str,
  374. comes_from: InstallRequirement | None = None,
  375. isolated: bool = False,
  376. user_supplied: bool = False,
  377. ) -> InstallRequirement:
  378. try:
  379. req = get_requirement(req_string)
  380. except InvalidRequirement as exc:
  381. raise InstallationError(f"Invalid requirement: {req_string!r}: {exc}")
  382. domains_not_allowed = [
  383. PyPI.file_storage_domain,
  384. TestPyPI.file_storage_domain,
  385. ]
  386. if (
  387. req.url
  388. and comes_from
  389. and comes_from.link
  390. and comes_from.link.netloc in domains_not_allowed
  391. ):
  392. # Explicitly disallow pypi packages that depend on external urls
  393. raise InstallationError(
  394. "Packages installed from PyPI cannot depend on packages "
  395. "which are not also hosted on PyPI.\n"
  396. f"{comes_from.name} depends on {req} "
  397. )
  398. return InstallRequirement(
  399. req,
  400. comes_from,
  401. isolated=isolated,
  402. user_supplied=user_supplied,
  403. )
  404. def install_req_from_parsed_requirement(
  405. parsed_req: ParsedRequirement,
  406. isolated: bool = False,
  407. user_supplied: bool = False,
  408. config_settings: dict[str, str | list[str]] | None = None,
  409. ) -> InstallRequirement:
  410. if parsed_req.is_editable:
  411. req = install_req_from_editable(
  412. parsed_req.requirement,
  413. comes_from=parsed_req.comes_from,
  414. constraint=parsed_req.constraint,
  415. isolated=isolated,
  416. user_supplied=user_supplied,
  417. config_settings=config_settings,
  418. )
  419. else:
  420. req = install_req_from_line(
  421. parsed_req.requirement,
  422. comes_from=parsed_req.comes_from,
  423. isolated=isolated,
  424. hash_options=(
  425. parsed_req.options.get("hashes", {}) if parsed_req.options else {}
  426. ),
  427. constraint=parsed_req.constraint,
  428. line_source=parsed_req.line_source,
  429. user_supplied=user_supplied,
  430. config_settings=config_settings,
  431. )
  432. return req
  433. def install_req_from_link_and_ireq(
  434. link: Link, ireq: InstallRequirement
  435. ) -> InstallRequirement:
  436. return InstallRequirement(
  437. req=ireq.req,
  438. comes_from=ireq.comes_from,
  439. editable=ireq.editable,
  440. link=link,
  441. markers=ireq.markers,
  442. isolated=ireq.isolated,
  443. hash_options=ireq.hash_options,
  444. config_settings=ireq.config_settings,
  445. user_supplied=ireq.user_supplied,
  446. )
  447. def install_req_drop_extras(ireq: InstallRequirement) -> InstallRequirement:
  448. """
  449. Creates a new InstallationRequirement using the given template but without
  450. any extras. Sets the original requirement as the new one's parent
  451. (comes_from).
  452. """
  453. return InstallRequirement(
  454. req=(
  455. _set_requirement_extras(ireq.req, set()) if ireq.req is not None else None
  456. ),
  457. comes_from=ireq,
  458. editable=ireq.editable,
  459. link=ireq.link,
  460. markers=ireq.markers,
  461. isolated=ireq.isolated,
  462. hash_options=ireq.hash_options,
  463. constraint=ireq.constraint,
  464. extras=[],
  465. config_settings=ireq.config_settings,
  466. user_supplied=ireq.user_supplied,
  467. permit_editable_wheels=ireq.permit_editable_wheels,
  468. )
  469. def install_req_extend_extras(
  470. ireq: InstallRequirement,
  471. extras: Collection[str],
  472. ) -> InstallRequirement:
  473. """
  474. Returns a copy of an installation requirement with some additional extras.
  475. Makes a shallow copy of the ireq object.
  476. """
  477. result = copy.copy(ireq)
  478. result.extras = {*ireq.extras, *extras}
  479. result.req = (
  480. _set_requirement_extras(ireq.req, result.extras)
  481. if ireq.req is not None
  482. else None
  483. )
  484. return result