|
| 1 | +# This script will bump the versions found in files (charts, pom.xml) during the Feast release process. |
| 2 | + |
| 3 | +import pathlib |
| 4 | +import sys |
| 5 | + |
| 6 | +USAGE = f"Usage: python {sys.argv[0]} [--help] | current_semver_version new_semver_version]" |
| 7 | +VERSIONS_TO_BUMP = 26 |
| 8 | + |
| 9 | + |
| 10 | +def main() -> None: |
| 11 | + args = sys.argv[1:] |
| 12 | + if not args or len(args) != 2: |
| 13 | + raise SystemExit(USAGE) |
| 14 | + |
| 15 | + current_version = args[0].strip() |
| 16 | + new_version = args[1].strip() |
| 17 | + |
| 18 | + if current_version == new_version: |
| 19 | + raise SystemExit(f"Current and new versions are the same: {current_version} == {new_version}") |
| 20 | + |
| 21 | + # Validate that the input arguments are semver versions |
| 22 | + if not is_semantic_version(current_version): |
| 23 | + raise SystemExit(f"Current version is not a valid semantic version: {current_version}") |
| 24 | + |
| 25 | + if not is_semantic_version(new_version): |
| 26 | + raise SystemExit(f"New version is not a valid semantic version: {new_version}") |
| 27 | + |
| 28 | + # Get git repo root directory |
| 29 | + repo_root = pathlib.Path(__file__).resolve().parent.parent.parent.parent |
| 30 | + path_to_file_list = repo_root.joinpath("infra", "scripts", "version_bump", "files_to_bump.txt") |
| 31 | + |
| 32 | + # Get files to bump versions within |
| 33 | + with open(path_to_file_list, "r") as f: |
| 34 | + files_to_bump = f.read().splitlines() |
| 35 | + |
| 36 | + # The current version should be 0.18.0 or 0.19.0 or 0.20.0 etc, but we should also make sure to support the |
| 37 | + # occasional patch release on the master branch like 0.18.1 or 0.18.2 |
| 38 | + versions_in_files = 0 |
| 39 | + if current_version[-2:] != ".0": |
| 40 | + print(current_version[-2:]) |
| 41 | + versions_in_files = count_version(current_version, files_to_bump, repo_root) |
| 42 | + if versions_in_files != VERSIONS_TO_BUMP: |
| 43 | + raise SystemExit(f"Found {versions_in_files} occurrences of {current_version} in files to bump, but " |
| 44 | + f"expected {VERSIONS_TO_BUMP}") |
| 45 | + else: |
| 46 | + found = False |
| 47 | + |
| 48 | + # Lets make sure the files don't contain a patch version (e.g, 0.x.0 -> 0.x.20) |
| 49 | + for patch_version in range(0, 20): |
| 50 | + current_version_patch = current_version[:-1] + str(patch_version) |
| 51 | + versions_in_files = count_version(current_version_patch, files_to_bump, repo_root) |
| 52 | + |
| 53 | + # We are using a patch version, let's change our version number |
| 54 | + if versions_in_files == VERSIONS_TO_BUMP: |
| 55 | + print(f"Found {versions_in_files} occurrences of {current_version_patch}, changing current version to " |
| 56 | + f"{current_version_patch}") |
| 57 | + current_version = current_version_patch |
| 58 | + found = True |
| 59 | + break |
| 60 | + if not found: |
| 61 | + raise SystemExit(f"Could not find {VERSIONS_TO_BUMP} versions of {current_version} in {path_to_file_list}") |
| 62 | + |
| 63 | + print(f"Found {versions_in_files} occurrences of {current_version} in files to bump {path_to_file_list}") |
| 64 | + |
| 65 | + # Bump the version in the files |
| 66 | + updated_count = 0 |
| 67 | + for file in files_to_bump: |
| 68 | + with open(repo_root.joinpath(file), "r") as f: |
| 69 | + file_contents = f.read() |
| 70 | + file_contents = file_contents.replace(current_version, new_version) |
| 71 | + |
| 72 | + with open(repo_root.joinpath(file), "w") as f: |
| 73 | + f.write(file_contents) |
| 74 | + updated_count += 1 |
| 75 | + |
| 76 | + print(f"Updated {updated_count} files with new version {new_version}") |
| 77 | + |
| 78 | + |
| 79 | +def is_semantic_version(version: str) -> bool: |
| 80 | + components = version.split(".") |
| 81 | + if len(components) != 3: |
| 82 | + return False |
| 83 | + for component in components: |
| 84 | + if not component.isdigit(): |
| 85 | + return False |
| 86 | + return True |
| 87 | + |
| 88 | + |
| 89 | +def count_version(current_version, files_to_bump, repo_root): |
| 90 | + # Count how many of the existing versions we find |
| 91 | + total = 0 |
| 92 | + for file in files_to_bump: |
| 93 | + with open(repo_root.joinpath(file), "r") as f: |
| 94 | + file_contents = f.read() |
| 95 | + total += file_contents.count(current_version) |
| 96 | + return total |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main() |
0 commit comments