-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathupdate_python_versions.py
More file actions
84 lines (65 loc) · 2.93 KB
/
update_python_versions.py
File metadata and controls
84 lines (65 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
"""
Script to update README.md and pyproject.toml with dynamically detected supported Python versions.
"""
import json
import re
def update_python_versions():
"""Update Python versions in README.md and pyproject.toml based on supported_versions.json"""
try:
with open("supported_versions.json", "r") as f:
versions_data = json.load(f)
except FileNotFoundError:
raise SystemExit("Error: supported_versions.json not found. Run supported_versions.py first.")
supported_python = versions_data.get("supported_python", [])
if not supported_python:
raise SystemExit("Error: No supported Python versions found in JSON.")
oldest_python = versions_data.get("oldest_supported_python", "")
if not oldest_python:
raise SystemExit("Error: No oldest supported Python version found in JSON.")
changes_made = []
# Update README.md
try:
with open("README.md", "r") as f:
readme_content = f.read()
except FileNotFoundError:
raise SystemExit("Error: README.md not found.")
new_python_section = []
for version in supported_python:
new_python_section.append(f"* {version}")
new_python_lines = "\n".join(new_python_section)
# Use regex to find and replace the Python versions section
pattern = r"(Supported Python versions:\n)((?:\* \d+\.\d+\n)*)"
replacement = f"\\1{new_python_lines}\n"
new_readme = re.sub(pattern, replacement, readme_content)
if new_readme != readme_content:
with open("README.md", "w") as f:
f.write(new_readme)
changes_made.append("README.md")
# Update pyproject.toml
try:
with open("pyproject.toml", "r") as f:
pyproject_content = f.read()
except FileNotFoundError:
raise SystemExit("Error: pyproject.toml not found.")
# Update ruff target-version (e.g., "py38")
py_version_compact = f"py{oldest_python.replace('.', '')}"
ruff_pattern = r'(target-version\s*=\s*["\'])py\d+(["\'])'
new_pyproject = re.sub(ruff_pattern, f"\\g<1>{py_version_compact}\\g<2>", pyproject_content)
# Update mypy python_version (e.g., "3.9")
mypy_pattern = r'(python_version\s*=\s*["\'])\d+\.\d+(["\'])'
new_pyproject = re.sub(mypy_pattern, f"\\g<1>{oldest_python}\\g<2>", new_pyproject)
if new_pyproject != pyproject_content:
with open("pyproject.toml", "w") as f:
f.write(new_pyproject)
changes_made.append("pyproject.toml")
if not changes_made:
print("ℹ️ No changes needed - Python versions are already up to date.")
return False
files_updated = " and ".join(changes_made)
print(f"✅ Updated {files_updated}")
print(f" Supported versions: {', '.join(supported_python)}")
print(f" Oldest supported Python version: {oldest_python}")
return True
update_python_versions()