|
| 1 | +# This script updates the requirements file to match with version constraints in pyproject.yml |
| 2 | +# and also removes subdependencies keeping only the main dependencies |
| 3 | + |
| 4 | +import toml |
| 5 | +import re |
| 6 | + |
| 7 | +# Load pyproject.toml |
| 8 | +with open("pyproject.toml", "r") as f: |
| 9 | + pyproject = toml.load(f) |
| 10 | + |
| 11 | +# Extract dependencies with their correct version constraints |
| 12 | +main_deps = pyproject["tool"]["poetry"]["dependencies"] |
| 13 | +main_deps.pop("python", None) # Remove Python entry |
| 14 | + |
| 15 | +# Read exported requirements.txt |
| 16 | +with open("requirements.txt", "r") as f: |
| 17 | + exported_reqs = f.readlines() |
| 18 | + |
| 19 | +main_reqs = [ |
| 20 | + line |
| 21 | + for line in exported_reqs |
| 22 | + if any(line.split("=")[0] == dep for dep in main_deps.keys()) |
| 23 | +] |
| 24 | + |
| 25 | + |
| 26 | +# Function to replace package version while keeping extras/markers |
| 27 | +def replace_version(line): |
| 28 | + match = re.match(r"^([a-zA-Z0-9\-_]+)(\[.*\])?([=><~!]+[^\s;]+)(.*)", line) |
| 29 | + if match: |
| 30 | + pkg_name, extras, constraint, rest = match.groups() |
| 31 | + pkg_name_lower = pkg_name.lower() |
| 32 | + |
| 33 | + if pkg_name_lower in main_deps: |
| 34 | + return f"{pkg_name}{extras or ''}{main_deps[pkg_name_lower]}{rest}\n" |
| 35 | + |
| 36 | + return line |
| 37 | + |
| 38 | + |
| 39 | +# Process and replace package versions |
| 40 | +updated_reqs = [replace_version(line) for line in main_reqs] |
| 41 | + |
| 42 | +# Write cleaned requirements.txt |
| 43 | +with open("requirements.txt", "w") as f: |
| 44 | + f.writelines(updated_reqs) |
0 commit comments