Blame lib/dbtexmf/dblatex/grubber/pdftex.py.enable-python3

Packit 5164a5
# This file is part of Rubber and thus covered by the GPL
Packit 5164a5
# (c) Emmanuel Beffara, 2002--2006
Packit 5164a5
"""
Packit 5164a5
pdfLaTeX support for Rubber.
Packit 5164a5
Packit 5164a5
When this module loaded with the otion 'dvi', the document is compiled to DVI
Packit 5164a5
using pdfTeX.
Packit 5164a5
Packit 5164a5
The module optimizes the pdflatex calls by setting -draftmode and apply a last
Packit 5164a5
call to build the final PDF output.
Packit 5164a5
"""
Packit 5164a5
import os
Packit 5164a5
import re
Packit 5164a5
import subprocess
Packit 5164a5
from subprocess import Popen, PIPE
Packit 5164a5
Packit 5164a5
from msg import _, msg
Packit 5164a5
from plugins import TexModule
Packit 5164a5
Packit 5164a5
Packit 5164a5
class Module (TexModule):
Packit 5164a5
    def __init__ (self, doc, dict):
Packit 5164a5
        self.doc = doc
Packit 5164a5
        doc.program = "pdflatex"
Packit 5164a5
        doc.engine = "pdfTeX"
Packit 5164a5
        # FIXME: how to handle opt=dvi with file.tex passed?
Packit 5164a5
        # FIXME: can we add commands after the file?
Packit 5164a5
        doc.set_format("pdf")
Packit 5164a5
Packit 5164a5
        # Check the version to know if -draftmode is supported
Packit 5164a5
        if (self._draft_is_supported()):
Packit 5164a5
            self.doc.draft_support = True
Packit 5164a5
        else:
Packit 5164a5
            self.doc.draft_support = False
Packit 5164a5
        #self.draft_support = False
Packit 5164a5
Packit 5164a5
    def _draft_is_supported(self):
Packit 5164a5
        # FIXME: find a clean method to pass these options
Packit 5164a5
        opts = os.getenv("DBLATEX_PDFTEX_OPTIONS", "")
Packit 5164a5
        if not("-draftmode" in opts):
Packit 5164a5
            return False
Packit 5164a5
        return (self._get_version() == "1.40")
Packit 5164a5
Packit 5164a5
    def pre_compile(self):
Packit 5164a5
        if not(self.doc.draft_support):
Packit 5164a5
            return
Packit 5164a5
Packit 5164a5
        # Add -draftmode to prevent intermediate pdf output
Packit 5164a5
        self.doc.opts.append("-draftmode")
Packit 5164a5
Packit 5164a5
    def last_compile(self):
Packit 5164a5
        # If pdftex has no ability to work in draftmode, or if no final PDF
Packit 5164a5
        # is required, do nothing
Packit 5164a5
        if not(self.doc.draft_support) or self.doc.draft_only:
Packit 5164a5
            return
Packit 5164a5
Packit 5164a5
        # Remove the -draftmode to have the PDF output, and compile again
Packit 5164a5
        self.doc.opts.remove("-draftmode")
Packit 5164a5
        rc = self.doc.compile()
Packit 5164a5
        return rc
Packit 5164a5
Packit 5164a5
    def _get_version(self):
Packit 5164a5
        """
Packit 5164a5
        Parse something like:
Packit 5164a5
Packit 5164a5
          pdfTeX using libpoppler 3.141592-1.40.3-2.2 (Web2C 7.5.6)
Packit 5164a5
          kpathsea version 3.5.6
Packit 5164a5
          Copyright 2007 Peter Breitenlohner (eTeX)/Han The Thanh (pdfTeX).
Packit 5164a5
          Kpathsea is copyright 2007 Karl Berry and Olaf Weber.
Packit 5164a5
          ...
Packit 5164a5
        and return '1.40'
Packit 5164a5
        """
Packit 5164a5
        # Grab the major version number
Packit 5164a5
        p = Popen("pdflatex -version", shell=True, stdout=PIPE)
Packit 5164a5
        data = p.communicate()[0]
Packit 5164a5
        m = re.search("pdfTeX.*3.14[^-]*-(\d*.\d*)", data, re.M)
Packit 5164a5
        if not(m):
Packit 5164a5
            return ""
Packit 5164a5
        else:
Packit 5164a5
            return m.group(1)
Packit 5164a5