Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # encoding: utf-8 |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 3 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 4 | # inspired by code by Hans-Martin von Gaudecker, 2012 |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 5 | |
| 6 | import os |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 7 | from waflib import Node, Task, TaskGen, Errors, Logs, Build, Utils |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 8 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 9 | class sphinx_build(Task.Task): |
| 10 | color = 'BLUE' |
Alexander Afanasyev | a06fdda | 2014-04-29 19:15:00 -0700 | [diff] [blame^] | 11 | run_str = '${SPHINX_BUILD} -D ${VERSION} -D ${RELEASE} -q -b ${BUILDERNAME} -d ${DOCTREEDIR} ${SRCDIR} ${OUTDIR}' |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 12 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 13 | def __str__(self): |
| 14 | env = self.env |
| 15 | src_str = ' '.join([a.nice_path()for a in self.inputs]) |
| 16 | tgt_str = ' '.join([a.nice_path()for a in self.outputs]) |
| 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) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 21 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 22 | @TaskGen.extension('.py', '.rst') |
| 23 | def sig_hook(self, node): |
| 24 | node.sig=Utils.h_file(node.abspath()) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 25 | |
| 26 | @TaskGen.feature("sphinx") |
| 27 | @TaskGen.before_method("process_source") |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 28 | def apply_sphinx(self): |
| 29 | """Set up the task generator with a Sphinx instance and create a task.""" |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 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) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 40 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 41 | task = self.create_task('sphinx_build', inputs) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 43 | conf = self.path.find_node(self.config) |
| 44 | task.inputs.append(conf) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 46 | confdir = conf.parent.abspath() |
| 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 | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 52 | task.env['BUILDERNAME'] = buildername |
| 53 | task.env['SRCDIR'] = srcdir |
| 54 | task.env['DOCTREEDIR'] = doctreedir |
| 55 | task.env['OUTDIR'] = outdir.abspath() |
Alexander Afanasyev | a06fdda | 2014-04-29 19:15:00 -0700 | [diff] [blame^] | 56 | task.env['VERSION'] = "version=%s" % self.VERSION |
| 57 | task.env['RELEASE'] = "release=%s" % self.VERSION |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 59 | import imp |
| 60 | confData = imp.load_source('sphinx_conf', conf.abspath()) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 62 | if buildername == "man": |
| 63 | for i in confData.man_pages: |
| 64 | target = outdir.find_or_declare('%s.%d' % (i[1], i[4])) |
| 65 | task.outputs.append(target) |
Alexander Afanasyev | a1ae0a1 | 2014-01-28 15:21:02 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 67 | if self.install_path: |
| 68 | self.bld.install_files("%s/man%d/" % (self.install_path, i[4]), target) |
| 69 | else: |
| 70 | task.outputs.append(outdir) |
| 71 | |
| 72 | def configure(conf): |
| 73 | conf.find_program('sphinx-build', var='SPHINX_BUILD', mandatory=False) |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 74 | |
| 75 | # sphinx docs |
| 76 | from waflib.Build import BuildContext |
Alexander Afanasyev | 1160baa | 2014-04-10 18:50:29 -0700 | [diff] [blame] | 77 | class sphinx(BuildContext): |
Alexander Afanasyev | 5e1288e | 2014-03-28 11:11:48 -0700 | [diff] [blame] | 78 | cmd = "sphinx" |
| 79 | fun = "sphinx" |