From e587f319826bce03df1dc7233842f054172c1fd2 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 25 Nov 2022 10:55:16 -0500 Subject: [PATCH] Re-implement glob directly, adding compatibility for older Pythons and avoids platform-specific concerns. --- zipp/__init__.py | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/zipp/__init__.py b/zipp/__init__.py index 570d44d..1757e71 100644 --- a/zipp/__init__.py +++ b/zipp/__init__.py @@ -4,6 +4,8 @@ import itertools import contextlib import pathlib +import re +import fnmatch from .py310compat import text_encoding @@ -327,36 +329,25 @@ def match(self, path_pattern): def is_symlink(self): return False # See #82102 - @contextlib.contextmanager - def _scandir(self): # Needed for glob - yield self.iterdir() - - def _make_child_relpath(self, relpath): # Needed for glob - return self / relpath + def _descendants(self): + for child in self.iterdir(): + yield child + if child.is_dir(): + yield from child._descendants() def glob(self, pattern): if not pattern: raise ValueError("Unacceptable pattern: {!r}".format(pattern)) - path = pathlib.Path(self.at) - drv, root, pattern_parts = path._flavour.parse_parts((pattern,)) - if drv or root: - raise NotImplementedError("Non-relative patterns are unsupported") - if pattern[-1] in (path._flavour.sep, path._flavour.altsep): - pattern_parts.append('') - selector = pathlib._make_selector(tuple(pattern_parts), path._flavour) - for p in selector.select_from(self): - yield p + + matches = re.compile(fnmatch.translate(pattern)).fullmatch + return ( + child + for child in self._descendants() + if matches(str(child.relative_to(self))) + ) def rglob(self, pattern): - path = pathlib.Path(self.at) - drv, root, pattern_parts = path._flavour.parse_parts((pattern,)) - if drv or root: - raise NotImplementedError("Non-relative patterns are unsupported") - if pattern and pattern[-1] in (path._flavour.sep, path._flavour.altsep): - pattern_parts.append('') - selector = pathlib._make_selector(("**",) + tuple(pattern_parts), path._flavour) - for p in selector.select_from(self): - yield p + return self.glob(f'**/{pattern}') def relative_to(self, *other, **kwargs): other = (pathlib.Path(each.at) for each in other)