Skip to content

Commit 4097e11

Browse files
committed
chore: simplify version logic, rewrite scripts in python
1 parent deda7e6 commit 4097e11

File tree

6 files changed

+149
-152
lines changed

6 files changed

+149
-152
lines changed

.circleci/config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ master_filter: &master_filter
66
tags:
77
ignore: /.*/
88
branches:
9-
only: /^master$/
9+
only:
10+
- master
1011

1112
workflows:
1213
version: 2
@@ -66,11 +67,10 @@ jobs:
6667
python3 -m pip install -r \
6768
.circleci/scripts/update_manifest_file/requirements.txt
6869
- run:
69-
name: Get Release Information
70+
name: Get Enterprise Information
7071
command: |
71-
.circleci/scripts/get-release-info
72+
python3 .circleci/scripts/get_enterprise_info
7273
- run:
7374
name: Do Enterprise Release
7475
command: |
75-
.circleci/scripts/get-enterprise-version
7676
.circleci/scripts/do-enterprise-release

.circleci/scripts/do-enterprise-release

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ set -o errexit \
33
-o nounset \
44
-o pipefail
55

6-
if [[ ${PRODUCT} != 'influxdb' ]] || ( \
7-
[[ ${SUBPRODUCT} != 'meta' ]] && \
8-
[[ ${SUBPRODUCT} != 'data' ]] \
9-
)
6+
if [[ ${PRODUCT:-} != 'influxdb' ]] || \
7+
[[ ${VERSION_MAJOR:-} != '1' ]] || \
8+
[[ ${VERSION_MINOR:-} != '9' ]]
109
then
1110
printf 'Release is not Enterprise skipping...\n'; exit 0
1211
fi

.circleci/scripts/get-enterprise-version

Lines changed: 0 additions & 60 deletions
This file was deleted.

.circleci/scripts/get-release-info

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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))

.circleci/scripts/update_manifest_file/__main__.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
import re
66
import sys
77

8+
def getenv(variable: str) -> str:
9+
"""
10+
Retrieves `variable` from the environment.
11+
"""
12+
# `os.getenv` returns `Dict[str]` which is incompatible with functions
13+
# that require `str` parameters. If `os.getenv` returns a value, it is
14+
# unwrapped and returned to the caller.
15+
value = os.getenv(variable)
16+
if value is not None:
17+
return value
18+
19+
raise RuntimeError('Missing environment variable "{}".'.format(variable))
820

921
def reg_major(major: int) -> str:
1022
"""
@@ -57,7 +69,7 @@ def paragraph_repo_cb(paragraph):
5769

5870
def item_cb(key, value):
5971
if key == "GitCommit":
60-
return os.environ.get("CIRCLE_SHA1")
72+
return getenv("CIRCLE_SHA1")
6173
else:
6274
return value
6375

@@ -77,10 +89,10 @@ def item_cb(key, value):
7789
if key == "Tags":
7890
# fmt: off
7991
return re.sub(
80-
reg_major(int(os.environ.get("VERSION_MAJOR"))) +
81-
reg_minor(int(os.environ.get("VERSION_MINOR"))) +
92+
reg_major(int(getenv("VERSION_MAJOR"))) +
93+
reg_minor(int(getenv("VERSION_MINOR"))) +
8294
reg_patch(),
83-
os.environ.get("VERSION"),
95+
getenv("VERSION"),
8496
value,
8597
)
8698
# fmt: on
@@ -96,6 +108,7 @@ def item_cb(key, value):
96108
)
97109
# fmt: on
98110

111+
99112
with open(sys.argv[1], "rb") as content:
100113
# fmt: off
101114
document = [

0 commit comments

Comments
 (0)