-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_frontmatter.py
More file actions
65 lines (50 loc) · 1.85 KB
/
Copy pathadd_frontmatter.py
File metadata and controls
65 lines (50 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Update the standards-version field in a YAML-frontmatter file.
Usage: python add_frontmatter.py <file_path> <new_version>
Finds the ``standards-version:`` key inside the leading ``---`` block and
replaces its value. Exits 0 on success, 1 if the marker is absent or the
file cannot be parsed.
"""
from __future__ import annotations
import re
import sys
from pathlib import Path
_FRONTMATTER_RE = re.compile(r"\Astandardsversion:", re.I) # loose sentinel
def _update(text: str, new_version: str) -> str | None:
"""Return updated text, or None if the standards-version key is absent."""
lines = text.splitlines(keepends=True)
in_block = False
for i, line in enumerate(lines):
stripped = line.lstrip()
if i == 0 and stripped.rstrip() == "---":
in_block = True
continue
if in_block and stripped.rstrip() in ("---", "..."):
break
if in_block and re.match(r"^standards-version\s*:", stripped):
lines[i] = re.sub(
r"(standards-version\s*:\s*).*",
rf"\g<1>{new_version}",
line,
)
return "".join(lines)
return None
def main(argv: list[str]) -> int:
if len(argv) != 3:
print(f"usage: {argv[0]} <file_path> <new_version>", file=sys.stderr)
return 1
path = Path(argv[1])
new_version = argv[2]
try:
text = path.read_text(encoding="utf-8")
except OSError as exc:
print(f"error: cannot read {path}: {exc}", file=sys.stderr)
return 1
updated = _update(text, new_version)
if updated is None:
print(f"error: standards-version not found in frontmatter of {path}", file=sys.stderr)
return 1
path.write_text(updated, encoding="utf-8")
print(f"updated {path}")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))