constructors.py 15 KB

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