From 19181df785a34fa39e7e6f20f2a383275af86501 Mon Sep 17 00:00:00 2001 From: Mike Hendricks Date: Wed, 6 Nov 2024 17:17:44 -0800 Subject: [PATCH] WIP: Experimental globbing a pathlib.Path object with glob strings in it --- hab/distro_finders/distro_finder.py | 3 ++- hab/utils.py | 13 +++++++++++++ tests/test_site.py | 4 ++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/hab/distro_finders/distro_finder.py b/hab/distro_finders/distro_finder.py index cc27da9..db2c4c4 100644 --- a/hab/distro_finders/distro_finder.py +++ b/hab/distro_finders/distro_finder.py @@ -2,6 +2,7 @@ import pathlib import shutil +from .. import utils from ..parsers.distro_version import DistroVersion logger = logging.getLogger(__name__) @@ -46,7 +47,7 @@ def distro(self, forest, resolver, path): def distro_files(self): """Generator returning each distro found by this distro finder.""" - for path in self.root.glob(self.glob_str): + for path in utils.glob_path(self.root / self.glob_str): yield pathlib.Path(path) def install(self, path, dest): diff --git a/hab/utils.py b/hab/utils.py index 5c1d690..33d0356 100644 --- a/hab/utils.py +++ b/hab/utils.py @@ -261,6 +261,19 @@ def encode_freeze(data, version=None, site=None): return f'v{version}:{data.decode("utf-8")}' +def glob_path(path): + """Process any wildcards in the provided `pathlib.Path` like instance. + + This runs the glob from the root of path. + + Based on https://stackoverflow.com/a/51108375 + """ + # Strip the path into its parts removing the root of absolute paths. + parts = path.parts[1:] + # From the root run a glob search on all parts of the glob string + return Path(path.parts[0]).glob(str(Path(*parts))) + + class HabJsonEncoder(_json.JSONEncoder): """JsonEncoder class that handles non-supported objects like hab.NotSet.""" diff --git a/tests/test_site.py b/tests/test_site.py index 1cdfa13..2b85a23 100644 --- a/tests/test_site.py +++ b/tests/test_site.py @@ -290,8 +290,8 @@ def test_dump_cached(config_root, habcached_site_file): f" {other_site}", f"{{green}}config_paths: {{reset}}config\\path\\{platform}", f" {config_root}\\configs\\*{{cached}}", - f"{{green}}distro_paths: {{reset}}DistroFinder: distro\\path\\{platform}", - f" DistroFinder: {config_root}\\distros\\*{{cached}}", + f"{{green}}distro_paths: {{reset}}distro\\path\\{platform} [DistroFinder]", + f" {config_root}\\distros\\*{{cached}} [DistroFinder]", ) check_template = "\n".join(check_template) colors = {