[PATCH 3 of 3] setup: install & build documentation using docutils

Dan Villiom Podlaski Christiansen danchr at gmail.com
Sat Aug 22 07:52:20 CDT 2009


# HG changeset patch
# User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
# Date 1250709409 -7200
# Node ID d63240fa0d51a8491dd2c276832dc19873647457
# Parent  a9d1eb0e14448f98804232f70e5cc25811923ec1
setup: install & build documentation using docutils.

diff --git a/setup.py b/setup.py
old mode 100644
new mode 100755
--- a/setup.py
+++ b/setup.py
@@ -29,6 +29,8 @@ except:
 import os, subprocess, time
 import shutil
 import tempfile
+import re
+
 from distutils.core import setup, Extension
 from distutils.dist import Distribution
 from distutils.command.install_data import install_data
@@ -193,6 +195,10 @@ class build_mo(build):
             self.distribution.package_data_files = []
         datafiles = self.distribution.package_data_files
 
+        if not hasattr(self.distribution, 'package_data_files'):
+            self.distribution.package_data_files = []
+        datafiles = self.distribution.package_data_files
+
         join = os.path.join
         for po in os.listdir(podir):
             if not po.endswith('.po'):
@@ -210,6 +216,125 @@ class build_mo(build):
 
 build.sub_commands.append(('build_mo', None))
 
+class build_doc(build):
+
+    description = "build documentation from ReST sources"
+
+    def _doscript(self, script, output):
+        p = subprocess.Popen([sys.executable, script], stdout=subprocess.PIPE)
+        r = p.communicate()[0]
+        file(output, 'w').write(r)
+
+    try:
+        import docutils.core
+        del docutils
+
+        def _rst2html(self, input, output):
+            from docutils import core
+
+            input, output = os.path.abspath(input), os.path.abspath(output)
+
+            opwd = os.path.abspath(os.curdir)
+            try:
+                os.chdir(os.path.dirname(input))
+
+                core.publish_file(source_path=input, destination_path=output,
+                                  writer_name='html')
+            finally:
+                os.chdir(opwd)
+    except ImportError:
+        _rst2html = None
+
+    try:
+        import docutils.writers.manpage
+        del docutils
+
+        def _rst2man(self, input, output):
+            from docutils import core
+            from docutils.writers import manpage
+
+            input, output = os.path.abspath(input), os.path.abspath(output)
+
+            try:
+                opwd = os.path.abspath(os.curdir)
+                os.chdir(os.path.dirname(input))
+
+                core.publish_file(source_path=input, destination_path=output,
+                                  writer_name='manpage')
+            finally:
+                os.chdir(opwd)
+    except ImportError:
+        _rst2man = None
+
+    def run(self):
+        from os.path import join
+
+        if not self._rst2html:
+            self.warn('docutils not found; '
+                      'documentation will not be built')
+            return
+        if not self._rst2man:
+            self.warn('docutils manpage writer not found; '
+                      'manuals will not be built')
+
+        srcdir = 'doc'
+
+        if not os.path.isdir(srcdir):
+            self.warn("could not find %s/ directory" % srcdir)
+            return
+
+        docdir = join(self.build_temp, 'doc')
+        docdestdir = join(self.distribution.get_name())
+        mandestdir = 'man'
+
+        self.mkpath(docdir)
+        self.make_file([join('mercurial', 'commands.py'),
+                        join('mercurial', 'help.py')],
+                       join(docdir, 'hg.1.gendoc.txt'),
+                       self._doscript,
+                       [join(srcdir, 'gendoc.py'),
+                        join(docdir, 'hg.1.gendoc.txt')],
+                       'extracting %s from source files'
+                           % join(docdir, 'hg.1.gendoc.txt'))
+
+        self.copy_tree(srcdir, docdir)
+
+        datafiles = self.distribution.data_files
+        pattern = re.compile(r'^((.*)\.([0-9]))\.txt$')
+
+        for srcfile in os.listdir(docdir):
+            match = pattern.match(srcfile)
+            if not match:
+                continue
+
+            htmlfile = match.group(1) + '.html'
+
+            dependancies = [join(docdir, dep)
+                            for dep in (srcfile, 'common.txt', 'README',
+                                        'hg.1.gendoc.txt')]
+
+            self.make_file(dependancies, join(docdir, htmlfile), self._rst2html,
+                           [join(docdir, srcfile), join(docdir, htmlfile)],
+                           'generating HTML document %s from %s'
+                           % (htmlfile, srcfile))
+
+            datafiles.append((docdestdir, [join(docdir, htmlfile)]))
+
+            mandir = 'man' + match.group(3)
+            manfile = match.group(1)
+
+            if self._rst2man:
+                self.make_file(dependancies,
+                               join(docdir, manfile), self._rst2man,
+                               [join(docdir, srcfile), join(docdir, manfile)],
+                               'generating manual page %s from %s'
+                               % (manfile, srcfile))
+
+                datafiles.append((join(mandestdir, mandir),
+                                  [join(docdir, manfile)]))
+
+build.sub_commands.append(('build_doc', None))
+
 Distribution.pure = 0
 Distribution.global_options.append(('pure', None, "use pure (slow) Python "
                                     "code instead of C extensions"))
@@ -238,6 +363,7 @@ class hg_build_py(build_py):
 
 cmdclass = {'install_data': install_package_data,
             'build_mo': build_mo,
+            'build_doc': build_doc,
             'build_py': hg_build_py}
 
 ext_modules=[


More information about the Mercurial-devel mailing list