|
| 1 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +# license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +"""Command line tool which changes the branch to be |
| 19 | +ready to build and test the given Elastic stack version. |
| 20 | +""" |
| 21 | + |
| 22 | +import re |
| 23 | +import sys |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | +SOURCE_DIR = Path(__file__).absolute().parent.parent |
| 27 | + |
| 28 | + |
| 29 | +def find_and_replace(path, pattern, replace): |
| 30 | + # Does a find and replace within a file path and complains |
| 31 | + # if the given pattern isn't found in the file. |
| 32 | + with open(path, "r") as f: |
| 33 | + old_data = f.read() |
| 34 | + |
| 35 | + if re.search(pattern, old_data, flags=re.MULTILINE) is None: |
| 36 | + print(f"Didn't find the pattern {pattern!r} in {path!s}") |
| 37 | + exit(1) |
| 38 | + |
| 39 | + new_data = re.sub(pattern, replace, old_data, flags=re.MULTILINE) |
| 40 | + with open(path, "w") as f: |
| 41 | + f.truncate() |
| 42 | + f.write(new_data) |
| 43 | + |
| 44 | + |
| 45 | +def main(): |
| 46 | + if len(sys.argv) != 2: |
| 47 | + print("usage: utils/bump-version.py [stack version]") |
| 48 | + exit(1) |
| 49 | + |
| 50 | + stack_version = sys.argv[1] |
| 51 | + try: |
| 52 | + python_version = re.search(r"^([0-9][0-9\.]*[0-9]+)", stack_version).group(1) |
| 53 | + except AttributeError: |
| 54 | + print(f"Couldn't match the given stack version {stack_version!r}") |
| 55 | + exit(1) |
| 56 | + |
| 57 | + # Pad the version value with .0 until there |
| 58 | + # we have the major, minor, and patch. |
| 59 | + for _ in range(3): |
| 60 | + if len(python_version.split(".")) >= 3: |
| 61 | + break |
| 62 | + python_version += ".0" |
| 63 | + |
| 64 | + find_and_replace( |
| 65 | + path=SOURCE_DIR / "elastic_enterprise_search/_version.py", |
| 66 | + pattern=r"__version__ = \"[0-9]+[0-9\.]*[0-9](?:\+dev)?\"", |
| 67 | + replace=f'__version__ = "{python_version}"', |
| 68 | + ) |
| 69 | + |
| 70 | + # These values should always be the 'major.minor-SNAPSHOT' |
| 71 | + major_minor_version = ".".join(python_version.split(".")[:2]) |
| 72 | + find_and_replace( |
| 73 | + path=SOURCE_DIR / ".ci/test-matrix.yml", |
| 74 | + pattern=r'STACK_VERSION:\s+\- "[0-9]+[0-9\.]*[0-9](?:\-SNAPSHOT)?"', |
| 75 | + replace=f'STACK_VERSION:\n - "{major_minor_version}.0-SNAPSHOT"', |
| 76 | + ) |
| 77 | + find_and_replace( |
| 78 | + path=SOURCE_DIR / ".github/workflows/unified-release.yml", |
| 79 | + pattern=r'STACK_VERSION:\s+"[0-9]+[0-9\.]*[0-9](?:\-SNAPSHOT)?"', |
| 80 | + replace=f'STACK_VERSION: "{major_minor_version}-SNAPSHOT"', |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + main() |
0 commit comments