forked from PiratesIRC/Dispatcharr-Lineuparr-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbump_version.py
More file actions
executable file
·83 lines (59 loc) · 2.52 KB
/
bump_version.py
File metadata and controls
executable file
·83 lines (59 loc) · 2.52 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
"""Bump the Lineuparr plugin version in plugin.json and plugin.py.
Version format: 1.26.{DDD}{HHMM} where DDD is day-of-year (3 digits) and
HHMM is 4-digit UTC time. Matches the convention in git history (e.g.
`1.26.1001146` = day 100, 11:46 UTC). Pass a version string to override.
Usage:
python3 bump_version.py # auto, current timestamp
python3 bump_version.py 1.26.1030900 # explicit
Exit codes: 0 on success, non-zero if the two files disagreed before/after.
"""
from __future__ import annotations
import json
import re
import sys
from datetime import datetime, timezone
from pathlib import Path
ROOT = Path(__file__).resolve().parent
PLUGIN_JSON = ROOT / "Lineuparr" / "plugin.json"
PLUGIN_PY = ROOT / "Lineuparr" / "plugin.py"
VERSION_RE = re.compile(r'^\d+\.\d+\.\d{7}$')
PY_VERSION_RE = re.compile(r'PLUGIN_VERSION\s*=\s*"([^"]+)"')
def auto_version() -> str:
now = datetime.now(timezone.utc)
return f"1.26.{now.timetuple().tm_yday:03d}{now.strftime('%H%M')}"
def read_json_version() -> str:
return json.loads(PLUGIN_JSON.read_text())["version"]
def read_py_version() -> str:
m = PY_VERSION_RE.search(PLUGIN_PY.read_text())
if not m:
raise RuntimeError("PLUGIN_VERSION not found in plugin.py")
return m.group(1)
def write_json_version(new: str) -> None:
text = PLUGIN_JSON.read_text()
updated = re.sub(r'("version"\s*:\s*)"[^"]+"', f'\\1"{new}"', text, count=1)
PLUGIN_JSON.write_text(updated)
def write_py_version(new: str) -> None:
text = PLUGIN_PY.read_text()
updated = PY_VERSION_RE.sub(f'PLUGIN_VERSION = "{new}"', text, count=1)
PLUGIN_PY.write_text(updated)
def main(argv: list[str]) -> int:
new = argv[1] if len(argv) > 1 else auto_version()
if not VERSION_RE.match(new):
print(f"error: version '{new}' must match 1.X.DDDHHMM (e.g. 1.26.1021420)", file=sys.stderr)
return 2
before_json = read_json_version()
before_py = read_py_version()
if before_json != before_py:
print(f"warning: plugin.json ({before_json}) and plugin.py ({before_py}) disagreed before bump", file=sys.stderr)
write_json_version(new)
write_py_version(new)
after_json = read_json_version()
after_py = read_py_version()
if after_json != after_py or after_json != new:
print(f"error: post-bump mismatch json={after_json} py={after_py} target={new}", file=sys.stderr)
return 1
print(f"bumped {before_json} -> {new}")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))