#!/usr/bin/env python3 # Author: Guoliang Chen, a Senior Digital Design Engineer """Stamp the author's name at the top and bottom of every tracked file that can carry a comment. Idempotent: a file that already has a stamp line is left alone, so this is safe to re-run after adding files. `--check` lists what would change and exits 1 if anything would. Left untouched, because a comment would break them or be thrown away: test fixtures / cases / snapshots (byte-exact test inputs), JSON and JSONL (no comment syntax), logs and data files, images, lockfiles (regenerated), the built site under docs/site/dist (regenerated), and the LICENSE files (which already carry the copyright line). """ import subprocess import sys from pathlib import Path NAME = "Guoliang Chen" HEADER = "Author: " + NAME + ", a Senior Digital Design Engineer" FOOTER = "Author: " + NAME + ". Lucisv is made by " + NAME + " with love, luciochen.com" # Comment wrapper per file kind: (open, close). LINE_SLASH = ("// ", "") LINE_HASH = ("# ", "") STYLES = { ".rs": LINE_SLASH, ".js": LINE_SLASH, ".mjs": LINE_SLASH, ".ts": LINE_SLASH, ".sh": LINE_HASH, ".py": LINE_HASH, ".toml": LINE_HASH, ".yml": LINE_HASH, ".css": ("/* ", " */"), ".md": (""), ".svg": (""), ".mdx": ("{/* ", " */}"), } NAMES = {".gitignore": LINE_HASH, ".vscodeignore": LINE_HASH, "robots.txt": LINE_HASH} SKIP_PREFIXES = ("docs/site/dist/",) SKIP_PARTS = ("/tests/fixtures/", "/tests/cases/", "/tests/snapshots/") def style_of(path: str): """The comment wrapper for `path`, or None when the file is left alone.""" if path.startswith(SKIP_PREFIXES) or any(part in path for part in SKIP_PARTS): return None name = Path(path).name return NAMES.get(name) or STYLES.get(Path(path).suffix) def header_offset(text: str, path: str) -> int: """Where the header goes: after a shebang line or a front-matter block.""" if text.startswith("#!"): return text.find("\n") + 1 if path.endswith((".md", ".mdx")) and text.startswith("---\n"): end = text.find("\n---\n", 4) if end != -1: return end + len("\n---\n") return 0 def stamped(text: str, path: str, style) -> str: """`text` with the header and footer lines added where missing.""" opener, closer = style head = opener + HEADER + closer foot = opener + FOOTER + closer lines = text.split("\n") if head not in lines: at = header_offset(text, path) # A shebang is followed directly; anything else gets a blank line after. gap = "\n" if text.startswith("#!") else "\n\n" lead = "\n" if at and not text.startswith("#!") else "" text = text[:at] + lead + head + gap + text[at:].lstrip("\n") if foot not in lines: text = text.rstrip("\n") + "\n\n" + foot + "\n" return text def main() -> int: check = "--check" in sys.argv[1:] root = Path(__file__).resolve().parent.parent tracked = subprocess.run( ["git", "ls-files", "-z", "--cached", "--others", "--exclude-standard"], cwd=root, check=True, capture_output=True, text=True, ).stdout.split("\0") changed, skipped = [], [] for path in filter(None, tracked): style = style_of(path) file = root / path if style is None or not file.is_file(): skipped.append(path) continue old = file.read_text(encoding="utf-8") new = stamped(old, path, style) if new != old: changed.append(path) if not check: file.write_text(new, encoding="utf-8") verb = "would stamp" if check else "stamped" print(f"{verb} {len(changed)} files; left {len(skipped)} untouched") if check: print("\n".join(changed)) return 1 if check and changed else 0 if __name__ == "__main__": sys.exit(main()) # Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com