view tasks/task_generate_doc.py @ 22:5fd933e238bb

massive refactoring from camelCase -> snake_case. See backend commit log for more details
author Goffi <goffi@goffi.org>
date Mon, 22 May 2023 09:11:54 +0200
parents a24d362796be
children e4002775d750
line wrap: on
line source

#!/ur/bin/env python3

import sys
import os.path
from sat.core.i18n import _
from sat.core.log import getLogger
from sat.tools.common import regex
from libervia.server.tasks import task


log = getLogger(__name__)


class Task(task.Task):
    DOC_DIRS_DEFAULT = ('doc', 'docs')

    def prepare(self):
        to_watch = set()
        # root documentation
        self.doc_path = self.config_get("doc_path")
        if self.doc_path is not None:
            to_watch.add(self.doc_path)
        # sub docs will be generated before the root documentation
        self.sub_docs = self.config_get("sub_docs_dict", value_type="path")
        if self.sub_docs is not None:
            for d in list(self.sub_docs.values()):
                to_watch.add(d)
        self.WATCH_DIRS = list(to_watch)

    async def start(self):
        if self.doc_path is None:
            # we check if there is documentation inside the site
            for dirname in self.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):
                    self.doc_path = path
                    break

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

        # we first generate the sub documentations
        for name, sub_doc_path in list(self.sub_docs.items()):
            sub_dir = regex.path_escape(name or '')
            build_path = os.path.join(self.build_path, 'doc', sub_dir)
            await self.runCommand(
                sys.executable, "-m", "sphinx", sub_doc_path, build_path,
            )

        # then the root one
        if self.doc_path is not None:
            build_path = os.path.join(self.build_path, 'doc')
            await self.runCommand(
                sys.executable, "-m", "sphinx", self.doc_path, build_path,
            )

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