1from __future__ import annotations
2
3import importlib.metadata
4import logging
5import os
6import pathlib
7import sys
8import zipfile
9from collections.abc import Iterator, Sequence
10
11from pip._vendor.packaging.utils import (
12 InvalidWheelFilename,
13 NormalizedName,
14 canonicalize_name,
15 parse_wheel_filename,
16)
17
18from pip._internal.metadata.base import BaseDistribution, BaseEnvironment
19from pip._internal.utils.filetypes import WHEEL_EXTENSION
20
21from ._compat import BadMetadata, BasePath, get_dist_canonical_name, get_info_location
22from ._dists import Distribution
23
24logger = logging.getLogger(__name__)
25
26
27def _looks_like_wheel(location: str) -> bool:
28 if not location.endswith(WHEEL_EXTENSION):
29 return False
30 if not os.path.isfile(location):
31 return False
32 try:
33 parse_wheel_filename(os.path.basename(location))
34 except InvalidWheelFilename:
35 return False
36 return zipfile.is_zipfile(location)
37
38
39class _DistributionFinder:
40 """Finder to locate distributions.
41
42 The main purpose of this class is to memoize found distributions' names, so
43 only one distribution is returned for each package name. At lot of pip code
44 assumes this (because it is setuptools's behavior), and not doing the same
45 can potentially cause a distribution in lower precedence path to override a
46 higher precedence one if the caller is not careful.
47
48 Eventually we probably want to make it possible to see lower precedence
49 installations as well. It's useful feature, after all.
50 """
51
52 FoundResult = tuple[importlib.metadata.Distribution, BasePath | None]
53
54 def __init__(self) -> None:
55 self._found_names: set[NormalizedName] = set()
56
57 def _find_impl(self, location: str) -> Iterator[FoundResult]:
58 """Find distributions in a location."""
59 # Skip looking inside a wheel. Since a package inside a wheel is not
60 # always valid (due to .data directories etc.), its .dist-info entry
61 # should not be considered an installed distribution.
62 if _looks_like_wheel(location):
63 return
64 # To know exactly where we find a distribution, we have to feed in the
65 # paths one by one, instead of dumping the list to importlib.metadata.
66 for dist in importlib.metadata.distributions(path=[location]):
67 info_location = get_info_location(dist)
68 try:
69 name = get_dist_canonical_name(dist)
70 except BadMetadata as e:
71 logger.warning("Skipping %s due to %s", info_location, e.reason)
72 continue
73 if name in self._found_names:
74 continue
75 self._found_names.add(name)
76 yield dist, info_location
77
78 def find(self, location: str) -> Iterator[BaseDistribution]:
79 """Find distributions in a location.
80
81 The path can be either a directory, or a ZIP archive.
82 """
83 for dist, info_location in self._find_impl(location):
84 if info_location is None:
85 installed_location: BasePath | None = None
86 else:
87 installed_location = info_location.parent
88 yield Distribution(dist, info_location, installed_location)
89
90 def find_legacy_editables(self, location: str) -> Iterator[BaseDistribution]:
91 """Read location in egg-link files and return distributions in there.
92
93 The path should be a directory; otherwise this returns nothing. This
94 follows how setuptools does this for compatibility. The first non-empty
95 line in the egg-link is read as a path (resolved against the egg-link's
96 containing directory if relative). Distributions found at that linked
97 location are returned.
98 """
99 path = pathlib.Path(location)
100 if not path.is_dir():
101 return
102 for child in path.iterdir():
103 if child.suffix != ".egg-link":
104 continue
105 with child.open() as f:
106 lines = (line.strip() for line in f)
107 target_rel = next((line for line in lines if line), "")
108 if not target_rel:
109 continue
110 target_location = str(path.joinpath(target_rel))
111 for dist, info_location in self._find_impl(target_location):
112 yield Distribution(dist, info_location, path)
113
114
115class Environment(BaseEnvironment):
116 def __init__(self, paths: Sequence[str]) -> None:
117 self._paths = paths
118
119 @classmethod
120 def default(cls) -> BaseEnvironment:
121 return cls(sys.path)
122
123 @classmethod
124 def from_paths(cls, paths: list[str] | None) -> BaseEnvironment:
125 if paths is None:
126 return cls(sys.path)
127 return cls(paths)
128
129 def _iter_distributions(self) -> Iterator[BaseDistribution]:
130 finder = _DistributionFinder()
131 for location in self._paths:
132 yield from finder.find(location)
133 yield from finder.find_legacy_editables(location)
134
135 def get_distribution(self, name: str) -> BaseDistribution | None:
136 canonical_name = canonicalize_name(name)
137 matches = (
138 distribution
139 for distribution in self.iter_all_distributions()
140 if distribution.canonical_name == canonical_name
141 )
142 return next(matches, None)