view tasks/task_generate_doc.py @ 35:347d32030451 default tip

presentation (docker): add info on how to connect
author Goffi <goffi@goffi.org>
date Tue, 12 Dec 2023 17:01:20 +0100
parents e7c7327f9f25
children
line wrap: on
line source

#!/ur/bin/env python3

import sys
import os.path
from libervia.backend.core.i18n import _
from libervia.backend.core.log import getLogger
from libervia.backend.tools.common import regex
from libervia.web.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)
            log.info(f"generating doc for {sub_doc_path}")
            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')
            log.info(f"generating doc for {self.doc_path}")
            await self.runCommand(
                sys.executable, "-m", "sphinx", self.doc_path, build_path,
            )

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