Files
FPGA_LVDS_PROTOCOL_ANALYSER/bump_version.py

44 lines
1.5 KiB
Python

#!/usr/bin/env python3
import re
import sys
from pathlib import Path
VERSION_FILE = Path("version.txt")
TOP_V_FILE = Path("top.v")
VERSION_FIELD_LEN = 9
def main():
if not VERSION_FILE.exists():
sys.exit("Error: version.txt not found")
if not TOP_V_FILE.exists():
sys.exit("Error: top.v not found")
old_version = VERSION_FILE.read_text(encoding='utf-8').strip()
parts = old_version.split(".")
if len(parts) != 3 or not all(p.isdigit() for p in parts):
sys.exit(f"Error: unexpected version format '{old_version}'")
new_version = f"{parts[0]}.{parts[1]}.{int(parts[2])+1}"
padded = new_version.ljust(VERSION_FIELD_LEN)
content = TOP_V_FILE.read_text(encoding='utf-8-sig', newline='\n')
m = re.search(r'version chars at indices (\d+)', content)
if not m:
sys.exit("Error: could not find 'version chars at indices' comment in top.v")
start_idx = int(m.group(1))
for offset, ch in enumerate(padded):
idx = start_idx + offset
pattern = rf"(9'd{idx}:\s+banner_char\s*=\s*)[^\n]+;"
new_content, n = re.subn(pattern, lambda mo, c=ch: mo.group(1) + f'"{c}";', content)
if n == 0:
sys.exit(f"Error: could not find banner_char entry for index {idx}")
content = new_content
TOP_V_FILE.write_text(content, encoding='utf-8', newline='\n')
VERSION_FILE.write_text(new_version + "\n", encoding='utf-8')
print(f"Version bumped to {new_version}")
if __name__ == "__main__":
main()