Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pin_requirements.py for release #405

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions pin_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import re
import subprocess
import toml

pyproject_file = "pyproject.toml"

pip_output = subprocess.run(["pip", "list", "--format=freeze"], capture_output=True, text=True).stdout

pip_versions = {}
for line in pip_output.splitlines():
parts = line.split("==")
if len(parts) == 2:
package, version = parts
pip_versions[package.lower()] = version

with open(pyproject_file, "r") as f:
pyproject_raw = f.read()

match = re.search(r"dependencies\s*=\s*\[(.*?)\]", pyproject_raw, re.DOTALL)
if match:
deps_raw = match.group(1)
deps = [d.strip().strip('"') for d in deps_raw.split(",")]

updated_deps = []
for dep in deps:
match = re.match(r"([a-zA-Z0-9_-]+)([<>=!].*)?", dep)
if match:
pkg_name = match.group(1).lower()
if pkg_name in pip_versions:
updated_deps.append(f'"{pkg_name}=={pip_versions[pkg_name]}"')
else:
updated_deps.append(f'"{dep}"') # Keep as is if not found in pip list

updated_deps_str = ",\n ".join(updated_deps)
pyproject_raw = re.sub(r"dependencies\s*=\s*\[.*?\]", f"dependencies = [\n {updated_deps_str}\n]", pyproject_raw, flags=re.DOTALL)

with open(pyproject_file, "w") as f:
f.write(pyproject_raw)