|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright 2021-2023 Axel Huebl |
| 4 | +# |
| 5 | +# This file is part of openPMD-api. |
| 6 | +# |
| 7 | + |
| 8 | +# This file is a maintainer tool to bump the versions inside openPMD-api's |
| 9 | +# source directory at all places where necessary. |
| 10 | +# |
| 11 | +from pathlib import Path |
| 12 | +import re |
| 13 | +import sys |
| 14 | + |
| 15 | +# Maintainer Inputs ########################################################### |
| 16 | + |
| 17 | +print( |
| 18 | + """Hi there, this is an openPMD maintainer tool to update the source |
| 19 | +code of openPMD-api to a new version. |
| 20 | +For it to work, you need write access on the source directory and |
| 21 | +you should be working in a clean git branch without ongoing |
| 22 | +rebase/merge/conflict resolves and without unstaged changes.""" |
| 23 | +) |
| 24 | + |
| 25 | +# check source dir |
| 26 | +# REPO_DIR = Path(__file__).parent.parent.parent.absolute() |
| 27 | +REPO_DIR = Path(__file__).parent.absolute() |
| 28 | +print(f"\nYour current source directory is: {REPO_DIR}") |
| 29 | + |
| 30 | +REPLY = input("Are you sure you want to continue? [Y/n] ") |
| 31 | +print() |
| 32 | +if REPLY not in ["Y", "y", ""]: |
| 33 | + print("You did not confirm with 'y', aborting.") |
| 34 | + sys.exit(1) |
| 35 | + |
| 36 | +MAJOR = input("MAJOR version? (e.g., 1) ") |
| 37 | +MINOR = input("MINOR version? (e.g., 0) ") |
| 38 | +PATCH = input("PATCH version? (e.g., 0) ") |
| 39 | +SUFFIX = input("SUFFIX? (e.g., dev) ") |
| 40 | + |
| 41 | +VERSION_STR = f"{MAJOR}.{MINOR}.{PATCH}" |
| 42 | +VERSION_STR_SUFFIX = VERSION_STR + (f"-{SUFFIX}" if SUFFIX else "") |
| 43 | + |
| 44 | +print() |
| 45 | +print(f"Your new version is: {VERSION_STR_SUFFIX}") |
| 46 | + |
| 47 | +# Recover the old version from the Readme. |
| 48 | +# Do not use CMakeLists.txt as it might already contain the upcoming version |
| 49 | +# code. |
| 50 | +with open(str(REPO_DIR.joinpath("README.md")), encoding="utf-8") as f: |
| 51 | + for line in f: |
| 52 | + match = re.search(r"find_package.*openPMD *([^ ]*) *CONFIG\).*", line) |
| 53 | + if match: |
| 54 | + OLD_VERSION_STR = match.group(1) |
| 55 | + break |
| 56 | + |
| 57 | +print(f"The old version is: {OLD_VERSION_STR}") |
| 58 | +print() |
| 59 | + |
| 60 | + |
| 61 | +REPLY = input("Is this information correct? Will now start updating! [y/N] ") |
| 62 | +print() |
| 63 | +if REPLY not in ["Y", "y", ""]: |
| 64 | + print("You did not confirm with 'y', aborting.") |
| 65 | + sys.exit(1) |
| 66 | + |
| 67 | + |
| 68 | +# Ask for new ################################################################# |
| 69 | + |
| 70 | +print("""We will now run a few sed commands on your source directory.\n""") |
| 71 | + |
| 72 | +# Updates ##################################################################### |
| 73 | + |
| 74 | +# run_test.sh (used also for Azure Pipelines) |
| 75 | +cmakelists_path = str(REPO_DIR.joinpath("CMakeLists.txt")) |
| 76 | +with open(cmakelists_path, encoding="utf-8") as f: |
| 77 | + cmakelists_content = f.read() |
| 78 | + cmakelists_content = re.sub( |
| 79 | + r"^(project.*openPMD.*VERSION *)(.*)(\).*)$", |
| 80 | + r"\g<1>{}\g<3>".format(VERSION_STR), |
| 81 | + cmakelists_content, |
| 82 | + flags=re.MULTILINE, |
| 83 | + ) |
| 84 | + |
| 85 | +with open(cmakelists_path, "w", encoding="utf-8") as f: |
| 86 | + f.write(cmakelists_content) |
| 87 | + |
| 88 | + |
| 89 | +def generic_replace(filename): |
| 90 | + filename = str(REPO_DIR.joinpath(filename)) |
| 91 | + with open(filename, encoding="utf-8") as f: |
| 92 | + content = f.read() |
| 93 | + content = re.sub(re.escape(OLD_VERSION_STR), VERSION_STR, content) |
| 94 | + |
| 95 | + with open(filename, "w", encoding="utf-8") as f: |
| 96 | + f.write(content) |
| 97 | + |
| 98 | + |
| 99 | +for file in ["docs/source/dev/linking.rst", "README.md"]: |
| 100 | + generic_replace(file) |
| 101 | + |
| 102 | +version_hpp_path = str(REPO_DIR.joinpath("include/openPMD/version.hpp")) |
| 103 | +with open(version_hpp_path, encoding="utf-8") as f: |
| 104 | + version_hpp_content = f.read() |
| 105 | + |
| 106 | + def replace(key, value): |
| 107 | + global version_hpp_content |
| 108 | + version_hpp_content = re.sub( |
| 109 | + r"^(#define OPENPMDAPI_VERSION_{}) .*$".format(re.escape(key)), |
| 110 | + r"\1 {}".format(value), |
| 111 | + version_hpp_content, |
| 112 | + flags=re.MULTILINE, |
| 113 | + ) |
| 114 | + |
| 115 | + replace("MAJOR", MAJOR) |
| 116 | + replace("MINOR", MINOR) |
| 117 | + replace("PATCH", PATCH) |
| 118 | + replace("LABEL", '"{}"'.format(SUFFIX)) |
| 119 | + |
| 120 | +with open(version_hpp_path, "w", encoding="utf-8") as f: |
| 121 | + f.write(version_hpp_content) |
| 122 | + |
| 123 | +# Epilogue #################################################################### |
| 124 | + |
| 125 | +print( |
| 126 | + """Done. Please check your source, e.g. via |
| 127 | + git diff |
| 128 | +now and commit the changes if no errors occurred.""" |
| 129 | +) |
0 commit comments