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