req_set.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import logging
  2. from collections import OrderedDict
  3. from typing import Dict, Iterable, List, Optional, Tuple
  4. from pip._vendor.packaging.utils import canonicalize_name
  5. from pip._internal.exceptions import InstallationError
  6. from pip._internal.models.wheel import Wheel
  7. from pip._internal.req.req_install import InstallRequirement
  8. from pip._internal.utils import compatibility_tags
  9. logger = logging.getLogger(__name__)
  10. class RequirementSet:
  11. def __init__(self, check_supported_wheels=True):
  12. # type: (bool) -> None
  13. """Create a RequirementSet.
  14. """
  15. self.requirements = OrderedDict() # type: Dict[str, InstallRequirement]
  16. self.check_supported_wheels = check_supported_wheels
  17. self.unnamed_requirements = [] # type: List[InstallRequirement]
  18. def __str__(self):
  19. # type: () -> str
  20. requirements = sorted(
  21. (req for req in self.requirements.values() if not req.comes_from),
  22. key=lambda req: canonicalize_name(req.name or ""),
  23. )
  24. return ' '.join(str(req.req) for req in requirements)
  25. def __repr__(self):
  26. # type: () -> str
  27. requirements = sorted(
  28. self.requirements.values(),
  29. key=lambda req: canonicalize_name(req.name or ""),
  30. )
  31. format_string = '<{classname} object; {count} requirement(s): {reqs}>'
  32. return format_string.format(
  33. classname=self.__class__.__name__,
  34. count=len(requirements),
  35. reqs=', '.join(str(req.req) for req in requirements),
  36. )
  37. def add_unnamed_requirement(self, install_req):
  38. # type: (InstallRequirement) -> None
  39. assert not install_req.name
  40. self.unnamed_requirements.append(install_req)
  41. def add_named_requirement(self, install_req):
  42. # type: (InstallRequirement) -> None
  43. assert install_req.name
  44. project_name = canonicalize_name(install_req.name)
  45. self.requirements[project_name] = install_req
  46. def add_requirement(
  47. self,
  48. install_req, # type: InstallRequirement
  49. parent_req_name=None, # type: Optional[str]
  50. extras_requested=None # type: Optional[Iterable[str]]
  51. ):
  52. # type: (...) -> Tuple[List[InstallRequirement], Optional[InstallRequirement]]
  53. """Add install_req as a requirement to install.
  54. :param parent_req_name: The name of the requirement that needed this
  55. added. The name is used because when multiple unnamed requirements
  56. resolve to the same name, we could otherwise end up with dependency
  57. links that point outside the Requirements set. parent_req must
  58. already be added. Note that None implies that this is a user
  59. supplied requirement, vs an inferred one.
  60. :param extras_requested: an iterable of extras used to evaluate the
  61. environment markers.
  62. :return: Additional requirements to scan. That is either [] if
  63. the requirement is not applicable, or [install_req] if the
  64. requirement is applicable and has just been added.
  65. """
  66. # If the markers do not match, ignore this requirement.
  67. if not install_req.match_markers(extras_requested):
  68. logger.info(
  69. "Ignoring %s: markers '%s' don't match your environment",
  70. install_req.name, install_req.markers,
  71. )
  72. return [], None
  73. # If the wheel is not supported, raise an error.
  74. # Should check this after filtering out based on environment markers to
  75. # allow specifying different wheels based on the environment/OS, in a
  76. # single requirements file.
  77. if install_req.link and install_req.link.is_wheel:
  78. wheel = Wheel(install_req.link.filename)
  79. tags = compatibility_tags.get_supported()
  80. if (self.check_supported_wheels and not wheel.supported(tags)):
  81. raise InstallationError(
  82. "{} is not a supported wheel on this platform.".format(
  83. wheel.filename)
  84. )
  85. # This next bit is really a sanity check.
  86. assert not install_req.user_supplied or parent_req_name is None, (
  87. "a user supplied req shouldn't have a parent"
  88. )
  89. # Unnamed requirements are scanned again and the requirement won't be
  90. # added as a dependency until after scanning.
  91. if not install_req.name:
  92. self.add_unnamed_requirement(install_req)
  93. return [install_req], None
  94. try:
  95. existing_req = self.get_requirement(
  96. install_req.name) # type: Optional[InstallRequirement]
  97. except KeyError:
  98. existing_req = None
  99. has_conflicting_requirement = (
  100. parent_req_name is None and
  101. existing_req and
  102. not existing_req.constraint and
  103. existing_req.extras == install_req.extras and
  104. existing_req.req and
  105. install_req.req and
  106. existing_req.req.specifier != install_req.req.specifier
  107. )
  108. if has_conflicting_requirement:
  109. raise InstallationError(
  110. "Double requirement given: {} (already in {}, name={!r})"
  111. .format(install_req, existing_req, install_req.name)
  112. )
  113. # When no existing requirement exists, add the requirement as a
  114. # dependency and it will be scanned again after.
  115. if not existing_req:
  116. self.add_named_requirement(install_req)
  117. # We'd want to rescan this requirement later
  118. return [install_req], install_req
  119. # Assume there's no need to scan, and that we've already
  120. # encountered this for scanning.
  121. if install_req.constraint or not existing_req.constraint:
  122. return [], existing_req
  123. does_not_satisfy_constraint = (
  124. install_req.link and
  125. not (
  126. existing_req.link and
  127. install_req.link.path == existing_req.link.path
  128. )
  129. )
  130. if does_not_satisfy_constraint:
  131. raise InstallationError(
  132. "Could not satisfy constraints for '{}': "
  133. "installation from path or url cannot be "
  134. "constrained to a version".format(install_req.name)
  135. )
  136. # If we're now installing a constraint, mark the existing
  137. # object for real installation.
  138. existing_req.constraint = False
  139. # If we're now installing a user supplied requirement,
  140. # mark the existing object as such.
  141. if install_req.user_supplied:
  142. existing_req.user_supplied = True
  143. existing_req.extras = tuple(sorted(
  144. set(existing_req.extras) | set(install_req.extras)
  145. ))
  146. logger.debug(
  147. "Setting %s extras to: %s",
  148. existing_req, existing_req.extras,
  149. )
  150. # Return the existing requirement for addition to the parent and
  151. # scanning again.
  152. return [existing_req], existing_req
  153. def has_requirement(self, name):
  154. # type: (str) -> bool
  155. project_name = canonicalize_name(name)
  156. return (
  157. project_name in self.requirements and
  158. not self.requirements[project_name].constraint
  159. )
  160. def get_requirement(self, name):
  161. # type: (str) -> InstallRequirement
  162. project_name = canonicalize_name(name)
  163. if project_name in self.requirements:
  164. return self.requirements[project_name]
  165. raise KeyError(f"No project with the name {name!r}")
  166. @property
  167. def all_requirements(self):
  168. # type: () -> List[InstallRequirement]
  169. return self.unnamed_requirements + list(self.requirements.values())