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