|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +######################################################### |
| 4 | +# Sets version numbers to a date-based dev version. |
| 5 | +# |
| 6 | +# Does nothing if not on a dev version. |
| 7 | +######################################################### |
| 8 | +# Example usage (from repo root): |
| 9 | +# |
| 10 | +# ./dev/overwrite_dev_versions_with_date.sh |
| 11 | +######################################################### |
| 12 | + |
| 13 | +import os |
| 14 | +import pathlib |
| 15 | +import re |
| 16 | +import subprocess |
| 17 | + |
| 18 | + |
| 19 | +def main(): |
| 20 | + os.chdir(pathlib.Path(__file__).parent) |
| 21 | + os.chdir(subprocess.check_output(["git", "rev-parse", "--show-toplevel"]).decode().strip()) |
| 22 | + |
| 23 | + # Generate dev version starting from major.minor version. |
| 24 | + # (Requires the existing version to have a 'dev' suffix.) |
| 25 | + # (Uses the timestamp of the HEAD commit, to ensure consistency when run multiple times.) |
| 26 | + with open('setup.py') as f: |
| 27 | + maj_min_version_line, = [line for line in f.read().splitlines() if re.match("^__version__ = '[^']+'", line)] |
| 28 | + maj_version, min_version, patch = maj_min_version_line.split()[-1].strip("'").split('.') |
| 29 | + if 'dev' not in patch: |
| 30 | + return # Do nothing for non-dev versions. |
| 31 | + timestamp = subprocess.check_output(['git', 'show', '-s', '--format=%ct', 'HEAD']).decode().strip() |
| 32 | + new_version = f"{maj_version}.{min_version}.dev{timestamp}" |
| 33 | + |
| 34 | + # Overwrite existing versions. |
| 35 | + package_setup_files = [ |
| 36 | + "setup.py", |
| 37 | + ] |
| 38 | + for path in package_setup_files: |
| 39 | + with open(path) as f: |
| 40 | + content = f.read() |
| 41 | + assert maj_min_version_line in content |
| 42 | + content = content.replace(maj_min_version_line, f"__version__ = '{new_version}'") |
| 43 | + with open(path, 'w') as f: |
| 44 | + print(content, file=f, end='') |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == '__main__': |
| 48 | + main() |
0 commit comments