# SPDX-License-Identifier: MIT """Add the SPDX MIT header to every tracked source file that lacks one (one-off).""" import subprocess from pathlib import Path ROOT = Path(__file__).resolve().parents[1] COMMENT = {".py": "#", ".s": ";", ".cfg": "#", ".toml": "#"} TAG = "SPDX-License-Identifier: MIT" files = subprocess.run(["git", "ls-files"], cwd=ROOT, capture_output=True, text=True, check=True).stdout.split() changed = 0 for name in files: path = ROOT / name mark = COMMENT.get(path.suffix) if mark is None: continue text = path.read_text(encoding="utf-8") if TAG in text.split("\n", 3)[0:3].__str__(): continue line = f"{mark} {TAG}\n" if text.startswith("#!"): first, _, rest = text.partition("\n") text = f"{first}\n{line}{rest}" else: text = line + text path.write_text(text, encoding="utf-8") changed += 1 print(name) print(f"{changed} files")