xref: /freebsd/contrib/libcbor/misc/update_version.py (revision 5d3e7166f6a0187fa3f8831b16a06bd9955c21ff)
110ff414cSEd Masteimport sys, re
210ff414cSEd Mastefrom datetime import date
3*5d3e7166SEd Masteimport logging
4*5d3e7166SEd Maste
5*5d3e7166SEd Mastelogging.basicConfig(level=logging.INFO)
6*5d3e7166SEd Maste
7*5d3e7166SEd Maste# Update version label in all configuration files
8*5d3e7166SEd Maste# Usage: python3 misc/update_version.py X.Y.Z
9*5d3e7166SEd Maste
10*5d3e7166SEd Maste# When testing, reset local state using:
11*5d3e7166SEd Maste# git checkout -- CHANGELOG.md Doxyfile CMakeLists.txt doc/source/conf.py examples/bazel/third_party/libcbor/cbor/configuration.h
1210ff414cSEd Maste
1310ff414cSEd Masteversion = sys.argv[1]
1410ff414cSEd Masterelease_date = date.today().strftime('%Y-%m-%d')
1510ff414cSEd Mastemajor, minor, patch = version.split('.')
1610ff414cSEd Maste
1710ff414cSEd Maste
1810ff414cSEd Mastedef replace(file_path, pattern, replacement):
19*5d3e7166SEd Maste    logging.info(f'Updating {file_path}')
20*5d3e7166SEd Maste    original = open(file_path).read()
21*5d3e7166SEd Maste    updated = re.sub(pattern, replacement, original)
22*5d3e7166SEd Maste    assert updated != original
2310ff414cSEd Maste    with open(file_path, 'w') as f:
2410ff414cSEd Maste        f.write(updated)
2510ff414cSEd Maste
2610ff414cSEd Maste# Update changelog
2710ff414cSEd MasteSEP = '---------------------'
2810ff414cSEd MasteNEXT = f'Next\n{SEP}'
2910ff414cSEd Mastechangelog_header = f'{NEXT}\n\n{version} ({release_date})\n{SEP}'
3010ff414cSEd Mastereplace('CHANGELOG.md', NEXT, changelog_header)
3110ff414cSEd Maste
3210ff414cSEd Maste# Update Doxyfile
3310ff414cSEd MasteDOXY_VERSION = 'PROJECT_NUMBER         = '
3410ff414cSEd Mastereplace('Doxyfile', DOXY_VERSION + '.*', DOXY_VERSION + version)
3510ff414cSEd Maste
3610ff414cSEd Maste# Update CMakeLists.txt
3710ff414cSEd Mastereplace('CMakeLists.txt',
38*5d3e7166SEd Maste        '''SET\\(CBOR_VERSION_MAJOR "\d+"\\)
39*5d3e7166SEd MasteSET\\(CBOR_VERSION_MINOR "\d+"\\)
40*5d3e7166SEd MasteSET\\(CBOR_VERSION_PATCH "\d+"\\)''',
4110ff414cSEd Maste        f'''SET(CBOR_VERSION_MAJOR "{major}")
4210ff414cSEd MasteSET(CBOR_VERSION_MINOR "{minor}")
4310ff414cSEd MasteSET(CBOR_VERSION_PATCH "{patch}")''')
4410ff414cSEd Maste
45*5d3e7166SEd Maste# Update Basel build example
46*5d3e7166SEd Mastereplace('examples/bazel/third_party/libcbor/cbor/configuration.h',
47*5d3e7166SEd Maste        '''#define CBOR_MAJOR_VERSION \d+
48*5d3e7166SEd Maste#define CBOR_MINOR_VERSION \d+
49*5d3e7166SEd Maste#define CBOR_PATCH_VERSION \d+''',
50*5d3e7166SEd Maste        f'''#define CBOR_MAJOR_VERSION {major}
51*5d3e7166SEd Maste#define CBOR_MINOR_VERSION {minor}
52*5d3e7166SEd Maste#define CBOR_PATCH_VERSION {patch}''')
53*5d3e7166SEd Maste
5410ff414cSEd Maste# Update Sphinx
5510ff414cSEd Mastereplace('doc/source/conf.py',
5610ff414cSEd Maste        """version = '.*'
5710ff414cSEd Masterelease = '.*'""",
5810ff414cSEd Maste        f"""version = '{major}.{minor}'
5910ff414cSEd Masterelease = '{major}.{minor}.{patch}'""")
60