Skip to content

Commit

Permalink
ci: add gh action for generating requirements.txt file based on pypro…
Browse files Browse the repository at this point in the history
…ject.toml (#400)

Provide requirements.txt file generated from pyproject.toml. This requirements.txt is only for reference purpose, for tools like snyk that doesn't support directly parsing pyproject.toml to read the dependencies.
  • Loading branch information
Bodong-Yang authored Oct 15, 2024
1 parent c43bfdd commit 364bcc2
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/tools/gen_requirements_txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This script requires python 3.12 to run.
from __future__ import annotations

import sys
from typing import Any

import tomllib


def gen_requirements_txt(pyproject_cfg: dict[str, Any]) -> str:
_res: list[str] = [
"# Automatically generated from pyproject.toml by gen_requirements_txt.py script.",
"# DO NOT EDIT! Only for reference use.",
]
try:
_res.extend(pyproject_cfg["project"]["dependencies"])
except KeyError:
print("WARNING: no deps are defined in pyproject.toml")
_res.append("")
return "\n".join(_res)


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))
48 changes: 48 additions & 0 deletions .github/workflows/gen_requirements_txt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: gen requirements.txt on pyproject.toml file changed

on:
pull_request:
branches:
- main
paths:
- 'pyproject.toml'
# allow manual dispatch of this workflow
workflow_dispatch:

jobs:
gen-requirements-txt:
runs-on: ubuntu-latest
timeout-minutes: 1

steps:
- name: checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
token: ${{ secrets.GITHUB_TOKEN }}

- name: setup python
uses: actions/setup-python@v4
with:
python-version: '3.12'

- name: generate requirements.txt
run: |
python .github/tools/gen_requirements_txt.py \
pyproject.toml requirements.txt
- name: commit change if needed
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
if git diff --exit-code requirements.txt; then
echo "skip commit as requirements.txt is not changed"
else
echo "detect requirements.txt updated, commit change ..."
git add requirements.txt
git commit -m "[GHA] Update requirements.txt on pyproject.toml changed"
git push
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17 changes: 17 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Automatically generated from pyproject.toml by gen_requirements_txt.py script.
# DO NOT EDIT! Only for reference use.
aiofiles<25,>=24.1
aiohttp>=3.9.5,<3.11
cryptography>=42.0.4,<44
grpcio>=1.53.2,<1.67
protobuf<4.22,>=4.21.12
pydantic<3,>=2.6
pydantic-settings<3,>=2.3
pyopenssl<25,>=24.1
pyyaml<7,>=6.0.1
requests<2.33,>=2.32
simple-sqlite3-orm<0.3,>=0.2
typing-extensions>=4.6.3
urllib3<2.3,>=2.2.2
uvicorn[standard]<0.31,>=0.30
zstandard<0.24,>=0.22

0 comments on commit 364bcc2

Please sign in to comment.