jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
6.5KB

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Jérôme Carretero, 2013 (zougloub)
  4. """
  5. reStructuredText support (experimental)
  6. Example::
  7. def configure(conf):
  8. conf.load('rst')
  9. if not conf.env.RST2HTML:
  10. conf.fatal('The program rst2html is required')
  11. def build(bld):
  12. bld(
  13. features = 'rst',
  14. type = 'rst2html', # rst2html, rst2pdf, ...
  15. source = 'index.rst', # mandatory, the source
  16. deps = 'image.png', # to give additional non-trivial dependencies
  17. )
  18. By default the tool looks for a set of programs in PATH.
  19. The tools are defined in `rst_progs`.
  20. To configure with a special program use::
  21. $ RST2HTML=/path/to/rst2html waf configure
  22. This tool is experimental; don't hesitate to contribute to it.
  23. """
  24. import os, re
  25. from waflib import Node, Utils, Task, Errors, Logs
  26. from waflib.TaskGen import feature, before_method
  27. rst_progs = "rst2html rst2xetex rst2latex rst2xml rst2pdf rst2s5 rst2man rst2odt rst2rtf".split()
  28. def parse_rst_node(node, nodes, names, seen):
  29. # TODO add extensibility, to handle custom rst include tags...
  30. if node in seen:
  31. return
  32. seen.append(node)
  33. code = node.read()
  34. re_rst = re.compile(r'^\s*.. ((?P<subst>\|\S+\|) )?(?P<type>include|image|figure):: (?P<file>.*)$', re.M)
  35. for match in re_rst.finditer(code):
  36. ipath = match.group('file')
  37. itype = match.group('type')
  38. Logs.debug("rst: visiting %s: %s" % (itype, ipath))
  39. found = node.parent.find_resource(ipath)
  40. if found:
  41. nodes.append(found)
  42. if itype == 'include':
  43. parse_rst_node(found, nodes, names, seen)
  44. else:
  45. names.append(ipath)
  46. class docutils(Task.Task):
  47. """
  48. Compile a rst file.
  49. """
  50. def scan(self):
  51. """
  52. A recursive regex-based scanner that finds rst dependencies.
  53. """
  54. nodes = []
  55. names = []
  56. seen = []
  57. node = self.inputs[0]
  58. if not node:
  59. return (nodes, names)
  60. parse_rst_node(node, nodes, names, seen)
  61. Logs.debug("rst: %s: found the following file deps: %s" % (repr(self), nodes))
  62. if names:
  63. Logs.warn("rst: %s: could not find the following file deps: %s" % (repr(self), names))
  64. return (nodes, names)
  65. def check_status(self, msg, retcode):
  66. """
  67. Check an exit status and raise an error with a particular message
  68. :param msg: message to display if the code is non-zero
  69. :type msg: string
  70. :param retcode: condition
  71. :type retcode: boolean
  72. """
  73. if retcode != 0:
  74. raise Errors.WafError("%r command exit status %r" % (msg, retcode))
  75. def run(self):
  76. """
  77. Runs the rst compilation using docutils
  78. """
  79. raise NotImplementedError()
  80. class rst2html(docutils):
  81. color = 'BLUE'
  82. def __init__(self, *args, **kw):
  83. docutils.__init__(self, *args, **kw)
  84. self.command = self.generator.env.RST2HTML
  85. self.attributes = ['stylesheet']
  86. def scan(self):
  87. nodes, names = docutils.scan(self)
  88. for attribute in self.attributes:
  89. stylesheet = getattr(self.generator, attribute, None)
  90. if stylesheet is not None:
  91. ssnode = self.generator.to_nodes(stylesheet)[0]
  92. nodes.append(ssnode)
  93. Logs.debug("rst: adding dep to %s %s" % (attribute, stylesheet))
  94. return nodes, names
  95. def run(self):
  96. cwdn = self.outputs[0].parent
  97. src = self.inputs[0].path_from(cwdn)
  98. dst = self.outputs[0].path_from(cwdn)
  99. cmd = self.command + [src, dst]
  100. cmd += Utils.to_list(getattr(self.generator, 'options', []))
  101. for attribute in self.attributes:
  102. stylesheet = getattr(self.generator, attribute, None)
  103. if stylesheet is not None:
  104. stylesheet = self.generator.to_nodes(stylesheet)[0]
  105. cmd += ['--%s' % attribute, stylesheet.path_from(cwdn)]
  106. return self.exec_command(cmd, cwd=cwdn.abspath())
  107. class rst2s5(rst2html):
  108. def __init__(self, *args, **kw):
  109. rst2html.__init__(self, *args, **kw)
  110. self.command = self.generator.env.RST2S5
  111. self.attributes = ['stylesheet']
  112. class rst2latex(rst2html):
  113. def __init__(self, *args, **kw):
  114. rst2html.__init__(self, *args, **kw)
  115. self.command = self.generator.env.RST2LATEX
  116. self.attributes = ['stylesheet']
  117. class rst2xetex(rst2html):
  118. def __init__(self, *args, **kw):
  119. rst2html.__init__(self, *args, **kw)
  120. self.command = self.generator.env.RST2XETEX
  121. self.attributes = ['stylesheet']
  122. class rst2pdf(docutils):
  123. color = 'BLUE'
  124. def run(self):
  125. cwdn = self.outputs[0].parent
  126. src = self.inputs[0].path_from(cwdn)
  127. dst = self.outputs[0].path_from(cwdn)
  128. cmd = self.generator.env.RST2PDF + [src, '-o', dst]
  129. cmd += Utils.to_list(getattr(self.generator, 'options', []))
  130. return self.exec_command(cmd, cwd=cwdn.abspath())
  131. @feature('rst')
  132. @before_method('process_source')
  133. def apply_rst(self):
  134. """
  135. Create :py:class:`rst` or other rst-related task objects
  136. """
  137. if self.target:
  138. if isinstance(self.target, Node.Node):
  139. tgt = self.target
  140. elif isinstance(self.target, str):
  141. tgt = self.path.get_bld().make_node(self.target)
  142. else:
  143. self.bld.fatal("rst: Don't know how to build target name %s which is not a string or Node for %s" % (self.target, self))
  144. else:
  145. tgt = None
  146. tsk_type = getattr(self, 'type', None)
  147. src = self.to_nodes(self.source)
  148. assert len(src) == 1
  149. src = src[0]
  150. if tsk_type is not None and tgt is None:
  151. if tsk_type.startswith('rst2'):
  152. ext = tsk_type[4:]
  153. else:
  154. self.bld.fatal("rst: Could not detect the output file extension for %s" % self)
  155. tgt = src.change_ext('.%s' % ext)
  156. elif tsk_type is None and tgt is not None:
  157. out = tgt.name
  158. ext = out[out.rfind('.')+1:]
  159. self.type = 'rst2' + ext
  160. elif tsk_type is not None and tgt is not None:
  161. # the user knows what he wants
  162. pass
  163. else:
  164. self.bld.fatal("rst: Need to indicate task type or target name for %s" % self)
  165. deps_lst = []
  166. if getattr(self, 'deps', None):
  167. deps = self.to_list(self.deps)
  168. for filename in deps:
  169. n = self.path.find_resource(filename)
  170. if not n:
  171. self.bld.fatal('Could not find %r for %r' % (filename, self))
  172. if not n in deps_lst:
  173. deps_lst.append(n)
  174. try:
  175. task = self.create_task(self.type, src, tgt)
  176. except KeyError:
  177. self.bld.fatal("rst: Task of type %s not implemented (created by %s)" % (self.type, self))
  178. task.env = self.env
  179. # add the manual dependencies
  180. if deps_lst:
  181. try:
  182. lst = self.bld.node_deps[task.uid()]
  183. for n in deps_lst:
  184. if not n in lst:
  185. lst.append(n)
  186. except KeyError:
  187. self.bld.node_deps[task.uid()] = deps_lst
  188. inst_to = getattr(self, 'install_path', None)
  189. if inst_to:
  190. self.install_task = self.bld.install_files(inst_to, task.outputs[:], env=self.env)
  191. self.source = []
  192. def configure(self):
  193. """
  194. Try to find the rst programs.
  195. Do not raise any error if they are not found.
  196. You'll have to use additional code in configure() to die
  197. if programs were not found.
  198. """
  199. for p in rst_progs:
  200. self.find_program(p, mandatory=False)