blob: a1784c99aa8caafbd73c86d3527bf53007730257 [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 Drakefcb87252000-08-29 18:15:05 +000049MYDIR = os.path.abspath(sys.path[0])
50TOPDIR = os.path.dirname(MYDIR)
Fred Drake8b880931999-03-03 20:24:30 +000051
52ISTFILE = os.path.join(TOPDIR, "texinputs", "python.ist")
53NODE2LABEL_SCRIPT = os.path.join(MYDIR, "node2label.pl")
54L2H_INIT_FILE = os.path.join(TOPDIR, "perl", "l2hinit.perl")
55
56BIBTEX_BINARY = "bibtex"
57DVIPS_BINARY = "dvips"
58LATEX_BINARY = "latex"
59LATEX2HTML_BINARY = "latex2html"
60LYNX_BINARY = "lynx"
61MAKEINDEX_BINARY = "makeindex"
62PDFLATEX_BINARY = "pdflatex"
63PERL_BINARY = "perl"
64PYTHON_BINARY = "python"
65
66
67def usage(options):
68 print __doc__ % options
69
70def error(options, message, err=2):
71 sys.stdout = sys.stderr
72 print message
73 print
74 usage(options)
75 sys.exit(2)
76
77
78class Options:
79 program = os.path.basename(sys.argv[0])
80 #
81 address = ''
Fred Drake50d1fcf2001-02-19 19:18:09 +000082 builddir = None
Fred Drake8b880931999-03-03 20:24:30 +000083 debugging = 0
84 discard_temps = 1
85 have_temps = 0
86 icon_server = None
Fred Drake52ea0ce1999-09-22 19:55:35 +000087 image_type = "gif"
Fred Drake8b880931999-03-03 20:24:30 +000088 logging = 0
89 max_link_depth = 3
90 max_split_depth = 6
91 paper = "letter"
92 quiet = 0
Fred Drake52ea0ce1999-09-22 19:55:35 +000093 runs = 0
Fred Drake9a257b42000-03-31 20:27:36 +000094 numeric = 0
Fred Drake42181db2001-01-09 22:02:10 +000095 global_module_index = None
Fred Drake8b880931999-03-03 20:24:30 +000096 style_file = os.path.join(TOPDIR, "html", "style.css")
Fred Drakecf1b06e1999-09-23 16:55:09 +000097 about_file = os.path.join(TOPDIR, "html", "about.dat")
Fred Drakedfa539d2000-08-31 06:58:34 +000098 up_link = None
99 up_title = None
Fred Drake8b880931999-03-03 20:24:30 +0000100 #
Fred Drake55994412001-01-30 22:30:01 +0000101 DEFAULT_FORMATS = ("html",)
Fred Drake8b880931999-03-03 20:24:30 +0000102 ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text")
103
104 def __init__(self):
Fred Drake8b880931999-03-03 20:24:30 +0000105 self.formats = []
Fred Drake8bc627a2000-08-31 06:14:38 +0000106 self.l2h_init_files = []
Fred Drake8b880931999-03-03 20:24:30 +0000107
108 def __getitem__(self, key):
109 # This is used when formatting the usage message.
110 try:
111 return getattr(self, key)
112 except AttributeError:
113 raise KeyError, key
114
115 def parse(self, args):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000116 opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:",
Fred Drake8b880931999-03-03 20:24:30 +0000117 ["all", "postscript", "help", "iconserver=",
Fred Drake8bc627a2000-08-31 06:14:38 +0000118 "address=", "a4", "letter", "l2h-init=",
Fred Drake8b880931999-03-03 20:24:30 +0000119 "link=", "split=", "logging", "debugging",
Fred Drakecf1b06e1999-09-23 16:55:09 +0000120 "keep", "quiet", "runs=", "image-type=",
Fred Drake50d1fcf2001-02-19 19:18:09 +0000121 "about=", "numeric", "style=", "paper=",
122 "up-link=", "up-title=", "dir=",
Fred Drake42181db2001-01-09 22:02:10 +0000123 "global-module-index="]
Fred Drake52ea0ce1999-09-22 19:55:35 +0000124 + list(self.ALL_FORMATS))
Fred Drake8b880931999-03-03 20:24:30 +0000125 for opt, arg in opts:
126 if opt == "--all":
127 self.formats = list(self.ALL_FORMATS)
128 elif opt in ("-H", "--help"):
129 usage(self)
130 sys.exit()
131 elif opt == "--iconserver":
132 self.icon_server = arg
133 elif opt in ("-a", "--address"):
134 self.address = arg
135 elif opt == "--a4":
136 self.paper = "a4"
137 elif opt == "--letter":
138 self.paper = "letter"
Fred Drake8b880931999-03-03 20:24:30 +0000139 elif opt == "--link":
140 self.max_link_depth = int(arg)
141 elif opt in ("-s", "--split"):
142 self.max_split_depth = int(arg)
143 elif opt in ("-l", "--logging"):
144 self.logging = self.logging + 1
145 elif opt in ("-D", "--debugging"):
146 self.debugging = self.debugging + 1
147 elif opt in ("-k", "--keep"):
148 self.discard_temps = 0
149 elif opt in ("-q", "--quiet"):
150 self.quiet = 1
Fred Drake52ea0ce1999-09-22 19:55:35 +0000151 elif opt in ("-r", "--runs"):
152 self.runs = int(arg)
153 elif opt == "--image-type":
154 self.image_type = arg
Fred Drakecf1b06e1999-09-23 16:55:09 +0000155 elif opt == "--about":
156 # always make this absolute:
157 self.about_file = os.path.normpath(
Fred Drakefcb87252000-08-29 18:15:05 +0000158 os.path.abspath(arg))
Fred Drake9a257b42000-03-31 20:27:36 +0000159 elif opt == "--numeric":
160 self.numeric = 1
Fred Drakefcb87252000-08-29 18:15:05 +0000161 elif opt == "--style":
162 self.style_file = os.path.abspath(arg)
Fred Drake8bc627a2000-08-31 06:14:38 +0000163 elif opt == "--l2h-init":
164 self.l2h_init_files.append(os.path.abspath(arg))
Fred Drakedfa539d2000-08-31 06:58:34 +0000165 elif opt == "--up-link":
166 self.up_link = arg
167 elif opt == "--up-title":
168 self.up_title = arg
Fred Drake42181db2001-01-09 22:02:10 +0000169 elif opt == "--global-module-index":
170 self.global_module_index = arg
Fred Drake50d1fcf2001-02-19 19:18:09 +0000171 elif opt == "--dir":
172 self.builddir = arg
173 elif opt == "--paper":
174 self.paper = arg
Fred Drake8b880931999-03-03 20:24:30 +0000175 #
176 # Format specifiers:
177 #
178 elif opt[2:] in self.ALL_FORMATS:
179 self.add_format(opt[2:])
180 elif opt == "--postscript":
181 # synonym for --ps
182 self.add_format("ps")
183 self.initialize()
184 #
185 # return the args to allow the caller access:
186 #
187 return args
188
189 def add_format(self, format):
190 """Add a format to the formats list if not present."""
191 if not format in self.formats:
192 self.formats.append(format)
193
194 def initialize(self):
195 """Complete initialization. This is needed if parse() isn't used."""
196 # add the default format if no formats were specified:
197 if not self.formats:
198 self.formats = self.DEFAULT_FORMATS
199 # determine the base set of texinputs directories:
200 texinputs = string.split(os.environ.get("TEXINPUTS", ""), os.pathsep)
201 if not texinputs:
202 texinputs = ['']
203 self.base_texinputs = [
204 os.path.join(TOPDIR, "paper-" + self.paper),
205 os.path.join(TOPDIR, "texinputs"),
206 ] + texinputs
207
208
209class Job:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000210 latex_runs = 0
211
Fred Drake8b880931999-03-03 20:24:30 +0000212 def __init__(self, options, path):
213 self.options = options
Fred Drakea871c2e1999-05-06 19:37:38 +0000214 self.doctype = get_doctype(path)
Fred Drake8b880931999-03-03 20:24:30 +0000215 self.filedir, self.doc = split_pathname(path)
216 self.log_filename = self.doc + ".how"
217 if os.path.exists(self.log_filename):
218 os.unlink(self.log_filename)
219 if os.path.exists(self.doc + ".l2h"):
220 self.l2h_aux_init_file = tempfile.mktemp()
221 else:
222 self.l2h_aux_init_file = self.doc + ".l2h"
223 self.write_l2h_aux_init_file()
224
225 def build(self):
226 self.setup_texinputs()
227 formats = self.options.formats
228 if "dvi" in formats or "ps" in formats:
229 self.build_dvi()
230 if "pdf" in formats:
231 self.build_pdf()
232 if "ps" in formats:
233 self.build_ps()
234 if "html" in formats:
235 self.require_temps()
Fred Drake50d1fcf2001-02-19 19:18:09 +0000236 self.build_html(self.options.builddir or self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000237 if self.options.icon_server == ".":
Fred Drake52ea0ce1999-09-22 19:55:35 +0000238 pattern = os.path.join(TOPDIR, "html", "icons",
239 "*." + self.options.image_type)
240 imgs = glob.glob(pattern)
241 if not imgs:
242 self.warning(
243 "Could not locate support images of type %s."
244 % `self.options.image_type`)
245 for fn in imgs:
Fred Drake8b880931999-03-03 20:24:30 +0000246 new_fn = os.path.join(self.doc, os.path.basename(fn))
247 shutil.copyfile(fn, new_fn)
248 if "text" in formats:
249 self.require_temps()
250 tempdir = self.doc
251 need_html = "html" not in formats
252 if self.options.max_split_depth != 1:
253 fp = open(self.l2h_aux_init_file, "a")
254 fp.write("# re-hack this file for --text:\n")
255 l2hoption(fp, "MAX_SPLIT_DEPTH", "1")
256 fp.write("1;\n")
257 fp.close()
258 tempdir = self.doc + "-temp-html"
259 need_html = 1
260 if need_html:
261 self.build_html(tempdir, max_split_depth=1)
262 self.build_text(tempdir)
263 if self.options.discard_temps:
264 self.cleanup()
265
266 def setup_texinputs(self):
267 texinputs = [self.filedir] + list(self.options.base_texinputs)
268 os.environ["TEXINPUTS"] = string.join(texinputs, os.pathsep)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000269 self.message("TEXINPUTS=" + os.environ["TEXINPUTS"])
Fred Drake8b880931999-03-03 20:24:30 +0000270
Fred Drake8b880931999-03-03 20:24:30 +0000271 def build_aux(self, binary=None):
272 if binary is None:
273 binary = LATEX_BINARY
274 new_index( "%s.ind" % self.doc, "genindex")
275 new_index("mod%s.ind" % self.doc, "modindex")
276 self.run("%s %s" % (binary, self.doc))
277 self.use_bibtex = check_for_bibtex(self.doc + ".aux")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000278 self.latex_runs = 1
Fred Drake8b880931999-03-03 20:24:30 +0000279
280 def build_dvi(self):
281 self.use_latex(LATEX_BINARY)
282
283 def build_pdf(self):
284 self.use_latex(PDFLATEX_BINARY)
285
286 def use_latex(self, binary):
287 self.require_temps(binary=binary)
Fred Drakedf84fac2000-09-20 05:49:09 +0000288 if self.latex_runs < 2:
289 if os.path.isfile("mod%s.idx" % self.doc):
290 self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000291 use_indfix = 0
Fred Drakedf84fac2000-09-20 05:49:09 +0000292 if os.path.isfile(self.doc + ".idx"):
Fred Drake9dce7b32000-11-03 02:57:31 +0000293 use_indfix = 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000294 # call to Doc/tools/fix_hack omitted; doesn't appear necessary
295 self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc))
296 import indfix
297 indfix.process(self.doc + ".ind")
298 if self.use_bibtex:
299 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
300 self.process_synopsis_files()
301 #
302 # let the doctype-specific handler do some intermediate work:
303 #
304 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000305 self.latex_runs = self.latex_runs + 1
Fred Drakedf84fac2000-09-20 05:49:09 +0000306 if os.path.isfile("mod%s.idx" % self.doc):
307 self.run("%s -s %s mod%s.idx"
308 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000309 if use_indfix:
Fred Drakedf84fac2000-09-20 05:49:09 +0000310 self.run("%s -s %s %s.idx"
311 % (MAKEINDEX_BINARY, ISTFILE, self.doc))
Fred Drake9dce7b32000-11-03 02:57:31 +0000312 indfix.process(self.doc + ".ind")
Fred Drakedf84fac2000-09-20 05:49:09 +0000313 self.process_synopsis_files()
Fred Drakea871c2e1999-05-06 19:37:38 +0000314 #
315 # and now finish it off:
316 #
317 if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY:
318 import toc2bkm
Fred Drake239e1d52000-09-05 21:45:11 +0000319 if self.doctype == "manual":
320 bigpart = "chapter"
321 else:
322 bigpart = "section"
323 toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", bigpart)
Fred Drakea871c2e1999-05-06 19:37:38 +0000324 if self.use_bibtex:
325 self.run("%s %s" % (BIBTEX_BINARY, self.doc))
326 self.run("%s %s" % (binary, self.doc))
Fred Drakeb258bed2001-02-12 15:30:22 +0000327 self.latex_runs = self.latex_runs + 1
Fred Drakea871c2e1999-05-06 19:37:38 +0000328
329 def process_synopsis_files(self):
330 synopsis_files = glob.glob(self.doc + "*.syn")
331 for path in synopsis_files:
332 uniqify_module_table(path)
Fred Drake8b880931999-03-03 20:24:30 +0000333
334 def build_ps(self):
335 self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc))
336
337 def build_html(self, builddir=None, max_split_depth=None):
338 if builddir is None:
339 builddir = self.doc
340 if max_split_depth is None:
341 max_split_depth = self.options.max_split_depth
342 texfile = None
343 for p in string.split(os.environ["TEXINPUTS"], os.pathsep):
344 fn = os.path.join(p, self.doc + ".tex")
345 if os.path.isfile(fn):
346 texfile = fn
347 break
348 if not texfile:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000349 self.warning("Could not locate %s.tex; aborting." % self.doc)
Fred Drake8b880931999-03-03 20:24:30 +0000350 sys.exit(1)
351 # remove leading ./ (or equiv.); might avoid problems w/ dvips
352 if texfile[:2] == os.curdir + os.sep:
353 texfile = texfile[2:]
354 # build the command line and run LaTeX2HTML:
Fred Drakeba828782000-04-03 04:19:14 +0000355 if not os.path.isdir(builddir):
356 os.mkdir(builddir)
Fred Drakef3d41272000-09-14 22:25:47 +0000357 else:
358 for fname in glob.glob(os.path.join(builddir, "*.html")):
359 os.unlink(fname)
Fred Drake8b880931999-03-03 20:24:30 +0000360 args = [LATEX2HTML_BINARY,
Fred Drake8b880931999-03-03 20:24:30 +0000361 "-init_file", self.l2h_aux_init_file,
362 "-dir", builddir,
363 texfile
364 ]
365 self.run(string.join(args)) # XXX need quoting!
366 # ... postprocess
367 shutil.copyfile(self.options.style_file,
368 os.path.join(builddir, self.doc + ".css"))
Fred Drake4437fdf1999-05-03 14:29:07 +0000369 shutil.copyfile(os.path.join(builddir, self.doc + ".html"),
370 os.path.join(builddir, "index.html"))
Fred Drake9a257b42000-03-31 20:27:36 +0000371 if max_split_depth != 1 and not self.options.numeric:
Fred Drake8b880931999-03-03 20:24:30 +0000372 pwd = os.getcwd()
373 try:
374 os.chdir(builddir)
375 self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT))
376 finally:
377 os.chdir(pwd)
378
379 def build_text(self, tempdir=None):
380 if tempdir is None:
381 tempdir = self.doc
382 indexfile = os.path.join(tempdir, "index.html")
383 self.run("%s -nolist -dump %s >%s.txt"
384 % (LYNX_BINARY, indexfile, self.doc))
385
386 def require_temps(self, binary=None):
Fred Drake52ea0ce1999-09-22 19:55:35 +0000387 if not self.latex_runs:
Fred Drake8b880931999-03-03 20:24:30 +0000388 self.build_aux(binary=binary)
389
390 def write_l2h_aux_init_file(self):
Fred Drake8bc627a2000-08-31 06:14:38 +0000391 options = self.options
Fred Drake8b880931999-03-03 20:24:30 +0000392 fp = open(self.l2h_aux_init_file, "w")
Fred Drake19157542000-07-31 17:47:49 +0000393 d = string_to_perl(os.path.dirname(L2H_INIT_FILE))
394 fp.write("package main;\n"
395 "push (@INC, '%s');\n"
396 "$mydir = '%s';\n"
397 % (d, d))
Fred Drake498c18f2000-07-24 23:03:32 +0000398 fp.write(open(L2H_INIT_FILE).read())
Fred Drake8bc627a2000-08-31 06:14:38 +0000399 for filename in options.l2h_init_files:
400 fp.write("\n# initialization code incorporated from:\n# ")
401 fp.write(filename)
402 fp.write("\n")
403 fp.write(open(filename).read())
Fred Drake498c18f2000-07-24 23:03:32 +0000404 fp.write("\n"
405 "# auxillary init file for latex2html\n"
Fred Drake8b880931999-03-03 20:24:30 +0000406 "# generated by mkhowto\n"
Fred Drake4437fdf1999-05-03 14:29:07 +0000407 "$NO_AUTO_LINK = 1;\n"
Fred Drake8b880931999-03-03 20:24:30 +0000408 )
Fred Drakecf1b06e1999-09-23 16:55:09 +0000409 l2hoption(fp, "ABOUT_FILE", options.about_file)
Fred Drake8b880931999-03-03 20:24:30 +0000410 l2hoption(fp, "ICONSERVER", options.icon_server)
Fred Drake52ea0ce1999-09-22 19:55:35 +0000411 l2hoption(fp, "IMAGE_TYPE", options.image_type)
Fred Drake8b880931999-03-03 20:24:30 +0000412 l2hoption(fp, "ADDRESS", options.address)
413 l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth)
414 l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth)
Fred Drakedfa539d2000-08-31 06:58:34 +0000415 l2hoption(fp, "EXTERNAL_UP_LINK", options.up_link)
416 l2hoption(fp, "EXTERNAL_UP_TITLE", options.up_title)
Fred Drake42181db2001-01-09 22:02:10 +0000417 l2hoption(fp, "GLOBAL_MODULE_INDEX", options.global_module_index)
Fred Drake8b880931999-03-03 20:24:30 +0000418 fp.write("1;\n")
419 fp.close()
420
421 def cleanup(self):
422 self.__have_temps = 0
423 for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm",
Fred Drakea871c2e1999-05-06 19:37:38 +0000424 "%s.idx", "%s.ilg", "%s.ind", "%s.pla",
Fred Drake8b880931999-03-03 20:24:30 +0000425 "%s.bbl", "%s.blg",
426 "mod%s.idx", "mod%s.ind", "mod%s.ilg",
427 ):
428 safe_unlink(pattern % self.doc)
Fred Drakea871c2e1999-05-06 19:37:38 +0000429 map(safe_unlink, glob.glob(self.doc + "*.syn"))
Fred Drake8b880931999-03-03 20:24:30 +0000430 for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"):
431 pattern = os.path.join(self.doc, spec)
432 map(safe_unlink, glob.glob(pattern))
433 if "dvi" not in self.options.formats:
434 safe_unlink(self.doc + ".dvi")
435 if os.path.isdir(self.doc + "-temp-html"):
436 shutil.rmtree(self.doc + "-temp-html", ignore_errors=1)
437 if not self.options.logging:
438 os.unlink(self.log_filename)
439 if not self.options.debugging:
440 os.unlink(self.l2h_aux_init_file)
441
442 def run(self, command):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000443 self.message(command)
444 rc = os.system("(%s) </dev/null >>%s 2>&1"
445 % (command, self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000446 if rc:
Fred Drake52ea0ce1999-09-22 19:55:35 +0000447 self.warning(
448 "Session transcript and error messages are in %s."
Fred Drake8b880931999-03-03 20:24:30 +0000449 % self.log_filename)
Fred Drake4e3f2752001-02-04 15:20:26 +0000450 sys.stderr.write("The relevant lines from the transcript are:\n")
451 sys.stderr.write("-" * 72 + "\n")
452 sys.stderr.writelines(get_run_transcript(self.log_filename))
Fred Drake8b880931999-03-03 20:24:30 +0000453 sys.exit(rc)
454
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000455 def message(self, msg):
456 msg = "+++ " + msg
457 if not self.options.quiet:
458 print msg
Fred Drake52ea0ce1999-09-22 19:55:35 +0000459 self.log(msg + "\n")
460
461 def warning(self, msg):
462 msg = "*** %s\n" % msg
463 sys.stderr.write(msg)
464 self.log(msg)
465
466 def log(self, msg):
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000467 fp = open(self.log_filename, "a")
Fred Drake52ea0ce1999-09-22 19:55:35 +0000468 fp.write(msg)
Fred Drakeaaa0d9a1999-03-03 21:57:58 +0000469 fp.close()
470
Fred Drake8b880931999-03-03 20:24:30 +0000471
Fred Drake4e3f2752001-02-04 15:20:26 +0000472def get_run_transcript(filename):
473 """Return lines from the transcript file for the most recent run() call."""
474 fp = open(filename)
475 lines = fp.readlines()
476 fp.close()
477 lines.reverse()
478 L = []
479 for line in lines:
480 L.append(line)
481 if line[:4] == "+++ ":
482 break
483 L.reverse()
484 return L
485
486
Fred Drake8b880931999-03-03 20:24:30 +0000487def safe_unlink(path):
Fred Drake4e3f2752001-02-04 15:20:26 +0000488 """Unlink a file without raising an error if it doesn't exist."""
Fred Drake8b880931999-03-03 20:24:30 +0000489 try:
490 os.unlink(path)
491 except os.error:
492 pass
493
494
Fred Drakea871c2e1999-05-06 19:37:38 +0000495def split_pathname(path):
496 path = os.path.normpath(os.path.join(os.getcwd(), path))
497 dirname, basename = os.path.split(path)
Fred Drake8b880931999-03-03 20:24:30 +0000498 if basename[-4:] == ".tex":
499 basename = basename[:-4]
500 return dirname, basename
501
502
Fred Drakea871c2e1999-05-06 19:37:38 +0000503_doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}")
504def get_doctype(path):
505 fp = open(path)
506 doctype = None
507 while 1:
508 line = fp.readline()
509 if not line:
510 break
511 m = _doctype_rx.match(line)
512 if m:
513 doctype = m.group(1)
514 break
515 fp.close()
516 return doctype
517
518
Fred Drake8b880931999-03-03 20:24:30 +0000519def main():
520 options = Options()
521 try:
522 args = options.parse(sys.argv[1:])
523 except getopt.error, msg:
524 error(options, msg)
525 if not args:
526 # attempt to locate single .tex file in current directory:
527 args = glob.glob("*.tex")
528 if not args:
529 error(options, "No file to process.")
530 if len(args) > 1:
531 error(options, "Could not deduce which files should be processed.")
532 #
533 # parameters are processed, let's go!
534 #
535 for path in args:
536 Job(options, path).build()
537
538
539def l2hoption(fp, option, value):
540 if value:
541 fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value))))
542
543
544_to_perl = {}
545for c in map(chr, range(1, 256)):
546 _to_perl[c] = c
547_to_perl["@"] = "\\@"
548_to_perl["$"] = "\\$"
549_to_perl['"'] = '\\"'
550
551def string_to_perl(s):
552 return string.join(map(_to_perl.get, s), '')
553
554
555def check_for_bibtex(filename):
556 fp = open(filename)
557 pos = string.find(fp.read(), r"\bibdata{")
558 fp.close()
559 return pos >= 0
560
561def uniqify_module_table(filename):
562 lines = open(filename).readlines()
563 if len(lines) > 1:
564 if lines[-1] == lines[-2]:
565 del lines[-1]
566 open(filename, "w").writelines(lines)
567
568
569def new_index(filename, label="genindex"):
570 fp = open(filename, "w")
571 fp.write(r"""\
572\begin{theindex}
573\label{%s}
574\end{theindex}
575""" % label)
576 fp.close()
577
578
579if __name__ == "__main__":
580 main()