From 3e206f4e1858fd4093124c8780f5021574b51d9d Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 12 Sep 2019 10:29:11 +0100 Subject: [3.8] bpo-38121: Sync importlib.metadata with 0.22 backport (GH-15993) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * bpo-38121: Sync importlib.metadata with 0.22 backport * 📜🤖 Added by blurb_it.. (cherry picked from commit 8ed6503eca4e3ea4949479d8d7fd9ffd54f81038) Co-authored-by: Jason R. Coombs --- Lib/importlib/_bootstrap_external.py | 52 +++--------------------------------- 1 file changed, 3 insertions(+), 49 deletions(-) (limited to 'Lib/importlib/_bootstrap_external.py') diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index ba78c57eeb..b8ac482994 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -1369,7 +1369,7 @@ class PathFinder: return spec.loader @classmethod - def find_distributions(self, context=None): + def find_distributions(cls, *args, **kwargs): """ Find distributions. @@ -1378,54 +1378,8 @@ class PathFinder: (or all names if ``None`` indicated) along the paths in the list of directories ``context.path``. """ - from importlib.metadata import PathDistribution, DistributionFinder - if context is None: - context = DistributionFinder.Context() - found = self._search_paths(context.pattern, context.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 - import pathlib - PYPY_OPEN_BUG = False - if not PYPY_OPEN_BUG or os.path.isfile(path): # pragma: no branch - with suppress(Exception): - return zipfile.Path(path) - return pathlib.Path(path) - - @classmethod - def _matches_info(cls, normalized, item): - import re - template = r'{pattern}(-.*)?\.(dist|egg)-info' - manifest = template.format(pattern=normalized) - return re.match(manifest, item.name, flags=re.IGNORECASE) - - @classmethod - def _matches_legacy(cls, normalized, item): - import re - template = r'{pattern}-.*\.egg[\\/]EGG-INFO' - manifest = template.format(pattern=normalized) - return re.search(manifest, str(item), flags=re.IGNORECASE) - - @classmethod - def _search_path(cls, root, pattern): - if not root.is_dir(): - return () - normalized = pattern.replace('-', '_') - return (item for item in root.iterdir() - if cls._matches_info(normalized, item) - or cls._matches_legacy(normalized, item)) + from importlib.metadata import MetadataPathFinder + return MetadataPathFinder.find_distributions(*args, **kwargs) class FileFinder: -- cgit v1.2.1