|
| 1 | + |
| 2 | +# Invoke this file from the ore directory to |
| 3 | +# overwrite the version number in one of the files below: |
| 4 | +# |
| 5 | +# ORE-SWIG/setup.py |
| 6 | +# QuantExt/qle/version.hpp |
| 7 | +# |
| 8 | +# e.g: |
| 9 | +# cd /path/to/ore |
| 10 | +# python /path/to/update_version_number.py -v 1.23.45 -f setup.py |
| 11 | +# python /path/to/update_version_number.py -v 1.23.45 -f version.hpp |
| 12 | + |
| 13 | +import optparse |
| 14 | +import shutil |
| 15 | +import re |
| 16 | +import os.path |
| 17 | + |
| 18 | +SETUP_PY = "ORE-SWIG/setup.py" |
| 19 | +VERSION_HPP = "QuantExt/qle/version.hpp" |
| 20 | + |
| 21 | +# Convert a version string into a string |
| 22 | +# containing a corresponding numerical value: |
| 23 | +# 1.8.14.0 -> 1081400 |
| 24 | +# 1.234.5 -> 12340500 |
| 25 | +# 1.234.5.dev1 -> 12340501 |
| 26 | +def version_string_to_number(v): |
| 27 | + ret = "" |
| 28 | + # match x.x.x or x.x.x.x |
| 29 | + # where x = one or more alphanumeric characters |
| 30 | + p = r"(\w*)\.(\w*)\.(\w*)(?:\.(\w*))?" |
| 31 | + m = re.match(p, v) |
| 32 | + if not m: |
| 33 | + raise Exception("version string '{v}' has invalid format, expected x.x.x or x.x.x.x where x in [a-zA-Z0-9_]") |
| 34 | + for x in m.groups(): |
| 35 | + x = re.sub("[^0-9]", "", x or "") |
| 36 | + if x: |
| 37 | + ret += x.zfill(2) |
| 38 | + else: |
| 39 | + ret += '00' |
| 40 | + return ret.lstrip('0') |
| 41 | + |
| 42 | +# Parse the command line arguments |
| 43 | +parser = optparse.OptionParser() |
| 44 | +parser.add_option('-f', '--file') |
| 45 | +parser.add_option('-v', '--version') |
| 46 | +opts, args = parser.parse_args() |
| 47 | +file = opts.file |
| 48 | +version = opts.version |
| 49 | + |
| 50 | +if file is None: |
| 51 | + raise Exception("missing input parameter --file") |
| 52 | + |
| 53 | +if version is None: |
| 54 | + raise Exception("missing input parameter --version") |
| 55 | + |
| 56 | +def find_and_replace(file, repl): |
| 57 | + |
| 58 | + if not os.path.isfile(file): |
| 59 | + raise Exception(f"invalid path: {file}") |
| 60 | + |
| 61 | + shutil.copy(file, file + ".bak") |
| 62 | + |
| 63 | + with open(file) as f: |
| 64 | + s = f.read() |
| 65 | + |
| 66 | + for (p, r) in repl: |
| 67 | + s = re.sub(p, r, s) |
| 68 | + |
| 69 | + with open(file, 'w') as f: |
| 70 | + f.write(s) |
| 71 | + |
| 72 | +if file == "setup.py": |
| 73 | + find_and_replace(SETUP_PY, [(r'(version\s*=\s*").*(")', fr'\g<1>{version}\g<2>')]) |
| 74 | +elif file == "version.hpp": |
| 75 | + version_num = version_string_to_number(version) |
| 76 | + find_and_replace(VERSION_HPP, [ |
| 77 | + (r'(#define OPEN_SOURCE_RISK_VERSION ").*(")', fr'\g<1>{version}\g<2>'), |
| 78 | + (r'(#define OPEN_SOURCE_RISK_VERSION_NUM ).*', fr'\g<1>{version_num}')]) |
| 79 | +else: |
| 80 | + raise Exception(f"unrecognized file: {file}") |
| 81 | + |
0 commit comments