Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post_build.py file for handling moving versioned docs automatically. #80

Merged
merged 1 commit into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
SPHINXBUILD = python3 -m sphinx
PAPER =
BUILDDIR = _build/_tmp

Expand Down Expand Up @@ -37,7 +37,8 @@ clean:
-rm -rf `dirname $(BUILDDIR)`/html/*/ $(BUILDDIR)

html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)
python3 post_build.py

dirhtml:
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
Expand Down
47 changes: 2 additions & 45 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

import datetime as dt
import glob
import shutil
from pathlib import Path

from packaging.version import parse as parse_version
from post_build import determine_versions

import data_morph

Expand All @@ -20,32 +17,7 @@
copyright = f'2023{f"-{current_year}" if current_year != 2023 else ""}, Stefanie Molin'
author = 'Stefanie Molin'
release = data_morph.__version__

# information on where temporary and permanent files will go
build_html_dir = Path('_build') / 'html'
tmp_build = Path('_build') / '_tmp' / 'html'

# for determining stable/dev/etc.
last_minor_release = sorted(
[
parse_version(Path(dir).name)
for dir in glob.glob(f'{build_html_dir}/[0-9].[0-9]/')
]
or [parse_version('0.0')]
)[-1]
docs_version = parse_version(release)
docs_version_group = parse_version(f'{docs_version.major}.{docs_version.minor}')

if docs_version.is_devrelease:
version_match = 'dev'
elif docs_version_group >= last_minor_release:
version_match = 'stable'
else:
version_match = f'{docs_version.major}.{docs_version.minor}'

# clean up the old version
if (old_build := build_html_dir / version_match).exists():
shutil.rmtree(old_build)
version_match, _ = determine_versions()

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down Expand Up @@ -159,21 +131,6 @@ def docstring_strip(app, what, name, obj, options, lines):
lines[0] = ' '.join(extended_summary)


def stable_version_sync(app, exception):
# copy the build to a permanent folder (can't delete here bc it raises an exception)
build = build_html_dir / version_match
shutil.copytree(tmp_build, build, dirs_exist_ok=True)

if version_match == 'stable':
shutil.copytree(
build,
build_html_dir / str(docs_version_group),
dirs_exist_ok=True,
)
print(f'Build finished. The HTML pages are in {build}.')


def setup(app):
app.connect('autodoc-skip-member', skip)
app.connect('autodoc-process-docstring', docstring_strip)
app.connect('build-finished', stable_version_sync)
53 changes: 53 additions & 0 deletions docs/post_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Maintains the stable docs on the latest version; groups others together."""

import glob
import shutil
from pathlib import Path

from packaging.version import parse as parse_version

import data_morph

# information on where temporary and permanent files will go
build_html_dir = Path('_build') / 'html'
tmp_build = Path('_build') / '_tmp'


def determine_versions():
"""Determine stable/dev/etc. and docs version number."""
last_minor_release = sorted(
[
parse_version(Path(dir).name)
for dir in glob.glob(f'{build_html_dir}/[0-9].[0-9]/')
]
or [parse_version('0.0')]
)[-1]
docs_version = parse_version(data_morph.__version__)
docs_version_group = parse_version(f'{docs_version.major}.{docs_version.minor}')

if docs_version.is_devrelease:
version_match = 'dev'
elif docs_version_group >= last_minor_release:
version_match = 'stable'
else:
version_match = f'{docs_version.major}.{docs_version.minor}'
return version_match, docs_version_group


if __name__ == '__main__':
version_match, docs_version_group = determine_versions()

# clean up the old version
if (old_build := build_html_dir / version_match).exists():
shutil.rmtree(old_build)

build = build_html_dir / version_match
shutil.move(tmp_build, build)

if version_match == 'stable':
shutil.copytree(
build,
build_html_dir / str(docs_version_group),
dirs_exist_ok=True,
)
print(f'Build finished. The HTML pages are in {build}.')