Converted mkpdf, mkman and mkhtml to Python.
[gtk-doc.git] / gtkdoc-mkpdf.in
blob5dcf758229ff96ffa2cd5bfa90ea8432985edf0e
1 #!@PYTHON@
3 # Support both Python 2 and 3
4 from __future__ import print_function
6 import os, sys, argparse, subprocess
7 import logging
9 version = '@VERSION@'
10 xsltproc = '@XSLTPROC@'
11 dblatex = '@DBLATEX@'
12 fop = '@FOP@'
14 parser = argparse.ArgumentParser(description='gtkdoc-mkpdf version %s - generate documentation in pdf format' % version)
16 parser.add_argument('--verbose', default=False, action='store_true',
17             help='Print extra output while processing.')
18 parser.add_argument('--path', default=[], action='append',
19             help='Extra source directories.')
20 parser.add_argument('--imgdir', default=[], action='append',
21             help='Extra image directories.')
22 parser.add_argument('--version', default=False, action='store_true',
23             help='Print the version of this program')
24 parser.add_argument('--uninstalled', action='store_true', default=False,
25                     help='???')
26 parser.add_argument('args', nargs=2,
27             help='MODULE DRIVER_FILE')
29 def cleanexit(exitval):
30     global module
31     fname = module + '.fo'
32     if os.path.exists(fname):
33         os.unlink(fname)
34     sys.exit(exitval)
36 options = parser.parse_args()
38 if options.version:
39     print(version)
40     sys.exit(0)
42 module=options.args[0]
43 document=options.args[1]
45 if options.uninstalled:
46     # this does not work from buiddir!=srcdir
47     # we could try this
48     # MAKE_SCRDIR=$(abs_srcdir) MAKE_BUILDDIR=$(abs_builddir) gtkdoc-mkpdf ...
49     gtkdocdir=os.path.split(sys.argv[0])[0]
50     logging.debug("uninstalled, gtkdocdir=" + gtkdocdir)
51 else:
52     # the first two are needed to resolve datadir
53     prefix='@prefix@'
54     datarootdir='@datarootdir@'
55     gtkdocdir='@datadir@'/gtk-doc/data
57 if "<?xml" in open(document).readline():
58     is_xml=True
59     path_option='--path'
60 else:
61     is_xml=False
62     path_option='--directory'
64 # We need to use a wrapper because there's no other way to conditionally pass
65 # a `--path $searchpath` argument with proper quoting for the path
66 def run_xsltproc(args):
67     global options, xsltproc
68     # we could do "$path_option $PWD "
69     # to avoid needing rewriting entities that are copied from the header
70     # into docs under xml
71     if len(options.path) == 0:
72         cmd = [xsltproc] + args
73     else:
74         cmd = [xsltproc, path_option] + options.searchpath + args
75     pc = subprocess.Popen(cmd, stderr=subprocess.PIPE)
76     (o, stde) = pc.communicate()
77     open('profile.txt', 'wb').write(stde)
78     if pc.returncode != 0:
79         cleanexit(pc.returncode)
81 if is_xml:
82     if dblatex != '':
83         # extra options to consider
84         # -I FIG_PATH
85         # -V is useful for debugging
86         # -T db2latex : different style
87         # -d : keep transient files (for debugging)
88         # -P abc.def=$quiet : once the stylesheets have a quiet mode
89         # xsltproc is already called with --xinclude
90         # does not work: --xslt-opts "$path_option $searchpath --nonet $@"
91         dblatex_options=['-o', module + '.pdf']
92         for i in options.imgdir:
93             dblatex_options += ['-I', i]
94         dblatex_options.append(document)
95         #echo "calling: @DBLATEX@ $dblatex_options"
96         if not options.verbose:
97             pc = subprocess.Popen([dblatex, '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
98             (stdo, stde) = pc.communicate()
99             if b'--quiet' in stdo or b'--quiet' in stde:
100                 dblatex_options= ['--quiet'] + dblatex_options
101         dbcmd = [dblatex] + dblatex_options
102         pc = subprocess.Popen(dbcmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
103         (stde, _) = pc.communicate()
104         for line in stde.split('\n'):
105             if 'programlisting or screen' not in line and line.strip():
106                 print(line)
107     else:
108         if fop != '':
109             run_xsltproc(['--nonet',
110                           '--xinclude',
111                           '--stringparam',
112                           'gtkdoc.bookname',
113                           module,
114                           '--stringparam',
115                           'gtkdoc.version',
116                           version,
117                           '--stringparam',
118                           'chunk.quietly',
119                           options.quiet,
120                           '--stringparam',
121                           'chunker.output.quiet',
122                           quiet,
123                           module,
124                           document,
125                           '-o',
126                           module + '.fo',
127                           gtkdocdir + '/gtk-doc-fo.xsl',
128                           document])
129             # fop dies too easily :(
130             # @FOP@ $module.fo $module.pdf
131         else:
132             print("dblatex or fop must be installed to use gtkdoc-mkpdf.")
133             cleanexit(1)
134 else:
135     # not very good output
136     # also for xxx-docs.sgml it will produce xxx-docs.pdf
137     subprocess.check_call(['docbook2pdf', '-e', 'no-valid', document])
139 open('pdf.stamp', 'w').write('timestamp')
140 cleanexit(0)