"""按固定清单统计手写维护量;非空行不冒充去注释后的代码行。""" import argparse import hashlib import json from pathlib import Path import subprocess def category(name): path = Path(name) if path.name == "link.md": return "generated-documentation" if path.suffix == ".md": return "documentation" if path.suffix in {".json", ".yaml", ".toml", ".lock", ".mod"} or path.name.startswith("."): return "configuration" if name.startswith("test/"): return "tests-and-benchmark-references" if name.startswith("rust/src/runtime/"): return "runtime" if name.startswith("rust/src/"): return "compiler-cli-process" if name.startswith("examples/next/"): return "new-syntax-design-examples" if name.startswith(("examples/", "skills/")): return "examples-and-skills" if name.startswith("lib/"): return "standard-library" if name.startswith("sdk/"): return "worker-sdk" if name.startswith("editor/"): return "editor" if name.startswith("build/"): return "build-and-release" return "other" def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--root", type=Path, required=True) parser.add_argument("--manifest", type=Path) parser.add_argument("--output", type=Path, required=True) args = parser.parse_args() root = args.root.resolve(strict=True) if args.manifest: names = json.loads(args.manifest.read_text())["files"] else: listed = subprocess.check_output(["git", "-C", str(root), "ls-files", "-z", "--cached", "--others", "--exclude-standard"]) names = set(listed.decode().split(chr(0))) - {""} files = [] groups = {} for name in sorted(names): path = root / name if not path.exists(): continue # Git 中已删除的文件没有当前维护行数。 data = path.read_bytes() row = dict(path=name, category=category(name), bytes=len(data), sha256=hashlib.sha256(data).hexdigest()) try: lines = data.decode("utf-8").splitlines() except UnicodeDecodeError: row.update(physical_lines=None, nonblank_lines=None) else: row.update(physical_lines=len(lines), nonblank_lines=sum(bool(line.strip()) for line in lines)) files.append(row) group = groups.setdefault(row["category"], dict(files=0, binary_files=0, physical_lines=0, nonblank_lines=0, longest=None)) group["files"] += 1 if row["physical_lines"] is None: group["binary_files"] += 1 else: group["physical_lines"] += row["physical_lines"] group["nonblank_lines"] += row["nonblank_lines"] if group["longest"] is None or row["physical_lines"] > group["longest"]["lines"]: group["longest"] = dict(path=name, lines=row["physical_lines"]) result = dict(root=str(root), method="All manifest/Git-listed files; UTF-8 splitlines; nonblank includes comments. No extension whitelist. Ignored dependencies/build outputs excluded.", counter_sha256=hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), groups=groups, files=files) with args.output.open("x") as output: json.dump(result, output, ensure_ascii=False, indent=2) output.write(chr(10)) print(json.dumps(groups, ensure_ascii=False)) if __name__ == "__main__": main()