From 5c63675ef5f1076bb8f0868e5428a3c0c4638b26 Mon Sep 17 00:00:00 2001 From: Marco D'Aleo Date: Sat, 23 May 2026 10:28:53 +0100 Subject: [PATCH] Record the root path in the state for restoring, make pre-commit black use generic python3 version --- .pre-commit-config.yaml | 2 +- chguard/scan.py | 58 ++++++++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4f097ba..ec9a59d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,7 +10,7 @@ repos: rev: 26.3.1 hooks: - id: black - language_version: python3.13 + language_version: python3 - repo: https://github.com/pre-commit/pre-commit-hooks rev: v6.0.0 diff --git a/chguard/scan.py b/chguard/scan.py index 7312816..ce256d2 100644 --- a/chguard/scan.py +++ b/chguard/scan.py @@ -27,9 +27,43 @@ def _is_excluded(rel: str, excludes: Iterable[str]) -> bool: return False +def _entry_for_path(p: Path, root: Path) -> Entry | None: + try: + st = p.lstat() # never follow symlinks + except FileNotFoundError: + return None + + if stat.S_ISDIR(st.st_mode): + typ = "dir" + elif stat.S_ISREG(st.st_mode): + typ = "file" + elif stat.S_ISLNK(st.st_mode): + typ = "symlink" + else: + # skip special files (devices, sockets, fifos) in v0.1 + return None + + rel = "" if p == root else str(p.relative_to(root)) + + return Entry( + path=rel, + type=typ, + mode=stat.S_IMODE(st.st_mode), + uid=st.st_uid, + gid=st.st_gid, + ) + + def scan_tree(root: Path, excludes: Iterable[str] = ()) -> Iterator[Entry]: root = root.resolve() + root_entry = _entry_for_path(root, root) + if root_entry is not None: + yield root_entry + + if not root.is_dir(): + return + for dirpath, dirnames, filenames in os.walk(root, followlinks=False): # prune excluded directories early rel_dir = ( @@ -56,24 +90,6 @@ def scan_tree(root: Path, excludes: Iterable[str] = ()) -> Iterator[Entry]: # record dirs and files for name in list(dirnames) + list(files): p = Path(dirpath) / name - try: - st = p.lstat() # never follow symlinks - except FileNotFoundError: - continue - - rel = str(p.relative_to(root)) - - if stat.S_ISDIR(st.st_mode): - typ = "dir" - elif stat.S_ISREG(st.st_mode): - typ = "file" - elif stat.S_ISLNK(st.st_mode): - typ = "symlink" - else: - # skip special files (devices, sockets, fifos) in v0.1 - continue - - mode = stat.S_IMODE(st.st_mode) - yield Entry( - path=rel, type=typ, mode=mode, uid=st.st_uid, gid=st.st_gid - ) + entry = _entry_for_path(p, root) + if entry is not None: + yield entry