Files
lucisv/tools/stamp_author.py
T
Guoliang ChenandClaude Mythos 5.2 71d13b5fbe
ci / checks (push) Successful in 1m0s
ci / corpus (push) Successful in 18s
ci / linux (push) Successful in 6m4s
release / build-and-publish (push) Successful in 5m31s
Release 0.66.2: every source file names its author
Every tracked file that can carry a comment (880 of them) now opens with
"Author: Guoliang Chen, a Senior Digital Design Engineer" and closes with
"Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love,
luciochen.com", in the file's own comment syntax. No behaviour change: the
diff outside the version bump and changelogs is comment lines only.

- tools/stamp_author.py: writes the lines; idempotent, `--check` lists
  what is missing. Headers go under a shebang and under Markdown front
  matter.
- Left alone: test fixtures, cases and snapshots (byte-exact inputs), JSON
  and JSONL, logs, images, lockfiles, docs/site/dist (regenerated) and the
  LICENSE files (already carry the copyright line).
- tools/linux_check.sh: --help reads its header from line 3, now that the
  author line sits under the shebang.
- tools/gates.sh green on this tree: 5 545 tests, corpus 814/6 as before.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-24 12:11:09 -08:00

114 lines
3.9 KiB
Python
Executable File

#!/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