| 1 |
#!/usr/bin/env python3 |
| 2 |
import sys |
| 3 |
import re |
| 4 |
import os |
| 5 |
|
| 6 |
VERSION_PATTERN = r'\d+\.\d+(?:\.\d+)?(?:-\w+)?' |
| 7 |
FILES_PATTERNS = [ ('bin/fypp', |
| 8 |
r'^VERSION\s*=\s*([\'"]){}\1'.format(VERSION_PATTERN), |
| 9 |
"VERSION = '{version}'"), |
| 10 |
('docs/fypp.rst', |
| 11 |
r'Fypp Version[ ]*{}.'.format(VERSION_PATTERN), |
| 12 |
'Fypp Version {shortversion}.'), |
| 13 |
('setup.py', |
| 14 |
r'version\s*=\s*([\'"]){}\1'.format(VERSION_PATTERN), |
| 15 |
"version='{version}'"), |
| 16 |
('docs/conf.py', |
| 17 |
r'version\s*=\s*([\'"]){}\1'.format(VERSION_PATTERN), |
| 18 |
"version = '{shortversion}'"), |
| 19 |
('docs/conf.py', |
| 20 |
r'release\s*=\s*([\'"]){}\1'.format(VERSION_PATTERN), |
| 21 |
"release = '{version}'"), |
| 22 |
] |
| 23 |
|
| 24 |
if len(sys.argv) < 2: |
| 25 |
print("Missing version string") |
| 26 |
sys.exit(1) |
| 27 |
|
| 28 |
|
| 29 |
version = sys.argv[1] |
| 30 |
shortversion = '.'.join(version.split('.')[0:2]) |
| 31 |
|
| 32 |
match = re.match(VERSION_PATTERN, version) |
| 33 |
if match is None: |
| 34 |
print("Invalid version string") |
| 35 |
sys.exit(1) |
| 36 |
|
| 37 |
rootdir = os.path.join(os.path.dirname(sys.argv[0]), '..') |
| 38 |
for fname, regexp, repl in FILES_PATTERNS: |
| 39 |
fname = os.path.join(rootdir, fname) |
| 40 |
print("Replacments in '{}': ".format(fname), end='') |
| 41 |
fp = open(fname, 'r') |
| 42 |
txt = fp.read() |
| 43 |
fp.close() |
| 44 |
replacement = repl.format(version=version, shortversion=shortversion) |
| 45 |
newtxt, nsub = re.subn(regexp, replacement, txt, flags=re.MULTILINE) |
| 46 |
print(nsub) |
| 47 |
fp = open(fname, 'w') |
| 48 |
fp.write(newtxt) |
| 49 |
fp.close() |
| 50 |
|
| 51 |
|
| 52 |
# Replace version number in Change Log and adapt decoration below |
| 53 |
fname = os.path.join(rootdir, 'CHANGELOG.rst') |
| 54 |
print("Replacments in '{}': ".format(fname), end='') |
| 55 |
fp = open(fname, 'r') |
| 56 |
txt = fp.read() |
| 57 |
fp.close() |
| 58 |
decoration = '=' * len(version) |
| 59 |
newtxt, nsub = re.subn( |
| 60 |
'^Unreleased\s*\n=+', version + '\n' + decoration, txt, |
| 61 |
count=1, flags=re.MULTILINE) |
| 62 |
print(nsub) |
| 63 |
fp = open(fname, 'w') |
| 64 |
fp.write(newtxt) |
| 65 |
fp.close() |