|
| 1 | +#!/usr/bin/python3 |
| 2 | +from typing import Dict |
| 3 | +from typing import Generator |
| 4 | +from typing import Optional |
| 5 | +import os |
| 6 | +import re |
| 7 | +import subprocess |
| 8 | +import enum |
| 9 | + |
| 10 | + |
| 11 | +def getenv(variable: str) -> str: |
| 12 | + """ |
| 13 | + Retrieves `variable` from the environment. |
| 14 | + """ |
| 15 | + # `os.getenv` returns `Dict[str]` which is incompatible with functions |
| 16 | + # that require `str` parameters. If `os.getenv` returns a value, it is |
| 17 | + # unwrapped and returned to the caller. |
| 18 | + value = os.getenv(variable) |
| 19 | + if value is not None: |
| 20 | + return value |
| 21 | + |
| 22 | + raise RuntimeError('Missing environment variable "{}".'.format(variable)) |
| 23 | + |
| 24 | + |
| 25 | +class GitDiff(enum.Enum): |
| 26 | + # fmt: off |
| 27 | + ADDED = "\+" |
| 28 | + REMOVED = "\-" |
| 29 | + # fmt: on |
| 30 | + |
| 31 | + def __str__(self): |
| 32 | + return str(self.value) |
| 33 | + |
| 34 | + |
| 35 | +def parse_env_line(line: str, diff: GitDiff) -> Optional[Dict[str, str]]: |
| 36 | + """ |
| 37 | + Matches version line in dockerfile. |
| 38 | + """ |
| 39 | + # fmt: off |
| 40 | + matches = re.match( |
| 41 | + r"^" + str(diff) + # changed? |
| 42 | + r"ENV\s+INFLUXDB_VERSION\s+" + # prelude |
| 43 | + r"((\d+)" + # major cg: 2 |
| 44 | + r"(?:\.(\d+))?" + # minor? cg: 3 |
| 45 | + r"(?:\.(\d+))?" + # patch? cg: 4 |
| 46 | + r"(?:\-?(rc\d+))?)" + # rc? cg: 5 |
| 47 | + r"-c" + # interlude |
| 48 | + r"(\d+)" + # repeat major cg: 6 |
| 49 | + r"(?:\.(\d+))?" + # repeat minor? cg: 7 |
| 50 | + r"(?:\.(\d+))?" + # repeat patch? cg: 8 |
| 51 | + r"(?:\-?(rc\d+))?", # repeat rc? cg: 9 |
| 52 | + line) |
| 53 | + |
| 54 | + if matches is not None: |
| 55 | + # I tried using capture-group back-references within the regular |
| 56 | + # expression. However, it couldn't handle optional capture- |
| 57 | + # groups. So, instead, we check for equality here. |
| 58 | + if (matches.group(2) == matches.group(6) and |
| 59 | + matches.group(3) == matches.group(7) and |
| 60 | + matches.group(4) == matches.group(8) and |
| 61 | + matches.group(5) == matches.group(9)): |
| 62 | + return { |
| 63 | + "VERSION": matches.group(1), |
| 64 | + "VERSION_MAJOR": matches.group(2), |
| 65 | + "VERSION_MINOR": matches.group(3) if matches.group(3) else "", |
| 66 | + "VERSION_PATCH": matches.group(4) if matches.group(4) else "", |
| 67 | + "VERSION_RC": matches.group(5) if matches.group(5) else "", |
| 68 | + } |
| 69 | + return None |
| 70 | + # fmt: on |
| 71 | + |
| 72 | + |
| 73 | +def parse_version() -> Optional[str]: |
| 74 | + """ |
| 75 | + Parse version from "ENV" line in git. |
| 76 | + """ |
| 77 | + # Retrieve all lines that have changed since the commit between |
| 78 | + # HEAD~1 and HEAD. This is more robust than just parsing the |
| 79 | + # current Dockerfile as it ensures that `INFLUXDB_VERSION` |
| 80 | + # actually changed. |
| 81 | + # fmt: off |
| 82 | + process = subprocess.run( |
| 83 | + ["git", "diff", "--unified=0", "HEAD~1..HEAD", "influxdb" ], |
| 84 | + stdout=subprocess.PIPE, |
| 85 | + stderr=subprocess.PIPE, |
| 86 | + ) |
| 87 | + # fmt: on |
| 88 | + |
| 89 | + version_prev = None |
| 90 | + version_curr = None |
| 91 | + for line in process.stdout.decode("utf-8").split("\n"): |
| 92 | + line = line.rstrip(" \t") |
| 93 | + prev = parse_env_line(line, GitDiff.REMOVED) |
| 94 | + curr = parse_env_line(line, GitDiff.ADDED) |
| 95 | + |
| 96 | + if prev is not None: |
| 97 | + version_prev = prev |
| 98 | + if curr is not None: |
| 99 | + version_curr = curr |
| 100 | + |
| 101 | + # fmt: off |
| 102 | + if (version_prev is not None and |
| 103 | + version_curr is not None): |
| 104 | + # if the versions differ, then this must be a release |
| 105 | + if version_prev["VERSION"] != version_curr["VERSION"]: |
| 106 | + return version_curr |
| 107 | + # fmt: on |
| 108 | + |
| 109 | + # If the `INFLUXDB_VERSION` line has changed but the version has |
| 110 | + # not changed, reset so the next encounter of `version_prev` |
| 111 | + # does not cause this to return the incorrect `version_curr`. |
| 112 | + if version_curr != None: |
| 113 | + version_prev = None |
| 114 | + version_curr = None |
| 115 | + |
| 116 | + # no version change in dockerfile |
| 117 | + return None |
| 118 | + |
| 119 | + |
| 120 | +version = parse_version() |
| 121 | +if version is not None: |
| 122 | + with open(getenv("BASH_ENV"), "a") as stream: |
| 123 | + stream.write("export PRODUCT=influxdb\n") |
| 124 | + for key, value in version.items(): |
| 125 | + stream.write("export {}={}\n".format(key, value)) |
0 commit comments