1#
2# Copyright (c) 2024 Nordic Semiconductor ASA
3#
4# SPDX-License-Identifier: Apache-2.0
5#
6
7from sys import argv
8from pathlib import Path
9from re import sub, match, S
10from datetime import datetime
11
12p_root = Path(__file__).absolute().parents[1]
13p_VERSION = Path(p_root, 'zcbor', 'VERSION')
14p_RELEASE_NOTES = Path(p_root, 'RELEASE_NOTES.md')
15p_MIGRATION_GUIDE = Path(p_root, 'MIGRATION_GUIDE.md')
16p_common_h = Path(p_root, 'include', 'zcbor_common.h')
17
18def update_relnotes(p_relnotes, version, include_date=True):
19    relnotes_contents = p_relnotes.read_text(encoding="utf-8")
20    relnotes_lines = relnotes_contents.splitlines()
21    if version not in relnotes_lines[0]:
22        new_date = f" ({datetime.today().strftime('%Y-%m-%d')})" if include_date else ""
23        relnotes_new_header = f"# zcbor v. {version}{new_date}\n"
24        if ".99" not in relnotes_lines[0]:
25            prev_entry = match(r"\A# zcbor.*?(?=# zcbor v.)", relnotes_contents, S).group(0)
26            relnotes_contents = prev_entry + relnotes_contents
27        relnotes_contents = sub(r".*?\n", relnotes_new_header, relnotes_contents, count=1)
28        p_relnotes.write_text(relnotes_contents, encoding="utf-8")
29
30
31if __name__ == "__main__":
32    if len(argv) != 2 or match(r'\d+\.\d+\.\d+', argv[1]) is None:
33        print(f"Usage: {argv[0]} <new zcbor version>")
34        exit(1)
35    version = argv[1]
36    (major, minor, bugfix) = version.split('.')
37
38    p_VERSION.write_text(version, encoding="utf-8")
39    update_relnotes(p_RELEASE_NOTES, version)
40    update_relnotes(p_MIGRATION_GUIDE, version, include_date=False)
41    p_common_h_contents = p_common_h.read_text(encoding="utf-8")
42    common_h_new_contents = sub(r"(#define ZCBOR_VERSION_MAJOR )\d+", f"\\g<1>{major}", p_common_h_contents)
43    common_h_new_contents = sub(r"(#define ZCBOR_VERSION_MINOR )\d+", f"\\g<1>{minor}", common_h_new_contents)
44    common_h_new_contents = sub(r"(#define ZCBOR_VERSION_BUGFIX )\d+", f"\\g<1>{bugfix}", common_h_new_contents)
45    p_common_h.write_text(common_h_new_contents, encoding="utf-8")
46