Skip to content

Commit 75cda81

Browse files
authored
Merge pull request #263 from joe733/workshop
fix: improves build process
2 parents 668f9f4 + 079280a commit 75cda81

File tree

5 files changed

+53
-28
lines changed

5 files changed

+53
-28
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ jobs:
3333
virtualenvs-in-project: true
3434
# install dependencies
3535
- name: Install dependencies
36-
run: poetry install --no-interaction --no-ansi --only docs
36+
run: poetry install --no-interaction --no-ansi --only sphinx
3737
# build package
3838
- name: Build package
3939
run: |
4040
source .venv/bin/activate
41-
python build.py
41+
python build_pkg.py
4242
# publish package
4343
- name: Publish to PyPI
4444
uses: pypa/gh-action-pypi-publish@release/v1

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ instance/
7272
docs/reference/
7373
docs/_build/
7474
docs/*.rst
75+
docs/*.1
7576

7677
# PyBuilder
7778
.pybuilder/

build.py renamed to build_pkg.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
# standard
44
from subprocess import run
5-
from shutil import rmtree
5+
6+
# from shutil import rmtree
67
from pathlib import Path
78

89
# local
910
from docs.gen_docs import generate_documentation
1011

1112
if __name__ == "__main__":
1213
project_dir = Path(__file__).parent
13-
generate_documentation(project_dir, only_md=True, discard_refs=False)
14+
generate_documentation(project_dir, only_rst_man=True)
15+
print()
1416
process = run(("poetry", "build"), capture_output=True)
1517
print(process.stderr.decode() + process.stdout.decode())
16-
rmtree(project_dir / "docs/reference", ignore_errors=True)

docs/gen_docs.py

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
from os.path import getsize
99
from subprocess import run
1010
from pathlib import Path
11-
from sys import argv
12-
13-
# external
14-
from yaml import safe_load, safe_dump
1511

1612
__all__ = ("generate_documentation",)
1713

@@ -56,6 +52,9 @@ def _generate_reference(source: Path, destination: Path, ext: str):
5652

5753
def _update_mkdocs_config(source: Path, destination: Path, nav_items: Dict[str, List[str]]):
5854
"""Temporary update to mkdocs config."""
55+
# external
56+
from yaml import safe_load, safe_dump
57+
5958
copy(source, destination)
6059
with open(source, "rt") as mkf:
6160
mkdocs_conf = safe_load(mkf)
@@ -70,17 +69,18 @@ def _gen_md_docs(source: Path, refs_path: Path):
7069
# backup mkdocs config
7170
_update_mkdocs_config(source / "mkdocs.yaml", source / "mkdocs.bak.yaml", nav_items)
7271
# build mkdocs as subprocess
73-
print(run(("mkdocs", "build"), capture_output=True).stderr.decode())
72+
mkdocs_build = run(("mkdocs", "build"), capture_output=True)
73+
print(mkdocs_build.stderr.decode() + mkdocs_build.stdout.decode())
7474
# restore mkdocs config
7575
move(str(source / "mkdocs.bak.yaml"), source / "mkdocs.yaml")
76+
return mkdocs_build.returncode
7677

7778

78-
def _gen_rst_docs(source: Path, refs_path: Path):
79+
def _gen_rst_docs(source: Path, refs_path: Path, only_web: bool = False, only_man: bool = False):
7980
"""Generate reStructuredText docs."""
8081
# external
8182
from pypandoc import convert_file # type: ignore
8283

83-
# generate index.rst
8484
with open(source / "docs/index.rst", "wt") as idx_f:
8585
idx_f.write(
8686
convert_file(source_file=source / "docs/index.md", format="md", to="rst")
@@ -93,19 +93,33 @@ def _gen_rst_docs(source: Path, refs_path: Path):
9393
)
9494
# generate RST reference documentation
9595
_generate_reference(source / "validators/__init__.py", refs_path, "rst")
96-
# build sphinx web pages as subprocess
97-
web_build = run(("sphinx-build", "docs", "docs/_build/web"), capture_output=True)
98-
print(web_build.stderr.decode(), "\n", web_build.stdout.decode(), sep="")
99-
# build sphinx man pages as subprocess
100-
man_build = run(("sphinx-build", "-b", "man", "docs", "docs/_build/man"), capture_output=True)
101-
print(man_build.stderr.decode(), "\n", man_build.stdout.decode(), sep="")
96+
rc = 0
97+
if not only_man:
98+
# build sphinx web pages as subprocess
99+
web_build = run(("sphinx-build", "docs", "docs/_build/web"), capture_output=True)
100+
print(web_build.stderr.decode() + web_build.stdout.decode())
101+
rc = web_build.returncode
102+
if not only_web:
103+
# build sphinx man pages as subprocess
104+
man_build = run(
105+
("sphinx-build", "-b", "man", "docs", "docs/_build/man"), capture_output=True
106+
)
107+
print(man_build.stderr.decode() + man_build.stdout.decode())
108+
copy(source / "docs/_build/man/validators.1", source / "docs/validators.1")
109+
print(f"Man page copied to: {source / 'docs/validators.1'}")
110+
rc = man_build.returncode if rc == 0 else rc
111+
return rc
102112

103113

104114
def generate_documentation(
105-
source: Path, only_md: bool = False, only_rst: bool = False, discard_refs: bool = True
115+
source: Path,
116+
only_md: bool = False,
117+
only_rst_web: bool = False,
118+
only_rst_man: bool = False,
119+
discard_refs: bool = True,
106120
):
107121
"""Generate documentation."""
108-
if only_md and only_rst:
122+
if only_md and only_rst_web and only_rst_man:
109123
return
110124
# copy readme as docs index file
111125
copy(source / "README.md", source / "docs/index.md")
@@ -114,21 +128,30 @@ def generate_documentation(
114128
if refs_path.exists() and refs_path.is_dir():
115129
rmtree(refs_path)
116130
refs_path.mkdir(exist_ok=True)
117-
# documentation for each kind
118-
if not only_rst:
119-
_gen_md_docs(source, refs_path)
131+
rc = 0 if (only_rst_web or only_rst_man) else _gen_md_docs(source, refs_path)
120132
if not only_md:
121-
_gen_rst_docs(source, refs_path)
133+
rc = _gen_rst_docs(source, refs_path, only_rst_web, only_rst_man) if rc == 0 else rc
122134
# optionally discard reference folder
123135
if discard_refs:
124136
rmtree(source / "docs/reference")
137+
return rc
125138

126139

127140
if __name__ == "__main__":
128141
project_root = Path(__file__).parent.parent
129-
generate_documentation(
142+
143+
# # standard
144+
# from sys import argv
145+
146+
rc = generate_documentation(
130147
project_root,
131148
only_md=True,
132-
only_rst=False,
133-
discard_refs=len(argv) <= 1 or argv[1] != "--keep",
149+
only_rst_web=False,
150+
only_rst_man=False,
151+
# # NOTE: use
152+
# discard_refs=len(argv) <= 1 or argv[1] != "--keep",
153+
# # instead of
154+
discard_refs=True,
155+
# # for debugging
134156
)
157+
quit(rc)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ classifiers = [
2222
"Programming Language :: Python :: Implementation :: CPython",
2323
"Topic :: Software Development :: Libraries :: Python Modules",
2424
]
25-
include = ["CHANGES.md", "docs/*", "docs/reference/*", "validators/py.typed"]
25+
include = ["CHANGES.md", "docs/*", "docs/validators.1", "validators/py.typed"]
2626

2727
[tool.poetry.dependencies]
2828
python = "^3.8"

0 commit comments

Comments
 (0)