omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tools / uxl / windows_paths.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Fail if a tracked path cannot be checked out on Windows. |
| 3 | |
| 4 | Python 3.8+ stdlib. Reserved device names (CON, PRN, AUX, NUL, COM1-9, |
| 5 | LPT1-9, any extension), the characters <>:"|?*, ASCII controls, a trailing |
| 6 | dot or space, and paths that collide under case-folding are findings. |
| 7 | evidence/ is reported the same way and is not rewritten. |
| 8 | """ |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import subprocess |
| 12 | import sys |
| 13 | from collections import defaultdict |
| 14 | from pathlib import Path |
| 15 | |
| 16 | RESERVED = {"CON", "PRN", "AUX", "NUL"} |
| 17 | for _n in range(1, 10): |
| 18 | RESERVED.add("COM%d" % _n) |
| 19 | RESERVED.add("LPT%d" % _n) |
| 20 | |
| 21 | FORBIDDEN = set('<>:"|?*') |
| 22 | |
| 23 | |
| 24 | def tracked_paths(root): |
| 25 | try: |
| 26 | out = subprocess.run( |
| 27 | ["git", "ls-files", "-z"], |
| 28 | cwd=str(root), |
| 29 | stdout=subprocess.PIPE, |
| 30 | stderr=subprocess.PIPE, |
| 31 | check=True, |
| 32 | ) |
| 33 | except (OSError, subprocess.CalledProcessError): |
| 34 | paths = [] |
| 35 | for path in root.rglob("*"): |
| 36 | if ".git" in path.parts: |
| 37 | continue |
| 38 | if path.is_file() or path.is_dir(): |
| 39 | rel = path.relative_to(root).as_posix() |
| 40 | if rel and not rel.startswith(".git"): |
| 41 | paths.append(rel) |
| 42 | return paths |
| 43 | paths = [] |
| 44 | for raw in out.stdout.split(b"\0"): |
| 45 | if raw: |
| 46 | paths.append(raw.decode("utf-8", "surrogateescape")) |
| 47 | return paths |
| 48 | |
| 49 | |
| 50 | def component_findings(path): |
| 51 | found = [] |
| 52 | for part in path.split("/"): |
| 53 | if part in ("", ".", ".."): |
| 54 | continue |
| 55 | if part.endswith(" ") or part.endswith("."): |
| 56 | found.append("trailing-dot-or-space") |
| 57 | if any(ord(ch) < 32 for ch in part) or any(ch in FORBIDDEN for ch in part): |
| 58 | found.append("forbidden-character") |
| 59 | stem = part.split(".", 1)[0].upper() |
| 60 | if stem in RESERVED: |
| 61 | found.append("reserved-name") |
| 62 | return found |
| 63 | |
| 64 | |
| 65 | def scan(paths): |
| 66 | findings = [] |
| 67 | folded = defaultdict(list) |
| 68 | for path in paths: |
| 69 | reasons = component_findings(path) |
| 70 | for reason in sorted(set(reasons)): |
| 71 | findings.append((reason, path)) |
| 72 | folded[path.casefold()].append(path) |
| 73 | for group in folded.values(): |
| 74 | unique = sorted(set(group)) |
| 75 | if len(unique) > 1: |
| 76 | findings.append(("case-collision", " | ".join(unique))) |
| 77 | return findings |
| 78 | |
| 79 | |
| 80 | def main(argv): |
| 81 | root = Path(argv[1]).resolve() if len(argv) > 1 else Path(__file__).resolve().parents[2] |
| 82 | paths = tracked_paths(root) |
| 83 | findings = scan(paths) |
| 84 | if not findings: |
| 85 | print("windows-paths: 0 findings (%d paths)" % len(paths)) |
| 86 | return 0 |
| 87 | for reason, path in findings: |
| 88 | note = "" |
| 89 | if path.startswith("evidence/") or path.split(" | ")[0].startswith("evidence/"): |
| 90 | note = " evidence path left unchanged" |
| 91 | print("windows-paths: FINDING %s %s%s" % (reason, path, note)) |
| 92 | print("windows-paths: %d findings (%d paths)" % (len(findings), len(paths))) |
| 93 | return 1 |
| 94 | |
| 95 | |
| 96 | if __name__ == "__main__": |
| 97 | sys.exit(main(sys.argv)) |