[gtk-doc] Converted mkpdf, mkman and mkhtml to Python.
- From: Stefan Sauer <stefkost src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk-doc] Converted mkpdf, mkman and mkhtml to Python.
- Date: Mon, 20 Mar 2017 19:33:12 +0000 (UTC)
commit 91bc782d50e035c5c2818d503426824d176322ff
Author: Jussi Pakkanen <jpakkane gmail com>
Date: Thu Mar 2 15:06:04 2017 +0200
Converted mkpdf, mkman and mkhtml to Python.
https://bugzilla.gnome.org/show_bug.cgi?id=779566
gtkdoc-mkhtml.in | 178 ++++++++++++++++++++---------------------
gtkdoc-mkman.in | 140 ++++++++++++++++-----------------
gtkdoc-mkpdf.in | 230 +++++++++++++++++++++++++++--------------------------
tests/tools.sh.in | 11 ++-
4 files changed, 277 insertions(+), 282 deletions(-)
---
diff --git a/gtkdoc-mkhtml.in b/gtkdoc-mkhtml.in
index 2c7d09c..2a4c420 100644
--- a/gtkdoc-mkhtml.in
+++ b/gtkdoc-mkhtml.in
@@ -1,109 +1,101 @@
-#!/bin/sh
-#
-
-usage() {
-cat <<EOF
-gtkdoc-mkhtml version @VERSION@ - generate documentation in html format
-
---verbose Print extra output while processing
---path=SEARCH_PATH Extra source directories
-MODULE Name of the doc module being parsed
-DRIVER_FILE File containing the toplevel DocBook file.
---version Print the version of this program
---help Print this help
-EOF
-}
-
-# echo "args $*\n";
-
-# parse options, ignore unknown options for future extensions
-
-verbose="0"
-searchpath=
-uninstalled=no
-while true; do
- case "X$1" in
- X--version) echo "@VERSION@"; exit 0;;
- X--help) usage; exit 0;;
- X--uninstalled) uninstalled=yes; shift;;
- X--verbose) verbose="1"; shift;;
- X--path=*) searchpath=`echo "$1" | sed s/.*=//`; shift;;
- X--*) shift;;
- X*) break;;
- esac
-done
-
-if test $# -lt 2; then
- usage 1>&2
- exit 1
-fi
-
-module="$1"
-shift
-document="$1"
-shift
-
-quiet="1"
-if test $verbose = "1"; then
- quiet="0"
-fi
-
-if test $uninstalled = yes; then
+#!@PYTHON@
+
+# Support both Python 2 and 3
+from __future__ import print_function
+
+import os, sys, argparse, subprocess, shutil
+from glob import glob
+
+version = '@VERSION@'
+xsltproc = '@XSLTPROC@'
+
+parser = argparse.ArgumentParser(description='gtkdoc-mkhtml version %s - generate documentation in html
format' % version)
+
+parser.add_argument('--verbose', default=False, action='store_true',
+ help='Print extra output while processing')
+parser.add_argument('--path', default=[], action='append',
+ help='Extra source directories')
+parser.add_argument('--version', default=False, action='store_true',
+ help='Print the version of this program')
+parser.add_argument('args', nargs='*',
+ help='MODULE DRIVER_FILE')
+parser.add_argument('--uninstalled', action='store_true', default=False,
+ help='???')
+
+options = parser.parse_args()
+if options.version:
+ print(version)
+ sys.exit(0)
+
+if len(options.args) < 2:
+ sys.exit('Too few arguments')
+
+module=options.args[0]
+document=options.args[1]
+if options.verbose:
+ quiet = '0'
+else:
+ quiet = '1'
+remaining_args = options.args[2:]
+
+if options.uninstalled:
# this does not work from buiddir!=srcdir
- gtkdocdir=`dirname $0`
- # traditional Bourne shells may not support -e here, use -f
- if test ! -f "$gtkdocdir/gtk-doc.xsl"; then
+ gtkdocdir = os.path.split(sys.argv[0])[0]
+ # traditional Bourne shells may not support -e here, use -f
+ if not os.path.exists(gtkdocdir + '/gtk-doc.xsl'):
# try to src dir (set from makefiles) too
- if test -f "$ABS_TOP_SRCDIR/gtk-doc.xsl"; then
- gtkdocdir=$ABS_TOP_SRCDIR
- fi
- fi
- styledir=$gtkdocdir/style
+ if os.path.exists(os.path.environ.get("ABS_TOP_SRCDIR", '') + '/gtk-doc.xsl'):
+ gtkdocdir=os.path.environ['ABS_TOP_SRCDIR']
+ styledir=gtkdocdir + '/style'
#echo "uninstalled, gtkdocdir=$gtkdocdir, cwd=$PWD"
-else
+else:
# the first two are needed to resolve datadir
- prefix=@prefix@
- datarootdir=@datarootdir@
- gtkdocdir=@datadir@/gtk-doc/data
- styledir=$gtkdocdir
-fi
+ prefix='@prefix@'
+ datarootdir='@datarootdir@'
+ gtkdocdir='@datadir@/gtk-doc/data'
+ styledir=gtkdocdir
# We need to use a wrapper because there's no other way to conditionally pass
# a `--path $searchpath` argument with proper quoting for the path
-run_xsltproc() {
+def run_xsltproc(args):
# we could do "$path_option $PWD "
# to avoid needing rewriting entities that are copied from the header
# into docs under xml
- if test "$GTKDOC_PROFILE" = ""; then
- if test "X$searchpath" = "X"; then
- @XSLTPROC@ "$@"
- else
- @XSLTPROC@ --path "$searchpath" "$@"
- fi
- else
- if test "X$searchpath" = "X"; then
- @XSLTPROC@ 2>profile.txt --profile "$@"
- else
- @XSLTPROC@ 2>profile.txt --profile --path "$searchpath" "$@"
- fi
- fi
-}
-
-run_xsltproc --nonet --xinclude \
- --stringparam gtkdoc.bookname "$module" \
- --stringparam gtkdoc.version "@VERSION@" \
- --stringparam chunk.quietly $quiet \
- --stringparam chunker.output.quiet $quiet \
- "$@" "$gtkdocdir/gtk-doc.xsl" "$document" || exit $?
+ if os.environ.get("GTKDOC_PROFILE", '') == '':
+ if len(options.path):
+ subprocess.check_call([xsltproc] + args)
+ else:
+ subprocess.check_call([xsltproc, '--path'] + options.path + args)
+ else:
+ if len(options.path) == 0:
+ subprocess.check_call([xsltproc, '--profile'] + args, stderr=open('profile.txt', 'w'))
+ else:
+ subprocess.check_call([xsltproc ,'--profile', '--path'] + options.path + args,
stderr=open('profile.txt', 'w'))
+
+run_xsltproc(['--nonet',
+ '--xinclude',
+ '--stringparam',
+ 'gtkdoc.bookname',
+ module,
+ '--stringparam',
+ 'gtkdoc.version',
+ version,
+ '--stringparam',
+ 'chunk.quietly',
+ quiet,
+ '--stringparam',
+ 'chunker.output.quiet',
+ quiet] + remaining_args + \
+ [gtkdocdir + '/gtk-doc.xsl',
+ document])
# profiling
-if test "$GTKDOC_PROFILE" != ""; then
- cat profile.txt | gprof2dot.py -e 0.01 -n 0.01 | dot -Tpng -o profile.png
-fi
+if os.environ.get("GTKDOC_PROFILE", '') != '':
+ subprocess.check_call('cat profile.txt | gprof2dot.py -e 0.01 -n 0.01 | dot -Tpng -o profile.png',
shell=True)
# copy navigation images and stylesheets to html directory ...
-cp -f "$styledir"/*.png "$styledir"/*.css ./
-
+for f in glob(styledir + '/*.png') + glob(styledir + '/*.css'):
+ shutil.copy(f, '.')
-echo "timestamp" > ../html.stamp
+open('../html.stamp', 'w').write('timestamp')
diff --git a/gtkdoc-mkman.in b/gtkdoc-mkman.in
index b6e91a5..6667301 100644
--- a/gtkdoc-mkman.in
+++ b/gtkdoc-mkman.in
@@ -1,95 +1,91 @@
-#!/bin/sh
-#
+#!@PYTHON@
-usage() {
-cat <<EOF
-gtkdoc-mkman version @VERSION@ - generate documentation in man format
+# Support both Python 2 and 3
+from __future__ import print_function
---verbose Print extra output while processing
---path=SEARCH_PATH Extra source directories
-MODULE Name of the doc module being parsed
-DRIVER_FILE File containing the toplevel DocBook file.
---version Print the version of this program
---help Print this help
-EOF
-}
+import os, sys, argparse, subprocess
+from glob import glob
-# parse options, ignore unknown options for future extensions
+version = '@VERSION@'
+xsltproc = '@XSLTPROC@'
-verbose=0
-searchpath=
-uninstalled=no
-while true; do
- case "X$1" in
- X--version) echo "@VERSION@"; exit 0;;
- X--help) usage; exit 0;;
- X--uninstalled) uninstalled=yes; shift;;
- X--verbose) verbose="1"; shift;;
- X--path=*) searchpath=`echo $1 | sed s/.*=//`; shift;;
- X--*) shift;;
- X*) break;;
- esac
-done
+parser = argparse.ArgumentParser(description='gtkdoc-mkman version %s - generate documentation in man
format' % version)
-if test $# -ne 2; then
- usage 1>&2
- exit 1
-fi
+parser.add_argument('--verbose', default=False, action='store_true',
+ help='Print extra output while processing')
+parser.add_argument('--path', default='',
+ help='Extra source directories')
+parser.add_argument('version', default=False, action='store_true',
+ help='Print the version of this program')
+parser.add_argument('args', nargs=2,
+ help='MODULE DRIVER_FILE')
+parser.add_argument('--uninstalled', action='store_true', default=False,
+ help='???')
-module=$1
-shift
-document=$1
-shift
+options = parser.parse_args()
+if options.version:
+ print(version)
+ sys.exit(0)
-quiet="1"
-if test $verbose = "1"; then
- quiet="0"
-fi
+module=options.args[0]
+document=options.args[1]
+if options.verbose:
+ quiet = '0'
+else:
+ quiet = '1'
-if test $uninstalled = yes; then
+if options.uninstalled:
# this does not work from buiddir!=srcdir
- gtkdocdir=`dirname $0`
+ gtkdocdir=os.path.split(sys.argv[0])[0]
#echo "uninstalled, gtkdocdir=$gtkdocdir"
-else
+else:
# the first two are needed to resolve datadir
- prefix=@prefix@
- datarootdir=@datarootdir@
- gtkdocdir=@datadir@/gtk-doc/data
-fi
+ prefix='@prefix@'
+ datarootdir='@datarootdir@'
+ gtkdocdir='@datadir@/gtk-doc/data'
-if head -n 1 $document | grep "<?xml" > /dev/null; then
- is_xml=true
+if "<?xml" in open(document).readline():
+ is_xml=True
path_option='--path'
-else
- is_xml=false
+else:
+ is_xml=False
path_option='--directory'
-fi
+
# we could do "$path_option $PWD "
# to avoid needing rewriting entities that are copied from the header
# into docs under xml
-if test "X$searchpath" = "X"; then
- path_arg=
-else
- path_arg="$path_option $searchpath"
-fi
+if options.path == '':
+ path_arg=[]
+else:
+ path_arg=[path_option, options.path]
# would it make sens to create man pages only for certain refentries
# e.g. for tools
-if $is_xml; then
+if is_xml:
# see http://bugzilla.gnome.org/show_bug.cgi?id=467488
- @XSLTPROC@ $path_arg --nonet --xinclude \
- --stringparam gtkdoc.bookname $module \
- --stringparam gtkdoc.version "@VERSION@" \
- --stringparam chunk.quietly $quiet \
- --stringparam chunker.output.quiet $quiet \
- http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl \
- $document || exit $?
-else
- for i in `cd sgml;ls *.sgml`; do
- j=`echo $i | sed 's/.sgml/.man/'`
- echo ": converting " $i $j
- docbook-to-man sgml/$i > man/$j 2> man/$j.log
- done
-fi
+ subprocess.check_call([xsltproc] + path_arg +[
+ '--nonet',
+ '--xinclude',
+ '--stringparam',
+ 'gtkdoc.bookname',
+ module,
+ '--stringparam',
+ 'gtkdoc.version',
+ version,
+ '--stringparam',
+ 'chunk.quietly ',
+ 'quiet',
+ '--stringparam',
+ 'chunker.output.quiet',
+ quiet,
+ 'http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl',
+ document])
+else:
+ for i in glob('sgml/*.sgml'):
+ j = os.path.split(i)[1].replace('.sgml', '.man')
+ print("converting ", i, j)
+ subproject.check_call(['docbook-to-man', i],
+ stdout=open('man'+j),
+ stderr=open('man/%s.log' % j))
diff --git a/gtkdoc-mkpdf.in b/gtkdoc-mkpdf.in
old mode 100644
new mode 100755
index 9b39ddf..5dcf758
--- a/gtkdoc-mkpdf.in
+++ b/gtkdoc-mkpdf.in
@@ -1,137 +1,141 @@
-#!/bin/sh
-#
+#!@PYTHON@
-usage() {
-cat <<EOF
-gtkdoc-mkpdf version @VERSION@ - generate documentation in pdf format
+# Support both Python 2 and 3
+from __future__ import print_function
---verbose Print extra output while processing
---path=SEARCH_PATH Extra source directories
---imgdir=DIR Extra image directories
-MODULE Name of the doc module being parsed
-DRIVER_FILE File containing the toplevel DocBook file.
---version Print the version of this program
---help Print this help
-EOF
-}
+import os, sys, argparse, subprocess
+import logging
-#echo "args $0\n";
+version = '@VERSION@'
+xsltproc = '@XSLTPROC@'
+dblatex = '@DBLATEX@'
+fop = '@FOP@'
-cleanexit() {
- rm -f $module.fo
- exit $1
-}
+parser = argparse.ArgumentParser(description='gtkdoc-mkpdf version %s - generate documentation in pdf
format' % version)
-# parse options, ignore unknown options for future extensions
+parser.add_argument('--verbose', default=False, action='store_true',
+ help='Print extra output while processing.')
+parser.add_argument('--path', default=[], action='append',
+ help='Extra source directories.')
+parser.add_argument('--imgdir', default=[], action='append',
+ help='Extra image directories.')
+parser.add_argument('--version', default=False, action='store_true',
+ help='Print the version of this program')
+parser.add_argument('--uninstalled', action='store_true', default=False,
+ help='???')
+parser.add_argument('args', nargs=2,
+ help='MODULE DRIVER_FILE')
-verbose="0"
-searchpath=
-uninstalled=no
-imgdirs=
-while true; do
- case "X$1" in
- X--version) echo "@VERSION@"; exit 0;;
- X--help) usage; exit 0;;
- X--uninstalled) uninstalled=yes; shift;;
- X--verbose) verbose="1"; shift;;
- X--path=*) searchpath=`echo "$1" | sed s/.*=//`; shift;;
- X--imgdir=*) imgdirs="$imgdirs -I `echo "$1" | sed s/.*=//`"; shift;;
- X--*) shift;;
- X*) break;;
- esac
-done
+def cleanexit(exitval):
+ global module
+ fname = module + '.fo'
+ if os.path.exists(fname):
+ os.unlink(fname)
+ sys.exit(exitval)
-if test $# -lt 2; then
- usage 1>&2
- exit 1
-fi
+options = parser.parse_args()
-module="$1"
-shift
-document="$1"
-shift
+if options.version:
+ print(version)
+ sys.exit(0)
-quiet="1"
-if test $verbose = "1"; then
- quiet="0"
-fi
+module=options.args[0]
+document=options.args[1]
-if test $uninstalled = yes; then
- # this does not work from buiddir!=srcdir
- # we could try this
- # MAKE_SCRDIR=$(abs_srcdir) MAKE_BUILDDIR=$(abs_builddir) gtkdoc-mkpdf ...
- gtkdocdir=`dirname $0`
- #echo "uninstalled, gtkdocdir=$gtkdocdir"
-else
- # the first two are needed to resolve datadir
- prefix=@prefix@
- datarootdir=@datarootdir@
- gtkdocdir=@datadir@/gtk-doc/data
-fi
+if options.uninstalled:
+ # this does not work from buiddir!=srcdir
+ # we could try this
+ # MAKE_SCRDIR=$(abs_srcdir) MAKE_BUILDDIR=$(abs_builddir) gtkdoc-mkpdf ...
+ gtkdocdir=os.path.split(sys.argv[0])[0]
+ logging.debug("uninstalled, gtkdocdir=" + gtkdocdir)
+else:
+ # the first two are needed to resolve datadir
+ prefix='@prefix@'
+ datarootdir='@datarootdir@'
+ gtkdocdir='@datadir@'/gtk-doc/data
-if head -n 1 $document | grep "<?xml" > /dev/null; then
- is_xml=true
+if "<?xml" in open(document).readline():
+ is_xml=True
path_option='--path'
-else
- is_xml=false
+else:
+ is_xml=False
path_option='--directory'
-fi
# We need to use a wrapper because there's no other way to conditionally pass
# a `--path $searchpath` argument with proper quoting for the path
-run_xsltproc() {
+def run_xsltproc(args):
+ global options, xsltproc
# we could do "$path_option $PWD "
# to avoid needing rewriting entities that are copied from the header
# into docs under xml
- if test "X$searchpath" = "X"; then
- @XSLTPROC@ 2>profile.txt "$@"
- else
- @XSLTPROC@ 2>profile.txt "$path_option" "$searchpath" "$@"
- fi
-}
+ if len(options.path) == 0:
+ cmd = [xsltproc] + args
+ else:
+ cmd = [xsltproc, path_option] + options.searchpath + args
+ pc = subprocess.Popen(cmd, stderr=subprocess.PIPE)
+ (o, stde) = pc.communicate()
+ open('profile.txt', 'wb').write(stde)
+ if pc.returncode != 0:
+ cleanexit(pc.returncode)
-if $is_xml; then
- if test -n "@DBLATEX@"; then
- # extra options to consider
- # -I FIG_PATH
- # -V is useful for debugging
- # -T db2latex : different style
- # -d : keep transient files (for debugging)
- # -P abc.def=$quiet : once the stylesheets have a quiet mode
- # xsltproc is already called with --xinclude
- # does not work: --xslt-opts "$path_option $searchpath --nonet $@"
- dblatex_options="-o $module.pdf $imgdirs $document"
- #echo "calling: @DBLATEX@ $dblatex_options"
- if test $verbose = "0"; then
- @DBLATEX@ 2>&1 --help | grep >/dev/null "\-\-quiet"
- if test "$?" = "0"; then
- dblatex_options="--quiet $dblatex_options";
- fi;
- @DBLATEX@ 2>&1 >/dev/null $dblatex_options | grep -v 'programlisting or screen'
- else
- { @DBLATEX@ 2>&1 >&3 $dblatex_options | grep -v 'programlisting or screen' >&2; } 3>&1
- fi
- else
- if test -n "@FOP@"; then
- run_xsltproc --nonet --xinclude \
- --stringparam gtkdoc.bookname "$module" \
- --stringparam gtkdoc.version "@VERSION@" \
- --stringparam chunk.quietly $quiet \
- --stringparam chunker.output.quiet $quiet \
- "$@" -o "$module.fo" "$gtkdocdir/gtk-doc-fo.xsl" "$document" || cleanexit $?
- # fop dies too easily :(
- # @FOP@ $module.fo $module.pdf
- else
- echo "dblatex or fop must be installed to use gtkdoc-mkpdf." >&2
- cleanexit 1
- fi
- fi
-else
+if is_xml:
+ if dblatex != '':
+ # extra options to consider
+ # -I FIG_PATH
+ # -V is useful for debugging
+ # -T db2latex : different style
+ # -d : keep transient files (for debugging)
+ # -P abc.def=$quiet : once the stylesheets have a quiet mode
+ # xsltproc is already called with --xinclude
+ # does not work: --xslt-opts "$path_option $searchpath --nonet $@"
+ dblatex_options=['-o', module + '.pdf']
+ for i in options.imgdir:
+ dblatex_options += ['-I', i]
+ dblatex_options.append(document)
+ #echo "calling: @DBLATEX@ $dblatex_options"
+ if not options.verbose:
+ pc = subprocess.Popen([dblatex, '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ (stdo, stde) = pc.communicate()
+ if b'--quiet' in stdo or b'--quiet' in stde:
+ dblatex_options= ['--quiet'] + dblatex_options
+ dbcmd = [dblatex] + dblatex_options
+ pc = subprocess.Popen(dbcmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+ (stde, _) = pc.communicate()
+ for line in stde.split('\n'):
+ if 'programlisting or screen' not in line and line.strip():
+ print(line)
+ else:
+ if fop != '':
+ run_xsltproc(['--nonet',
+ '--xinclude',
+ '--stringparam',
+ 'gtkdoc.bookname',
+ module,
+ '--stringparam',
+ 'gtkdoc.version',
+ version,
+ '--stringparam',
+ 'chunk.quietly',
+ options.quiet,
+ '--stringparam',
+ 'chunker.output.quiet',
+ quiet,
+ module,
+ document,
+ '-o',
+ module + '.fo',
+ gtkdocdir + '/gtk-doc-fo.xsl',
+ document])
+ # fop dies too easily :(
+ # @FOP@ $module.fo $module.pdf
+ else:
+ print("dblatex or fop must be installed to use gtkdoc-mkpdf.")
+ cleanexit(1)
+else:
# not very good output
# also for xxx-docs.sgml it will produce xxx-docs.pdf
- docbook2pdf -e no-valid "$document"
-fi
+ subprocess.check_call(['docbook2pdf', '-e', 'no-valid', document])
-echo "timestamp" > pdf.stamp
-cleanexit 0
+open('pdf.stamp', 'w').write('timestamp')
+cleanexit(0)
diff --git a/tests/tools.sh.in b/tests/tools.sh.in
index a114a42..232c544 100644
--- a/tests/tools.sh.in
+++ b/tests/tools.sh.in
@@ -18,7 +18,7 @@ done
# test shell scripts
-for file in gtkdoc-mkhtml gtkdoc-mkman gtkdoc-mkpdf gtkdocize; do
+for file in gtkdocize; do
sh -n `which $file`
if test $? != 0 ; then failed=`expr $failed + 1`; fi
tested=`expr $tested + 1`
@@ -34,9 +34,12 @@ done
# test python scripts
-@PYTHON@ -m py_compile `which gtkdoc-depscan`
-if test $? != 0 ; then failed=`expr $failed + 1`; fi
-tested=`expr $tested + 1`
+for file in gtkdoc-depscan gtkdoc-mkhtml gtkdoc-mkman gtkdoc-mkpdf; do
+ fullfile=`which $file`
+ @PYTHON@ -m py_compile $fullfile
+ if test $? != 0 ; then failed=`expr $failed + 1`; fi
+ tested=`expr $tested + 1`
+done
# summary
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]