| 1 |
# |
| 2 |
# DbTex base class handling the compilation of a DocBook file via |
| 3 |
# XSL Transformation and some TeX engine compilation. |
| 4 |
# |
| 5 |
from __future__ import print_function |
| 6 |
|
| 7 |
import sys |
| 8 |
import os |
| 9 |
import re |
| 10 |
import shlex |
| 11 |
import tempfile |
| 12 |
import shutil |
| 13 |
try: |
| 14 |
from urllib import pathname2url |
| 15 |
except ImportError: |
| 16 |
from urllib.request import pathname2url |
| 17 |
import glob |
| •18 |
import importlib.machinery |
| 19 |
import importlib.util |
| 20 |
from optparse import OptionParser |
| 21 |
from io import open |
| 22 |
|
| 23 |
from dbtexmf.core.txtparser import texinputs_parse, texstyle_parse |
| 24 |
from dbtexmf.core.confparser import DbtexConfig |
| 25 |
from dbtexmf.xslt import xslt |
| 26 |
import dbtexmf.core.logger as logger |
| 27 |
from dbtexmf.core.error import signal_error, failed_exit, dump_stack |
| 28 |
|
| 29 |
|
| 30 |
def suffix_replace(path, oldext, newext=""): |
| 31 |
(root, ext) = os.path.splitext(path) |
| 32 |
if ext == oldext: |
| 33 |
return (root+newext) |
| 34 |
else: |
| 35 |
return (path+newext) |
| 36 |
|
| 37 |
def path_to_uri(path): |
| 38 |
if os.name == 'nt': |
| 39 |
return 'file:' + pathname2url(path).replace('|', ':', 1) |
| 40 |
else: |
| 41 |
return pathname2url(path) |
| 42 |
|
| 43 |
|
| 44 |
class Document: |
| 45 |
""" |
| 46 |
Wrapper structure of the files built during the compilation per document |
| 47 |
""" |
| 48 |
def __init__(self, filename, binfmt="pdf"): |
| 49 |
self.inputname = filename |
| 50 |
self.basename = os.path.splitext(filename)[0] |
| 51 |
self.rawfile = self.basename + ".rtex" |
| 52 |
self.texfile = self.basename + ".tex" |
| 53 |
self.binfile = self.basename + "." + binfmt |
| 54 |
|
| 55 |
def has_subext(self, ext): |
| 56 |
return (os.path.splitext(self.basename)[1] == ext) |
| 57 |
|
| 58 |
def __eq__(self, other): |
| 59 |
if self.rawfile == other: |
| 60 |
return True |
| 61 |
if self.texfile == other: |
| 62 |
return True |
| 63 |
if self.binfile == other: |
| 64 |
return True |
| 65 |
return False |
| 66 |
|
| 67 |
def __ne__(self, other): |
| 68 |
return not self.__eq__(other) |
| 69 |
|
| 70 |
def __lt__(self, other): |
| 71 |
return self.__ne__(other) |
| 72 |
|
| 73 |
def __le__(self, other): |
| 74 |
return False |
| 75 |
|
| 76 |
def __gt__(self, other): |
| 77 |
return False |
| 78 |
|
| 79 |
def __ge__(self, other): |
| 80 |
return False |
| 81 |
|
| 82 |
|
| 83 |
class DbTex: |
| 84 |
USE_MKLISTINGS = 1 |
| 85 |
|
| 86 |
xsl_header = \ |
| 87 |
"""<?xml version="1.0"?> |
| 88 |
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
| 89 |
xmlns:m="http://www.w3.org/1998/Math/MathML" |
| 90 |
version="1.0"> |
| 91 |
\n""" |
| 92 |
|
| 93 |
def __init__(self, base=""): |
| 94 |
self.name = None |
| 95 |
self.debug = 0 |
| 96 |
self.verbose = 0 |
| 97 |
if base: |
| 98 |
self.set_base(base) |
| 99 |
self.xslopts = [] |
| 100 |
self.xslparams = [] |
| 101 |
self.xslusers = [] |
| 102 |
self.flags = self.USE_MKLISTINGS |
| 103 |
self.stdindir = "" |
| 104 |
self.inputdir = "" |
| 105 |
self.input = "" |
| 106 |
self.input_format = "xml" |
| 107 |
self.outputdir = "" |
| 108 |
self.output = "" |
| 109 |
self.format = "pdf" |
| 110 |
self.tmpdir = "" |
| 111 |
self.tmpdir_user = None |
| 112 |
self.fig_paths = [] |
| 113 |
self.bib_paths = [] |
| 114 |
self.bst_paths = [] |
| 115 |
self.texinputs = [] |
| 116 |
self.texbatch = 1 |
| 117 |
self.texpost = "" |
| 118 |
self.fig_format = "" |
| 119 |
self.backend = "" |
| 120 |
|
| 121 |
# Temporary files |
| 122 |
self.documents = [] |
| 123 |
self.interms = [] |
| 124 |
self.included = [] |
| 125 |
self.basefile = "" |
| 126 |
self.rawfile = "" |
| 127 |
|
| 128 |
# Engines to use |
| 129 |
self.runtex = None |
| 130 |
self.rawtex = None |
| 131 |
self.xsltproc = None |
| 132 |
self.sgmlxml = None |
| 133 |
|
| 134 |
def set_base(self, topdir): |
| 135 |
self.topdir = os.path.realpath(topdir) |
| 136 |
self.xslmain = os.path.join(self.topdir, "xsl", "docbook.xsl") |
| 137 |
self.xsllist = os.path.join(self.topdir, "xsl", "common", "mklistings.xsl") |
| 138 |
self.xslset = os.path.join(self.topdir, "xsl", "common", "mkdoclist.xsl") |
| 139 |
self.texdir = os.path.join(self.topdir, "texstyle") |
| 140 |
self.texlocal = "" |
| 141 |
self.confdir = os.path.join(self.topdir, "confstyle") |
| 142 |
|
| 143 |
def update_texinputs(self): |
| 144 |
sep = os.pathsep |
| 145 |
# Get a uniform list of paths (not a list of lists) |
| 146 |
ti = [] |
| 147 |
for t in self.texinputs: |
| 148 |
ti += t.split(sep) |
| 149 |
|
| 150 |
# Systematically put the package style in TEXINPUTS |
| 151 |
ti_opts = ti + [self.texdir + "//"] |
| 152 |
|
| 153 |
# The original environment variable |
| 154 |
texinputs = os.getenv("TEXINPUTS") or "" |
| 155 |
ti_env = texinputs.split(sep) |
| 156 |
|
| 157 |
# Find where system default is in the paths |
| 158 |
try: |
| 159 |
syspos = ti_env.index('') |
| 160 |
except: |
| 161 |
# By default system has precedence (i.e. is the first one) |
| 162 |
ti_env = [''] + ti_env |
| 163 |
syspos = 0 |
| 164 |
|
| 165 |
ti_before = ti_env[:syspos] |
| 166 |
ti_after = ti_env[syspos+1:] |
| 167 |
|
| 168 |
# Paths passed by options have no precedence over the system |
| 169 |
ti_after = ti_opts + ti_after |
| 170 |
|
| 171 |
# Texlocal has precedence over the standard (and added) paths |
| 172 |
if self.texlocal: |
| 173 |
ti_before = [ self.texlocal + "//" ] + ti_before |
| 174 |
|
| 175 |
# Export the whole paths |
| 176 |
texinputs = sep.join(ti_before + [''] + ti_after) |
| 177 |
os.environ["TEXINPUTS"] = texinputs |
| 178 |
|
| 179 |
def set_xslt(self, xsltmod=None): |
| 180 |
# Set the XSLT to use. Set a default XSLT if none specified. |
| 181 |
# One can replace an already defined XSLT if explicitely required. |
| 182 |
if not(xsltmod): |
| 183 |
if self.xsltproc: |
| 184 |
return |
| 185 |
xsltmod = "xsltproc" |
| 186 |
self.xsltproc = xslt.load(xsltmod) |
| 187 |
|
| 188 |
def set_backend(self): |
| 189 |
# Set the backend to use or retrieve the default one |
| 190 |
if self.backend: |
| 191 |
self.runtex.set_backend(self.backend) |
| 192 |
else: |
| 193 |
self.backend = self.runtex.get_backend() |
| 194 |
|
| 195 |
def set_format(self, format): |
| 196 |
if not(format in ("rtex", "tex", "dvi", "ps", "pdf")): |
| 197 |
raise ValueError("unknown format '%s'" % format) |
| 198 |
else: |
| 199 |
self.format = format |
| 200 |
|
| 201 |
def unset_flags(self, what): |
| 202 |
self.flags &= ~what |
| 203 |
|
| 204 |
def get_version(self): |
| 205 |
f = open(os.path.join(self.topdir, "xsl", "version.xsl"), "rt", encoding="latin-1") |
| 206 |
versions = re.findall("<xsl:variable[^>]*>([^<]*)<", f.read()) |
| 207 |
f.close() |
| 208 |
if versions: |
| 209 |
return versions[0].strip() |
| 210 |
else: |
| 211 |
return "unknown" |
| 212 |
|
| 213 |
def build_stylesheet(self, wrapper="custom.xsl"): |
| 214 |
if not(self.xslparams or self.xslusers): |
| 215 |
self.xslbuild = self.xslmain |
| 216 |
return |
| 217 |
|
| 218 |
f = open(wrapper, "wt", encoding="latin-1") |
| 219 |
f.write(self.xsl_header) |
| 220 |
f.write('<xsl:import href="%s"/>\n' % path_to_uri(self.xslmain)) |
| 221 |
for xsluser in self.xslusers: |
| 222 |
f.write('<xsl:import href="%s"/>\n' % path_to_uri(xsluser)) |
| 223 |
|
| 224 |
# Reverse to set the latest parameter first (case of overriding) |
| 225 |
self.xslparams.reverse() |
| 226 |
for param in self.xslparams: |
| 227 |
v = param.split("=", 1) |
| 228 |
f.write('<xsl:param name="%s">' % v[0]) |
| 229 |
if len(v) == 2: |
| 230 |
f.write('%s' % v[1]) |
| 231 |
f.write('</xsl:param>\n') |
| 232 |
|
| 233 |
f.write('</xsl:stylesheet>\n') |
| 234 |
f.close() |
| 235 |
self.xslbuild = os.path.realpath(wrapper) |
| 236 |
|
| 237 |
def make_xml(self): |
| 238 |
self.log.info("Build the XML file...") |
| 239 |
xmlfile = self.basefile + ".xml" |
| 240 |
self.sgmlxml.run(self.input, xmlfile) |
| 241 |
self.input = xmlfile |
| 242 |
|
| 243 |
def make_listings(self): |
| 244 |
self.listings = os.path.join(self.tmpdir, "listings.xml") |
| 245 |
if (self.flags & self.USE_MKLISTINGS): |
| 246 |
self.log.info("Build the listings...") |
| 247 |
param = {"current.dir": self.inputdir} |
| 248 |
self.xsltproc.use_catalogs = 0 |
| 249 |
self.xsltproc.run(self.xsllist, self.input, |
| 250 |
self.listings, opts=self.xslopts, params=param) |
| 251 |
else: |
| 252 |
self.log.info("No external file support") |
| 253 |
f = open(self.listings, "wt", encoding="latin-1") |
| 254 |
f.write("<listings/>\n") |
| 255 |
f.close() |
| 256 |
|
| 257 |
def _single_setup(self): |
| 258 |
# If not specified the output name can be deduced from the input one: |
| 259 |
# /path/to/input.{xml|sgml} -> /path/to/input.{tex|pdf|dvi|ps} |
| 260 |
if not(self.output): |
| 261 |
output = suffix_replace(self.input, "."+self.input_format, |
| 262 |
".%s" % self.format) |
| 263 |
self.output = output |
| 264 |
|
| 265 |
self.documents.append(Document(self.basefile + \ |
| 266 |
"." + self.input_format, |
| 267 |
binfmt=self.format)) |
| 268 |
|
| 269 |
def _multiple_setup(self, doclist): |
| 270 |
# If not specified, output the chunked books in the working dir |
| 271 |
if not(self.outputdir): |
| 272 |
self.log.info("No specified output dir (-O). "\ |
| 273 |
"Use the working directory") |
| 274 |
self.outputdir = self.cwdir |
| 275 |
|
| 276 |
f = open(doclist, "rt", encoding="latin-1") |
| 277 |
books = f.readlines() |
| 278 |
f.close() |
| 279 |
|
| 280 |
for b in books: |
| 281 |
d = Document(b.strip() + ".tex", binfmt=self.format) |
| 282 |
self.documents.append(d) |
| 283 |
|
| 284 |
def build_doclist(self): |
| 285 |
# The stylesheet must include the building stylesheets to have the |
| 286 |
# actual parameter values (e.g. set.book.num) needed to give the book |
| 287 |
# set list |
| 288 |
self.log.info("Build the book set list...") |
| 289 |
xslset = "doclist.xsl" |
| 290 |
f = open(xslset, "wt", encoding="latin-1") |
| 291 |
f.write(self.xsl_header) |
| 292 |
f.write('<xsl:import href="%s"/>\n' % path_to_uri(self.xslbuild)) |
| 293 |
f.write('<xsl:import href="%s"/>\n' % path_to_uri(self.xslset)) |
| 294 |
f.write('</xsl:stylesheet>\n') |
| 295 |
f.close() |
| 296 |
|
| 297 |
doclist = os.path.join(self.tmpdir, "doclist.txt") |
| 298 |
self.xsltproc.use_catalogs = 0 |
| 299 |
self.xsltproc.run(xslset, self.input, doclist, opts=self.xslopts) |
| 300 |
|
| 301 |
# If <doclist> is missing or is empty, there's no set, or only one |
| 302 |
# book from the set is compiled |
| 303 |
if os.path.isfile(doclist) and os.path.getsize(doclist) > 0: |
| 304 |
self._multiple_setup(doclist) |
| 305 |
else: |
| 306 |
self._single_setup() |
| 307 |
|
| 308 |
def make_rawtex(self): |
| 309 |
if len(self.documents) == 1: |
| 310 |
self.rawfile = self.documents[0].rawfile |
| 311 |
else: |
| 312 |
self.rawfile = "output.rtex" |
| 313 |
|
| 314 |
param = {"listings.xml": self.listings, |
| 315 |
"current.dir": self.inputdir} |
| 316 |
self.xsltproc.use_catalogs = 1 |
| 317 |
self.xsltproc.run(self.xslbuild, self.input, |
| 318 |
self.rawfile, opts=self.xslopts, params=param) |
| 319 |
|
| 320 |
# Now, find the intermediate raw files |
| 321 |
rawfiles = glob.glob("*.rtex") |
| 322 |
for rawfile in rawfiles: |
| 323 |
if not(rawfile in self.documents): |
| 324 |
d = Document(rawfile, binfmt=self.format) |
| 325 |
if d.has_subext(".input"): |
| 326 |
self.included.append(d) |
| 327 |
else: |
| 328 |
self.interms.append(d) |
| 329 |
|
| 330 |
def make_tex(self): |
| 331 |
self.rawtex.set_format(self.format, self.backend) |
| 332 |
if self.fig_format: |
| 333 |
self.rawtex.fig_format(self.fig_format) |
| 334 |
|
| 335 |
# By default figures are relative to the source file directory |
| 336 |
self.rawtex.set_fig_paths([self.inputdir] + self.fig_paths) |
| 337 |
|
| 338 |
for d in self.documents + self.interms + self.included: |
| 339 |
self.rawtex.parse(d.rawfile, d.texfile) |
| 340 |
|
| 341 |
def make_bin(self): |
| 342 |
self.runtex.texpost = self.texpost |
| 343 |
self.runtex.set_fig_paths([self.inputdir] + self.fig_paths) |
| 344 |
self.runtex.set_bib_paths([self.inputdir] + self.bib_paths, |
| 345 |
[self.inputdir] + self.bst_paths) |
| 346 |
|
| 347 |
# Build the intermediate files and (after) the main documents |
| 348 |
for d in self.interms + self.documents: |
| 349 |
self.log.info("Build %s" % d.binfile) |
| 350 |
self.runtex.compile(d.texfile, d.binfile, self.format, |
| 351 |
batch=self.texbatch) |
| 352 |
# Only reinit, to not lose the produced working files |
| 353 |
# used to track the dependencies on other documents |
| 354 |
self.runtex.reinit() |
| 355 |
|
| 356 |
def compile(self): |
| 357 |
self.set_xslt() |
| 358 |
self.set_backend() |
| 359 |
self.cwdir = os.getcwd() |
| 360 |
self.tmpdir = self.tmpdir_user or tempfile.mkdtemp() |
| 361 |
if self.input: |
| 362 |
self.inputdir = os.path.dirname(self.input) |
| 363 |
else: |
| 364 |
self._stdin_write() |
| 365 |
os.chdir(self.tmpdir) |
| 366 |
try: |
| 367 |
donefiles = self._compile() |
| 368 |
if len(donefiles) == 1: |
| 369 |
shutil.move(donefiles[0], self.output) |
| 370 |
self.log.info("'%s' successfully built" % \ |
| 371 |
os.path.basename(self.output)) |
| 372 |
else: |
| 373 |
for d in donefiles: |
| 374 |
shutil.move(d, self.outputdir) |
| 375 |
donefiles.sort() |
| 376 |
self.log.info("Files successfully built in '%s':\n%s" % \ |
| 377 |
(self.outputdir, "\n".join(donefiles))) |
| 378 |
finally: |
| 379 |
os.chdir(self.cwdir) |
| 380 |
if not(self.debug): |
| 381 |
shutil.rmtree(self.tmpdir) |
| 382 |
else: |
| 383 |
self.log.info("%s not removed" % self.tmpdir) |
| 384 |
|
| 385 |
def _stdin_write(self): |
| 386 |
# Find out the stdin working directory |
| 387 |
self.inputdir = self.stdindir or self.cwdir |
| 388 |
|
| 389 |
# Need to dump the stdin input, because of the two passes |
| 390 |
self.input = os.path.join(self.tmpdir, "stdin.xml") |
| 391 |
if sys.stdin.encoding is None: |
| 392 |
f = open(self.input, "wb") |
| 393 |
else: |
| 394 |
f = open(self.input, "wt", encoding=sys.stdin.encoding) |
| 395 |
for line in sys.stdin: |
| 396 |
f.write(line) |
| 397 |
f.close() |
| 398 |
|
| 399 |
def _compile(self): |
| 400 |
# The temporary output file |
| 401 |
tmpout = os.path.basename(self.input) |
| 402 |
for s in (" ", "\t"): |
| 403 |
tmpout = tmpout.replace(s, "_") |
| 404 |
self.basefile = suffix_replace(tmpout, "." + self.input_format) |
| 405 |
|
| 406 |
# Convert SGML to XML if needed |
| 407 |
if self.input_format == "sgml": |
| 408 |
self.make_xml() |
| 409 |
|
| 410 |
# Build the user XSL stylesheet if needed |
| 411 |
self.build_stylesheet() |
| 412 |
|
| 413 |
# List the documents to build |
| 414 |
self.build_doclist() |
| 415 |
|
| 416 |
# Refresh the TEXINPUTS |
| 417 |
self.update_texinputs() |
| 418 |
|
| 419 |
# For easy debug |
| 420 |
if self.debug and "TEXINPUTS" in os.environ: |
| 421 |
if os.name != "nt": |
| 422 |
f = open("env_tex", "wt") |
| 423 |
f.write("TEXINPUTS=%s\nexport TEXINPUTS\n" % \ |
| 424 |
os.environ["TEXINPUTS"]) |
| 425 |
f.close() |
| 426 |
else: |
| 427 |
f = open("env_tex.bat", "wt") |
| 428 |
f.write("set TEXINPUTS=%s\n" % os.environ["TEXINPUTS"]) |
| 429 |
f.close() |
| 430 |
|
| 431 |
# Build the tex file(s), and compile it(them) |
| 432 |
self.make_listings() |
| 433 |
self.make_rawtex() |
| 434 |
if self.format == "rtex": |
| 435 |
return [ d.rawfile for d in self.documents ] |
| 436 |
|
| 437 |
self.make_tex() |
| 438 |
if self.format == "tex": |
| 439 |
return [ d.texfile for d in self.documents ] |
| 440 |
|
| 441 |
self.make_bin() |
| 442 |
return [ d.binfile for d in self.documents ] |
| 443 |
|
| 444 |
|
| 445 |
# |
| 446 |
# Command entry point |
| 447 |
# |
| 448 |
class DbTexCommand: |
| 449 |
def __init__(self, base): |
| 450 |
prog = os.path.splitext(os.path.basename(sys.argv[0]))[0] |
| 451 |
usage = "%s [options] file" % prog |
| 452 |
parser = OptionParser(usage=usage) |
| 453 |
parser.add_option("-b", "--backend", |
| 454 |
help="Backend driver to use. The available drivers" |
| 455 |
" are 'pdftex' (default), 'dvips' and 'xetex'.") |
| 456 |
parser.add_option("-B", "--no-batch", action="store_true", |
| 457 |
help="All the tex output is printed") |
| 458 |
parser.add_option("-c", "-S", "--config", action="append", |
| 459 |
help="Configuration file") |
| 460 |
parser.add_option("-C", "--changedir", |
| 461 |
help="Standard input working directory") |
| 462 |
parser.add_option("-d", "--debug", action="store_true", |
| 463 |
help="Debug mode. Keep the temporary directory in " |
| 464 |
"which %s actually works" % prog) |
| 465 |
parser.add_option("-D", "--dump", action="store_true", |
| 466 |
help="Dump error stack (debug purpose)") |
| 467 |
parser.add_option("-e", "--indexstyle", |
| 468 |
help="Index Style file to pass to makeindex") |
| 469 |
parser.add_option("-f", "--fig-format", |
| 470 |
help="Input figure format, used when not deduced " |
| 471 |
"from figure extension") |
| 472 |
parser.add_option("-F", "--input-format", |
| 473 |
help="Input file format: sgml, xml. (default=xml)") |
| 474 |
parser.add_option("-i", "--texinputs", action="append", |
| 475 |
help="Path added to TEXINPUTS") |
| 476 |
parser.add_option("-I", "--fig-path", action="append", |
| 477 |
dest="fig_paths", metavar="FIG_PATH", |
| 478 |
help="Additional lookup path of the figures") |
| 479 |
parser.add_option("-l", "--bst-path", action="append", |
| 480 |
dest="bst_paths", metavar="BST_PATH", |
| 481 |
help="Bibliography style file path") |
| 482 |
parser.add_option("-L", "--bib-path", action="append", |
| 483 |
dest="bib_paths", metavar="BIB_PATH", |
| 484 |
help="BibTeX database path") |
| 485 |
parser.add_option("-m", "--xslt", |
| 486 |
help="XSLT engine to use. (default=xsltproc)") |
| 487 |
parser.add_option("-o", "--output", dest="output", |
| 488 |
help="Output filename. " |
| 489 |
"When not used, the input filename " |
| 490 |
"is used, with the suffix of the output format") |
| 491 |
parser.add_option("-O", "--output-dir", |
| 492 |
help="Output directory for the built books." |
| 493 |
" When not defined, the current working " |
| 494 |
"directory is used. Option used only for " |
| 495 |
"a document having a <set>") |
| 496 |
parser.add_option("-p", "--xsl-user", action="append", |
| 497 |
help="XSL user configuration file to use") |
| 498 |
parser.add_option("-P", "--param", dest="xslparams", |
| 499 |
action="append", metavar="PARAM=VALUE", |
| 500 |
help="Set an XSL parameter value from command line") |
| 501 |
parser.add_option("-q", "--quiet", action="store_true", |
| 502 |
help="Less verbose, showing only error messages") |
| 503 |
parser.add_option("-r", "--texpost", metavar="SCRIPT", |
| 504 |
help="Script called at the very end of the tex " |
| 505 |
"compilation. Its role is to modify the tex file " |
| 506 |
"or one of the compilation file before the last " |
| 507 |
"round.") |
| 508 |
parser.add_option("-s", "--texstyle", metavar="STYFILE", |
| 509 |
help="Latex style to apply. It can be a package name, or " |
| 510 |
"directly a package path that must ends with " |
| 511 |
"'.sty'") |
| 512 |
parser.add_option("-t", "--type", dest="format", |
| 513 |
help="Output format. Available formats:\n" |
| 514 |
"tex, dvi, ps, pdf (default=pdf)") |
| 515 |
parser.add_option("--dvi", action="store_true", dest="format_dvi", |
| 516 |
help="DVI output. Equivalent to -tdvi") |
| 517 |
parser.add_option("--pdf", action="store_true", dest="format_pdf", |
| 518 |
help="PDF output. Equivalent to -tpdf") |
| 519 |
parser.add_option("--ps", action="store_true", dest="format_ps", |
| 520 |
help="PostScript output. Equivalent to -tps") |
| 521 |
parser.add_option("-T", "--style", |
| 522 |
help="Predefined output style") |
| 523 |
parser.add_option("--tmpdir", |
| 524 |
help="Temporary working directory to use (for debug only)") |
| 525 |
parser.add_option("-v", "--version", action="store_true", |
| 526 |
help="Print the %s version" % prog) |
| 527 |
parser.add_option("-V", "--verbose", action="store_true", |
| 528 |
help="Verbose mode, showing the running commands") |
| 529 |
parser.add_option("-x", "--xslt-opts", dest="xslopts", |
| 530 |
action="append", metavar="XSLT_OPTIONS", |
| 531 |
help="Arguments directly passed to the XSLT engine") |
| 532 |
parser.add_option("-X", "--no-external", action="store_true", |
| 533 |
help="Disable the external text file support used for " |
| 534 |
"some callout processing") |
| 535 |
|
| 536 |
self.parser = parser |
| 537 |
self.base = base |
| 538 |
self.prog = prog |
| 539 |
# The actual engine to use is unknown |
| 540 |
self.run = None |
| 541 |
|
| 542 |
def load_plugin(self, pathname): |
| 543 |
moddir, modname = os.path.split(pathname) |
| •544 |
spec = importlib.machinery.PathFinder.find_spec(modname, [moddir]) |
| 545 |
if not spec: |
| 546 |
spec = importlib.machinery.PathFinder.find_spec(modname) |
| 547 |
if not spec: |
| 548 |
failed_exit("Error: '%s' module not found" % modname) |
| 549 |
mod = importlib.util.module_from_spec(spec) |
| 550 |
spec.loader.exec_module(mod) |
| 551 |
sys.modules[modname] = mod |
| 552 |
return mod |
| 553 |
|
| 554 |
def run_setup(self, options): |
| 555 |
run = self.run |
| 556 |
|
| 557 |
if not(options.format): |
| 558 |
if options.format_pdf: |
| 559 |
options.format = "pdf" |
| 560 |
elif options.format_ps: |
| 561 |
options.format = "ps" |
| 562 |
elif options.format_dvi: |
| 563 |
options.format = "dvi" |
| 564 |
|
| 565 |
if options.format: |
| 566 |
try: |
| 567 |
run.set_format(options.format) |
| 568 |
except Exception as e: |
| 569 |
failed_exit("Error: %s" % e) |
| 570 |
|
| 571 |
# Always set the XSLT (default or not) |
| 572 |
try: |
| 573 |
run.set_xslt(options.xslt) |
| 574 |
except Exception as e: |
| 575 |
failed_exit("Error: %s" % e) |
| 576 |
|
| 577 |
if options.xslopts: |
| 578 |
for o in options.xslopts: |
| 579 |
run.xslopts += shlex.split(o) |
| 580 |
|
| 581 |
if options.xslparams: |
| 582 |
run.xslparams += options.xslparams |
| 583 |
|
| 584 |
if options.debug: |
| 585 |
run.debug = options.debug |
| 586 |
|
| 587 |
if options.fig_paths: |
| 588 |
run.fig_paths += [os.path.realpath(p) for p in options.fig_paths] |
| 589 |
|
| 590 |
if options.bib_paths: |
| 591 |
run.bib_paths += [os.path.realpath(p) for p in options.bib_paths] |
| 592 |
|
| 593 |
if options.bst_paths: |
| 594 |
run.bst_paths += [os.path.realpath(p) for p in options.bst_paths] |
| 595 |
|
| 596 |
if options.texstyle: |
| 597 |
try: |
| 598 |
xslparam, texpath = texstyle_parse(options.texstyle) |
| 599 |
except Exception as e: |
| 600 |
failed_exit("Error: %s" % e) |
| 601 |
run.xslparams.append(xslparam) |
| 602 |
if texpath: run.texinputs.append(texpath) |
| 603 |
|
| 604 |
if options.indexstyle: |
| 605 |
run.runtex.index_style = os.path.abspath(options.indexstyle) |
| 606 |
|
| 607 |
if options.texinputs: |
| 608 |
for texinputs in options.texinputs: |
| 609 |
run.texinputs += texinputs_parse(texinputs) |
| 610 |
|
| 611 |
if options.fig_format: |
| 612 |
run.fig_format = options.fig_format |
| 613 |
|
| 614 |
if options.input_format: |
| 615 |
run.input_format = options.input_format |
| 616 |
|
| 617 |
if options.no_batch: |
| 618 |
run.texbatch = 0 |
| 619 |
|
| 620 |
if options.backend: |
| 621 |
run.backend = options.backend |
| 622 |
|
| 623 |
if options.xsl_user: |
| 624 |
for xfile in options.xsl_user: |
| 625 |
xsluser = os.path.realpath(xfile) |
| 626 |
if not(os.path.isfile(xsluser)): |
| 627 |
failed_exit("Error: '%s' does not exist" % options.xsl_user) |
| 628 |
run.xslusers.append(xsluser) |
| 629 |
|
| 630 |
if options.texpost: |
| 631 |
is_plugin = options.texpost.startswith("plugin:") |
| 632 |
if is_plugin: |
| 633 |
path = self.load_plugin(options.texpost[len("plugin:"):]) |
| 634 |
else: |
| 635 |
path = os.path.realpath(options.texpost) |
| 636 |
if not(os.path.isfile(path)): |
| 637 |
failed_exit("Error: '%s' does not exist" % options.texpost) |
| 638 |
run.texpost = path |
| 639 |
|
| 640 |
if options.no_external: |
| 641 |
run.unset_flags(run.USE_MKLISTINGS) |
| 642 |
|
| 643 |
if options.verbose: |
| 644 |
run.verbose = options.verbose |
| 645 |
|
| 646 |
if options.quiet: |
| 647 |
run.verbose = logger.QUIET |
| 648 |
run.xslparams.append("output.quietly=1") |
| 649 |
|
| 650 |
if options.tmpdir: |
| 651 |
if not(os.path.exists(options.tmpdir)): |
| 652 |
try: |
| 653 |
os.mkdir(options.tmpdir) |
| 654 |
except Exception as e: |
| 655 |
failed_exit("Error: %s" % e) |
| 656 |
run.tmpdir_user = os.path.abspath(options.tmpdir) |
| 657 |
|
| 658 |
if options.dump: |
| 659 |
dump_stack() |
| 660 |
|
| 661 |
def get_config_paths(self): |
| 662 |
# Allows user directories where to look for configuration files |
| 663 |
paths = [os.getcwd()] |
| 664 |
paths.append(os.path.expanduser(os.path.join("~", "."+self.prog))) |
| 665 |
|
| 666 |
# Unix specific system-wide config files |
| 667 |
if "posix" in sys.builtin_module_names: |
| 668 |
paths.append(os.path.join("/etc", self.prog)) |
| 669 |
|
| 670 |
# Last but not least, the tool config dir |
| 671 |
paths.append(self.run.confdir) |
| 672 |
|
| 673 |
# Optionally the paths from an environment variable |
| 674 |
conf_paths = os.getenv("DBLATEX_CONFIG_FILES") |
| 675 |
if not(conf_paths): |
| 676 |
return paths |
| 677 |
|
| 678 |
paths += conf_paths.split(os.pathsep) |
| 679 |
return paths |
| 680 |
|
| 681 |
def main(self): |
| 682 |
(options, args) = self.parser.parse_args() |
| 683 |
|
| 684 |
run = self.run |
| 685 |
parser = self.parser |
| 686 |
|
| 687 |
if options.version: |
| 688 |
version = run.get_version() |
| 689 |
print("%s version %s" % (self.prog, version)) |
| 690 |
if not(args): |
| 691 |
sys.exit(0) |
| 692 |
|
| 693 |
# At least the input file is expected |
| 694 |
if not(args): |
| 695 |
parser.parse_args(args=["-h"]) |
| 696 |
|
| 697 |
# Load the specified configurations |
| 698 |
conf = DbtexConfig() |
| 699 |
if options.dump: |
| 700 |
dump_stack() |
| 701 |
|
| 702 |
if options.style: |
| 703 |
try: |
| 704 |
conf.paths = self.get_config_paths() |
| 705 |
conf.fromstyle(options.style) |
| 706 |
except Exception as e: |
| 707 |
failed_exit("Error: %s" % e) |
| 708 |
|
| 709 |
if options.config: |
| 710 |
try: |
| 711 |
for config in options.config: |
| 712 |
conf.fromfile(config) |
| 713 |
except Exception as e: |
| 714 |
failed_exit("Error: %s" % e) |
| 715 |
|
| 716 |
if conf.options: |
| 717 |
options2, args2 = parser.parse_args(conf.options) |
| 718 |
self.run_setup(options2) |
| 719 |
|
| 720 |
# Now apply the command line setup |
| 721 |
self.run_setup(options) |
| 722 |
|
| 723 |
# Verbose mode |
| 724 |
run.log = logger.logger(self.prog, run.verbose) |
| 725 |
|
| 726 |
# Data from standard input? |
| 727 |
if args[0] == "-": |
| 728 |
if not(options.output): |
| 729 |
failed_exit("Error: -o expected when input from stdin") |
| 730 |
input = "" |
| 731 |
if options.changedir: |
| 732 |
run.stdindir = os.path.realpath(options.changedir) |
| 733 |
else: |
| 734 |
input = os.path.realpath(args[0]) |
| 735 |
|
| 736 |
# Output file in case of single document (main case) |
| 737 |
if not(options.output): |
| 738 |
output = None |
| 739 |
else: |
| 740 |
output = os.path.realpath(options.output) |
| 741 |
|
| 742 |
# Output directory in case of chunked books (from a set) |
| 743 |
if not(options.output_dir): |
| 744 |
outputdir = None |
| 745 |
else: |
| 746 |
# Check the output dir is OK |
| 747 |
outputdir = os.path.realpath(options.output_dir) |
| 748 |
if not(os.path.isdir(outputdir)): |
| 749 |
failed_exit("Error: '%s' is not a directory" %\ |
| 750 |
options.output_dir) |
| 751 |
|
| 752 |
run.input = input |
| 753 |
run.output = output |
| 754 |
run.outputdir = outputdir |
| 755 |
|
| 756 |
# Try to buid the file |
| 757 |
try: |
| 758 |
run.compile() |
| 759 |
except Exception as e: |
| 760 |
signal_error(self, e) |
| 761 |
failed_exit("Error: %s" % e) |
| 762 |
|