Skip to content

Commit

Permalink
WIP: Experimental globbing a pathlib.Path object with glob strings in it
Browse files Browse the repository at this point in the history
  • Loading branch information
MHendricks committed Nov 15, 2024
1 parent e949bc2 commit 19181df
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
3 changes: 2 additions & 1 deletion hab/distro_finders/distro_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pathlib
import shutil

from .. import utils
from ..parsers.distro_version import DistroVersion

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -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):
Expand Down
13 changes: 13 additions & 0 deletions hab/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
4 changes: 2 additions & 2 deletions tests/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 19181df

Please sign in to comment.