|
1 | 1 | """ |
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. |
4 | 4 | """ |
5 | 5 | import argparse |
6 | 6 | import re |
7 | 7 | import sys |
8 | 8 |
|
9 | 9 | from packaging.version import InvalidVersion, Version |
10 | 10 |
|
| 11 | +def get_version_pyproject(file_path: str) -> str: |
| 12 | + with open(file_path, "r") as f: |
| 13 | + pyproject_content = f.read() |
11 | 14 |
|
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: |
13 | 34 | # Load and check `requires-python` version from `pyproject.toml` |
14 | 35 | 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) |
22 | 40 |
|
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}) " |
26 | 43 | f"is smaller than `min_version` ({min_version}).") |
27 | 44 | sys.exit(1) |
28 | 45 |
|
29 | 46 | except FileNotFoundError: |
30 | | - print(f"Error: File not found: `{pyproject_toml_path}`.") |
| 47 | + print(f"Error: File not found: `{file_path}`.") |
31 | 48 | sys.exit(1) |
32 | 49 |
|
33 | | - print(f"Success: Version in pyproject.toml `requires-python` (>={pyproject_version}) " |
| 50 | + print(f"Success: Version in `{file_path}` ({used_version}) " |
34 | 51 | f"matches expected versions ([{min_version} to {max_version}]).") |
35 | 52 |
|
36 | 53 |
|
37 | 54 | 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.") |
40 | 59 | parser.add_argument("min_version", help="The minimum Python version.") |
41 | 60 | parser.add_argument("max_version", help="The maximum Python version.") |
42 | 61 | args = parser.parse_args() |
43 | 62 |
|
44 | 63 | 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) |
46 | 65 | except InvalidVersion: |
47 | 66 | print("Error: Invalid version format provided.") |
48 | 67 | sys.exit(1) |
0 commit comments