Skip to content

Commit 1f72b28

Browse files
authored
Merge pull request #255 from joe733/workshop
feat: add build for pypi workflow
2 parents 7c61275 + e612c6e commit 1f72b28

File tree

4 files changed

+75
-13
lines changed

4 files changed

+75
-13
lines changed

.github/workflows/build.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This workflow will upload a Python Package using Twine when a release is created
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
3+
4+
name: Build for PyPI
5+
6+
on:
7+
release:
8+
types: [published]
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
build_and_publish:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
# checkout repository
19+
- uses: actions/checkout@v3
20+
# setup lowest supported python version
21+
- name: Setup Python
22+
uses: actions/setup-python@v4
23+
with:
24+
python-version: "3.8"
25+
# install & configure poetry
26+
- name: Install Poetry
27+
uses: snok/install-poetry@v1
28+
with:
29+
version: 1.4.1
30+
virtualenvs-create: true
31+
virtualenvs-in-project: true
32+
# install dependencies
33+
- name: Install dependencies
34+
run: poetry install --no-interaction --no-ansi --only docs
35+
# build package
36+
- name: Build package
37+
run: python build.py
38+
# publish package
39+
- name: Publish to PyPI
40+
uses: pypa/gh-action-pypi-publish@release/v1
41+
with:
42+
password: ${{ secrets.PYPI_API_TOKEN }}

build.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Remove Refs."""
2+
3+
# standard
4+
from subprocess import run
5+
from shutil import rmtree
6+
from pathlib import Path
7+
8+
# local
9+
from docs.gen_docs import generate_documentation
10+
11+
if __name__ == "__main__":
12+
project_dir = Path(__file__).parent
13+
generate_documentation(project_dir, discard_refs=False)
14+
process = run(("poetry", "build"), capture_output=True)
15+
print(process.stderr.decode() + process.stdout.decode())
16+
rmtree(project_dir / "docs/reference", ignore_errors=True)

docs/gen_docs.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
from os.path import getsize
99
from subprocess import run
1010
from pathlib import Path
11-
from sys import argv
1211

1312
# external
1413
from yaml import safe_load, safe_dump
1514

15+
__all__ = ("generate_documentation",)
16+
1617

1718
def _write_ref_content(source: Path, module_name: str, func_name: str):
1819
"""Write content."""
@@ -23,7 +24,7 @@ def _write_ref_content(source: Path, module_name: str, func_name: str):
2324
)
2425

2526

26-
def generate_reference(source: Path, destination: Path):
27+
def _generate_reference(source: Path, destination: Path):
2728
"""Generate reference."""
2829
nav_items: Dict[str, List[str]] = {"Code Reference": []}
2930
# clean destination
@@ -43,7 +44,7 @@ def generate_reference(source: Path, destination: Path):
4344
return nav_items
4445

4546

46-
def update_mkdocs_config(source: Path, destination: Path, nav_items: Dict[str, List[str]]):
47+
def _update_mkdocs_config(source: Path, destination: Path, nav_items: Dict[str, List[str]]):
4748
"""Temporary update to mkdocs config."""
4849
copy(source, destination)
4950
with open(source, "rt") as mkf:
@@ -53,25 +54,27 @@ def update_mkdocs_config(source: Path, destination: Path, nav_items: Dict[str, L
5354
safe_dump(mkdocs_conf, mkf, sort_keys=False)
5455

5556

56-
def generate_documentation(source: Path):
57+
def generate_documentation(source: Path, discard_refs: bool = True):
5758
"""Generate documentation."""
5859
# copy readme as docs index file
5960
copy(source / "README.md", source / "docs/index.md")
6061
# generate reference documentation
61-
nav_items = generate_reference(source / "validators/__init__.py", source / "docs/reference")
62+
nav_items = _generate_reference(source / "validators/__init__.py", source / "docs/reference")
6263
# backup mkdocs config
63-
update_mkdocs_config(source / "mkdocs.yml", source / "mkdocs.bak.yml", nav_items)
64+
_update_mkdocs_config(source / "mkdocs.yml", source / "mkdocs.bak.yml", nav_items)
6465
# build docs as subprocess
6566
print(run(("mkdocs", "build"), capture_output=True).stderr.decode())
6667
# restore mkdocs config
6768
move(str(source / "mkdocs.bak.yml"), source / "mkdocs.yml")
69+
# optionally discard reference folder
70+
if discard_refs:
71+
rmtree(source / "docs/reference")
6872

6973

7074
if __name__ == "__main__":
71-
project_dir = Path(__file__).parent.parent
72-
generate_documentation(project_dir)
73-
# use this option before building package
74-
# with `poetry build` to include refs
75-
if len(argv) > 1 and argv[1] == "--keep":
76-
quit()
77-
rmtree(project_dir / "docs/reference")
75+
project_root = Path(__file__).parent.parent
76+
generate_documentation(project_root)
77+
# NOTE: use following lines only for testing/debugging
78+
# generate_documentation(project_root, discard_refs=False)
79+
# from sys import argv
80+
# generate_documentation(project_root, len(argv) > 1 and argv[1] == "--keep")

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ exclude_dirs = [".github", ".pytest_cache", ".tox", ".vscode", "site", "tests"]
7171
ensure_newline_before_comments = true
7272
force_grid_wrap = 0
7373
force_sort_within_sections = true
74+
import_heading_firstparty = "local"
7475
import_heading_localfolder = "local"
7576
import_heading_stdlib = "standard"
7677
import_heading_thirdparty = "external"

0 commit comments

Comments
 (0)