From 2562983c76f73e776b1cfea9ec51f1c3117600ce Mon Sep 17 00:00:00 2001 From: "bodong.yang" Date: Tue, 15 Oct 2024 07:16:11 +0000 Subject: [PATCH] add gen_requirements_txt.py --- .github/tools/gen_requirements_txt.py | 26 +++++++++++++++++++++ .github/workflows/gen_requirements_txt.yaml | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 .github/tools/gen_requirements_txt.py diff --git a/.github/tools/gen_requirements_txt.py b/.github/tools/gen_requirements_txt.py new file mode 100644 index 000000000..2342b94f9 --- /dev/null +++ b/.github/tools/gen_requirements_txt.py @@ -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} ") + 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)) diff --git a/.github/workflows/gen_requirements_txt.yaml b/.github/workflows/gen_requirements_txt.yaml index a8f016d34..827b8ddab 100644 --- a/.github/workflows/gen_requirements_txt.yaml +++ b/.github/workflows/gen_requirements_txt.yaml @@ -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