| 1 |
# |
| 2 |
# Attempt to analyse a dblatex failure occured on a Debian platform. |
| 3 |
# |
| 4 |
# Author: Andreas Hoenen |
| 5 |
# |
| 6 |
from __future__ import print_function |
| 7 |
|
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
import apt |
| 11 |
import os |
| 12 |
|
| 13 |
from dbtexmf.core.error import ErrorHandler |
| 14 |
from dbtexmf.core.imagedata import ImageConverter |
| 15 |
from dbtexmf.core.dbtex import DbTexCommand |
| 16 |
|
| 17 |
class AptSilentProgress(apt.progress.text.OpProgress): |
| 18 |
""" |
| 19 |
Avoid the annoying progress messages when building the apt cache: |
| 20 |
Reading package lists... Done |
| 21 |
Building dependency tree |
| 22 |
Reading state information... Done |
| 23 |
Building data structures... Done |
| 24 |
""" |
| 25 |
def __init__(self, outfile=None): |
| 26 |
pass |
| 27 |
def done(self): |
| 28 |
pass |
| 29 |
def update(self, percent=None): |
| 30 |
pass |
| 31 |
|
| 32 |
class DebianHandler(ErrorHandler): |
| 33 |
def __init__(self): |
| 34 |
ErrorHandler.__init__(self) |
| 35 |
self.object = None |
| 36 |
self.aptcache = None |
| 37 |
|
| 38 |
def signal(self, failed_object, *args, **kwargs): |
| 39 |
self.object = failed_object |
| 40 |
if not self.aptcache: |
| 41 |
self.aptcache = apt.Cache(progress=AptSilentProgress()) |
| 42 |
if (isinstance(self.object, DbTexCommand)): |
| 43 |
error_handled = self._check_dbtexrun() |
| 44 |
elif (isinstance(self.object, ImageConverter)): |
| 45 |
error_handled = self._check_imagerun(*args) |
| 46 |
else: |
| 47 |
error_handled = False |
| 48 |
if not error_handled: |
| 49 |
super(DebianHandler, self).signal(failed_object, *args, **kwargs) |
| 50 |
|
| 51 |
def _check_dbtexrun(self): |
| 52 |
# First, check the XML input sanity |
| 53 |
if (self._check_input()): |
| 54 |
return True |
| 55 |
# Check that all the required utilities are there |
| 56 |
if (self._check_dependencies()): |
| 57 |
return True |
| 58 |
# Check some alternative reasons |
| 59 |
if (self._check_cyrillic()): |
| 60 |
return True |
| 61 |
return False |
| 62 |
|
| 63 |
def _check_imagerun(self, cmd): |
| 64 |
""" |
| 65 |
In case of failed image converter calls check on dependency problems. |
| 66 |
|
| 67 |
In Debian dblatex package dependencies on image converters are not |
| 68 |
absolute, as image conversion is not dblatex's core functionality. |
| 69 |
Thus the converters may be not installed. Therefore check for each one: |
| 70 |
If it is used but missing, dump an appropriate hint. |
| 71 |
""" |
| 72 |
aptcache = self.aptcache |
| 73 |
warn_msgs = [] |
| 74 |
if ((cmd.startswith('convert') or cmd.find('&& convert') > -1) |
| 75 |
and not aptcache['graphicsmagick-imagemagick-compat'].is_installed |
| 76 |
and not aptcache['imagemagick'].is_installed): |
| 77 |
warn_msgs.append('For image conversion one of Debian packages' |
| 78 |
+ ' graphicsmagick-imagemagick-compat') |
| 79 |
warn_msgs.append('or imagemagick is needed') |
| 80 |
if ((cmd.startswith('epstopdf') or cmd.find('&& epstopdf') > -1) |
| 81 |
and not aptcache['ghostscript'].is_installed): |
| 82 |
warn_msgs.append('For image conversion Debian package ghostscript' |
| 83 |
+ ' is needed') |
| 84 |
if ((cmd.startswith('fig2dev') or cmd.find('&& fig2dev') > -1) |
| 85 |
and not aptcache['transfig'].is_installed): |
| 86 |
warn_msgs.append('For image conversion Debian package transfig is' |
| 87 |
+ ' needed') |
| 88 |
if ((cmd.startswith('inkscape') or cmd.find('&& inkscape') > -1) |
| 89 |
and not aptcache['inkscape'].is_installed): |
| 90 |
warn_msgs.append('For image conversion Debian package inkscape is' |
| 91 |
+ ' needed') |
| 92 |
if warn_msgs: |
| 93 |
print("\n" + "\n".join(warn_msgs) + "\n", file=sys.stderr) |
| 94 |
return True |
| 95 |
else: |
| 96 |
return False |
| 97 |
|
| 98 |
def _check_input(self): |
| 99 |
""" |
| 100 |
In case of failed processing try to validate the input. |
| 101 |
|
| 102 |
As invalid DocBook sometimes results in strange TeX error messages, a |
| 103 |
hint about the failure cause may be helpful. |
| 104 |
Post failure validation is a convenience function and thus works in |
| 105 |
a best effort approach, that is it will silently skip any problems, |
| 106 |
e.g. the external validation program xmllint not installed. |
| 107 |
""" |
| 108 |
obj = self.object.run |
| 109 |
nulldev0 = open(os.devnull, "r") |
| 110 |
nulldev1 = open(os.devnull, "w") |
| 111 |
try: |
| 112 |
rc = subprocess.Popen(['xmllint', '--noout', '--postvalid', |
| 113 |
'--xinclude', obj.input], |
| 114 |
stdin=nulldev0, |
| 115 |
stderr=nulldev1, |
| 116 |
stdout=nulldev1).wait() |
| 117 |
except: |
| 118 |
rc = -1 |
| 119 |
|
| 120 |
nulldev0.close() |
| 121 |
nulldev1.close() |
| 122 |
|
| 123 |
if rc == 3 or rc == 4: |
| 124 |
print(file=sys.stderr) |
| 125 |
print('A possible reason for transformation', end=' ', file=sys.stderr) |
| 126 |
print('failure is invalid DocBook', file=sys.stderr) |
| 127 |
print('(as reported by xmllint)', file=sys.stderr) |
| 128 |
print(file=sys.stderr) |
| 129 |
return True |
| 130 |
else: |
| 131 |
return False |
| 132 |
|
| 133 |
def _check_dependencies(self): |
| 134 |
""" |
| 135 |
In case of failed processing check on dependency problems. |
| 136 |
|
| 137 |
For not commonly used dblatex functionality the Debian package |
| 138 |
dependencies are not absolute, thus the functionality may be not |
| 139 |
installed. Therefore check for each one: |
| 140 |
If it is used but a needed dependency is missing, dump an appropriate |
| 141 |
hint. |
| 142 |
""" |
| 143 |
obj = self.object.run |
| 144 |
aptcache = self.aptcache |
| 145 |
warn_msgs = [] |
| 146 |
if obj.backend == 'xetex': |
| 147 |
for debian_pkg in 'texlive-xetex', 'lmodern': |
| 148 |
if not aptcache[debian_pkg].is_installed: |
| 149 |
warn_msgs.append('For xetex backend Debian package ' |
| 150 |
+ debian_pkg + ' is needed') |
| 151 |
if obj.input_format == 'sgml': |
| 152 |
for debian_pkg in 'docbook', 'opensp': |
| 153 |
if not aptcache[debian_pkg].is_installed: |
| 154 |
warn_msgs.append('For SGML documents Debian package ' |
| 155 |
+ debian_pkg + ' is needed') |
| 156 |
if obj.runtex.texer.encoding == 'utf8': |
| 157 |
debian_pkg = 'texlive-lang-cyrillic' |
| 158 |
if not aptcache[debian_pkg].is_installed: |
| 159 |
warn_msgs.append('For utf8 encoding Debian package ' |
| 160 |
+ debian_pkg + ' is needed') |
| 161 |
if warn_msgs: |
| 162 |
print("\n" + "\n".join(warn_msgs) + "\n", file=sys.stderr) |
| 163 |
return True |
| 164 |
else: |
| 165 |
return False |
| 166 |
|
| 167 |
def _check_cyrillic(self): |
| 168 |
obj = self.object.run |
| 169 |
""" |
| 170 |
In case of failed processing check on the "cyrillic scenario": |
| 171 |
|
| 172 |
Transforming cyrillic documents will fail when neither using the |
| 173 |
XeTeX backend nor setting option latex.unicode.use |
| 174 |
In this case a hint to XeTeX (as the preferred way) may be helpful. |
| 175 |
Post failure validation is a convenience function and thus works in |
| 176 |
a best effort approach, that is it will silently skip any problems. |
| 177 |
""" |
| 178 |
# This kind of error cannot occur with backends that natively support |
| 179 |
# Unicode |
| 180 |
if obj.backend == 'xetex': |
| 181 |
return False |
| 182 |
|
| 183 |
try: |
| 184 |
for log_entry in obj.runtex.texer.tex.log.get_errors(): |
| 185 |
if (log_entry['text'] |
| 186 |
== r'Undefined control sequence \cyrchar.'): |
| 187 |
print(file=sys.stderr) |
| 188 |
print('Transformation failure', end=' ', file=sys.stderr) |
| 189 |
print('might be caused by handling a', end=' ', file=sys.stderr) |
| 190 |
print('cyrillic document', file=sys.stderr) |
| 191 |
print('without the XeTeX backend', file=sys.stderr) |
| 192 |
print(file=sys.stderr) |
| 193 |
return True |
| 194 |
except: |
| 195 |
pass |
| 196 |
return False |
| 197 |
|