pkg_resources.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import zipfile
  2. from typing import Iterator, List, Optional
  3. from pip._vendor import pkg_resources
  4. from pip._vendor.packaging.utils import canonicalize_name
  5. from pip._vendor.packaging.version import parse as parse_version
  6. from pip._internal.utils import misc # TODO: Move definition here.
  7. from pip._internal.utils.packaging import get_installer
  8. from pip._internal.utils.wheel import pkg_resources_distribution_for_wheel
  9. from .base import BaseDistribution, BaseEnvironment, DistributionVersion
  10. class Distribution(BaseDistribution):
  11. def __init__(self, dist):
  12. # type: (pkg_resources.Distribution) -> None
  13. self._dist = dist
  14. @classmethod
  15. def from_wheel(cls, path, name):
  16. # type: (str, str) -> Distribution
  17. with zipfile.ZipFile(path, allowZip64=True) as zf:
  18. dist = pkg_resources_distribution_for_wheel(zf, name, path)
  19. return cls(dist)
  20. @property
  21. def location(self):
  22. # type: () -> Optional[str]
  23. return self._dist.location
  24. @property
  25. def metadata_version(self):
  26. # type: () -> Optional[str]
  27. for line in self._dist.get_metadata_lines(self._dist.PKG_INFO):
  28. if line.lower().startswith("metadata-version:"):
  29. return line.split(":", 1)[-1].strip()
  30. return None
  31. @property
  32. def canonical_name(self):
  33. # type: () -> str
  34. return canonicalize_name(self._dist.project_name)
  35. @property
  36. def version(self):
  37. # type: () -> DistributionVersion
  38. return parse_version(self._dist.version)
  39. @property
  40. def installer(self):
  41. # type: () -> str
  42. return get_installer(self._dist)
  43. @property
  44. def editable(self):
  45. # type: () -> bool
  46. return misc.dist_is_editable(self._dist)
  47. @property
  48. def local(self):
  49. # type: () -> bool
  50. return misc.dist_is_local(self._dist)
  51. @property
  52. def in_usersite(self):
  53. # type: () -> bool
  54. return misc.dist_in_usersite(self._dist)
  55. class Environment(BaseEnvironment):
  56. def __init__(self, ws):
  57. # type: (pkg_resources.WorkingSet) -> None
  58. self._ws = ws
  59. @classmethod
  60. def default(cls):
  61. # type: () -> BaseEnvironment
  62. return cls(pkg_resources.working_set)
  63. @classmethod
  64. def from_paths(cls, paths):
  65. # type: (Optional[List[str]]) -> BaseEnvironment
  66. return cls(pkg_resources.WorkingSet(paths))
  67. def _search_distribution(self, name):
  68. # type: (str) -> Optional[BaseDistribution]
  69. """Find a distribution matching the ``name`` in the environment.
  70. This searches from *all* distributions available in the environment, to
  71. match the behavior of ``pkg_resources.get_distribution()``.
  72. """
  73. canonical_name = canonicalize_name(name)
  74. for dist in self.iter_distributions():
  75. if dist.canonical_name == canonical_name:
  76. return dist
  77. return None
  78. def get_distribution(self, name):
  79. # type: (str) -> Optional[BaseDistribution]
  80. # Search the distribution by looking through the working set.
  81. dist = self._search_distribution(name)
  82. if dist:
  83. return dist
  84. # If distribution could not be found, call working_set.require to
  85. # update the working set, and try to find the distribution again.
  86. # This might happen for e.g. when you install a package twice, once
  87. # using setup.py develop and again using setup.py install. Now when
  88. # running pip uninstall twice, the package gets removed from the
  89. # working set in the first uninstall, so we have to populate the
  90. # working set again so that pip knows about it and the packages gets
  91. # picked up and is successfully uninstalled the second time too.
  92. try:
  93. # We didn't pass in any version specifiers, so this can never
  94. # raise pkg_resources.VersionConflict.
  95. self._ws.require(name)
  96. except pkg_resources.DistributionNotFound:
  97. return None
  98. return self._search_distribution(name)
  99. def _iter_distributions(self):
  100. # type: () -> Iterator[BaseDistribution]
  101. for dist in self._ws:
  102. yield Distribution(dist)