Davide Pesavento | fd1dd60 | 2024-03-13 14:42:25 -0400 | [diff] [blame] | 1 | # inspired by code by Hans-Martin von Gaudecker, 2012 |
| 2 | |
| 3 | """Support for Sphinx documentation""" |
| 4 | |
| 5 | import os |
| 6 | from waflib import Task, TaskGen |
| 7 | |
| 8 | |
| 9 | class sphinx_build(Task.Task): |
| 10 | color = 'BLUE' |
| 11 | run_str = '${SPHINX_BUILD} -q -b ${BUILDERNAME} -D ${VERSION} -D ${RELEASE} -d ${DOCTREEDIR} ${SRCDIR} ${OUTDIR}' |
| 12 | |
| 13 | def keyword(self): |
| 14 | return f'Processing ({self.env.BUILDERNAME})' |
| 15 | |
| 16 | |
| 17 | # from https://docs.python.org/3.12/whatsnew/3.12.html#imp |
| 18 | def load_source(modname, filename): |
| 19 | import importlib.util |
| 20 | from importlib.machinery import SourceFileLoader |
| 21 | loader = SourceFileLoader(modname, filename) |
| 22 | spec = importlib.util.spec_from_file_location(modname, filename, loader=loader) |
| 23 | module = importlib.util.module_from_spec(spec) |
| 24 | loader.exec_module(module) |
| 25 | return module |
| 26 | |
| 27 | |
| 28 | @TaskGen.feature('sphinx') |
| 29 | @TaskGen.before_method('process_source') |
| 30 | def process_sphinx(self): |
| 31 | """Set up the task generator with a Sphinx instance and create a task.""" |
| 32 | |
| 33 | conf = self.path.find_node(self.config) |
| 34 | if not conf: |
| 35 | self.bld.fatal(f'Sphinx configuration file {repr(self.config)} not found') |
| 36 | |
| 37 | inputs = [conf] + self.to_nodes(self.source) |
| 38 | task = self.create_task('sphinx_build', inputs, always_run=getattr(self, 'always', False)) |
| 39 | |
| 40 | confdir = conf.parent.abspath() |
| 41 | buildername = getattr(self, 'builder', 'html') |
| 42 | srcdir = getattr(self, 'srcdir', confdir) |
| 43 | outdir = self.path.find_or_declare(getattr(self, 'outdir', buildername)).get_bld() |
| 44 | doctreedir = getattr(self, 'doctreedir', os.path.join(outdir.abspath(), '.doctrees')) |
| 45 | release = getattr(self, 'release', self.version) |
| 46 | |
| 47 | task.env['BUILDERNAME'] = buildername |
| 48 | task.env['SRCDIR'] = srcdir |
| 49 | task.env['OUTDIR'] = outdir.abspath() |
| 50 | task.env['DOCTREEDIR'] = doctreedir |
| 51 | task.env['VERSION'] = f'version={self.version}' |
| 52 | task.env['RELEASE'] = f'release={release}' |
| 53 | |
| 54 | if buildername == 'man': |
| 55 | confdata = load_source('sphinx_conf', conf.abspath()) |
| 56 | for i in confdata.man_pages: |
| 57 | target = outdir.find_or_declare(f'{i[1]}.{i[4]}') |
| 58 | task.outputs.append(target) |
| 59 | if self.install_path: |
| 60 | self.bld.install_files(f'{self.install_path}/man{i[4]}/', target) |
| 61 | else: |
| 62 | task.outputs.append(outdir) |
| 63 | |
| 64 | # prevent process_source from complaining that there is no extension mapping for .rst files |
| 65 | self.source = [] |
| 66 | |
| 67 | |
| 68 | def configure(conf): |
| 69 | """Check if sphinx-build program is available.""" |
| 70 | conf.find_program('sphinx-build', var='SPHINX_BUILD', mandatory=False) |
| 71 | |
| 72 | |
| 73 | # sphinx command |
| 74 | from waflib.Build import BuildContext |
| 75 | class sphinx(BuildContext): |
| 76 | cmd = 'sphinx' |
| 77 | fun = 'sphinx' |