Skip to content

Commit fcfbdc4

Browse files
committed
CI: extend python version check to Dockerfiles
Previously we checked in the pull_request CI only that all `pyproject.toml` files requrire a python version that lies in bounds with `X_PYTHON_MIN_VERSION` and `X_PYTHON_MAX_VERSION`. We extended the check to also check the `Dockerfile` of the server directory.
1 parent f4cdff5 commit fcfbdc4

2 files changed

Lines changed: 48 additions & 20 deletions

File tree

.github/workflows/pr.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,23 @@ jobs:
4444
4545
- name: Check Python Versions coincide with all pyproject.toml Files
4646
run: |
47-
for file in ../../sdk/pyproject.toml ../../compliance_tool/pyproject.toml; do
47+
for file in ../../sdk/pyproject.toml ../../compliance_tool/pyproject.toml ../../server/pyproject.toml; do
4848
python check_python_versions_coincide.py \
4949
$file \
5050
${{ env.X_PYTHON_MIN_VERSION }} \
5151
${{ env.X_PYTHON_MAX_VERSION }}
5252
done
5353
54-
# Todo: Check other pyproject.toml here as well, as we add them
54+
- name: Check Python Versions coincide with all Dockerfiles
55+
run: |
56+
for file in ../../server/docker/repository/Dockerfile \
57+
../../server/docker/registry/Dockerfile \
58+
../../server/docker/discovery/Dockerfile; do
59+
python check_python_versions_coincide.py --docker \
60+
$file \
61+
${{ env.X_PYTHON_MIN_VERSION }} \
62+
${{ env.X_PYTHON_MAX_VERSION }}
63+
done
5564
5665
sdk-test:
5766
# This job runs the unittests on the python versions specified down at the matrix
Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,67 @@
11
"""
2-
This helper script checks if the Python versions defined in a `pyproject.toml` coincide with the given `min_version`
3-
and `max_version` and returns an error if they don't.
2+
This helper script checks if the Python versions defined in a `pyproject.toml` or `Dockerfile` coincide with the given
3+
`min_version` and `max_version` and returns an error if they don't.
44
"""
55
import argparse
66
import re
77
import sys
88

99
from packaging.version import InvalidVersion, Version
1010

11+
def get_version_pyproject(file_path: str) -> str:
12+
with open(file_path, "r") as f:
13+
pyproject_content = f.read()
1114

12-
def main(pyproject_toml_path: str, min_version: str, max_version: str) -> None:
15+
match = re.search(r'requires-python\s*=\s*">=([\d.]+)"', pyproject_content)
16+
if not match:
17+
print(f"Error: `requires-python` field not found or invalid format in `{file_path}`")
18+
sys.exit(1)
19+
20+
return match.group(1)
21+
22+
def get_version_dockerfile(file_path: str) -> str:
23+
with open(file_path, "r") as f:
24+
pyproject_content = f.read()
25+
26+
match = re.search(r'^FROM\s+python:([\d.]+)', pyproject_content)
27+
if not match:
28+
print(f"Error: Definition of base image `FROM python:x.x` not found in `{file_path}`")
29+
sys.exit(1)
30+
31+
return match.group(1)
32+
33+
def main(file_path: str, is_dockerfile: bool, min_version: str, max_version: str) -> None:
1334
# Load and check `requires-python` version from `pyproject.toml`
1435
try:
15-
with open(pyproject_toml_path, "r") as f:
16-
pyproject_content = f.read()
17-
18-
match = re.search(r'requires-python\s*=\s*">=([\d.]+)"', pyproject_content)
19-
if not match:
20-
print(f"Error: `requires-python` field not found or invalid format in `{pyproject_toml_path}`")
21-
sys.exit(1)
36+
if is_dockerfile:
37+
used_version = get_version_dockerfile(file_path)
38+
else:
39+
used_version = get_version_pyproject(file_path)
2240

23-
pyproject_version = match.group(1)
24-
if Version(pyproject_version) < Version(min_version):
25-
print(f"Error: Python version in `{pyproject_toml_path}` `requires-python` ({pyproject_version}) "
41+
if Version(used_version) < Version(min_version):
42+
print(f"Error: Python version in `{file_path}` ({used_version}) "
2643
f"is smaller than `min_version` ({min_version}).")
2744
sys.exit(1)
2845

2946
except FileNotFoundError:
30-
print(f"Error: File not found: `{pyproject_toml_path}`.")
47+
print(f"Error: File not found: `{file_path}`.")
3148
sys.exit(1)
3249

33-
print(f"Success: Version in pyproject.toml `requires-python` (>={pyproject_version}) "
50+
print(f"Success: Version in `{file_path}` ({used_version}) "
3451
f"matches expected versions ([{min_version} to {max_version}]).")
3552

3653

3754
if __name__ == "__main__":
38-
parser = argparse.ArgumentParser(description="Check Python version support and alignment with pyproject.toml.")
39-
parser.add_argument("pyproject_toml_path", help="Path to the `pyproject.toml` file to check.")
55+
parser = argparse.ArgumentParser(description="Check Python version support and alignment with pyproject.toml or Dockerfile.")
56+
parser.add_argument("file_path", help="Path to the `pyproject.toml` or `Dockerfile` file to check.")
57+
parser.add_argument("--docker", action="store_true",
58+
help="Set, if checking a `Dockerfile`, otherwise `pyproject.toml` is assumed.")
4059
parser.add_argument("min_version", help="The minimum Python version.")
4160
parser.add_argument("max_version", help="The maximum Python version.")
4261
args = parser.parse_args()
4362

4463
try:
45-
main(args.pyproject_toml_path, args.min_version, args.max_version)
64+
main(args.file_path, args.docker, args.min_version, args.max_version)
4665
except InvalidVersion:
4766
print("Error: Invalid version format provided.")
4867
sys.exit(1)

0 commit comments

Comments
 (0)