3 # Support both Python 2 and 3
4 from __future__ import print_function
6 import os, sys, argparse, subprocess
10 xsltproc = '@XSLTPROC@'
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,
26 parser.add_argument('args', nargs=2,
27 help='MODULE DRIVER_FILE')
29 def cleanexit(exitval):
31 fname = module + '.fo'
32 if os.path.exists(fname):
36 options = parser.parse_args()
42 module=options.args[0]
43 document=options.args[1]
45 if options.uninstalled:
46 # this does not work from buiddir!=srcdir
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)
52 # the first two are needed to resolve datadir
54 datarootdir='@datarootdir@'
55 gtkdocdir='@datadir@'/gtk-doc/data
57 if "<?xml" in open(document).readline():
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
71 if len(options.path) == 0:
72 cmd = [xsltproc] + args
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)
83 # extra options to consider
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():
109 run_xsltproc(['--nonet',
121 'chunker.output.quiet',
127 gtkdocdir + '/gtk-doc-fo.xsl',
129 # fop dies too easily :(
130 # @FOP@ $module.fo $module.pdf
132 print("dblatex or fop must be installed to use gtkdoc-mkpdf.")
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')