docs.py 1.96 KB
Newer Older
1 2 3 4
"""
Open edX Documentation Builder
Ties into Sphinx to generate files at the specified location(s)
"""
5
from __future__ import print_function
6

Will Daly committed
7
import sys
8 9

from paver.easy import cmdopts, needs, sh, task
Will Daly committed
10

11 12
from .utils.timer import timed

Will Daly committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
DOC_PATHS = {
    "dev": "docs/en_us/developers",
    "author": "docs/en_us/course_authors",
    "data": "docs/en_us/data",
    "default": "docs/en_us"
}


def valid_doc_types():
    """
    Return a comma-separated string of valid doc types.
    """
    return ", ".join(DOC_PATHS.keys())


def doc_path(options, allow_default=True):
    """
    Parse `options` (from the Paver task args) to determine the path
    to the documentation directory.
    If the specified path is not one of the valid options, print an error
    message and exit.

    If `allow_default` is False, then require that a type is specified,
    and exit with an error message if it isn't.
    """
    doc_type = getattr(options, 'type', 'default')
    path = DOC_PATHS.get(doc_type)

    if doc_type == 'default' and not allow_default:
42 43 44 45 46 47
        print(
            "You must specify a documentation type using '--type'.  "
            "Valid options are: {options}".format(
                options=valid_doc_types()
            )
        )
Will Daly committed
48 49 50
        sys.exit(1)

    if path is None:
51 52 53 54 55 56
        print(
            "Invalid documentation type '{doc_type}'.  "
            "Valid options are: {options}".format(
                doc_type=doc_type, options=valid_doc_types()
            )
        )
Will Daly committed
57 58 59 60 61 62 63 64 65 66 67 68
        sys.exit(1)

    else:
        return path


@task
@needs('pavelib.prereqs.install_prereqs')
@cmdopts([
    ("type=", "t", "Type of docs to compile"),
    ("verbose", "v", "Display verbose output"),
])
69
@timed
Will Daly committed
70 71 72 73 74 75 76 77 78 79 80 81
def build_docs(options):
    """
    Invoke sphinx 'make build' to generate docs.
    """
    verbose = getattr(options, 'verbose', False)

    cmd = "cd {dir}; make html quiet={quiet}".format(
        dir=doc_path(options),
        quiet="false" if verbose else "true"
    )

    sh(cmd)