#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Sync a script directory to a publish location, and audit it for machine-specific leftovers. WHY THIS EXISTS --------------- An Agent Skill gets copied across machines and across runtimes (Claude Code, Codex, DeepSeek Harness). Anything machine-specific that survives the copy becomes a landmine: a baked-in JDK path, a keystore password, a home directory that only exists on the author's box. This tool does two things: 1. AUDIT (default, read-only): report every absolute-path literal and every credential-looking constant, with file and line number. 2. SYNC (--apply): copy matching files to the destination, optionally after neutralising the absolute-path literals it found (--scrub). The audit is the default on purpose: a silent copy is exactly how the leftovers travel from the author's machine into someone else's first run. USAGE ----- python build_scripts.py scripts2 out # audit only, nothing written python build_scripts.py scripts out --apply # copy python build_scripts.py scripts out --apply --scrub # copy, neutralising abs paths python build_scripts.py scripts out --pattern '*.py' --pattern '*.js' Exit codes: 0 = clean, 2 = findings reported (audit mode), 1 = usage or IO error. NOTES ----- * A finding is a heuristic, not a verdict: `/data/local/tmp/...` is a legitimate Android device path and is deliberately NOT reported; a drive-letter path is. * --scrub rewrites `''` literals into `''`, which keeps the file syntactically valid but leaves the decision (and the fix) to a human. It never runs unless --apply is also given. """ import argparse import fnmatch import os import re import shutil import sys DEFAULT_PATTERNS = ('*.py', '*.js', '*.java', '*.md', '*.txt') # Absolute-path literals that belong to one particular machine. # Deliberately excluded: /data/... (an Android device path, not a host path). WINDOWS_LITERAL = re.compile(r"(?P(?:r|u|b)?r?)(?P['\"])" r"(?P[A-Za-z]:[\\/][^'\"]*)(?P=quote)") POSIX_LITERAL = re.compile(r"(?P(?:r|u|b)?r?)(?P['\"])" r"(?P/(?:home|Users|mnt)/[^'\"]*)(?P=quote)") # Any drive-letter or foreign-home path outside a quoted literal as well. # Tuned to avoid the two false-positive families that made an earlier version of # this audit useless: a URL scheme ("https://" contains "s:" + "/") and a Python # escape sequence ("is:\n" contains "s:" + "\"). Hence: the previous character must # not be alphanumeric, a drive letter must be followed by a real backslash (not an # escape letter, not a second separator), and the POSIX form must start a path. BARE_ABS = re.compile( r"(? copied with %d absolute-path literal(s) neutralised' % count) copied += 1 continue shutil.copy2(full, target) copied += 1 print('') if not args.apply: print('== audit: %d file(s) checked, %d finding(s). Nothing was written.' % (len(files), total_findings)) print(' re-run with --apply to copy, and with --scrub to neutralise ' 'absolute-path literals.') return 2 if total_findings else 0 print('== sync: %d file(s) copied to %s (%d literal(s) scrubbed), %d finding(s) reported.' % (copied, args.dst, scrubbed, total_findings)) return 0 if __name__ == '__main__': sys.exit(main())