|
| 1 | +"""Pin dependencies to the oldest compatible version for testing.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import tomlkit |
| 9 | +from tomlkit.items import AbstractTable, Array |
| 10 | + |
| 11 | + |
| 12 | +def main(args: list[str]) -> int | str | None: |
| 13 | + """Pin dependencies to the oldest compatible version for testing.""" |
| 14 | + pyproject_path = Path(args.pop()) |
| 15 | + if args: |
| 16 | + return f"Unsupported arguments: {args!r}" |
| 17 | + pyproject = tomlkit.loads(pyproject_path.read_text()) |
| 18 | + poetry_deps = pyproject["tool"]["poetry"]["dependencies"] # type: ignore[index] |
| 19 | + assert isinstance(poetry_deps, AbstractTable) |
| 20 | + |
| 21 | + for dep, value in poetry_deps.items(): |
| 22 | + if dep == "python": |
| 23 | + continue |
| 24 | + if isinstance(value, str) and ( |
| 25 | + value.startswith("^") or value.startswith("~") or value.startswith(">=") |
| 26 | + ): |
| 27 | + poetry_deps[dep] = "==" + value.lstrip("^~>=") |
| 28 | + elif isinstance(value, Array): |
| 29 | + for constraint in value: |
| 30 | + if "version" in constraint and ( |
| 31 | + constraint["version"].startswith("^") |
| 32 | + or constraint["version"].startswith("~") |
| 33 | + or constraint["version"].startswith(">=") |
| 34 | + ): |
| 35 | + constraint["version"] = "==" + constraint["version"].lstrip("^~>=") |
| 36 | + |
| 37 | + pyproject_path.write_text(tomlkit.dumps(pyproject)) |
| 38 | + print("Updated pyproject.toml with pinned dependencies.") |
| 39 | + return None |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + sys.exit(main(sys.argv[1:])) |
0 commit comments