blob: 0d5d687e2981136a6e287fff4049a434ce5cca49 [file] [log] [blame]
Fred Drake8b880931999-03-03 20:24:30 +00001#! /usr/bin/env python
2# -*- Python -*-
3"""usage: %(program)s [options...] file ...
4
5Options specifying formats to build:
Fred Drake55994412001-01-30 22:30:01 +00006 --html HyperText Markup Language (default)
7 --pdf Portable Document Format
Fred Drake8b880931999-03-03 20:24:30 +00008 --ps PostScript
9 --dvi 'DeVice Indepentent' format from TeX
10 --text ASCII text (requires lynx)
11
12 More than one output format may be specified, or --all.
13
14HTML options:
15 --address, -a Specify an address for page footers.
16 --link Specify the number of levels to include on each page.
17 --split, -s Specify a section level for page splitting, default: %(max_split_depth)s.
18 --iconserver, -i Specify location of icons (default: ../).
Fred Drake52ea0ce1999-09-22 19:55:35 +000019 --image-type Specify the image type to use in HTML output;
20 values: gif (default), png.
Fred Drake9a257b42000-03-31 20:27:36 +000021 --numeric Don't rename the HTML files; just keep node#.html for
22 the filenames.
Fred Drakefcb87252000-08-29 18:15:05 +000023 --style Specify the CSS file to use for the output (filename,
24 not a URL).
Fred Drakedfa539d2000-08-31 06:58:34 +000025 --up-link URL to a parent document.
26 --up-title Title of a parent document.
Fred Drake8b880931999-03-03 20:24:30 +000027
28Other options:
29 --a4 Format for A4 paper.
30 --letter Format for US letter paper (the default).
31 --help, -H Show this text.
32 --logging, -l Log stdout and stderr to a file (*.how).
33 --debugging, -D Echo commands as they are executed.
34 --keep, -k Keep temporary files around.
35 --quiet, -q Do not print command output to stdout.
36 (stderr is also lost, sorry; see *.how for errors)
37"""
38
39import getopt
40import glob
41import os
Fred Drakea871c2e1999-05-06 19:37:38 +000042import re
Fred Drake8b880931999-03-03 20:24:30 +000043import shutil
44import string
45import sys
46import tempfile
47
48
Fred Drake964c0742001-05-29 16:10:07 +000049if not hasattr(os.path, "abspath"):
Fred Drakebfd80dd2001-06-23 03:06:01 +000050 # Python 1.5.1 or earlier
Fred Drake964c0742001-05-29 16:10:07 +000051 def abspath(path):
52 """Return an absolute path."""
53 if not os.path.isabs(path):
54 path = os.path.join(os.getcwd(), path)
55 return os.path.normpath(path)
56
57 os.path.abspath = abspath
58
59
Fred Drakefcb87252000-08-29 18:15:05 +000060MYDIR = os.path.abspath(sys.path[0])
61TOPDIR = os.path.dirname(MYDIR)
Fred Drake8b880931999-03-03 20:24:30 +000062
63ISTFILE = os.path.join(TOPDIR, "texinputs", "python.ist")
64NODE2LABEL_SCRIPT = os.path.join(MYDIR, "node2label.pl")
65L2H_INIT_FILE = os.path.join(TOPDIR, "perl", "l2hinit.perl")
66
67BIBTEX_BINARY = "bibtex"
68DVIPS_BINARY = "dvips"
69LATEX_BINARY = "latex"
70LATEX2HTML_BINARY = "latex2html"
71LYNX_BINARY = "lynx"
72MAKEINDEX_BINARY = "makeindex"
73PDFLATEX_BINARY = "pdflatex"
74PERL_BINARY = "perl"
75PYTHON_BINARY = "python"
76
77
78def usage(options):
79 print __doc__ % options
80
81def error(options, message, err=2):
82 sys.stdout = sys.stderr
83 print message
84 print
85 usage(options)
86 sys.exit(2)
87
88
89class Options:
90 program = os.path.basename(sys.argv[0])
91 #
92 address = ''
Fred Drake50d1fcf2001-02-19 19:18:09 +000093 builddir = None
Fred Drake8b880931999-03-03 20:24:30 +000094 debugging = 0
95 discard_temps = 1
96 have_temps = 0
97 icon_server = None
Fred Drake52ea0ce1999-09-22 19:55:35 +000098 image_type = "gif"
Fred Drake8b880931999-03-03 20:24:30 +000099 logging = 0
100 max_link_depth = 3
101 max_split_depth = 6
102 paper = "letter"
103 quiet = 0
Fred Drake52ea0ce1999-09-22 19:55:35 +0000104 runs = 0
Fred Drake9a257b42000-03-31 20:27:36 +0000105 numeric = 0
Fred Drake42181db2001-01-09 22:02:10 +0000106 global_module_index = None
Fred Drake8b880931999-03-03 20:24:30 +0000107 style_file = os.path.join(TOPDIR, "html", "style.css")
Fred Drakecf1b06e1999-09-23 16:55:09 +0000108 about_file = os.path.join(TOPDIR, "html", "about.dat")
Fred Drakedfa539d2000-08-31 06:58:34 +0000109 up_link = None
110 up_title = None
Fred Drake8b880931999-03-03 20:24:30 +0000111 #
Fred Drake55994412001-01-30 22:30:01 +0000112 DEFAULT_FORMATS = ("html",)
Fred Drake8b880931999-03-03 20:24:30 +0000113 ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text")
114
115 def __init__(self):
Fred Drake8b880931999-03-03 20:24:30 +0000116 self.formats = []
Fred Drake8bc627a2000-08-31 06:14:38 +0000117 self.l2h_init_files = []
Fred Drake8b880931999-03-03 20:24:30 +0000118
119 def __getitem__(self, key):
120 # This is used when formatting the usage message.
121 try:
122 return getattr(self, key)
123 except AttributeError:
124 raise KeyError, key
125
126 def parse(self, args):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000127 opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:",
Fred Drake8b880931999-03-03 20:24:30 +0000128 ["all", "postscript", "help", "iconserver=",
Fred Drake8bc627a2000-08-31 06:14:38 +0000129 "address=", "a4", "letter", "l2h-init=",
Fred Drake8b880931999-03-03 20:24:30 +0000130 "link=", "split=", "logging", "debugging",
Fred Drakecf1b06e1999-09-23 16:55:09 +0000131 "keep", "quiet", "runs=", "image-type=",
Fred Drake50d1fcf2001-02-19 19:18:09 +0000132 "about=", "numeric", "style=", "paper=",
133 "up-link=", "up-title=", "dir=",
Fred Drake42181db2001-01-09 22:02:10 +0000134 "global-module-index="]
Fred Drake52ea0ce1999-09-22 19:55:35 +0000135 + list(self.ALL_FORMATS))
Fred Drake8b880931999-03-03 20:24:30 +0000136 for opt, arg in opts:
137 if opt == "--all":
138 self.formats = list(self.ALL_FORMATS)
139 elif opt in ("-H", "--help"):
140 usage(self)
141 sys.exit()
142 elif opt == "--iconserver":
143 self.icon_server = arg
144 elif opt in ("-a", "--address"):
145 self.address = arg
146 elif opt == "--a4":
147 self.paper = "a4"
148 elif opt == "--letter":
149 self.paper = "letter"
Fred Drake8b880931999-03-03 20:24:30 +0000150 elif opt == "--link":
151 self.max_link_depth = int(arg)
152 elif opt in ("-s", "--split"):
153 self.max_split_depth = int(arg)
154 elif opt in ("-l", "--logging"):
155 self.logging = self.logging + 1
156 elif opt in ("-D", "--debugging"):
157 self.debugging = self.debugging + 1
158 elif opt in ("-k", "--keep"):
159 self.discard_temps = 0
160 elif opt in ("-q", "--quiet"):
161 self.quiet = 1
Fred Drake52ea0ce1999-09-22 19:55:35 +0000162 elif opt in ("-r", "--runs"):
163 self.runs = int(arg)
164 elif opt == "--image-type":
165 self.image_type = arg
Fred Drakecf1b06e1999-09-23 16:55:09 +0000166 elif opt == "--about":
167 # always make this absolute:
168 self.about_file = os.path.normpath(
Fred Drakefcb87252000-08-29 18:15:05 +0000169 os.path.abspath(arg))
Fred Drake9a257b42000-03-31 20:27:36 +0000170 elif opt == "--numeric":
171 self.numeric = 1
Fred Drakefcb87252000-08-29 18:15:05 +0000172 elif opt == "--style":
173 self.style_file = os.path.abspath(arg)
Fred Drake8bc627a2000-08-31 06:14:38 +0000174 elif opt == "--l2h-init":
175 self.l2h_init_files.append(os.path.abspath(arg))
Fred Drakedfa539d2000-08-31 06:58:34 +0000176 elif opt == "--up-link":
177 self.up_link = arg
178 elif opt == "--up-title":
179 self.up_title = arg
Fred Drake42181db2001-01-09 22:02:10 +0000180 elif opt == "--global-module-index":
181 self.global_module_index = arg
Fred Drake50d1fcf2001-02-19 19:18:09 +0000182 elif opt == "--dir":
183 self.builddir = arg
184 elif opt == "--paper":
185 self.paper = arg
Fred Drake8b880931999-03-03 20:24:30 +0000186 #
187 # Format specifiers:
188 #
189 elif opt[2:] in self.ALL_FORMATS:
190 self.add_format(opt[2:])
191 elif opt == "--postscript":
192 # synonym for --ps
193 self.add_format("ps")
194 self.initialize()
195 #
196 # return the args to allow the caller access:
197 #
198 return args
199
200 def add_format(self, format):
201 """Add a format to the formats list if not present."""
202 if not format in self.formats:
203 self.formats.append(format)
204
205 def initialize(self):
206 """Complete initialization. This is needed if parse() isn't used."""
207 # add the default format if no formats were specified:
208 if not self.formats:
209 self.formats = self.DEFAULT_FORMATS
210 # determine the base set of texinputs directories:
211 texinputs = string.split(os.environ.get("TEXINPUTS", ""), os.pathsep)
212 if not texinputs:
213 texinputs = ['']
214 self.base_texinputs = [
215 os.path.join(TOPDIR, "paper-" + self.paper),
216 os.path.join(TOPDIR, "texinputs"),
217 ] + texinputs
Fred Drakebfd80dd2001-06-23 03:06:01 +0000218 if self.builddir:
219 self.builddir = os.path.abspath(self.builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000220
221
222class Job:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000223 latex_runs = 0
224
Fred Drake8b880931999-03-03 20:24:30 +0000225 def __init__(self, options, path):
226 self.options = options
Fred Drakea871c2e1999-05-06 19:37:38 +0000227 self.doctype = get_doctype(path)
Fred Drake8b880931999-03-03 20:24:30 +0000228 self.filedir, self.doc = split_pathname(path)
Fred Drakebfd80dd2001-06-23 03:06:01 +0000229 self.builddir = os.path.abspath(options.builddir or self.doc)
Fred Drakeaebbca32001-07-17 14:46:09 +0000230 if ("html" in options.formats or "text" in options.formats):
231 if not os.path.exists(self.builddir):
232 os.mkdir(self.builddir)
233 self.log_filename = os.path.join(self.builddir, self.doc + ".how")
234 else:
235 self.log_filename = os.path.abspath(self.doc + ".how")
Fred Drake8b880931999-03-03 20:24:30 +0000236 if os.path.exists(self.log_filename):
237 os.unlink(self.log_filename)
238 if os.path.exists(self.doc + ".l2h"):
239 self.l2h_aux_init_file = tempfile.mktemp()
240 else:
241 self.l2h_aux_init_file = self.doc + ".l2h"
242 self.write_l2h_aux_init_file()
243
244 def build(self):
245 self.setup_texinputs()
246 formats = self.options.formats
247 if "dvi" in formats or "ps" in formats:
248 self.build_dvi()
249 if "pdf" in formats:
250 self.build_pdf()
251 if "ps" in formats:
252 self.build_ps()
253 if "html" in formats:
254 self.require_temps()
Fred Drakebfd80dd2001-06-23 03:06:01 +0000255 self.build_html(self.builddir)
Fred Drake8b880931999-03-03 20:24:30 +0000256 if self.options.icon_server == ".":
Fred Drake52ea0ce1999-09-22 19:55:35 +0000257 pattern = os.path.join(TOPDIR, "html", "icons",
258 "*." + self.options.image_type)
259 imgs = glob.glob(pattern)
260 if not imgs:
261 self.warning(
262 "Could not locate support images of type %s."
263 % `self.options.image_type`)
264 for fn in imgs:
Fred Drake8b880931999-03-03 20:24:30 +0000265 new_fn = os.path.join(self.doc, os.path.basename(fn))
266 shutil.copyfile(fn, new_fn)
267 if "text" in formats:
268 self.require_temps()
269 tempdir = self.doc
270 need_html = "html" not in formats
271 if self.options.max_split_depth != 1:
272 fp = open(self.l2h_aux_init_file, "a")
273 fp.write("# re-hack this file for --text:\n")
274 l2hoption(fp, "MAX_SPLIT_DEPTH", "1")
275 fp.write("1;\n")
276 fp.close()
277 tempdir = self.doc + "-temp-html"
278 need_html = 1
279 if need_html:
280 self.build_html(tempdir, max_split_depth=1)
281 self.build_text(tempdir)
282 if self.options.discard_temps:
283 self.cleanup()
284
285 def setup_texinputs(self):
286 texinputs = [self.filedir] + list(self.options.base_texinputs)
287 os.environ["TEXINPUTS"] = string.join(texinputs, os.pathsep)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000288 self.message("TEXINPUTS=" + os.environ["TEXINPUTS"])
Fred Drake8b880931999-03-03 20:24:30 +0000289
Fred Drake8b880931999-03-03 20:24:30 +0000290 def build_aux(self, binary=None):
291 if binary is None:
292 binary = LATEX_BINARY
293 new_index( "%s.ind" % self.doc, "genindex")
294 new_index("mod%s.ind" % self.doc, "modindex")
295 self.run("%s %s" % (binary, self.doc))
296 self.use_bibtex = check_for_bibtex(self.doc + ".aux")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000297 self.latex_runs = 1
Fred Drake8b880931999-03-03 20:24:30 +0000298
299 def build_dvi(self):
300 self.use_latex(LATEX_BINARY)
301
302 def build_pdf(self):
303 self.use_latex(PDFLATEX_BINARY)
304
305 def use_latex(self, binary):
306 self.require_temps(binary=binary)
Fred Drakedf84fac2000-09-20 05:49:09 +0000307 if self.latex_runs < 2:
308 if os.path.isfile("mod%s.idx" % self.doc):
309 self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000310 use_indfix = 0
Fred Drakedf84fac2000-09-20 05:49:09 +0000311 if os.path.isfile(self.doc + ".idx"):
Fred Drake9dce7b32000-11-03 02:57:31 +0000312 use_indfix = 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000313 # call to Doc/tools/fix_hack omitted; doesn't appear necessary
314 self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc))
315 import indfix
316 indfix.process(self.doc + ".ind")
317 if self.use_bibtex:
318 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
319 self.process_synopsis_files()
320 #
321 # let the doctype-specific handler do some intermediate work:
322 #
323 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000324 self.latex_runs = self.latex_runs + 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000325 if os.path.isfile("mod%s.idx" % self.doc):
326 self.run("%s -s %s mod%s.idx"
327 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000328 if use_indfix:
Fred Drakedf84fac2000-09-20 05:49:09 +0000329 self.run("%s -s %s %s.idx"
330 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000331 indfix.process(self.doc + ".ind")
Fred Drakedf84fac2000-09-20 05:49:09 +0000332 self.process_synopsis_files()
Fred Drakea871c2e1999-05-06 19:37:38 +0000333 #
334 # and now finish it off:
335 #
336 if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY:
337 import toc2bkm
Fred Drake239e1d52000-09-05 21:45:11 +0000338 if self.doctype == "manual":
339 bigpart = "chapter"
340 else:
341 bigpart = "section"
342 toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", bigpart)
Fred Drakea871c2e1999-05-06 19:37:38 +0000343 if self.use_bibtex:
344 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
345 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000346 self.latex_runs = self.latex_runs + 1
Fred Drakea871c2e1999-05-06 19:37:38 +0000347
348 def process_synopsis_files(self):
349 synopsis_files = glob.glob(self.doc + "*.syn")
350 for path in synopsis_files:
351 uniqify_module_table(path)
Fred Drake8b880931999-03-03 20:24:30 +0000352
353 def build_ps(self):
354 self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc))
355
Fred Drakeaebbca32001-07-17 14:46:09 +0000356 def build_html(self, builddir, max_split_depth=None):
Fred Drake8b880931999-03-03 20:24:30 +0000357 if max_split_depth is None:
358 max_split_depth = self.options.max_split_depth
359 texfile = None
360 for p in string.split(os.environ["TEXINPUTS"], os.pathsep):
361 fn = os.path.join(p, self.doc + ".tex")
362 if os.path.isfile(fn):
363 texfile = fn
364 break
365 if not texfile:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000366 self.warning("Could not locate %s.tex; aborting." % self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000367 sys.exit(1)
368 # remove leading ./ (or equiv.); might avoid problems w/ dvips
369 if texfile[:2] == os.curdir + os.sep:
370 texfile = texfile[2:]
371 # build the command line and run LaTeX2HTML:
Fred Drakeba828782000-04-03 04:19:14 +0000372 if not os.path.isdir(builddir):
373 os.mkdir(builddir)
Fred Drakef3d41272000-09-14 22:25:47 +0000374 else:
375 for fname in glob.glob(os.path.join(builddir, "*.html")):
376 os.unlink(fname)
Fred Drake8b880931999-03-03 20:24:30 +0000377 args = [LATEX2HTML_BINARY,
Fred Drake8b880931999-03-03 20:24:30 +0000378 "-init_file", self.l2h_aux_init_file,
379 "-dir", builddir,
380 texfile
381 ]
382 self.run(string.join(args)) # XXX need quoting!
383 # ... postprocess
384 shutil.copyfile(self.options.style_file,
385 os.path.join(builddir, self.doc + ".css"))
Fred Drake4437fdf1999-05-03 14:29:07 +0000386 shutil.copyfile(os.path.join(builddir, self.doc + ".html"),
387 os.path.join(builddir, "index.html"))
Fred Drakecfef00962001-03-02 16:26:45 +0000388 if max_split_depth != 1:
Fred Drakeaf922182001-05-09 04:03:16 +0000389 label_file = os.path.join(builddir, "labels.pl")
390 fp = open(label_file)
391 about_node = None
392 target = " = q/about/;\n"
393 x = len(target)
394 while 1:
395 line = fp.readline()
396 if not line:
397 break
398 if line[-x:] == target:
Fred Drakecfef00962001-03-02 16:26:45 +0000399 line = fp.readline()
Fred Drakeaf922182001-05-09 04:03:16 +0000400 m = re.search(r"\|(node\d+\.[a-z]+)\|", line)
401 about_node = m.group(1)
402 shutil.copyfile(os.path.join(builddir, about_node),
403 os.path.join(builddir, "about.html"))
404 break
405 if not self.options.numeric:
Fred Drakecfef00962001-03-02 16:26:45 +0000406 pwd = os.getcwd()
407 try:
408 os.chdir(builddir)
409 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT))
410 finally:
411 os.chdir(pwd)
Fred Drake8b880931999-03-03 20:24:30 +0000412
413 def build_text(self, tempdir=None):
414 if tempdir is None:
415 tempdir = self.doc
416 indexfile = os.path.join(tempdir, "index.html")
417 self.run("%s -nolist -dump %s >%s.txt"
418 % (LYNX_BINARY, indexfile, self.doc))
419
420 def require_temps(self, binary=None):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000421 if not self.latex_runs:
Fred Drake8b880931999-03-03 20:24:30 +0000422 self.build_aux(binary=binary)
423
424 def write_l2h_aux_init_file(self):
Fred Drake8bc627a2000-08-31 06:14:38 +0000425 options = self.options
Fred Drake8b880931999-03-03 20:24:30 +0000426 fp = open(self.l2h_aux_init_file, "w")
Fred Drake19157542000-07-31 17:47:49 +0000427 d = string_to_perl(os.path.dirname(L2H_INIT_FILE))
428 fp.write("package main;\n"
429 "push (@INC, '%s');\n"
430 "$mydir = '%s';\n"
431 % (d, d))
Fred Drake498c18f2000-07-24 23:03:32 +0000432 fp.write(open(L2H_INIT_FILE).read())
Fred Drake8bc627a2000-08-31 06:14:38 +0000433 for filename in options.l2h_init_files:
434 fp.write("\n# initialization code incorporated from:\n# ")
435 fp.write(filename)
436 fp.write("\n")
437 fp.write(open(filename).read())
Fred Drake498c18f2000-07-24 23:03:32 +0000438 fp.write("\n"
439 "# auxillary init file for latex2html\n"
Fred Drake8b880931999-03-03 20:24:30 +0000440 "# generated by mkhowto\n"
Fred Drake4437fdf1999-05-03 14:29:07 +0000441 "$NO_AUTO_LINK = 1;\n"
Fred Drake8b880931999-03-03 20:24:30 +0000442 )
Fred Drakecf1b06e1999-09-23 16:55:09 +0000443 l2hoption(fp, "ABOUT_FILE", options.about_file)
Fred Drake8b880931999-03-03 20:24:30 +0000444 l2hoption(fp, "ICONSERVER", options.icon_server)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000445 l2hoption(fp, "IMAGE_TYPE", options.image_type)
Fred Drake8b880931999-03-03 20:24:30 +0000446 l2hoption(fp, "ADDRESS", options.address)
447 l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth)
448 l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth)
Fred Drakedfa539d2000-08-31 06:58:34 +0000449 l2hoption(fp, "EXTERNAL_UP_LINK", options.up_link)
450 l2hoption(fp, "EXTERNAL_UP_TITLE", options.up_title)
Fred Drake42181db2001-01-09 22:02:10 +0000451 l2hoption(fp, "GLOBAL_MODULE_INDEX", options.global_module_index)
Fred Drake8b880931999-03-03 20:24:30 +0000452 fp.write("1;\n")
453 fp.close()
454
455 def cleanup(self):
456 self.__have_temps = 0
457 for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm",
Fred Drakea871c2e1999-05-06 19:37:38 +0000458 "%s.idx", "%s.ilg", "%s.ind", "%s.pla",
Fred Drake8b880931999-03-03 20:24:30 +0000459 "%s.bbl", "%s.blg",
460 "mod%s.idx", "mod%s.ind", "mod%s.ilg",
461 ):
462 safe_unlink(pattern % self.doc)
Fred Drakea871c2e1999-05-06 19:37:38 +0000463 map(safe_unlink, glob.glob(self.doc + "*.syn"))
Fred Drake8b880931999-03-03 20:24:30 +0000464 for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"):
465 pattern = os.path.join(self.doc, spec)
466 map(safe_unlink, glob.glob(pattern))
467 if "dvi" not in self.options.formats:
468 safe_unlink(self.doc + ".dvi")
469 if os.path.isdir(self.doc + "-temp-html"):
470 shutil.rmtree(self.doc + "-temp-html", ignore_errors=1)
471 if not self.options.logging:
472 os.unlink(self.log_filename)
473 if not self.options.debugging:
474 os.unlink(self.l2h_aux_init_file)
475
476 def run(self, command):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000477 self.message(command)
478 rc = os.system("(%s) </dev/null >>%s 2>&1"
479 % (command, self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000480 if rc:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000481 self.warning(
482 "Session transcript and error messages are in %s."
Fred Drake8b880931999-03-03 20:24:30 +0000483 % self.log_filename)
Fred Drake4e3f2752001-02-04 15:20:26 +0000484 sys.stderr.write("The relevant lines from the transcript are:\n")
485 sys.stderr.write("-" * 72 + "\n")
486 sys.stderr.writelines(get_run_transcript(self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000487 sys.exit(rc)
488
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000489 def message(self, msg):
490 msg = "+++ " + msg
491 if not self.options.quiet:
492 print msg
Fred Drake52ea0ce1999-09-22 19:55:35 +0000493 self.log(msg + "\n")
494
495 def warning(self, msg):
496 msg = "*** %s\n" % msg
497 sys.stderr.write(msg)
498 self.log(msg)
499
500 def log(self, msg):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000501 fp = open(self.log_filename, "a")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000502 fp.write(msg)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000503 fp.close()
504
Fred Drake8b880931999-03-03 20:24:30 +0000505
Fred Drake4e3f2752001-02-04 15:20:26 +0000506def get_run_transcript(filename):
507 """Return lines from the transcript file for the most recent run() call."""
508 fp = open(filename)
509 lines = fp.readlines()
510 fp.close()
511 lines.reverse()
512 L = []
513 for line in lines:
514 L.append(line)
515 if line[:4] == "+++ ":
516 break
517 L.reverse()
518 return L
519
520
Fred Drake8b880931999-03-03 20:24:30 +0000521def safe_unlink(path):
Fred Drake4e3f2752001-02-04 15:20:26 +0000522 """Unlink a file without raising an error if it doesn't exist."""
Fred Drake8b880931999-03-03 20:24:30 +0000523 try:
524 os.unlink(path)
525 except os.error:
526 pass
527
528
Fred Drakea871c2e1999-05-06 19:37:38 +0000529def split_pathname(path):
Fred Drakebfd80dd2001-06-23 03:06:01 +0000530 path = os.path.abspath(path)
Fred Drakea871c2e1999-05-06 19:37:38 +0000531 dirname, basename = os.path.split(path)
Fred Drake8b880931999-03-03 20:24:30 +0000532 if basename[-4:] == ".tex":
533 basename = basename[:-4]
534 return dirname, basename
535
536
Fred Drakea871c2e1999-05-06 19:37:38 +0000537_doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}")
538def get_doctype(path):
539 fp = open(path)
540 doctype = None
541 while 1:
542 line = fp.readline()
543 if not line:
544 break
545 m = _doctype_rx.match(line)
546 if m:
547 doctype = m.group(1)
548 break
549 fp.close()
550 return doctype
551
552
Fred Drake8b880931999-03-03 20:24:30 +0000553def main():
554 options = Options()
555 try:
556 args = options.parse(sys.argv[1:])
557 except getopt.error, msg:
558 error(options, msg)
559 if not args:
560 # attempt to locate single .tex file in current directory:
561 args = glob.glob("*.tex")
562 if not args:
563 error(options, "No file to process.")
564 if len(args) > 1:
565 error(options, "Could not deduce which files should be processed.")
566 #
567 # parameters are processed, let's go!
568 #
569 for path in args:
570 Job(options, path).build()
571
572
573def l2hoption(fp, option, value):
574 if value:
575 fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value))))
576
577
578_to_perl = {}
579for c in map(chr, range(1, 256)):
580 _to_perl[c] = c
581_to_perl["@"] = "\\@"
582_to_perl["$"] = "\\$"
583_to_perl['"'] = '\\"'
584
585def string_to_perl(s):
586 return string.join(map(_to_perl.get, s), '')
587
588
589def check_for_bibtex(filename):
590 fp = open(filename)
591 pos = string.find(fp.read(), r"\bibdata{")
592 fp.close()
593 return pos >= 0
594
595def uniqify_module_table(filename):
596 lines = open(filename).readlines()
597 if len(lines) > 1:
598 if lines[-1] == lines[-2]:
599 del lines[-1]
600 open(filename, "w").writelines(lines)
601
602
603def new_index(filename, label="genindex"):
604 fp = open(filename, "w")
605 fp.write(r"""\
606\begin{theindex}
607\label{%s}
608\end{theindex}
609""" % label)
610 fp.close()
611
612
613if __name__ == "__main__":
614 main()