1import sys, re
2from datetime import date
3import logging
4
5logging.basicConfig(level=logging.INFO)
6
7# Update version label in all configuration files
8# Usage: python3 misc/update_version.py X.Y.Z
9
10# When testing, reset local state using:
11# git checkout -- CHANGELOG.md Doxyfile CMakeLists.txt doc/source/conf.py examples/bazel/third_party/libcbor/cbor/configuration.h
12
13version = sys.argv[1]
14release_date = date.today().strftime('%Y-%m-%d')
15major, minor, patch = version.split('.')
16
17
18def replace(file_path, pattern, replacement):
19    logging.info(f'Updating {file_path}')
20    original = open(file_path).read()
21    updated = re.sub(pattern, replacement, original)
22    assert updated != original
23    with open(file_path, 'w') as f:
24        f.write(updated)
25
26# Update changelog
27SEP = '---------------------'
28NEXT = f'Next\n{SEP}'
29changelog_header = f'{NEXT}\n\n{version} ({release_date})\n{SEP}'
30replace('CHANGELOG.md', NEXT, changelog_header)
31
32# Update Doxyfile
33DOXY_VERSION = 'PROJECT_NUMBER         = '
34replace('Doxyfile', DOXY_VERSION + '.*', DOXY_VERSION + version)
35
36# Update CMakeLists.txt
37replace('CMakeLists.txt',
38        '''SET\\(CBOR_VERSION_MAJOR "\d+"\\)
39SET\\(CBOR_VERSION_MINOR "\d+"\\)
40SET\\(CBOR_VERSION_PATCH "\d+"\\)''',
41        f'''SET(CBOR_VERSION_MAJOR "{major}")
42SET(CBOR_VERSION_MINOR "{minor}")
43SET(CBOR_VERSION_PATCH "{patch}")''')
44
45# Update Basel build example
46replace('examples/bazel/third_party/libcbor/cbor/configuration.h',
47        '''#define CBOR_MAJOR_VERSION \d+
48#define CBOR_MINOR_VERSION \d+
49#define CBOR_PATCH_VERSION \d+''',
50        f'''#define CBOR_MAJOR_VERSION {major}
51#define CBOR_MINOR_VERSION {minor}
52#define CBOR_PATCH_VERSION {patch}''')
53
54# Update Sphinx
55replace('doc/source/conf.py',
56        """version = '.*'
57release = '.*'""",
58        f"""version = '{major}.{minor}'
59release = '{major}.{minor}.{patch}'""")
60