Skip to content

Commit

Permalink
add gen_requirements_txt.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodong-Yang committed Oct 15, 2024
1 parent a90682f commit 2562983
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .github/tools/gen_requirements_txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This script requires python 3.12 to run.
from __future__ import annotations

import sys
import tomllib
from typing import Any


def gen_requirements_txt(pyproject_cfg: dict[str, Any]) -> str:
try:
_res: list[str] = pyproject_cfg["project"]["dependencies"]
return "\n".join(_res)
except KeyError:
return ""


if __name__ == "__main__":
script_name, *args = sys.argv
if len(args) < 2:
print(f"Usage: {script_name} <pyproject.toml> <requirements.txt>")
sys.exit(1)

pyproject_toml, requirements_txt, *_ = args
with open(pyproject_toml, "rb") as src, open(requirements_txt, "w") as dst:
_parsed = tomllib.load(src)
dst.write(gen_requirements_txt(_parsed))
2 changes: 1 addition & 1 deletion .github/workflows/gen_requirements_txt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

- name: generate requirements.txt
run: |
.github/tools/gen_requirements_txt.py \
python .github/tools/gen_requirements_txt.py \
pyproject.toml requirements.txt
- name: commit change if needed
Expand Down

0 comments on commit 2562983

Please sign in to comment.