Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # -*- Python -*- |
| 3 | """usage: %(program)s [options...] file ... |
| 4 | |
| 5 | Options specifying formats to build: |
| 6 | --html HyperText Markup Language |
| 7 | --pdf Portable Document Format (default) |
| 8 | --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 | |
| 14 | HTML 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 Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 19 | --image-type Specify the image type to use in HTML output; |
| 20 | values: gif (default), png. |
Fred Drake | 9a257b4 | 2000-03-31 20:27:36 +0000 | [diff] [blame] | 21 | --numeric Don't rename the HTML files; just keep node#.html for |
| 22 | the filenames. |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 23 | |
| 24 | Other options: |
| 25 | --a4 Format for A4 paper. |
| 26 | --letter Format for US letter paper (the default). |
| 27 | --help, -H Show this text. |
| 28 | --logging, -l Log stdout and stderr to a file (*.how). |
| 29 | --debugging, -D Echo commands as they are executed. |
| 30 | --keep, -k Keep temporary files around. |
| 31 | --quiet, -q Do not print command output to stdout. |
| 32 | (stderr is also lost, sorry; see *.how for errors) |
| 33 | """ |
| 34 | |
| 35 | import getopt |
| 36 | import glob |
| 37 | import os |
Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 38 | import re |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 39 | import shutil |
| 40 | import string |
| 41 | import sys |
| 42 | import tempfile |
| 43 | |
| 44 | |
| 45 | MYDIR = os.path.normpath(os.path.join(os.getcwd(), sys.path[0])) |
| 46 | TOPDIR = os.path.normpath(os.path.join(MYDIR, os.pardir)) |
| 47 | |
| 48 | ISTFILE = os.path.join(TOPDIR, "texinputs", "python.ist") |
| 49 | NODE2LABEL_SCRIPT = os.path.join(MYDIR, "node2label.pl") |
| 50 | L2H_INIT_FILE = os.path.join(TOPDIR, "perl", "l2hinit.perl") |
| 51 | |
| 52 | BIBTEX_BINARY = "bibtex" |
| 53 | DVIPS_BINARY = "dvips" |
| 54 | LATEX_BINARY = "latex" |
| 55 | LATEX2HTML_BINARY = "latex2html" |
| 56 | LYNX_BINARY = "lynx" |
| 57 | MAKEINDEX_BINARY = "makeindex" |
| 58 | PDFLATEX_BINARY = "pdflatex" |
| 59 | PERL_BINARY = "perl" |
| 60 | PYTHON_BINARY = "python" |
| 61 | |
| 62 | |
| 63 | def usage(options): |
| 64 | print __doc__ % options |
| 65 | |
| 66 | def error(options, message, err=2): |
| 67 | sys.stdout = sys.stderr |
| 68 | print message |
| 69 | print |
| 70 | usage(options) |
| 71 | sys.exit(2) |
| 72 | |
| 73 | |
| 74 | class Options: |
| 75 | program = os.path.basename(sys.argv[0]) |
| 76 | # |
| 77 | address = '' |
| 78 | debugging = 0 |
| 79 | discard_temps = 1 |
| 80 | have_temps = 0 |
| 81 | icon_server = None |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 82 | image_type = "gif" |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 83 | logging = 0 |
| 84 | max_link_depth = 3 |
| 85 | max_split_depth = 6 |
| 86 | paper = "letter" |
| 87 | quiet = 0 |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 88 | runs = 0 |
Fred Drake | 9a257b4 | 2000-03-31 20:27:36 +0000 | [diff] [blame] | 89 | numeric = 0 |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 90 | style_file = os.path.join(TOPDIR, "html", "style.css") |
Fred Drake | cf1b06e | 1999-09-23 16:55:09 +0000 | [diff] [blame] | 91 | about_file = os.path.join(TOPDIR, "html", "about.dat") |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 92 | # |
| 93 | DEFAULT_FORMATS = ("pdf",) |
| 94 | ALL_FORMATS = ("dvi", "html", "pdf", "ps", "text") |
| 95 | |
| 96 | def __init__(self): |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 97 | self.formats = [] |
| 98 | |
| 99 | def __getitem__(self, key): |
| 100 | # This is used when formatting the usage message. |
| 101 | try: |
| 102 | return getattr(self, key) |
| 103 | except AttributeError: |
| 104 | raise KeyError, key |
| 105 | |
| 106 | def parse(self, args): |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 107 | opts, args = getopt.getopt(args, "Hi:a:s:lDkqr:", |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 108 | ["all", "postscript", "help", "iconserver=", |
Fred Drake | cc7371c | 2000-06-29 23:01:40 +0000 | [diff] [blame] | 109 | "address=", "a4", "letter", |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 110 | "link=", "split=", "logging", "debugging", |
Fred Drake | cf1b06e | 1999-09-23 16:55:09 +0000 | [diff] [blame] | 111 | "keep", "quiet", "runs=", "image-type=", |
Fred Drake | 9a257b4 | 2000-03-31 20:27:36 +0000 | [diff] [blame] | 112 | "about=", "numeric"] |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 113 | + list(self.ALL_FORMATS)) |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 114 | for opt, arg in opts: |
| 115 | if opt == "--all": |
| 116 | self.formats = list(self.ALL_FORMATS) |
| 117 | elif opt in ("-H", "--help"): |
| 118 | usage(self) |
| 119 | sys.exit() |
| 120 | elif opt == "--iconserver": |
| 121 | self.icon_server = arg |
| 122 | elif opt in ("-a", "--address"): |
| 123 | self.address = arg |
| 124 | elif opt == "--a4": |
| 125 | self.paper = "a4" |
| 126 | elif opt == "--letter": |
| 127 | self.paper = "letter" |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 128 | elif opt == "--link": |
| 129 | self.max_link_depth = int(arg) |
| 130 | elif opt in ("-s", "--split"): |
| 131 | self.max_split_depth = int(arg) |
| 132 | elif opt in ("-l", "--logging"): |
| 133 | self.logging = self.logging + 1 |
| 134 | elif opt in ("-D", "--debugging"): |
| 135 | self.debugging = self.debugging + 1 |
| 136 | elif opt in ("-k", "--keep"): |
| 137 | self.discard_temps = 0 |
| 138 | elif opt in ("-q", "--quiet"): |
| 139 | self.quiet = 1 |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 140 | elif opt in ("-r", "--runs"): |
| 141 | self.runs = int(arg) |
| 142 | elif opt == "--image-type": |
| 143 | self.image_type = arg |
Fred Drake | cf1b06e | 1999-09-23 16:55:09 +0000 | [diff] [blame] | 144 | elif opt == "--about": |
| 145 | # always make this absolute: |
| 146 | self.about_file = os.path.normpath( |
| 147 | os.path.join(os.getcwd(), arg)) |
Fred Drake | 9a257b4 | 2000-03-31 20:27:36 +0000 | [diff] [blame] | 148 | elif opt == "--numeric": |
| 149 | self.numeric = 1 |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 150 | # |
| 151 | # Format specifiers: |
| 152 | # |
| 153 | elif opt[2:] in self.ALL_FORMATS: |
| 154 | self.add_format(opt[2:]) |
| 155 | elif opt == "--postscript": |
| 156 | # synonym for --ps |
| 157 | self.add_format("ps") |
| 158 | self.initialize() |
| 159 | # |
| 160 | # return the args to allow the caller access: |
| 161 | # |
| 162 | return args |
| 163 | |
| 164 | def add_format(self, format): |
| 165 | """Add a format to the formats list if not present.""" |
| 166 | if not format in self.formats: |
| 167 | self.formats.append(format) |
| 168 | |
| 169 | def initialize(self): |
| 170 | """Complete initialization. This is needed if parse() isn't used.""" |
| 171 | # add the default format if no formats were specified: |
| 172 | if not self.formats: |
| 173 | self.formats = self.DEFAULT_FORMATS |
| 174 | # determine the base set of texinputs directories: |
| 175 | texinputs = string.split(os.environ.get("TEXINPUTS", ""), os.pathsep) |
| 176 | if not texinputs: |
| 177 | texinputs = [''] |
| 178 | self.base_texinputs = [ |
| 179 | os.path.join(TOPDIR, "paper-" + self.paper), |
| 180 | os.path.join(TOPDIR, "texinputs"), |
| 181 | ] + texinputs |
| 182 | |
| 183 | |
| 184 | class Job: |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 185 | latex_runs = 0 |
| 186 | |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 187 | def __init__(self, options, path): |
| 188 | self.options = options |
Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 189 | self.doctype = get_doctype(path) |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 190 | self.filedir, self.doc = split_pathname(path) |
| 191 | self.log_filename = self.doc + ".how" |
| 192 | if os.path.exists(self.log_filename): |
| 193 | os.unlink(self.log_filename) |
| 194 | if os.path.exists(self.doc + ".l2h"): |
| 195 | self.l2h_aux_init_file = tempfile.mktemp() |
| 196 | else: |
| 197 | self.l2h_aux_init_file = self.doc + ".l2h" |
| 198 | self.write_l2h_aux_init_file() |
| 199 | |
| 200 | def build(self): |
| 201 | self.setup_texinputs() |
| 202 | formats = self.options.formats |
| 203 | if "dvi" in formats or "ps" in formats: |
| 204 | self.build_dvi() |
| 205 | if "pdf" in formats: |
| 206 | self.build_pdf() |
| 207 | if "ps" in formats: |
| 208 | self.build_ps() |
| 209 | if "html" in formats: |
| 210 | self.require_temps() |
| 211 | self.build_html(self.doc) |
| 212 | if self.options.icon_server == ".": |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 213 | pattern = os.path.join(TOPDIR, "html", "icons", |
| 214 | "*." + self.options.image_type) |
| 215 | imgs = glob.glob(pattern) |
| 216 | if not imgs: |
| 217 | self.warning( |
| 218 | "Could not locate support images of type %s." |
| 219 | % `self.options.image_type`) |
| 220 | for fn in imgs: |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 221 | new_fn = os.path.join(self.doc, os.path.basename(fn)) |
| 222 | shutil.copyfile(fn, new_fn) |
| 223 | if "text" in formats: |
| 224 | self.require_temps() |
| 225 | tempdir = self.doc |
| 226 | need_html = "html" not in formats |
| 227 | if self.options.max_split_depth != 1: |
| 228 | fp = open(self.l2h_aux_init_file, "a") |
| 229 | fp.write("# re-hack this file for --text:\n") |
| 230 | l2hoption(fp, "MAX_SPLIT_DEPTH", "1") |
| 231 | fp.write("1;\n") |
| 232 | fp.close() |
| 233 | tempdir = self.doc + "-temp-html" |
| 234 | need_html = 1 |
| 235 | if need_html: |
| 236 | self.build_html(tempdir, max_split_depth=1) |
| 237 | self.build_text(tempdir) |
| 238 | if self.options.discard_temps: |
| 239 | self.cleanup() |
| 240 | |
| 241 | def setup_texinputs(self): |
| 242 | texinputs = [self.filedir] + list(self.options.base_texinputs) |
| 243 | os.environ["TEXINPUTS"] = string.join(texinputs, os.pathsep) |
Fred Drake | aaa0d9a | 1999-03-03 21:57:58 +0000 | [diff] [blame] | 244 | self.message("TEXINPUTS=" + os.environ["TEXINPUTS"]) |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 245 | |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 246 | def build_aux(self, binary=None): |
| 247 | if binary is None: |
| 248 | binary = LATEX_BINARY |
| 249 | new_index( "%s.ind" % self.doc, "genindex") |
| 250 | new_index("mod%s.ind" % self.doc, "modindex") |
| 251 | self.run("%s %s" % (binary, self.doc)) |
| 252 | self.use_bibtex = check_for_bibtex(self.doc + ".aux") |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 253 | self.latex_runs = 1 |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 254 | |
| 255 | def build_dvi(self): |
| 256 | self.use_latex(LATEX_BINARY) |
| 257 | |
| 258 | def build_pdf(self): |
| 259 | self.use_latex(PDFLATEX_BINARY) |
| 260 | |
| 261 | def use_latex(self, binary): |
| 262 | self.require_temps(binary=binary) |
| 263 | if os.path.isfile("mod%s.idx" % self.doc): |
| 264 | self.run("%s mod%s.idx" % (MAKEINDEX_BINARY, self.doc)) |
| 265 | if os.path.isfile(self.doc + ".idx"): |
| 266 | # call to Doc/tools/fix_hack omitted; doesn't appear necessary |
| 267 | self.run("%s %s.idx" % (MAKEINDEX_BINARY, self.doc)) |
| 268 | import indfix |
| 269 | indfix.process(self.doc + ".ind") |
| 270 | if self.use_bibtex: |
| 271 | self.run("%s %s" % (BIBTEX_BINARY, self.doc)) |
Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 272 | self.process_synopsis_files() |
| 273 | # |
| 274 | # let the doctype-specific handler do some intermediate work: |
| 275 | # |
| 276 | if self.doctype == "manual": |
| 277 | self.use_latex_manual(binary=binary) |
| 278 | elif self.doctype == "howto": |
| 279 | self.use_latex_howto(binary=binary) |
| 280 | else: |
| 281 | raise RuntimeError, "unsupported document type: " + self.doctype |
| 282 | # |
| 283 | # and now finish it off: |
| 284 | # |
| 285 | if os.path.isfile(self.doc + ".toc") and binary == PDFLATEX_BINARY: |
| 286 | import toc2bkm |
| 287 | toc2bkm.process(self.doc + ".toc", self.doc + ".bkm", "section") |
| 288 | if self.use_bibtex: |
| 289 | self.run("%s %s" % (BIBTEX_BINARY, self.doc)) |
| 290 | self.run("%s %s" % (binary, self.doc)) |
| 291 | |
| 292 | def use_latex_howto(self, binary): |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 293 | self.run("%s %s" % (binary, self.doc)) |
| 294 | if os.path.isfile("mod%s.idx" % self.doc): |
| 295 | self.run("%s -s %s mod%s.idx" |
| 296 | % (MAKEINDEX_BINARY, ISTFILE, self.doc)) |
| 297 | if os.path.isfile(self.doc + ".idx"): |
| 298 | self.run("%s -s %s %s.idx" % (MAKEINDEX_BINARY, ISTFILE, self.doc)) |
Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 299 | self.process_synopsis_files() |
| 300 | |
| 301 | def use_latex_manual(self, binary): |
| 302 | pass |
| 303 | |
| 304 | def process_synopsis_files(self): |
| 305 | synopsis_files = glob.glob(self.doc + "*.syn") |
| 306 | for path in synopsis_files: |
| 307 | uniqify_module_table(path) |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 308 | |
| 309 | def build_ps(self): |
| 310 | self.run("%s -N0 -o %s.ps %s" % (DVIPS_BINARY, self.doc, self.doc)) |
| 311 | |
| 312 | def build_html(self, builddir=None, max_split_depth=None): |
| 313 | if builddir is None: |
| 314 | builddir = self.doc |
| 315 | if max_split_depth is None: |
| 316 | max_split_depth = self.options.max_split_depth |
| 317 | texfile = None |
| 318 | for p in string.split(os.environ["TEXINPUTS"], os.pathsep): |
| 319 | fn = os.path.join(p, self.doc + ".tex") |
| 320 | if os.path.isfile(fn): |
| 321 | texfile = fn |
| 322 | break |
| 323 | if not texfile: |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 324 | self.warning("Could not locate %s.tex; aborting." % self.doc) |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 325 | sys.exit(1) |
| 326 | # remove leading ./ (or equiv.); might avoid problems w/ dvips |
| 327 | if texfile[:2] == os.curdir + os.sep: |
| 328 | texfile = texfile[2:] |
| 329 | # build the command line and run LaTeX2HTML: |
Fred Drake | ba82878 | 2000-04-03 04:19:14 +0000 | [diff] [blame] | 330 | if not os.path.isdir(builddir): |
| 331 | os.mkdir(builddir) |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 332 | args = [LATEX2HTML_BINARY, |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 333 | "-init_file", self.l2h_aux_init_file, |
| 334 | "-dir", builddir, |
| 335 | texfile |
| 336 | ] |
| 337 | self.run(string.join(args)) # XXX need quoting! |
| 338 | # ... postprocess |
| 339 | shutil.copyfile(self.options.style_file, |
| 340 | os.path.join(builddir, self.doc + ".css")) |
Fred Drake | 4437fdf | 1999-05-03 14:29:07 +0000 | [diff] [blame] | 341 | shutil.copyfile(os.path.join(builddir, self.doc + ".html"), |
| 342 | os.path.join(builddir, "index.html")) |
Fred Drake | 9a257b4 | 2000-03-31 20:27:36 +0000 | [diff] [blame] | 343 | if max_split_depth != 1 and not self.options.numeric: |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 344 | pwd = os.getcwd() |
| 345 | try: |
| 346 | os.chdir(builddir) |
| 347 | self.run("%s %s *.html" % (PERL_BINARY, NODE2LABEL_SCRIPT)) |
| 348 | finally: |
| 349 | os.chdir(pwd) |
| 350 | |
| 351 | def build_text(self, tempdir=None): |
| 352 | if tempdir is None: |
| 353 | tempdir = self.doc |
| 354 | indexfile = os.path.join(tempdir, "index.html") |
| 355 | self.run("%s -nolist -dump %s >%s.txt" |
| 356 | % (LYNX_BINARY, indexfile, self.doc)) |
| 357 | |
| 358 | def require_temps(self, binary=None): |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 359 | if not self.latex_runs: |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 360 | self.build_aux(binary=binary) |
| 361 | |
| 362 | def write_l2h_aux_init_file(self): |
| 363 | fp = open(self.l2h_aux_init_file, "w") |
Fred Drake | 1915754 | 2000-07-31 17:47:49 +0000 | [diff] [blame^] | 364 | d = string_to_perl(os.path.dirname(L2H_INIT_FILE)) |
| 365 | fp.write("package main;\n" |
| 366 | "push (@INC, '%s');\n" |
| 367 | "$mydir = '%s';\n" |
| 368 | % (d, d)) |
Fred Drake | 498c18f | 2000-07-24 23:03:32 +0000 | [diff] [blame] | 369 | fp.write(open(L2H_INIT_FILE).read()) |
| 370 | fp.write("\n" |
| 371 | "# auxillary init file for latex2html\n" |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 372 | "# generated by mkhowto\n" |
Fred Drake | 4437fdf | 1999-05-03 14:29:07 +0000 | [diff] [blame] | 373 | "$NO_AUTO_LINK = 1;\n" |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 374 | ) |
| 375 | options = self.options |
Fred Drake | cf1b06e | 1999-09-23 16:55:09 +0000 | [diff] [blame] | 376 | l2hoption(fp, "ABOUT_FILE", options.about_file) |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 377 | l2hoption(fp, "ICONSERVER", options.icon_server) |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 378 | l2hoption(fp, "IMAGE_TYPE", options.image_type) |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 379 | l2hoption(fp, "ADDRESS", options.address) |
| 380 | l2hoption(fp, "MAX_LINK_DEPTH", options.max_link_depth) |
| 381 | l2hoption(fp, "MAX_SPLIT_DEPTH", options.max_split_depth) |
| 382 | fp.write("1;\n") |
| 383 | fp.close() |
| 384 | |
| 385 | def cleanup(self): |
| 386 | self.__have_temps = 0 |
| 387 | for pattern in ("%s.aux", "%s.log", "%s.out", "%s.toc", "%s.bkm", |
Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 388 | "%s.idx", "%s.ilg", "%s.ind", "%s.pla", |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 389 | "%s.bbl", "%s.blg", |
| 390 | "mod%s.idx", "mod%s.ind", "mod%s.ilg", |
| 391 | ): |
| 392 | safe_unlink(pattern % self.doc) |
Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 393 | map(safe_unlink, glob.glob(self.doc + "*.syn")) |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 394 | for spec in ("IMG*", "*.pl", "WARNINGS", "index.dat", "modindex.dat"): |
| 395 | pattern = os.path.join(self.doc, spec) |
| 396 | map(safe_unlink, glob.glob(pattern)) |
| 397 | if "dvi" not in self.options.formats: |
| 398 | safe_unlink(self.doc + ".dvi") |
| 399 | if os.path.isdir(self.doc + "-temp-html"): |
| 400 | shutil.rmtree(self.doc + "-temp-html", ignore_errors=1) |
| 401 | if not self.options.logging: |
| 402 | os.unlink(self.log_filename) |
| 403 | if not self.options.debugging: |
| 404 | os.unlink(self.l2h_aux_init_file) |
| 405 | |
| 406 | def run(self, command): |
Fred Drake | aaa0d9a | 1999-03-03 21:57:58 +0000 | [diff] [blame] | 407 | self.message(command) |
| 408 | rc = os.system("(%s) </dev/null >>%s 2>&1" |
| 409 | % (command, self.log_filename)) |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 410 | if rc: |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 411 | self.warning( |
| 412 | "Session transcript and error messages are in %s." |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 413 | % self.log_filename) |
| 414 | sys.exit(rc) |
| 415 | |
Fred Drake | aaa0d9a | 1999-03-03 21:57:58 +0000 | [diff] [blame] | 416 | def message(self, msg): |
| 417 | msg = "+++ " + msg |
| 418 | if not self.options.quiet: |
| 419 | print msg |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 420 | self.log(msg + "\n") |
| 421 | |
| 422 | def warning(self, msg): |
| 423 | msg = "*** %s\n" % msg |
| 424 | sys.stderr.write(msg) |
| 425 | self.log(msg) |
| 426 | |
| 427 | def log(self, msg): |
Fred Drake | aaa0d9a | 1999-03-03 21:57:58 +0000 | [diff] [blame] | 428 | fp = open(self.log_filename, "a") |
Fred Drake | 52ea0ce | 1999-09-22 19:55:35 +0000 | [diff] [blame] | 429 | fp.write(msg) |
Fred Drake | aaa0d9a | 1999-03-03 21:57:58 +0000 | [diff] [blame] | 430 | fp.close() |
| 431 | |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 432 | |
| 433 | def safe_unlink(path): |
| 434 | try: |
| 435 | os.unlink(path) |
| 436 | except os.error: |
| 437 | pass |
| 438 | |
| 439 | |
Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 440 | def split_pathname(path): |
| 441 | path = os.path.normpath(os.path.join(os.getcwd(), path)) |
| 442 | dirname, basename = os.path.split(path) |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 443 | if basename[-4:] == ".tex": |
| 444 | basename = basename[:-4] |
| 445 | return dirname, basename |
| 446 | |
| 447 | |
Fred Drake | a871c2e | 1999-05-06 19:37:38 +0000 | [diff] [blame] | 448 | _doctype_rx = re.compile(r"\\documentclass(?:\[[^]]*\])?{([a-zA-Z]*)}") |
| 449 | def get_doctype(path): |
| 450 | fp = open(path) |
| 451 | doctype = None |
| 452 | while 1: |
| 453 | line = fp.readline() |
| 454 | if not line: |
| 455 | break |
| 456 | m = _doctype_rx.match(line) |
| 457 | if m: |
| 458 | doctype = m.group(1) |
| 459 | break |
| 460 | fp.close() |
| 461 | return doctype |
| 462 | |
| 463 | |
Fred Drake | 8b88093 | 1999-03-03 20:24:30 +0000 | [diff] [blame] | 464 | def main(): |
| 465 | options = Options() |
| 466 | try: |
| 467 | args = options.parse(sys.argv[1:]) |
| 468 | except getopt.error, msg: |
| 469 | error(options, msg) |
| 470 | if not args: |
| 471 | # attempt to locate single .tex file in current directory: |
| 472 | args = glob.glob("*.tex") |
| 473 | if not args: |
| 474 | error(options, "No file to process.") |
| 475 | if len(args) > 1: |
| 476 | error(options, "Could not deduce which files should be processed.") |
| 477 | # |
| 478 | # parameters are processed, let's go! |
| 479 | # |
| 480 | for path in args: |
| 481 | Job(options, path).build() |
| 482 | |
| 483 | |
| 484 | def l2hoption(fp, option, value): |
| 485 | if value: |
| 486 | fp.write('$%s = "%s";\n' % (option, string_to_perl(str(value)))) |
| 487 | |
| 488 | |
| 489 | _to_perl = {} |
| 490 | for c in map(chr, range(1, 256)): |
| 491 | _to_perl[c] = c |
| 492 | _to_perl["@"] = "\\@" |
| 493 | _to_perl["$"] = "\\$" |
| 494 | _to_perl['"'] = '\\"' |
| 495 | |
| 496 | def string_to_perl(s): |
| 497 | return string.join(map(_to_perl.get, s), '') |
| 498 | |
| 499 | |
| 500 | def check_for_bibtex(filename): |
| 501 | fp = open(filename) |
| 502 | pos = string.find(fp.read(), r"\bibdata{") |
| 503 | fp.close() |
| 504 | return pos >= 0 |
| 505 | |
| 506 | def uniqify_module_table(filename): |
| 507 | lines = open(filename).readlines() |
| 508 | if len(lines) > 1: |
| 509 | if lines[-1] == lines[-2]: |
| 510 | del lines[-1] |
| 511 | open(filename, "w").writelines(lines) |
| 512 | |
| 513 | |
| 514 | def new_index(filename, label="genindex"): |
| 515 | fp = open(filename, "w") |
| 516 | fp.write(r"""\ |
| 517 | \begin{theindex} |
| 518 | \label{%s} |
| 519 | \end{theindex} |
| 520 | """ % label) |
| 521 | fp.close() |
| 522 | |
| 523 | |
| 524 | if __name__ == "__main__": |
| 525 | main() |