Skip to content

Commit 93cd1eb

Browse files
committed
Add script for setting version globally
1 parent 78146c0 commit 93cd1eb

3 files changed

Lines changed: 94 additions & 0 deletions

File tree

devtools/set_version

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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()

docs/introduction.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ scientific simulation tool (`DFTB+ <https://github.com/dftbplus/dftbplus>`_),
1313
but is of general purpose. Data stored in HSD can be easily mapped to a subset
1414
of JSON, YAML or XML and *vice versa*.
1515

16+
This document describes hsd-python version 0.1.
17+
1618

1719
Installation
1820
============

src/hsd/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
from hsd.formatter import HsdFormatter
1515
from hsd.io import load, load_string, dump, dump_string
1616
from hsd.parser import HsdParser
17+
18+
__version__ = '0.1'

0 commit comments

Comments
 (0)