view tasks/task_generate_doc.py @ 6:9ce41ef66dfa

python 3 port
author Goffi <goffi@goffi.org>
date Sat, 05 Oct 2019 01:26:51 +0200
parents 09d66acc7c73
children b5fc67c97c50
line wrap: on
line source

#!/ur/bin/env python3

import os.path
from twisted.internet import defer
from sat.core.i18n import _
from sat.core.log import getLogger
from sat.tools.common import regex
log = getLogger(__name__)

WATCH_DIRS = []
DOC_DIRS_DEFAULT = ('doc', 'docs')


def prepare(self):
    to_watch = set()
    doc_path = self.getConfig("doc_path")
    if doc_path is not None:
        to_watch.add(doc_path)
    sub_docs = self.getConfig("sub_docs_dict", value_type="path")
    if sub_docs is not None:
        for d in list(sub_docs.values()):
            to_watch.add(d)
    global WATCH_DIRS
    WATCH_DIRS = list(to_watch)


@defer.inlineCallbacks
def start(self):
    # root documentation
    doc_path = self.getConfig("doc_path")
    # sub docs will be generated before the root documentation
    sub_docs = self.getConfig("sub_docs_dict", value_type="path")

    if doc_path is None:
        # we check if there is documentation inside the site
        for dirname in DOC_DIRS_DEFAULT:
            path = os.path.join(self.site_path, dirname)
            conf_file = os.path.join(path, 'conf.py')
            if os.path.isdir(path) and os.path.exists(conf_file):
                doc_path = path
                break

    if doc_path is None and sub_docs is None:
        log.info("No documentation found for {site_name}, skipping".format(
            site_name = self.site_name))
        return

    sphinx = self.findCommand('sphinx-build')

    # we first generate the sub documentations
    for name, sub_doc_path in list(sub_docs.items()):
        sub_dir = regex.pathEscape(name or '')
        build_path = os.path.join(self.build_path, 'doc', sub_dir)
        yield self.runCommand(sphinx, sub_doc_path, build_path)

    # then the root one
    if doc_path is not None:
        build_path = os.path.join(self.build_path, 'doc')
        yield self.runCommand(sphinx, doc_path, build_path)

    log.info(_("documentation has been generated"))