"""Fail if any committed (or about-to-be-committed) file contains identifying information. The identifiers to block are never written into the repo: they are derived at runtime from the same environment variables the pipeline reads (dataset / model ids and the pinned revision), plus optional extra terms in PRIVACY_EXTRA_TERMS (comma-separated). Independently of the environment, it also blocks literal email addresses and links to model/code hosting profiles. python scripts/privacy_check.py # tracked + untracked-not-ignored files python scripts/privacy_check.py --staged # exactly what `git commit` would record (pre-commit hook) """ import os import re import subprocess import sys ID_VARS = ["DATASET_MINIMAL_PAIRS", "DATASET_TERSE", "DATASET_INDIRECT", "BASE_MODEL", "BASE_MODEL_REVISION"] EMAIL = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}") HOSTING = re.compile(r"(?= MIN_PART] def forbidden_terms(): terms = set() for var in ID_VARS: v = os.environ.get(var, "").strip() if not v: continue terms.add(v) if "/" in v: owner, name = v.split("/", 1) terms.update([owner, name]) terms.update(split_parts(owner)) for t in os.environ.get("PRIVACY_EXTRA_TERMS", "").split(","): if t.strip(): terms.add(t.strip()) return sorted(terms, key=len, reverse=True) def git(*args): return subprocess.run(["git", *args], capture_output=True, text=True, check=True).stdout def files_to_scan(staged): if staged: names = git("diff", "--cached", "--name-only", "--diff-filter=ACMR").split("\n") return [(n, lambda n=n: git("show", f":{n}")) for n in names if n] names = set(git("ls-files").split("\n")) | set(git("ls-files", "--others", "--exclude-standard").split("\n")) out = [] for n in sorted(x for x in names if x and os.path.isfile(x)): out.append((n, lambda n=n: open(n, encoding="utf-8", errors="replace").read())) return out def main(): staged = "--staged" in sys.argv terms = forbidden_terms() missing = [v for v in ID_VARS if not os.environ.get(v)] if missing: print(f"privacy_check: WARNING env vars not set, their identifiers are not checked: {missing}", file=sys.stderr) pats = [(re.compile(re.escape(t), re.I), "identifier") for t in terms] + [(EMAIL, "email"), (HOSTING, "profile link")] hits = 0 for name, read in files_to_scan(staged): try: text = read() except (subprocess.CalledProcessError, UnicodeDecodeError): continue for ln, line in enumerate(text.split("\n"), 1): for pat, kind in pats: if pat.search(line): hits += 1 print(f"privacy_check: {kind} in {name}:{ln}", file=sys.stderr) if hits: print(f"privacy_check: FAILED ({hits} hits)", file=sys.stderr) sys.exit(1) print(f"privacy_check: ok ({len(terms)} runtime identifiers + email/profile-link patterns)") if __name__ == "__main__": main()