Files
leleka/scripts/dev/set_header.py

26 lines
883 B
Python

"""Replace the leading `//!` doc block of a Rust file by the given lines (the real "what and why" of a file).
Usage: python scripts/dev/set_header.py FILE LINE [LINE ...] (each LINE at most 116 columns; `//! ` is added)
Offline refactoring aid (STRA-338 debt (c)), not part of the product.
"""
import pathlib
import sys
def set_header(path, lines):
for line in lines:
if len(line) > 116:
sys.exit(f"{path}: header line too long ({len(line)}): {line}")
text = pathlib.Path(path).read_text(encoding="utf-8")
rows = text.split("\n")
end = 0
while end < len(rows) and rows[end].startswith("//!"):
end += 1
rows[:end] = ["//! " + line for line in lines]
with open(path, "w", encoding="utf-8", newline="\n") as handle:
handle.write("\n".join(rows))
if __name__ == "__main__":
set_header(sys.argv[1], sys.argv[2:])