mirror of
https://github.com/python/cpython.git
synced 2025-08-20 16:53:19 +00:00
Inline the find_distributions functionality on PathFinder.
This commit is contained in:
parent
58533a71b2
commit
3b19b9d823
4 changed files with 822 additions and 693 deletions
|
@ -1359,6 +1359,57 @@ class PathFinder:
|
|||
return None
|
||||
return spec.loader
|
||||
|
||||
search_template = r'{pattern}(-.*)?\.(dist|egg)-info'
|
||||
|
||||
@classmethod
|
||||
def find_distributions(cls, name=None, path=None):
|
||||
"""Return an iterable of all Distribution instances capable of
|
||||
loading the metadata for packages matching the name
|
||||
(or all names if not supplied) along the paths in the list
|
||||
of directories ``path`` (defaults to sys.path).
|
||||
"""
|
||||
import re
|
||||
from importlib.metadata._hooks import PathDistribution
|
||||
if path is None:
|
||||
path = sys.path
|
||||
pattern = '.*' if name is None else re.escape(name)
|
||||
found = cls._search_paths(pattern, path)
|
||||
return map(PathDistribution, found)
|
||||
|
||||
@classmethod
|
||||
def _search_paths(cls, pattern, paths):
|
||||
"""
|
||||
Find metadata directories in paths heuristically.
|
||||
"""
|
||||
import itertools
|
||||
return itertools.chain.from_iterable(
|
||||
cls._search_path(path, pattern)
|
||||
for path in map(cls._switch_path, paths)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _switch_path(path):
|
||||
from contextlib import suppress
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
with suppress(Exception):
|
||||
return zipfile.Path(path)
|
||||
return Path(path)
|
||||
|
||||
@classmethod
|
||||
def _predicate(cls, pattern, root, item):
|
||||
import re
|
||||
return re.match(pattern, str(item.name), flags=re.IGNORECASE)
|
||||
|
||||
@classmethod
|
||||
def _search_path(cls, root, pattern):
|
||||
if not root.is_dir():
|
||||
return ()
|
||||
normalized = pattern.replace('-', '_')
|
||||
matcher = cls.search_template.format(pattern=normalized)
|
||||
return (item for item in root.iterdir()
|
||||
if cls._predicate(matcher, root, item))
|
||||
|
||||
|
||||
class FileFinder:
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue