Alexander Afanasyev | 49272f7 | 2014-04-06 21:49:46 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # encoding: utf-8 |
Beichuan Zhang | 55b8ed4 | 2014-04-26 22:25:44 -0700 | [diff] [blame] | 3 | |
| 4 | # inspired by code by Hans-Martin von Gaudecker, 2012 |
Alexander Afanasyev | 49272f7 | 2014-04-06 21:49:46 -0700 | [diff] [blame] | 5 | |
| 6 | import os |
| 7 | from waflib import Node, Task, TaskGen, Errors, Logs, Build, Utils |
| 8 | |
| 9 | class sphinx_build(Task.Task): |
| 10 | color = 'BLUE' |
Beichuan Zhang | 55b8ed4 | 2014-04-26 22:25:44 -0700 | [diff] [blame] | 11 | run_str = '${SPHINX_BUILD} -D ${VERSION} -D ${RELEASE} -q -b ${BUILDERNAME} -d ${DOCTREEDIR} ${SRCDIR} ${OUTDIR}' |
Alexander Afanasyev | 49272f7 | 2014-04-06 21:49:46 -0700 | [diff] [blame] | 12 | |
| 13 | def __str__(self): |
| 14 | env = self.env |
Alexander Afanasyev | 58ea458 | 2014-05-30 08:38:56 +0300 | [diff] [blame] | 15 | src_str = ' '.join([a.path_from(a.ctx.launch_node()) for a in self.inputs]) |
| 16 | tgt_str = ' '.join([a.path_from(a.ctx.launch_node()) for a in self.outputs]) |
Alexander Afanasyev | 49272f7 | 2014-04-06 21:49:46 -0700 | [diff] [blame] | 17 | if self.outputs: sep = ' -> ' |
| 18 | else: sep = '' |
| 19 | return'%s [%s]: %s%s%s\n'%(self.__class__.__name__.replace('_task',''), |
| 20 | self.env['BUILDERNAME'], src_str, sep, tgt_str) |
| 21 | |
| 22 | @TaskGen.extension('.py', '.rst') |
| 23 | def sig_hook(self, node): |
| 24 | node.sig=Utils.h_file(node.abspath()) |
| 25 | |
| 26 | @TaskGen.feature("sphinx") |
| 27 | @TaskGen.before_method("process_source") |
| 28 | def apply_sphinx(self): |
| 29 | """Set up the task generator with a Sphinx instance and create a task.""" |
| 30 | |
| 31 | inputs = [] |
| 32 | for i in Utils.to_list(self.source): |
| 33 | if not isinstance(i, Node.Node): |
| 34 | node = self.path.find_node(node) |
| 35 | else: |
| 36 | node = i |
| 37 | if not node: |
| 38 | raise ValueError('[%s] file not found' % i) |
| 39 | inputs.append(node) |
| 40 | |
| 41 | task = self.create_task('sphinx_build', inputs) |
| 42 | |
| 43 | conf = self.path.find_node(self.config) |
| 44 | task.inputs.append(conf) |
| 45 | |
| 46 | confdir = conf.parent.abspath() |
Davide Pesavento | 08b91c8 | 2019-04-13 19:42:10 -0400 | [diff] [blame] | 47 | buildername = getattr(self, 'builder', 'html') |
| 48 | srcdir = getattr(self, 'srcdir', confdir) |
| 49 | outdir = self.path.find_or_declare(getattr(self, 'outdir', buildername)).get_bld() |
| 50 | doctreedir = getattr(self, 'doctreedir', os.path.join(outdir.abspath(), '.doctrees')) |
Alexander Afanasyev | 49272f7 | 2014-04-06 21:49:46 -0700 | [diff] [blame] | 51 | |
| 52 | task.env['BUILDERNAME'] = buildername |
| 53 | task.env['SRCDIR'] = srcdir |
| 54 | task.env['DOCTREEDIR'] = doctreedir |
| 55 | task.env['OUTDIR'] = outdir.abspath() |
Davide Pesavento | 08b91c8 | 2019-04-13 19:42:10 -0400 | [diff] [blame] | 56 | task.env['VERSION'] = 'version=%s' % self.version |
| 57 | task.env['RELEASE'] = 'release=%s' % getattr(self, 'release', self.version) |
Alexander Afanasyev | 49272f7 | 2014-04-06 21:49:46 -0700 | [diff] [blame] | 58 | |
| 59 | import imp |
| 60 | confData = imp.load_source('sphinx_conf', conf.abspath()) |
| 61 | |
Davide Pesavento | 08b91c8 | 2019-04-13 19:42:10 -0400 | [diff] [blame] | 62 | if buildername == 'man': |
Alexander Afanasyev | 49272f7 | 2014-04-06 21:49:46 -0700 | [diff] [blame] | 63 | for i in confData.man_pages: |
| 64 | target = outdir.find_or_declare('%s.%d' % (i[1], i[4])) |
| 65 | task.outputs.append(target) |
| 66 | |
| 67 | if self.install_path: |
Davide Pesavento | 08b91c8 | 2019-04-13 19:42:10 -0400 | [diff] [blame] | 68 | self.bld.install_files('%s/man%d/' % (self.install_path, i[4]), target) |
Alexander Afanasyev | 49272f7 | 2014-04-06 21:49:46 -0700 | [diff] [blame] | 69 | else: |
| 70 | task.outputs.append(outdir) |
| 71 | |
| 72 | def configure(conf): |
| 73 | conf.find_program('sphinx-build', var='SPHINX_BUILD', mandatory=False) |
| 74 | |
| 75 | # sphinx docs |
| 76 | from waflib.Build import BuildContext |
| 77 | class sphinx(BuildContext): |
| 78 | cmd = "sphinx" |
| 79 | fun = "sphinx" |