blob: f92cbda1aae48c780ccc755ab04d580af39a096d [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"):
50 def abspath(path):
51 """Return an absolute path."""
52 if not os.path.isabs(path):
53 path = os.path.join(os.getcwd(), path)
54 return os.path.normpath(path)
55
56 os.path.abspath = abspath
57
58
Fred Drakefcb87252000-08-29 18:15:05 +000059MYDIR = os.path.abspath(sys.path[0])
60TOPDIR = os.path.dirname(MYDIR)
Fred Drake8b880931999-03-03 20:24:30 +000061
62ISTFILE = os.path.join(TOPDIR, "texinputs", "python.ist")
63NODE2LABEL_SCRIPT = os.path.join(MYDIR, "node2label.pl")
64L2H_INIT_FILE = os.path.join(TOPDIR, "perl", "l2hinit.perl")
65
66BIBTEX_BINARY = "bibtex"
67DVIPS_BINARY = "dvips"
68LATEX_BINARY = "latex"
69LATEX2HTML_BINARY = "latex2html"
70LYNX_BINARY = "lynx"
71MAKEINDEX_BINARY = "makeindex"
72PDFLATEX_BINARY = "pdflatex"
73PERL_BINARY = "perl"
74PYTHON_BINARY = "python"
75
76
77def usage(options):
78 print __doc__ % options
79
80def error(options, message, err=2):
81 sys.stdout = sys.stderr
82 print message
83 print
84 usage(options)
85 sys.exit(2)
86
87
88class Options:
89 program = os.path.basename(sys.argv[0])
90 #
91 address = ''
Fred Drake50d1fcf2001-02-19 19:18:09 +000092 builddir = None
Fred Drake8b880931999-03-03 20:24:30 +000093 debugging = 0
94 discard_temps = 1
95 have_temps = 0
96 icon_server = None
Fred Drake52ea0ce1999-09-22 19:55:35 +000097 image_type = "gif"
Fred Drake8b880931999-03-03 20:24:30 +000098 logging = 0
99 max_link_depth = 3
100 max_split_depth = 6
101 paper = "letter"
102 quiet = 0
Fred Drake52ea0ce1999-09-22 19:55:35 +0000103 runs = 0
Fred Drake9a257b42000-03-31 20:27:36 +0000104 numeric = 0
Fred Drake42181db2001-01-09 22:02:10 +0000105 global_module_index = None
Fred Drake8b880931999-03-03 20:24:30 +0000106 style_file = os.path.join(TOPDIR, "html", "style.css")
Fred Drakecf1b06e1999-09-23 16:55:09 +0000107 about_file = os.path.join(TOPDIR, "html", "about.dat")
Fred Drakedfa539d2000-08-31 06:58:34 +0000108 up_link = None
109 up_title = None
Fred Drake8b880931999-03-03 20:24:30 +0000110 #
Fred Drake55994412001-01-30 22:30:01 +0000111 DEFAULT_FORMATS = ("html",)
Fred Drake8b880931999-03-03 20:24:30 +0000112 ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text")
113
114 def __init__(self):
Fred Drake8b880931999-03-03 20:24:30 +0000115 self.formats = []
Fred Drake8bc627a2000-08-31 06:14:38 +0000116 self.l2h_init_files = []
Fred Drake8b880931999-03-03 20:24:30 +0000117
118 def __getitem__(self, key):
119 # This is used when formatting the usage message.
120 try:
121 return getattr(self, key)
122 except AttributeError:
123 raise KeyError, key
124
125 def parse(self, args):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000126 opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:",
Fred Drake8b880931999-03-03 20:24:30 +0000127 ["all", "postscript", "help", "iconserver=",
Fred Drake8bc627a2000-08-31 06:14:38 +0000128 "address=", "a4", "letter", "l2h-init=",
Fred Drake8b880931999-03-03 20:24:30 +0000129 "link=", "split=", "logging", "debugging",
Fred Drakecf1b06e1999-09-23 16:55:09 +0000130 "keep", "quiet", "runs=", "image-type=",
Fred Drake50d1fcf2001-02-19 19:18:09 +0000131 "about=", "numeric", "style=", "paper=",
132 "up-link=", "up-title=", "dir=",
Fred Drake42181db2001-01-09 22:02:10 +0000133 "global-module-index="]
Fred Drake52ea0ce1999-09-22 19:55:35 +0000134 + list(self.ALL_FORMATS))
Fred Drake8b880931999-03-03 20:24:30 +0000135 for opt, arg in opts:
136 if opt == "--all":
137 self.formats = list(self.ALL_FORMATS)
138 elif opt in ("-H", "--help"):
139 usage(self)
140 sys.exit()
141 elif opt == "--iconserver":
142 self.icon_server = arg
143 elif opt in ("-a", "--address"):
144 self.address = arg
145 elif opt == "--a4":
146 self.paper = "a4"
147 elif opt == "--letter":
148 self.paper = "letter"
Fred Drake8b880931999-03-03 20:24:30 +0000149 elif opt == "--link":
150 self.max_link_depth = int(arg)
151 elif opt in ("-s", "--split"):
152 self.max_split_depth = int(arg)
153 elif opt in ("-l", "--logging"):
154 self.logging = self.logging + 1
155 elif opt in ("-D", "--debugging"):
156 self.debugging = self.debugging + 1
157 elif opt in ("-k", "--keep"):
158 self.discard_temps = 0
159 elif opt in ("-q", "--quiet"):
160 self.quiet = 1
Fred Drake52ea0ce1999-09-22 19:55:35 +0000161 elif opt in ("-r", "--runs"):
162 self.runs = int(arg)
163 elif opt == "--image-type":
164 self.image_type = arg
Fred Drakecf1b06e1999-09-23 16:55:09 +0000165 elif opt == "--about":
166 # always make this absolute:
167 self.about_file = os.path.normpath(
Fred Drakefcb87252000-08-29 18:15:05 +0000168 os.path.abspath(arg))
Fred Drake9a257b42000-03-31 20:27:36 +0000169 elif opt == "--numeric":
170 self.numeric = 1
Fred Drakefcb87252000-08-29 18:15:05 +0000171 elif opt == "--style":
172 self.style_file = os.path.abspath(arg)
Fred Drake8bc627a2000-08-31 06:14:38 +0000173 elif opt == "--l2h-init":
174 self.l2h_init_files.append(os.path.abspath(arg))
Fred Drakedfa539d2000-08-31 06:58:34 +0000175 elif opt == "--up-link":
176 self.up_link = arg
177 elif opt == "--up-title":
178 self.up_title = arg
Fred Drake42181db2001-01-09 22:02:10 +0000179 elif opt == "--global-module-index":
180 self.global_module_index = arg
Fred Drake50d1fcf2001-02-19 19:18:09 +0000181 elif opt == "--dir":
182 self.builddir = arg
183 elif opt == "--paper":
184 self.paper = arg
Fred Drake8b880931999-03-03 20:24:30 +0000185 #
186 # Format specifiers:
187 #
188 elif opt[2:] in self.ALL_FORMATS:
189 self.add_format(opt[2:])
190 elif opt == "--postscript":
191 # synonym for --ps
192 self.add_format("ps")
193 self.initialize()
194 #
195 # return the args to allow the caller access:
196 #
197 return args
198
199 def add_format(self, format):
200 """Add a format to the formats list if not present."""
201 if not format in self.formats:
202 self.formats.append(format)
203
204 def initialize(self):
205 """Complete initialization. This is needed if parse() isn't used."""
206 # add the default format if no formats were specified:
207 if not self.formats:
208 self.formats = self.DEFAULT_FORMATS
209 # determine the base set of texinputs directories:
210 texinputs = string.split(os.environ.get("TEXINPUTS", ""), os.pathsep)
211 if not texinputs:
212 texinputs = ['']
213 self.base_texinputs = [
214 os.path.join(TOPDIR, "paper-" + self.paper),
215 os.path.join(TOPDIR, "texinputs"),
216 ] + texinputs
217
218
219class Job:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000220 latex_runs = 0
221
Fred Drake8b880931999-03-03 20:24:30 +0000222 def __init__(self, options, path):
223 self.options = options
Fred Drakea871c2e1999-05-06 19:37:38 +0000224 self.doctype = get_doctype(path)
Fred Drake8b880931999-03-03 20:24:30 +0000225 self.filedir, self.doc = split_pathname(path)
226 self.log_filename = self.doc + ".how"
227 if os.path.exists(self.log_filename):
228 os.unlink(self.log_filename)
229 if os.path.exists(self.doc + ".l2h"):
230 self.l2h_aux_init_file = tempfile.mktemp()
231 else:
232 self.l2h_aux_init_file = self.doc + ".l2h"
233 self.write_l2h_aux_init_file()
234
235 def build(self):
236 self.setup_texinputs()
237 formats = self.options.formats
238 if "dvi" in formats or "ps" in formats:
239 self.build_dvi()
240 if "pdf" in formats:
241 self.build_pdf()
242 if "ps" in formats:
243 self.build_ps()
244 if "html" in formats:
245 self.require_temps()
Fred Drake50d1fcf2001-02-19 19:18:09 +0000246 self.build_html(self.options.builddir or self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000247 if self.options.icon_server == ".":
Fred Drake52ea0ce1999-09-22 19:55:35 +0000248 pattern = os.path.join(TOPDIR, "html", "icons",
249 "*." + self.options.image_type)
250 imgs = glob.glob(pattern)
251 if not imgs:
252 self.warning(
253 "Could not locate support images of type %s."
254 % `self.options.image_type`)
255 for fn in imgs:
Fred Drake8b880931999-03-03 20:24:30 +0000256 new_fn = os.path.join(self.doc, os.path.basename(fn))
257 shutil.copyfile(fn, new_fn)
258 if "text" in formats:
259 self.require_temps()
260 tempdir = self.doc
261 need_html = "html" not in formats
262 if self.options.max_split_depth != 1:
263 fp = open(self.l2h_aux_init_file, "a")
264 fp.write("# re-hack this file for --text:\n")
265 l2hoption(fp, "MAX_SPLIT_DEPTH", "1")
266 fp.write("1;\n")
267 fp.close()
268 tempdir = self.doc + "-temp-html"
269 need_html = 1
270 if need_html:
271 self.build_html(tempdir, max_split_depth=1)
272 self.build_text(tempdir)
273 if self.options.discard_temps:
274 self.cleanup()
275
276 def setup_texinputs(self):
277 texinputs = [self.filedir] + list(self.options.base_texinputs)
278 os.environ["TEXINPUTS"] = string.join(texinputs, os.pathsep)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000279 self.message("TEXINPUTS=" + os.environ["TEXINPUTS"])
Fred Drake8b880931999-03-03 20:24:30 +0000280
Fred Drake8b880931999-03-03 20:24:30 +0000281 def build_aux(self, binary=None):
282 if binary is None:
283 binary = LATEX_BINARY
284 new_index( "%s.ind" % self.doc, "genindex")
285 new_index("mod%s.ind" % self.doc, "modindex")
286 self.run("%s %s" % (binary, self.doc))
287 self.use_bibtex = check_for_bibtex(self.doc + ".aux")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000288 self.latex_runs = 1
Fred Drake8b880931999-03-03 20:24:30 +0000289
290 def build_dvi(self):
291 self.use_latex(LATEX_BINARY)
292
293 def build_pdf(self):
294 self.use_latex(PDFLATEX_BINARY)
295
296 def use_latex(self, binary):
297 self.require_temps(binary=binary)
Fred Drakedf84fac2000-09-20 05:49:09 +0000298 if self.latex_runs < 2:
299 if os.path.isfile("mod%s.idx" % self.doc):
300 self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000301 use_indfix = 0
Fred Drakedf84fac2000-09-20 05:49:09 +0000302 if os.path.isfile(self.doc + ".idx"):
Fred Drake9dce7b32000-11-03 02:57:31 +0000303 use_indfix = 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000304 # call to Doc/tools/fix_hack omitted; doesn't appear necessary
305 self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc))
306 import indfix
307 indfix.process(self.doc + ".ind")
308 if self.use_bibtex:
309 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
310 self.process_synopsis_files()
311 #
312 # let the doctype-specific handler do some intermediate work:
313 #
314 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000315 self.latex_runs = self.latex_runs + 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000316 if os.path.isfile("mod%s.idx" % self.doc):
317 self.run("%s -s %s mod%s.idx"
318 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000319 if use_indfix:
Fred Drakedf84fac2000-09-20 05:49:09 +0000320 self.run("%s -s %s %s.idx"
321 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000322 indfix.process(self.doc + ".ind")
Fred Drakedf84fac2000-09-20 05:49:09 +0000323 self.process_synopsis_files()
Fred Drakea871c2e1999-05-06 19:37:38 +0000324 #
325 # and now finish it off:
326 #
327 if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY:
328 import toc2bkm
Fred Drake239e1d52000-09-05 21:45:11 +0000329 if self.doctype == "manual":
330 bigpart = "chapter"
331 else:
332 bigpart = "section"
333 toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", bigpart)
Fred Drakea871c2e1999-05-06 19:37:38 +0000334 if self.use_bibtex:
335 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
336 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000337 self.latex_runs = self.latex_runs + 1
Fred Drakea871c2e1999-05-06 19:37:38 +0000338
339 def process_synopsis_files(self):
340 synopsis_files = glob.glob(self.doc + "*.syn")
341 for path in synopsis_files:
342 uniqify_module_table(path)
Fred Drake8b880931999-03-03 20:24:30 +0000343
344 def build_ps(self):
345 self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc))
346
347 def build_html(self, builddir=None, max_split_depth=None):
348 if builddir is None:
349 builddir = self.doc
350 if max_split_depth is None:
351 max_split_depth = self.options.max_split_depth
352 texfile = None
353 for p in string.split(os.environ["TEXINPUTS"], os.pathsep):
354 fn = os.path.join(p, self.doc + ".tex")
355 if os.path.isfile(fn):
356 texfile = fn
357 break
358 if not texfile:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000359 self.warning("Could not locate %s.tex; aborting." % self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000360 sys.exit(1)
361 # remove leading ./ (or equiv.); might avoid problems w/ dvips
362 if texfile[:2] == os.curdir + os.sep:
363 texfile = texfile[2:]
364 # build the command line and run LaTeX2HTML:
Fred Drakeba828782000-04-03 04:19:14 +0000365 if not os.path.isdir(builddir):
366 os.mkdir(builddir)
Fred Drakef3d41272000-09-14 22:25:47 +0000367 else:
368 for fname in glob.glob(os.path.join(builddir, "*.html")):
369 os.unlink(fname)
Fred Drake8b880931999-03-03 20:24:30 +0000370 args = [LATEX2HTML_BINARY,
Fred Drake8b880931999-03-03 20:24:30 +0000371 "-init_file", self.l2h_aux_init_file,
372 "-dir", builddir,
373 texfile
374 ]
375 self.run(string.join(args)) # XXX need quoting!
376 # ... postprocess
377 shutil.copyfile(self.options.style_file,
378 os.path.join(builddir, self.doc + ".css"))
Fred Drake4437fdf1999-05-03 14:29:07 +0000379 shutil.copyfile(os.path.join(builddir, self.doc + ".html"),
380 os.path.join(builddir, "index.html"))
Fred Drakecfef00962001-03-02 16:26:45 +0000381 if max_split_depth != 1:
Fred Drakeaf922182001-05-09 04:03:16 +0000382 label_file = os.path.join(builddir, "labels.pl")
383 fp = open(label_file)
384 about_node = None
385 target = " = q/about/;\n"
386 x = len(target)
387 while 1:
388 line = fp.readline()
389 if not line:
390 break
391 if line[-x:] == target:
Fred Drakecfef00962001-03-02 16:26:45 +0000392 line = fp.readline()
Fred Drakeaf922182001-05-09 04:03:16 +0000393 m = re.search(r"\|(node\d+\.[a-z]+)\|", line)
394 about_node = m.group(1)
395 shutil.copyfile(os.path.join(builddir, about_node),
396 os.path.join(builddir, "about.html"))
397 break
398 if not self.options.numeric:
Fred Drakecfef00962001-03-02 16:26:45 +0000399 pwd = os.getcwd()
400 try:
401 os.chdir(builddir)
402 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT))
403 finally:
404 os.chdir(pwd)
Fred Drake8b880931999-03-03 20:24:30 +0000405
406 def build_text(self, tempdir=None):
407 if tempdir is None:
408 tempdir = self.doc
409 indexfile = os.path.join(tempdir, "index.html")
410 self.run("%s -nolist -dump %s >%s.txt"
411 % (LYNX_BINARY, indexfile, self.doc))
412
413 def require_temps(self, binary=None):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000414 if not self.latex_runs:
Fred Drake8b880931999-03-03 20:24:30 +0000415 self.build_aux(binary=binary)
416
417 def write_l2h_aux_init_file(self):
Fred Drake8bc627a2000-08-31 06:14:38 +0000418 options = self.options
Fred Drake8b880931999-03-03 20:24:30 +0000419 fp = open(self.l2h_aux_init_file, "w")
Fred Drake19157542000-07-31 17:47:49 +0000420 d = string_to_perl(os.path.dirname(L2H_INIT_FILE))
421 fp.write("package main;\n"
422 "push (@INC, '%s');\n"
423 "$mydir = '%s';\n"
424 % (d, d))
Fred Drake498c18f2000-07-24 23:03:32 +0000425 fp.write(open(L2H_INIT_FILE).read())
Fred Drake8bc627a2000-08-31 06:14:38 +0000426 for filename in options.l2h_init_files:
427 fp.write("\n# initialization code incorporated from:\n# ")
428 fp.write(filename)
429 fp.write("\n")
430 fp.write(open(filename).read())
Fred Drake498c18f2000-07-24 23:03:32 +0000431 fp.write("\n"
432 "# auxillary init file for latex2html\n"
Fred Drake8b880931999-03-03 20:24:30 +0000433 "# generated by mkhowto\n"
Fred Drake4437fdf1999-05-03 14:29:07 +0000434 "$NO_AUTO_LINK = 1;\n"
Fred Drake8b880931999-03-03 20:24:30 +0000435 )
Fred Drakecf1b06e1999-09-23 16:55:09 +0000436 l2hoption(fp, "ABOUT_FILE", options.about_file)
Fred Drake8b880931999-03-03 20:24:30 +0000437 l2hoption(fp, "ICONSERVER", options.icon_server)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000438 l2hoption(fp, "IMAGE_TYPE", options.image_type)
Fred Drake8b880931999-03-03 20:24:30 +0000439 l2hoption(fp, "ADDRESS", options.address)
440 l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth)
441 l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth)
Fred Drakedfa539d2000-08-31 06:58:34 +0000442 l2hoption(fp, "EXTERNAL_UP_LINK", options.up_link)
443 l2hoption(fp, "EXTERNAL_UP_TITLE", options.up_title)
Fred Drake42181db2001-01-09 22:02:10 +0000444 l2hoption(fp, "GLOBAL_MODULE_INDEX", options.global_module_index)
Fred Drake8b880931999-03-03 20:24:30 +0000445 fp.write("1;\n")
446 fp.close()
447
448 def cleanup(self):
449 self.__have_temps = 0
450 for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm",
Fred Drakea871c2e1999-05-06 19:37:38 +0000451 "%s.idx", "%s.ilg", "%s.ind", "%s.pla",
Fred Drake8b880931999-03-03 20:24:30 +0000452 "%s.bbl", "%s.blg",
453 "mod%s.idx", "mod%s.ind", "mod%s.ilg",
454 ):
455 safe_unlink(pattern % self.doc)
Fred Drakea871c2e1999-05-06 19:37:38 +0000456 map(safe_unlink, glob.glob(self.doc + "*.syn"))
Fred Drake8b880931999-03-03 20:24:30 +0000457 for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"):
458 pattern = os.path.join(self.doc, spec)
459 map(safe_unlink, glob.glob(pattern))
460 if "dvi" not in self.options.formats:
461 safe_unlink(self.doc + ".dvi")
462 if os.path.isdir(self.doc + "-temp-html"):
463 shutil.rmtree(self.doc + "-temp-html", ignore_errors=1)
464 if not self.options.logging:
465 os.unlink(self.log_filename)
466 if not self.options.debugging:
467 os.unlink(self.l2h_aux_init_file)
468
469 def run(self, command):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000470 self.message(command)
471 rc = os.system("(%s) </dev/null >>%s 2>&1"
472 % (command, self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000473 if rc:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000474 self.warning(
475 "Session transcript and error messages are in %s."
Fred Drake8b880931999-03-03 20:24:30 +0000476 % self.log_filename)
Fred Drake4e3f2752001-02-04 15:20:26 +0000477 sys.stderr.write("The relevant lines from the transcript are:\n")
478 sys.stderr.write("-" * 72 + "\n")
479 sys.stderr.writelines(get_run_transcript(self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000480 sys.exit(rc)
481
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000482 def message(self, msg):
483 msg = "+++ " + msg
484 if not self.options.quiet:
485 print msg
Fred Drake52ea0ce1999-09-22 19:55:35 +0000486 self.log(msg + "\n")
487
488 def warning(self, msg):
489 msg = "*** %s\n" % msg
490 sys.stderr.write(msg)
491 self.log(msg)
492
493 def log(self, msg):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000494 fp = open(self.log_filename, "a")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000495 fp.write(msg)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000496 fp.close()
497
Fred Drake8b880931999-03-03 20:24:30 +0000498
Fred Drake4e3f2752001-02-04 15:20:26 +0000499def get_run_transcript(filename):
500 """Return lines from the transcript file for the most recent run() call."""
501 fp = open(filename)
502 lines = fp.readlines()
503 fp.close()
504 lines.reverse()
505 L = []
506 for line in lines:
507 L.append(line)
508 if line[:4] == "+++ ":
509 break
510 L.reverse()
511 return L
512
513
Fred Drake8b880931999-03-03 20:24:30 +0000514def safe_unlink(path):
Fred Drake4e3f2752001-02-04 15:20:26 +0000515 """Unlink a file without raising an error if it doesn't exist."""
Fred Drake8b880931999-03-03 20:24:30 +0000516 try:
517 os.unlink(path)
518 except os.error:
519 pass
520
521
Fred Drakea871c2e1999-05-06 19:37:38 +0000522def split_pathname(path):
523 path = os.path.normpath(os.path.join(os.getcwd(), path))
524 dirname, basename = os.path.split(path)
Fred Drake8b880931999-03-03 20:24:30 +0000525 if basename[-4:] == ".tex":
526 basename = basename[:-4]
527 return dirname, basename
528
529
Fred Drakea871c2e1999-05-06 19:37:38 +0000530_doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}")
531def get_doctype(path):
532 fp = open(path)
533 doctype = None
534 while 1:
535 line = fp.readline()
536 if not line:
537 break
538 m = _doctype_rx.match(line)
539 if m:
540 doctype = m.group(1)
541 break
542 fp.close()
543 return doctype
544
545
Fred Drake8b880931999-03-03 20:24:30 +0000546def main():
547 options = Options()
548 try:
549 args = options.parse(sys.argv[1:])
550 except getopt.error, msg:
551 error(options, msg)
552 if not args:
553 # attempt to locate single .tex file in current directory:
554 args = glob.glob("*.tex")
555 if not args:
556 error(options, "No file to process.")
557 if len(args) > 1:
558 error(options, "Could not deduce which files should be processed.")
559 #
560 # parameters are processed, let's go!
561 #
562 for path in args:
563 Job(options, path).build()
564
565
566def l2hoption(fp, option, value):
567 if value:
568 fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value))))
569
570
571_to_perl = {}
572for c in map(chr, range(1, 256)):
573 _to_perl[c] = c
574_to_perl["@"] = "\\@"
575_to_perl["$"] = "\\$"
576_to_perl['"'] = '\\"'
577
578def string_to_perl(s):
579 return string.join(map(_to_perl.get, s), '')
580
581
582def check_for_bibtex(filename):
583 fp = open(filename)
584 pos = string.find(fp.read(), r"\bibdata{")
585 fp.close()
586 return pos >= 0
587
588def uniqify_module_table(filename):
589 lines = open(filename).readlines()
590 if len(lines) > 1:
591 if lines[-1] == lines[-2]:
592 del lines[-1]
593 open(filename, "w").writelines(lines)
594
595
596def new_index(filename, label="genindex"):
597 fp = open(filename, "w")
598 fp.write(r"""\
599\begin{theindex}
600\label{%s}
601\end{theindex}
602""" % label)
603 fp.close()
604
605
606if __name__ == "__main__":
607 main()