Skip to content

Commit

Permalink
feat: gen-init-py with ruff. gen-init-py and check-mkdocs src-dir dep…
Browse files Browse the repository at this point in the history
…recated.
  • Loading branch information
kiyoon committed Dec 17, 2024
1 parent 5e7e514 commit e99efa3
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 215 deletions.
15 changes: 11 additions & 4 deletions actions/check-mkdocs/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ inputs:
default: deps/lock/x86_64-unknown-linux-gnu/requirements_docs.txt
src-dir:
description: Path to the source directory, for generating __init__.py files
required: true
default: src
deprecationMessage: Do not use
pyproject-toml-file:
description: Path to pyproject.toml to infer the minimum python version
default: pyproject.toml

runs:
using: 'composite'
steps:
- name: Check deprecated inputs
shell: bash
run: |
if [[ -n "${{ inputs.src-dir }}" ]]; then
echo "❌ Warning: 'src-dir' input is deprecated."
fi
- uses: deargen/workflows/actions/setup-python-and-uv@master
with:
pyproject-toml-file: ${{ inputs.pyproject-toml-file }}
Expand All @@ -42,11 +47,13 @@ runs:
if [[ "${exit_code}" -ne 0 ]]; then
# If the build failed, try to generate the __init__.py file and build again
python ${{ github.action_path }}/../../scripts/gen_init_py.py "${{ inputs.src-dir }}"
uv pip install ruff
project_dir=$(dirname "${{ inputs.pyproject-toml-file }}")
python ${{ github.action_path }}/../../scripts/gen_init_py.py "${project_dir}"
out=$(mkdocs build 2> stderr.txt)
new_exit_code=$?
if [[ "${new_exit_code}" -eq 0 ]]; then
echo "Tip: the build succeeded after generating __init__.py. Try running deargen/workflows/actions/gen-init-py." >> "$GITHUB_STEP_SUMMARY"
echo 'Tip: the build succeeded after generating __init__.py. Try running `projector gen-init-py`.' >> "$GITHUB_STEP_SUMMARY"
fi
fi
exit ${exit_code}
20 changes: 16 additions & 4 deletions actions/gen-init-py/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,29 @@ author: Kiyoon Kim
inputs:
src-dir:
description: Source directory to search for directories
required: true
default: src
deprecationMessage: Use `cwd` instead
cwd:
description: Directory with pyproject.toml

runs:
using: 'composite'
steps:
- shell: bash
run: |
python3 ${{ github.action_path }}/../../scripts/gen_init_py.py "${{ inputs.src-dir }}"
if [ -n "${{ inputs.cwd }}" ]; then
cd "${{ inputs.cwd }}"
else
if [ -n "${{ inputs.src-dir }}" ]; then
echo "❗ Action Required! src-dir is deprecated. Use cwd instead."
cd "${{ inputs.src-dir }}"
fi
fi
if [ ! command -v ruff &> /dev/null ]; then
pip install ruff
fi
python3 ${{ github.action_path }}/../../scripts/gen_init_py.py
git config user.name github-actions[bot]
git config user.email github-actions[bot]@users.noreply.github.com
git add .
git commit -m "build: generate __init__.py [skip ci]"
git commit -m "chore: generate __init__.py [skip ci]"
git push
4 changes: 4 additions & 0 deletions python-projector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
| [![uv](https://img.shields.io/badge/uv-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)](https://github.com/astral-sh/uv) | [![Actions status](https://github.com/deargen/workflows/workflows/%28not%20reusable%29%20Check%20pip%20compile%20sync/badge.svg)](https://github.com/deargen/workflows/actions) |

## 🛠️ Installation

```sh
# install with pip
pip install python-projector
Expand All @@ -16,6 +17,9 @@ pip install python-projector
uv tool install python-projector
```

> [!NOTE]
> `pip-compile` and `gen-init-py` commands depend on `uv` and `ruff`.
## 🚀 Usage

```
Expand Down
11 changes: 9 additions & 2 deletions python-projector/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ license-files = []
dependencies = {file = ["deps/requirements.in"]}

[tool.setuptools.package-data]
python_projector = ["shell/*.sh"] # CHANGE (name of the importing module name)
python_projector = ["scripts/**/*.sh", "scripts/**/*.py"] # CHANGE (name of the importing module name)

[tool.setuptools.packages.find]
where = ["src"]
Expand Down Expand Up @@ -80,7 +80,10 @@ pythonVersion = "3.11"
reportDuplicateImport = true

[tool.ruff]
namespace-packages = ["tools", "scripts", "tests"] # for INP rule, suppress on these directories
# for INP rule, suppress on these directories
namespace-packages = [
"src/python_projector/scripts",
]
extend-exclude = [
"src/python_projector/_version.py", # CHANGE
]
Expand Down Expand Up @@ -161,6 +164,10 @@ ignore = [
"__init__.py" = [
"F401", # Ignore seemingly unused imports (they're meant for re-export)
]
"src/python_projector/scripts/**/*.py" = [
"T20", # print statements
]
"[!s][!r][!c]*/**" = ["INP001"] # Implicit namespace package (no __init__.py) ignored not under src/

[tool.ruff.lint.pydocstyle]
convention = "google"
Expand Down
2 changes: 2 additions & 0 deletions python-projector/src/python_projector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
if PROJECT_DIR.name.startswith("python3."):
PROJECT_DIR = None

SCRIPTS_DIR = Path(__file__).parent / "scripts"

_env_deferred_logger = DeferredLogger()


Expand Down
28 changes: 5 additions & 23 deletions python-projector/src/python_projector/cli/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
from pathlib import Path


class InvalidConfigError(Exception):
pass


def get_min_python_version(
project_dir: str | PathLike | None = None,
):
Expand All @@ -16,18 +12,14 @@ def get_min_python_version(
import tomllib

from python_projector.utils.files import find_pyproject_toml
from python_projector.utils.toml import get_toml_value
from python_projector.utils.version import min_version_requires_python

pyproject_toml = find_pyproject_toml(project_dir)
with pyproject_toml.open("rb") as f:
pyproject = tomllib.load(f)

try:
version_range = pyproject["project"]["requires-python"]
except KeyError as e:
raise InvalidConfigError(
"Missing key project.requires-python in pyproject.toml"
) from e
version_range: str = get_toml_value(pyproject, ["project", "requires-python"])
min_version = min_version_requires_python(version_range)
return min_version

Expand Down Expand Up @@ -55,6 +47,7 @@ def get_versioneer_version(
import versioneer

from python_projector.utils.files import find_pyproject_toml
from python_projector.utils.toml import get_toml_value
from python_projector.utils.version import (
versioneer_render_chrome_ext_compat_version,
)
Expand All @@ -68,19 +61,8 @@ def get_versioneer_version(
elif isinstance(project_dir, str):
project_dir = Path(project_dir)

try:
tag_prefix = pyproject["tool"]["versioneer"]["tag_prefix"] # usually "v"
except KeyError as e:
raise InvalidConfigError(
f"Missing key tool.versioneer.tag_prefix in {pyproject_toml}"
) from e

try:
style = pyproject["tool"]["versioneer"]["style"] # pep440
except KeyError as e:
raise InvalidConfigError(
f"Missing key tool.versioneer.style in {pyproject_toml}"
) from e
tag_prefix: str = get_toml_value(pyproject, ["tool", "versioneer", "tag_prefix"])
style: str = get_toml_value(pyproject, ["tool", "versioneer", "style"])

pieces = versioneer.git_pieces_from_vcs(
tag_prefix=tag_prefix, root=project_dir, verbose=True
Expand Down
Loading

0 comments on commit e99efa3

Please sign in to comment.