| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
""" |
| 3 |
Provides tasks to build documentation with sphinx, etc. |
| 4 |
""" |
| 5 |
|
| 6 |
from __future__ import absolute_import, print_function |
| 7 |
import sys |
| 8 |
from invoke import task, Collection |
| 9 |
from invoke.util import cd |
| 10 |
from path import Path |
| 11 |
|
| 12 |
# -- TASK-LIBRARY: |
| 13 |
from ._tasklet_cleanup import cleanup_tasks, cleanup_dirs |
| 14 |
|
| 15 |
|
| 16 |
# ----------------------------------------------------------------------------- |
| 17 |
# TASKS: |
| 18 |
# ----------------------------------------------------------------------------- |
| 19 |
@task |
| 20 |
def clean(ctx, dry_run=False): |
| 21 |
"""Cleanup generated document artifacts.""" |
| 22 |
basedir = ctx.sphinx.destdir or "build/docs" |
| 23 |
cleanup_dirs([basedir], dry_run=dry_run) |
| 24 |
|
| 25 |
|
| 26 |
@task(help={ |
| 27 |
"builder": "Builder to use (html, ...)", |
| 28 |
"options": "Additional options for sphinx-build", |
| 29 |
}) |
| 30 |
def build(ctx, builder="html", options=""): |
| 31 |
"""Build docs with sphinx-build""" |
| 32 |
sourcedir = ctx.config.sphinx.sourcedir |
| 33 |
destdir = Path(ctx.config.sphinx.destdir or "build")/builder |
| 34 |
destdir = destdir.abspath() |
| 35 |
with cd(sourcedir): |
| 36 |
destdir_relative = Path(".").relpathto(destdir) |
| 37 |
command = "sphinx-build {opts} -b {builder} {sourcedir} {destdir}" \ |
| 38 |
.format(builder=builder, sourcedir=".", |
| 39 |
destdir=destdir_relative, opts=options) |
| 40 |
ctx.run(command) |
| 41 |
|
| 42 |
@task(help={ |
| 43 |
"builder": "Builder to use (html, ...)", |
| 44 |
"options": "Additional options for sphinx-build", |
| 45 |
}) |
| 46 |
def rebuild(ctx, builder="html", options=""): |
| 47 |
"""Rebuilds the docs. |
| 48 |
Perform the steps: clean, build |
| 49 |
""" |
| 50 |
clean(ctx) |
| 51 |
build(ctx, builder=builder, options=options) |
| 52 |
|
| 53 |
@task |
| 54 |
def linkcheck(ctx): |
| 55 |
"""Check if all links are corect.""" |
| 56 |
build(ctx, builder="linkcheck") |
| 57 |
|
| 58 |
|
| 59 |
@task |
| 60 |
def browse(ctx): |
| 61 |
"""Open documentation in web browser.""" |
| 62 |
page_html = Path(ctx.config.sphinx.destdir)/"html"/"index.html" |
| 63 |
if not page_html.exists(): |
| 64 |
build(ctx, builder="html") |
| 65 |
assert page_html.exists() |
| 66 |
open_cmd = "open" # -- WORKS ON: MACOSX |
| 67 |
if sys.platform.startswith("win"): |
| 68 |
open_cmd = "start" |
| 69 |
ctx.run("{open} {page_html}".format(open=open_cmd, page_html=page_html)) |
| 70 |
# ctx.run('python -m webbrowser -t {page_html}'.format(page_html=page_html)) |
| 71 |
# -- DISABLED: |
| 72 |
# import webbrowser |
| 73 |
# print("Starting webbrowser with page=%s" % page_html) |
| 74 |
# webbrowser.open(str(page_html)) |
| 75 |
|
| 76 |
|
| 77 |
@task(help={"dest": "Destination directory to save docs"}) |
| 78 |
# pylint: disable=redefined-builtin |
| 79 |
def save(ctx, dest="docs.html", format="html"): |
| 80 |
"""Save/update docs under destination directory.""" |
| 81 |
print("STEP: Generate docs in HTML format") |
| 82 |
build(ctx, builder=format) |
| 83 |
|
| 84 |
print("STEP: Save docs under %s/" % dest) |
| 85 |
source_dir = Path(ctx.config.sphinx.destdir)/format |
| 86 |
Path(dest).rmtree_p() |
| 87 |
source_dir.copytree(dest) |
| 88 |
|
| 89 |
# -- POST-PROCESSING: Polish up. |
| 90 |
for part in [".buildinfo", ".doctrees"]: |
| 91 |
partpath = Path(dest)/part |
| 92 |
if partpath.isdir(): |
| 93 |
partpath.rmtree_p() |
| 94 |
elif partpath.exists(): |
| 95 |
partpath.remove_p() |
| 96 |
|
| 97 |
|
| 98 |
# ----------------------------------------------------------------------------- |
| 99 |
# TASK CONFIGURATION: |
| 100 |
# ----------------------------------------------------------------------------- |
| 101 |
namespace = Collection(clean, rebuild, linkcheck, browse, save) |
| 102 |
namespace.add_task(build, default=True) |
| 103 |
namespace.configure({ |
| 104 |
"sphinx": { |
| 105 |
"sourcedir": "docs", |
| 106 |
"destdir": "build/docs" |
| 107 |
} |
| 108 |
}) |
| 109 |
|
| 110 |
# -- ADD CLEANUP TASK: |
| 111 |
cleanup_tasks.add_task(clean, "clean_docs") |
| 112 |
cleanup_tasks.configure(namespace.configuration()) |