|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""Sets a version number in all relevant project files""" |
| 4 | + |
| 5 | +import sys |
| 6 | +import re |
| 7 | +import os |
| 8 | + |
| 9 | +# The pattern the version number must satisfy |
| 10 | +VERSION_PATTERN = r'\d+\.\d+(?:\.\d+)?(?:-\w+)?' |
| 11 | + |
| 12 | +# List of (file name, search pattern, replacement pattern) tuples for all |
| 13 | +# the occurancies to be replaced. |
| 14 | +FILES_PATTERNS = [('src/hsd/__init__.py', |
| 15 | + r'^__version__\s*=\s*([\'"]){}\1'.format(VERSION_PATTERN), |
| 16 | + "__version__ = '{version}'"), |
| 17 | + ('docs/introduction.rst', |
| 18 | + r'hsd-python version[ ]*{}.'.format(VERSION_PATTERN), |
| 19 | + 'hsd-python version {shortversion}.'), |
| 20 | + ('setup.cfg', |
| 21 | + r'version\s*=\s*{}'.format(VERSION_PATTERN), |
| 22 | + "version = {version}"), |
| 23 | + ('docs/conf.py', |
| 24 | + r'release\s*=\s*([\'"]){}\1'.format(VERSION_PATTERN), |
| 25 | + "release = '{version}'"), |
| 26 | + ] |
| 27 | + |
| 28 | + |
| 29 | +def main(): |
| 30 | + """Main script.""" |
| 31 | + |
| 32 | + if len(sys.argv) < 2: |
| 33 | + sys.stderr.write("Missing version string\n") |
| 34 | + sys.exit(1) |
| 35 | + |
| 36 | + version, shortversion = _get_version_strings(sys.argv[1]) |
| 37 | + rootdir = os.path.join(os.path.dirname(sys.argv[0]), '..') |
| 38 | + _replace_version_in_files(FILES_PATTERNS, rootdir, version, shortversion) |
| 39 | + _replace_version_in_changelog(rootdir, version) |
| 40 | + |
| 41 | + |
| 42 | +def _get_version_strings(version): |
| 43 | + """Returns version and the short version as string""" |
| 44 | + |
| 45 | + match = re.match(VERSION_PATTERN, version) |
| 46 | + if match is None: |
| 47 | + print("Invalid version string") |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + shortversion = '.'.join(version.split('.')[0:2]) |
| 51 | + return version, shortversion |
| 52 | + |
| 53 | + |
| 54 | +def _replace_version_in_files(files_patterns, rootdir, version, shortversion): |
| 55 | + """Replaces version number in given files with given search/replacement patterns""" |
| 56 | + |
| 57 | + for fname, regexp, repl in files_patterns: |
| 58 | + fname = os.path.join(rootdir, fname) |
| 59 | + print("Replacments in '{}': ".format(os.path.relpath(fname, rootdir)), end='') |
| 60 | + fp = open(fname, 'r') |
| 61 | + txt = fp.read() |
| 62 | + fp.close() |
| 63 | + replacement = repl.format(version=version, shortversion=shortversion) |
| 64 | + newtxt, nsub = re.subn(regexp, replacement, txt, flags=re.MULTILINE) |
| 65 | + print(nsub) |
| 66 | + fp = open(fname, 'w') |
| 67 | + fp.write(newtxt) |
| 68 | + fp.close() |
| 69 | + |
| 70 | + |
| 71 | +def _replace_version_in_changelog(rootdir, version): |
| 72 | + """Replaces the unreleased section in CHANGELOG.rst""" |
| 73 | + |
| 74 | + fname = os.path.join(rootdir, 'CHANGELOG.rst') |
| 75 | + print("Replacments in '{}': ".format(os.path.relpath(fname, rootdir)), end='') |
| 76 | + fp = open(fname, 'r') |
| 77 | + txt = fp.read() |
| 78 | + fp.close() |
| 79 | + decoration = '=' * len(version) |
| 80 | + newtxt, nsub = re.subn( |
| 81 | + r'^Unreleased\s*\n=+', version + r'\n' + decoration, txt, |
| 82 | + count=1, flags=re.MULTILINE) |
| 83 | + print(nsub) |
| 84 | + fp = open(fname, 'w') |
| 85 | + fp.write(newtxt) |
| 86 | + fp.close() |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + main() |
0 commit comments