[565] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
[576] | 4 | # Tea4CUPS : Tee for CUPS |
---|
[565] | 5 | # |
---|
| 6 | # (c) 2005 Jerome Alet <alet@librelogiciel.com> |
---|
| 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
| 23 | # |
---|
| 24 | |
---|
| 25 | import sys |
---|
| 26 | import os |
---|
[584] | 27 | import popen2 |
---|
[568] | 28 | import errno |
---|
[577] | 29 | import md5 |
---|
[565] | 30 | import cStringIO |
---|
| 31 | import shlex |
---|
[568] | 32 | import tempfile |
---|
[570] | 33 | import ConfigParser |
---|
[588] | 34 | import select |
---|
| 35 | import signal |
---|
| 36 | import time |
---|
[581] | 37 | from struct import unpack |
---|
[565] | 38 | |
---|
[597] | 39 | version = "1.5alpha" |
---|
[585] | 40 | |
---|
[570] | 41 | class TeeError(Exception): |
---|
[576] | 42 | """Base exception for Tea4CUPS related stuff.""" |
---|
[570] | 43 | def __init__(self, message = ""): |
---|
| 44 | self.message = message |
---|
| 45 | Exception.__init__(self, message) |
---|
| 46 | def __repr__(self): |
---|
| 47 | return self.message |
---|
| 48 | __str__ = __repr__ |
---|
| 49 | |
---|
| 50 | class ConfigError(TeeError) : |
---|
| 51 | """Configuration related exceptions.""" |
---|
| 52 | pass |
---|
| 53 | |
---|
[581] | 54 | class IPPError(TeeError) : |
---|
| 55 | """IPP related exceptions.""" |
---|
| 56 | pass |
---|
| 57 | |
---|
[584] | 58 | class Popen4ForCUPS(popen2.Popen4) : |
---|
| 59 | """Our own class to execute real backends. |
---|
| 60 | |
---|
| 61 | Their first argument is different from their path so using |
---|
| 62 | native popen2.Popen3 would not be feasible. |
---|
| 63 | """ |
---|
| 64 | def __init__(self, cmd, bufsize=-1, arg0=None) : |
---|
| 65 | self.arg0 = arg0 |
---|
| 66 | popen2.Popen4.__init__(self, cmd, bufsize) |
---|
| 67 | |
---|
| 68 | def _run_child(self, cmd): |
---|
| 69 | try : |
---|
| 70 | MAXFD = os.sysconf("SC_OPEN_MAX") |
---|
| 71 | except (AttributeError, ValueError) : |
---|
| 72 | MAXFD = 256 |
---|
| 73 | for i in range(3, MAXFD) : |
---|
| 74 | try: |
---|
| 75 | os.close(i) |
---|
| 76 | except OSError: |
---|
| 77 | pass |
---|
| 78 | try: |
---|
| 79 | os.execvpe(cmd[0], [self.arg0 or cmd[0]] + cmd[1:], os.environ) |
---|
| 80 | finally: |
---|
| 81 | os._exit(1) |
---|
| 82 | |
---|
[582] | 83 | # Some IPP constants |
---|
| 84 | OPERATION_ATTRIBUTES_TAG = 0x01 |
---|
| 85 | JOB_ATTRIBUTES_TAG = 0x02 |
---|
| 86 | END_OF_ATTRIBUTES_TAG = 0x03 |
---|
| 87 | PRINTER_ATTRIBUTES_TAG = 0x04 |
---|
| 88 | UNSUPPORTED_ATTRIBUTES_TAG = 0x05 |
---|
| 89 | |
---|
[581] | 90 | class IPPMessage : |
---|
| 91 | """A class for IPP message files.""" |
---|
| 92 | def __init__(self, data) : |
---|
| 93 | """Initializes an IPP Message object.""" |
---|
| 94 | self.data = data |
---|
| 95 | self._attributes = {} |
---|
| 96 | self.curname = None |
---|
| 97 | self.tags = [ None ] * 256 # by default all tags reserved |
---|
| 98 | |
---|
| 99 | # Delimiter tags |
---|
| 100 | self.tags[0x01] = "operation-attributes-tag" |
---|
| 101 | self.tags[0x02] = "job-attributes-tag" |
---|
| 102 | self.tags[0x03] = "end-of-attributes-tag" |
---|
| 103 | self.tags[0x04] = "printer-attributes-tag" |
---|
| 104 | self.tags[0x05] = "unsupported-attributes-tag" |
---|
| 105 | |
---|
| 106 | # out of band values |
---|
| 107 | self.tags[0x10] = "unsupported" |
---|
| 108 | self.tags[0x11] = "reserved-for-future-default" |
---|
| 109 | self.tags[0x12] = "unknown" |
---|
| 110 | self.tags[0x13] = "no-value" |
---|
| 111 | |
---|
| 112 | # integer values |
---|
| 113 | self.tags[0x20] = "generic-integer" |
---|
| 114 | self.tags[0x21] = "integer" |
---|
| 115 | self.tags[0x22] = "boolean" |
---|
| 116 | self.tags[0x23] = "enum" |
---|
| 117 | |
---|
| 118 | # octetString |
---|
| 119 | self.tags[0x30] = "octetString-with-an-unspecified-format" |
---|
| 120 | self.tags[0x31] = "dateTime" |
---|
| 121 | self.tags[0x32] = "resolution" |
---|
| 122 | self.tags[0x33] = "rangeOfInteger" |
---|
| 123 | self.tags[0x34] = "reserved-for-collection" |
---|
| 124 | self.tags[0x35] = "textWithLanguage" |
---|
| 125 | self.tags[0x36] = "nameWithLanguage" |
---|
| 126 | |
---|
| 127 | # character strings |
---|
| 128 | self.tags[0x20] = "generic-character-string" |
---|
| 129 | self.tags[0x41] = "textWithoutLanguage" |
---|
| 130 | self.tags[0x42] = "nameWithoutLanguage" |
---|
| 131 | # self.tags[0x43] = "reserved" |
---|
| 132 | self.tags[0x44] = "keyword" |
---|
| 133 | self.tags[0x45] = "uri" |
---|
| 134 | self.tags[0x46] = "uriScheme" |
---|
| 135 | self.tags[0x47] = "charset" |
---|
| 136 | self.tags[0x48] = "naturalLanguage" |
---|
| 137 | self.tags[0x49] = "mimeMediaType" |
---|
| 138 | |
---|
| 139 | # now parses the IPP message |
---|
| 140 | self.parse() |
---|
| 141 | |
---|
| 142 | def __getattr__(self, attrname) : |
---|
| 143 | """Allows self.attributes to return the attributes names.""" |
---|
| 144 | if attrname == "attributes" : |
---|
| 145 | keys = self._attributes.keys() |
---|
| 146 | keys.sort() |
---|
| 147 | return keys |
---|
| 148 | raise AttributeError, attrname |
---|
| 149 | |
---|
| 150 | def __getitem__(self, ippattrname) : |
---|
| 151 | """Fakes a dictionnary d['key'] notation.""" |
---|
| 152 | value = self._attributes.get(ippattrname) |
---|
| 153 | if value is not None : |
---|
| 154 | if len(value) == 1 : |
---|
| 155 | value = value[0] |
---|
| 156 | return value |
---|
| 157 | get = __getitem__ |
---|
| 158 | |
---|
| 159 | def parseTag(self) : |
---|
| 160 | """Extracts information from an IPP tag.""" |
---|
| 161 | pos = self.position |
---|
| 162 | valuetag = self.tags[ord(self.data[pos])] |
---|
| 163 | # print valuetag.get("name") |
---|
| 164 | pos += 1 |
---|
| 165 | posend = pos2 = pos + 2 |
---|
| 166 | namelength = unpack(">H", self.data[pos:pos2])[0] |
---|
| 167 | if not namelength : |
---|
| 168 | name = self.curname |
---|
| 169 | else : |
---|
| 170 | posend += namelength |
---|
| 171 | self.curname = name = self.data[pos2:posend] |
---|
| 172 | pos2 = posend + 2 |
---|
| 173 | valuelength = unpack(">H", self.data[posend:pos2])[0] |
---|
| 174 | posend = pos2 + valuelength |
---|
| 175 | value = self.data[pos2:posend] |
---|
| 176 | oldval = self._attributes.setdefault(name, []) |
---|
| 177 | oldval.append(value) |
---|
| 178 | return posend - self.position |
---|
| 179 | |
---|
| 180 | def operation_attributes_tag(self) : |
---|
| 181 | """Indicates that the parser enters into an operation-attributes-tag group.""" |
---|
| 182 | return self.parseTag() |
---|
| 183 | |
---|
| 184 | def job_attributes_tag(self) : |
---|
| 185 | """Indicates that the parser enters into an operation-attributes-tag group.""" |
---|
| 186 | return self.parseTag() |
---|
| 187 | |
---|
| 188 | def printer_attributes_tag(self) : |
---|
| 189 | """Indicates that the parser enters into an operation-attributes-tag group.""" |
---|
| 190 | return self.parseTag() |
---|
| 191 | |
---|
| 192 | def parse(self) : |
---|
| 193 | """Parses an IPP Message. |
---|
| 194 | |
---|
| 195 | NB : Only a subset of RFC2910 is implemented. |
---|
| 196 | We are only interested in textual informations for now anyway. |
---|
| 197 | """ |
---|
| 198 | self.version = "%s.%s" % (ord(self.data[0]), ord(self.data[1])) |
---|
| 199 | self.operation_id = "0x%04x" % unpack(">H", self.data[2:4])[0] |
---|
| 200 | self.request_id = "0x%08x" % unpack(">I", self.data[4:8])[0] |
---|
| 201 | self.position = 8 |
---|
| 202 | try : |
---|
| 203 | tag = ord(self.data[self.position]) |
---|
| 204 | while tag != END_OF_ATTRIBUTES_TAG : |
---|
| 205 | self.position += 1 |
---|
| 206 | name = self.tags[tag] |
---|
| 207 | if name is not None : |
---|
| 208 | func = getattr(self, name.replace("-", "_"), None) |
---|
| 209 | if func is not None : |
---|
| 210 | self.position += func() |
---|
| 211 | if ord(self.data[self.position]) > UNSUPPORTED_ATTRIBUTES_TAG : |
---|
| 212 | self.position -= 1 |
---|
| 213 | continue |
---|
| 214 | tag = ord(self.data[self.position]) |
---|
| 215 | except IndexError : |
---|
| 216 | raise IPPError, "Unexpected end of IPP message." |
---|
| 217 | |
---|
[570] | 218 | class FakeConfig : |
---|
| 219 | """Fakes a configuration file parser.""" |
---|
| 220 | def get(self, section, option, raw=0) : |
---|
| 221 | """Fakes the retrieval of a global option.""" |
---|
| 222 | raise ConfigError, "Invalid configuration file : no option %s in section [%s]" % (option, section) |
---|
| 223 | |
---|
[568] | 224 | class CupsBackend : |
---|
| 225 | """Base class for tools with no database access.""" |
---|
| 226 | def __init__(self) : |
---|
| 227 | """Initializes the CUPS backend wrapper.""" |
---|
[588] | 228 | self.gotSigTerm = 0 |
---|
| 229 | signal.signal(signal.SIGTERM, signal.SIG_IGN) |
---|
| 230 | signal.signal(signal.SIGPIPE, signal.SIG_IGN) |
---|
[577] | 231 | self.MyName = "Tea4CUPS" |
---|
| 232 | self.myname = "tea4cups" |
---|
| 233 | self.pid = os.getpid() |
---|
[570] | 234 | confdir = os.environ.get("CUPS_SERVERROOT", ".") |
---|
[577] | 235 | self.conffile = os.path.join(confdir, "%s.conf" % self.myname) |
---|
[570] | 236 | if os.path.isfile(self.conffile) : |
---|
| 237 | self.config = ConfigParser.ConfigParser() |
---|
| 238 | self.config.read([self.conffile]) |
---|
[573] | 239 | self.debug = self.isTrue(self.getGlobalOption("debug", ignore=1)) |
---|
[570] | 240 | else : |
---|
| 241 | self.config = FakeConfig() |
---|
| 242 | self.debug = 1 # no config, so force debug mode ! |
---|
| 243 | |
---|
[574] | 244 | def logInfo(self, message, level="info") : |
---|
| 245 | """Logs a message to CUPS' error_log file.""" |
---|
[589] | 246 | sys.stderr.write("%s: %s v%s (PID %i) : %s\n" % (level.upper(), self.MyName, version, os.getpid(), message)) |
---|
[574] | 247 | sys.stderr.flush() |
---|
| 248 | |
---|
[585] | 249 | def logDebug(self, message) : |
---|
| 250 | """Logs something to debug output if debug is enabled.""" |
---|
| 251 | if self.debug : |
---|
| 252 | self.logInfo(message, level="debug") |
---|
| 253 | |
---|
[570] | 254 | def isTrue(self, option) : |
---|
| 255 | """Returns 1 if option is set to true, else 0.""" |
---|
| 256 | if (option is not None) and (option.upper().strip() in ['Y', 'YES', '1', 'ON', 'T', 'TRUE']) : |
---|
| 257 | return 1 |
---|
| 258 | else : |
---|
| 259 | return 0 |
---|
| 260 | |
---|
| 261 | def getGlobalOption(self, option, ignore=0) : |
---|
| 262 | """Returns an option from the global section, or raises a ConfigError if ignore is not set, else returns None.""" |
---|
| 263 | try : |
---|
| 264 | return self.config.get("global", option, raw=1) |
---|
| 265 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
[577] | 266 | if not ignore : |
---|
[570] | 267 | raise ConfigError, "Option %s not found in section global of %s" % (option, self.conffile) |
---|
| 268 | |
---|
[577] | 269 | def getPrintQueueOption(self, printqueuename, option, ignore=0) : |
---|
[570] | 270 | """Returns an option from the printer section, or the global section, or raises a ConfigError.""" |
---|
| 271 | globaloption = self.getGlobalOption(option, ignore=1) |
---|
| 272 | try : |
---|
[574] | 273 | return self.config.get(printqueuename, option, raw=1) |
---|
[570] | 274 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) : |
---|
| 275 | if globaloption is not None : |
---|
| 276 | return globaloption |
---|
[577] | 277 | elif not ignore : |
---|
[574] | 278 | raise ConfigError, "Option %s not found in section [%s] of %s" % (option, printqueuename, self.conffile) |
---|
[571] | 279 | |
---|
[597] | 280 | def enumBranches(self, printqueuename, branchtype="tee") : |
---|
| 281 | """Returns the list of branchtypes branches for a particular section's.""" |
---|
| 282 | branchbasename = "%s_" % branchtype.lower() |
---|
[573] | 283 | try : |
---|
[597] | 284 | globalbranches = [ (k, v) for (k, v) in self.config.items("global") if k.startswith(branchbasename) ] |
---|
[573] | 285 | except ConfigParser.NoSectionError, msg : |
---|
[579] | 286 | raise ConfigError, "Invalid configuration file : %s" % msg |
---|
[573] | 287 | try : |
---|
[597] | 288 | sectionbranches = [ (k, v) for (k, v) in self.config.items(printqueuename) if k.startswith(branchbasename) ] |
---|
[573] | 289 | except ConfigParser.NoSectionError, msg : |
---|
[583] | 290 | self.logInfo("No section for print queue %s : %s" % (printqueuename, msg)) |
---|
[573] | 291 | sectionbranches = [] |
---|
| 292 | branches = {} |
---|
| 293 | for (k, v) in globalbranches : |
---|
| 294 | value = v.strip() |
---|
| 295 | if value : |
---|
| 296 | branches[k] = value |
---|
| 297 | for (k, v) in sectionbranches : |
---|
| 298 | value = v.strip() |
---|
| 299 | if value : |
---|
| 300 | branches[k] = value # overwrite any global option or set a new value |
---|
| 301 | else : |
---|
| 302 | del branches[k] # empty value disables a global option |
---|
| 303 | return branches |
---|
[568] | 304 | |
---|
| 305 | def discoverOtherBackends(self) : |
---|
| 306 | """Discovers the other CUPS backends. |
---|
| 307 | |
---|
| 308 | Executes each existing backend in turn in device enumeration mode. |
---|
| 309 | Returns the list of available backends. |
---|
| 310 | """ |
---|
[569] | 311 | # Unfortunately this method can't output any debug information |
---|
| 312 | # to stdout or stderr, else CUPS considers that the device is |
---|
| 313 | # not available. |
---|
[568] | 314 | available = [] |
---|
[569] | 315 | (directory, myname) = os.path.split(sys.argv[0]) |
---|
[588] | 316 | if not directory : |
---|
| 317 | directory = "./" |
---|
[568] | 318 | tmpdir = tempfile.gettempdir() |
---|
[569] | 319 | lockfilename = os.path.join(tmpdir, "%s..LCK" % myname) |
---|
[568] | 320 | if os.path.exists(lockfilename) : |
---|
| 321 | lockfile = open(lockfilename, "r") |
---|
| 322 | pid = int(lockfile.read()) |
---|
| 323 | lockfile.close() |
---|
| 324 | try : |
---|
| 325 | # see if the pid contained in the lock file is still running |
---|
| 326 | os.kill(pid, 0) |
---|
| 327 | except OSError, e : |
---|
| 328 | if e.errno != errno.EPERM : |
---|
| 329 | # process doesn't exist anymore |
---|
| 330 | os.remove(lockfilename) |
---|
| 331 | |
---|
| 332 | if not os.path.exists(lockfilename) : |
---|
| 333 | lockfile = open(lockfilename, "w") |
---|
[577] | 334 | lockfile.write("%i" % self.pid) |
---|
[568] | 335 | lockfile.close() |
---|
[569] | 336 | allbackends = [ os.path.join(directory, b) \ |
---|
| 337 | for b in os.listdir(directory) |
---|
| 338 | if os.access(os.path.join(directory, b), os.X_OK) \ |
---|
| 339 | and (b != myname)] |
---|
| 340 | for backend in allbackends : |
---|
[568] | 341 | answer = os.popen(backend, "r") |
---|
| 342 | try : |
---|
| 343 | devices = [line.strip() for line in answer.readlines()] |
---|
| 344 | except : |
---|
| 345 | devices = [] |
---|
| 346 | status = answer.close() |
---|
| 347 | if status is None : |
---|
| 348 | for d in devices : |
---|
| 349 | # each line is of the form : |
---|
| 350 | # 'xxxx xxxx "xxxx xxx" "xxxx xxx"' |
---|
| 351 | # so we have to decompose it carefully |
---|
| 352 | fdevice = cStringIO.StringIO(d) |
---|
| 353 | tokenizer = shlex.shlex(fdevice) |
---|
| 354 | tokenizer.wordchars = tokenizer.wordchars + \ |
---|
| 355 | r".:,?!~/\_$*-+={}[]()#" |
---|
| 356 | arguments = [] |
---|
| 357 | while 1 : |
---|
| 358 | token = tokenizer.get_token() |
---|
| 359 | if token : |
---|
| 360 | arguments.append(token) |
---|
| 361 | else : |
---|
| 362 | break |
---|
| 363 | fdevice.close() |
---|
| 364 | try : |
---|
| 365 | (devicetype, device, name, fullname) = arguments |
---|
| 366 | except ValueError : |
---|
| 367 | pass # ignore this 'bizarre' device |
---|
| 368 | else : |
---|
| 369 | if name.startswith('"') and name.endswith('"') : |
---|
| 370 | name = name[1:-1] |
---|
| 371 | if fullname.startswith('"') and fullname.endswith('"') : |
---|
| 372 | fullname = fullname[1:-1] |
---|
[577] | 373 | available.append('%s %s:%s "%s+%s" "%s managed %s"' \ |
---|
| 374 | % (devicetype, self.myname, device, self.MyName, name, self.MyName, fullname)) |
---|
[568] | 375 | os.remove(lockfilename) |
---|
| 376 | return available |
---|
| 377 | |
---|
[577] | 378 | def initBackend(self) : |
---|
| 379 | """Initializes the backend's attributes.""" |
---|
| 380 | # check that the DEVICE_URI environment variable's value is |
---|
| 381 | # prefixed with self.myname otherwise don't touch it. |
---|
| 382 | # If this is the case, we have to remove the prefix from |
---|
| 383 | # the environment before launching the real backend |
---|
| 384 | muststartwith = "%s:" % self.myname |
---|
| 385 | device_uri = os.environ.get("DEVICE_URI", "") |
---|
| 386 | if device_uri.startswith(muststartwith) : |
---|
| 387 | fulldevice_uri = device_uri[:] |
---|
| 388 | device_uri = fulldevice_uri[len(muststartwith):] |
---|
[583] | 389 | for i in range(2) : |
---|
| 390 | if device_uri.startswith("/") : |
---|
| 391 | device_uri = device_uri[1:] |
---|
[577] | 392 | try : |
---|
| 393 | (backend, destination) = device_uri.split(":", 1) |
---|
| 394 | except ValueError : |
---|
[583] | 395 | if not device_uri : |
---|
| 396 | self.logInfo("Not attached to an existing print queue.") |
---|
| 397 | backend = "" |
---|
| 398 | else : |
---|
| 399 | raise TeeError, "Invalid DEVICE_URI : %s\n" % device_uri |
---|
[577] | 400 | |
---|
| 401 | self.JobId = sys.argv[1].strip() |
---|
| 402 | self.UserName = sys.argv[2].strip() |
---|
| 403 | self.Title = sys.argv[3].strip() |
---|
| 404 | self.Copies = int(sys.argv[4].strip()) |
---|
| 405 | self.Options = sys.argv[5].strip() |
---|
| 406 | if len(sys.argv) == 7 : |
---|
| 407 | self.InputFile = sys.argv[6] # read job's datas from file |
---|
| 408 | else : |
---|
| 409 | self.InputFile = None # read job's datas from stdin |
---|
| 410 | |
---|
| 411 | self.RealBackend = backend |
---|
| 412 | self.DeviceURI = device_uri |
---|
| 413 | self.PrinterName = os.environ.get("PRINTER", "") |
---|
| 414 | self.Directory = self.getPrintQueueOption(self.PrinterName, "directory") |
---|
[581] | 415 | self.DataFile = os.path.join(self.Directory, "%s-%s-%s-%s" % (self.myname, self.PrinterName, self.UserName, self.JobId)) |
---|
[582] | 416 | self.ClientHost = self.extractJobOriginatingHostName() |
---|
[577] | 417 | |
---|
[583] | 418 | def getCupsConfigDirectives(self, directives=[]) : |
---|
| 419 | """Retrieves some CUPS directives from its configuration file. |
---|
| 420 | |
---|
| 421 | Returns a mapping with lowercased directives as keys and |
---|
| 422 | their setting as values. |
---|
| 423 | """ |
---|
| 424 | dirvalues = {} |
---|
| 425 | cupsroot = os.environ.get("CUPS_SERVERROOT", "/etc/cups") |
---|
| 426 | cupsdconf = os.path.join(cupsroot, "cupsd.conf") |
---|
| 427 | try : |
---|
| 428 | conffile = open(cupsdconf, "r") |
---|
| 429 | except IOError : |
---|
| 430 | raise TeeError, "Unable to open %s" % cupsdconf |
---|
| 431 | else : |
---|
| 432 | for line in conffile.readlines() : |
---|
| 433 | linecopy = line.strip().lower() |
---|
| 434 | for di in [d.lower() for d in directives] : |
---|
| 435 | if linecopy.startswith("%s " % di) : |
---|
| 436 | try : |
---|
| 437 | val = line.split()[1] |
---|
| 438 | except : |
---|
| 439 | pass # ignore errors, we take the last value in any case. |
---|
| 440 | else : |
---|
| 441 | dirvalues[di] = val |
---|
| 442 | conffile.close() |
---|
| 443 | return dirvalues |
---|
| 444 | |
---|
[582] | 445 | def extractJobOriginatingHostName(self) : |
---|
| 446 | """Extracts the client's hostname or IP address from the CUPS message file for current job.""" |
---|
[583] | 447 | cupsdconf = self.getCupsConfigDirectives(["RequestRoot"]) |
---|
[582] | 448 | requestroot = cupsdconf.get("requestroot", "/var/spool/cups") |
---|
| 449 | if (len(self.JobId) < 5) and self.JobId.isdigit() : |
---|
| 450 | ippmessagefile = "c%05i" % int(self.JobId) |
---|
| 451 | else : |
---|
| 452 | ippmessagefile = "c%s" % self.JobId |
---|
| 453 | ippmessagefile = os.path.join(requestroot, ippmessagefile) |
---|
| 454 | ippmessage = {} |
---|
| 455 | try : |
---|
| 456 | ippdatafile = open(ippmessagefile) |
---|
| 457 | except : |
---|
| 458 | self.logInfo("Unable to open IPP message file %s" % ippmessagefile, "warn") |
---|
| 459 | else : |
---|
| 460 | self.logDebug("Parsing of IPP message file %s begins." % ippmessagefile) |
---|
| 461 | try : |
---|
| 462 | ippmessage = IPPMessage(ippdatafile.read()) |
---|
| 463 | except IPPError, msg : |
---|
| 464 | self.logInfo("Error while parsing %s : %s" % (ippmessagefile, msg), "warn") |
---|
| 465 | else : |
---|
| 466 | self.logDebug("Parsing of IPP message file %s ends." % ippmessagefile) |
---|
| 467 | ippdatafile.close() |
---|
| 468 | return ippmessage.get("job-originating-host-name") |
---|
| 469 | |
---|
[577] | 470 | def exportAttributes(self) : |
---|
| 471 | """Exports our backend's attributes to the environment.""" |
---|
| 472 | os.environ["DEVICE_URI"] = self.DeviceURI # WARNING ! |
---|
| 473 | os.environ["TEAPRINTERNAME"] = self.PrinterName |
---|
| 474 | os.environ["TEADIRECTORY"] = self.Directory |
---|
| 475 | os.environ["TEADATAFILE"] = self.DataFile |
---|
[579] | 476 | os.environ["TEAJOBSIZE"] = str(self.JobSize) |
---|
[577] | 477 | os.environ["TEAMD5SUM"] = self.JobMD5Sum |
---|
[582] | 478 | os.environ["TEACLIENTHOST"] = self.ClientHost or "" |
---|
[577] | 479 | os.environ["TEAJOBID"] = self.JobId |
---|
| 480 | os.environ["TEAUSERNAME"] = self.UserName |
---|
| 481 | os.environ["TEATITLE"] = self.Title |
---|
| 482 | os.environ["TEACOPIES"] = str(self.Copies) |
---|
| 483 | os.environ["TEAOPTIONS"] = self.Options |
---|
| 484 | os.environ["TEAINPUTFILE"] = self.InputFile or "" |
---|
| 485 | |
---|
| 486 | def saveDatasAndCheckSum(self) : |
---|
| 487 | """Saves the input datas into a static file.""" |
---|
[583] | 488 | self.logDebug("Duplicating data stream into %s" % self.DataFile) |
---|
[577] | 489 | mustclose = 0 |
---|
| 490 | if self.InputFile is not None : |
---|
| 491 | infile = open(self.InputFile, "rb") |
---|
| 492 | mustclose = 1 |
---|
| 493 | else : |
---|
| 494 | infile = sys.stdin |
---|
| 495 | CHUNK = 64*1024 # read 64 Kb at a time |
---|
| 496 | dummy = 0 |
---|
| 497 | sizeread = 0 |
---|
| 498 | checksum = md5.new() |
---|
| 499 | outfile = open(self.DataFile, "wb") |
---|
| 500 | while 1 : |
---|
| 501 | data = infile.read(CHUNK) |
---|
| 502 | if not data : |
---|
| 503 | break |
---|
| 504 | sizeread += len(data) |
---|
| 505 | outfile.write(data) |
---|
| 506 | checksum.update(data) |
---|
| 507 | if not (dummy % 32) : # Only display every 2 Mb |
---|
| 508 | self.logDebug("%s bytes saved..." % sizeread) |
---|
| 509 | dummy += 1 |
---|
| 510 | outfile.close() |
---|
| 511 | if mustclose : |
---|
[579] | 512 | infile.close() |
---|
[577] | 513 | self.JobSize = sizeread |
---|
| 514 | self.JobMD5Sum = checksum.hexdigest() |
---|
| 515 | self.logDebug("Job %s is %s bytes long." % (self.JobId, self.JobSize)) |
---|
| 516 | self.logDebug("Job %s MD5 sum is %s" % (self.JobId, self.JobMD5Sum)) |
---|
[568] | 517 | |
---|
[577] | 518 | def cleanUp(self) : |
---|
| 519 | """Cleans up the place.""" |
---|
| 520 | if not self.isTrue(self.getPrintQueueOption(self.PrinterName, "keepfiles", ignore=1)) : |
---|
| 521 | os.remove(self.DataFile) |
---|
[579] | 522 | |
---|
[588] | 523 | def sigtermHandler(self, signum, frame) : |
---|
| 524 | """Sets an attribute whenever SIGTERM is received.""" |
---|
| 525 | self.gotSigTerm = 1 |
---|
| 526 | self.logInfo("SIGTERM received for Job %s." % self.JobId) |
---|
| 527 | |
---|
[579] | 528 | def runBranches(self) : |
---|
[598] | 529 | """Launches each hook or tee defined for the current print queue.""" |
---|
[584] | 530 | exitcode = 0 |
---|
[588] | 531 | signal.signal(signal.SIGTERM, self.sigtermHandler) |
---|
[598] | 532 | serialize = self.isTrue(self.getPrintQueueOption(self.PrinterName, "serialize", ignore=1)) |
---|
| 533 | for branchtype in ["prehook", "tee", "posthook"] : |
---|
| 534 | branches = self.enumBranches(self.PrinterName, branchtype) |
---|
| 535 | status = self.runCommands(branchtype, branches, serialize) |
---|
| 536 | if status : |
---|
| 537 | exitcode = status |
---|
| 538 | signal.signal(signal.SIGTERM, signal.SIG_IGN) |
---|
| 539 | return exitcode |
---|
| 540 | |
---|
| 541 | def runCommands(self, btype, branches, serialize) : |
---|
| 542 | """Runs the commands for a particular branch type.""" |
---|
| 543 | exitcode = 0 |
---|
| 544 | btype = btype.lower() |
---|
| 545 | btypetitle = btype.title() |
---|
| 546 | branchlist = branches.keys() |
---|
| 547 | branchlist.sort() |
---|
| 548 | if serialize : |
---|
| 549 | self.logDebug("Serialized %ss" % btypetitle) |
---|
| 550 | if (btype == "tee") and self.RealBackend : |
---|
[587] | 551 | self.logDebug("Launching original backend %s for printer %s" % (self.RealBackend, self.PrinterName)) |
---|
| 552 | exitcode = self.runOriginalBackend() |
---|
[598] | 553 | for branch in branchlist : |
---|
| 554 | command = branches[branch] |
---|
[588] | 555 | if self.gotSigTerm : |
---|
| 556 | break |
---|
[581] | 557 | self.logDebug("Launching %s : %s" % (branch, command)) |
---|
[584] | 558 | retcode = os.system(command) |
---|
[598] | 559 | self.logDebug("Exit code for %s %s on printer %s is %s" % (btype, branch, self.PrinterName, retcode)) |
---|
[584] | 560 | if os.WIFEXITED(retcode) : |
---|
| 561 | retcode = os.WEXITSTATUS(retcode) |
---|
| 562 | if retcode : |
---|
[598] | 563 | self.logInfo("%s %s on printer %s didn't exit successfully." % (btypetitle, branch, self.PrinterName), "error") |
---|
[584] | 564 | exitcode = 1 |
---|
[581] | 565 | else : |
---|
[598] | 566 | self.logDebug("Forked %ss" % btypetitle) |
---|
[584] | 567 | pids = {} |
---|
[598] | 568 | if (btype == "tee") and self.RealBackend : |
---|
[587] | 569 | branches["Original backend"] = None # Fakes a tee to launch one more child |
---|
[598] | 570 | branchlist = ["Original backend"] + branchlist |
---|
| 571 | for branch in branchlist : |
---|
| 572 | command = branches[branch] |
---|
[588] | 573 | if self.gotSigTerm : |
---|
| 574 | break |
---|
[584] | 575 | pid = os.fork() |
---|
| 576 | if pid : |
---|
| 577 | pids[branch] = pid |
---|
| 578 | else : |
---|
[587] | 579 | if branch == "Original backend" : |
---|
| 580 | self.logDebug("Launching original backend %s for printer %s" % (self.RealBackend, self.PrinterName)) |
---|
| 581 | sys.exit(self.runOriginalBackend()) |
---|
| 582 | else : |
---|
| 583 | self.logDebug("Launching %s : %s" % (branch, command)) |
---|
| 584 | retcode = os.system(command) |
---|
| 585 | if os.WIFEXITED(retcode) : |
---|
| 586 | retcode = os.WEXITSTATUS(retcode) |
---|
| 587 | else : |
---|
| 588 | retcode = -1 |
---|
| 589 | sys.exit(retcode) |
---|
[584] | 590 | for (branch, pid) in pids.items() : |
---|
| 591 | (childpid, retcode) = os.waitpid(pid, 0) |
---|
[598] | 592 | self.logDebug("Exit code for %s %s (PID %s) on printer %s is %s" % (btype, branch, childpid, self.PrinterName, retcode)) |
---|
[584] | 593 | if os.WIFEXITED(retcode) : |
---|
| 594 | retcode = os.WEXITSTATUS(retcode) |
---|
| 595 | if retcode : |
---|
[598] | 596 | self.logInfo("%s %s (PID %s) on printer %s didn't exit successfully." % (btypetitle, branch, childpid, self.PrinterName), "error") |
---|
[584] | 597 | exitcode = 1 |
---|
[598] | 598 | if branch == "Original backend" : |
---|
| 599 | os.environ["TEASTATUS"] = str(retcode) |
---|
[584] | 600 | return exitcode |
---|
[577] | 601 | |
---|
[588] | 602 | def unregisterFileNo(self, pollobj, fileno) : |
---|
| 603 | """Removes a file handle from the polling object.""" |
---|
| 604 | try : |
---|
| 605 | pollobj.unregister(fileno) |
---|
| 606 | except KeyError : |
---|
| 607 | self.logInfo("File number %s unregistered twice from polling object, ignored." % fileno, "warn") |
---|
| 608 | except : |
---|
| 609 | self.logDebug("Error while unregistering file number %s from polling object." % fileno) |
---|
| 610 | else : |
---|
| 611 | self.logDebug("File number %s unregistered from polling object." % fileno) |
---|
| 612 | |
---|
| 613 | def formatFileEvent(self, fd, mask) : |
---|
| 614 | """Formats file debug info.""" |
---|
| 615 | maskval = [] |
---|
| 616 | if mask & select.POLLIN : |
---|
| 617 | maskval.append("POLLIN") |
---|
| 618 | if mask & select.POLLOUT : |
---|
| 619 | maskval.append("POLLOUT") |
---|
| 620 | if mask & select.POLLPRI : |
---|
| 621 | maskval.append("POLLPRI") |
---|
| 622 | if mask & select.POLLERR : |
---|
| 623 | maskval.append("POLLERR") |
---|
| 624 | if mask & select.POLLHUP : |
---|
| 625 | maskval.append("POLLHUP") |
---|
| 626 | if mask & select.POLLNVAL : |
---|
| 627 | maskval.append("POLLNVAL") |
---|
| 628 | return "%s (%s)" % (fd, " | ".join(maskval)) |
---|
| 629 | |
---|
[587] | 630 | def runOriginalBackend(self) : |
---|
| 631 | """Launches the original backend.""" |
---|
| 632 | originalbackend = os.path.join(os.path.split(sys.argv[0])[0], self.RealBackend) |
---|
| 633 | arguments = sys.argv |
---|
| 634 | self.logDebug("Starting original backend %s with args %s" % (originalbackend, " ".join(['"%s"' % a for a in ([os.environ["DEVICE_URI"]] + arguments[1:])]))) |
---|
| 635 | subprocess = Popen4ForCUPS([originalbackend] + arguments[1:], bufsize=0, arg0=os.environ["DEVICE_URI"]) |
---|
[588] | 636 | |
---|
| 637 | # Save file descriptors, we will need them later. |
---|
| 638 | stderrfno = sys.stderr.fileno() |
---|
| 639 | fromcfno = subprocess.fromchild.fileno() |
---|
| 640 | tocfno = subprocess.tochild.fileno() |
---|
| 641 | |
---|
| 642 | # We will have to be careful when dealing with I/O |
---|
| 643 | # So we use a poll object to know when to read or write |
---|
| 644 | pollster = select.poll() |
---|
| 645 | pollster.register(fromcfno, select.POLLIN | select.POLLPRI) |
---|
| 646 | pollster.register(stderrfno, select.POLLOUT) |
---|
| 647 | pollster.register(tocfno, select.POLLOUT) |
---|
| 648 | |
---|
| 649 | # Initialize our buffers |
---|
| 650 | indata = "" |
---|
| 651 | outdata = "" |
---|
| 652 | endinput = endoutput = 0 |
---|
| 653 | inputclosed = outputclosed = 0 |
---|
| 654 | totaltochild = totalfromcups = 0 |
---|
| 655 | totalfromchild = totaltocups = 0 |
---|
| 656 | |
---|
[587] | 657 | if self.InputFile is None : |
---|
[588] | 658 | # this is not a real file, we read the job's data |
---|
| 659 | # from our temporary file which is a copy of stdin |
---|
| 660 | inf = open(self.DataFile, "rb") |
---|
| 661 | infno = inf.fileno() |
---|
| 662 | pollster.register(infno, select.POLLIN | select.POLLPRI) |
---|
| 663 | else : |
---|
| 664 | # job's data is in a file, no need to pass the data |
---|
| 665 | # to the original backend |
---|
| 666 | self.logDebug("Job's data is in %s" % self.InputFile) |
---|
| 667 | infno = None |
---|
| 668 | endinput = 1 |
---|
| 669 | |
---|
| 670 | self.logDebug("Entering streams polling loop...") |
---|
| 671 | MEGABYTE = 1024*1024 |
---|
| 672 | killed = 0 |
---|
| 673 | status = -1 |
---|
| 674 | while (status == -1) and (not killed) and not (inputclosed and outputclosed) : |
---|
| 675 | # First check if original backend is still alive |
---|
| 676 | status = subprocess.poll() |
---|
[587] | 677 | |
---|
[588] | 678 | # Now if we got SIGTERM, we have |
---|
| 679 | # to kill -TERM the original backend |
---|
| 680 | if self.gotSigTerm and not killed : |
---|
| 681 | try : |
---|
| 682 | os.kill(subprocess.pid, signal.SIGTERM) |
---|
| 683 | except OSError, msg : # ignore but logs if process was already killed. |
---|
| 684 | self.logDebug("Error while sending signal to pid %s : %s" % (subprocess.pid, msg)) |
---|
| 685 | else : |
---|
| 686 | self.logInfo(_("SIGTERM was sent to original backend %s (PID %s)") % (originalbackend, subprocess.pid)) |
---|
| 687 | killed = 1 |
---|
| 688 | |
---|
| 689 | # In any case, deal with any remaining I/O |
---|
| 690 | try : |
---|
| 691 | availablefds = pollster.poll(5000) |
---|
| 692 | except select.error, msg : |
---|
| 693 | self.logDebug("Interrupted poll : %s" % msg) |
---|
| 694 | availablefds = [] |
---|
| 695 | if not availablefds : |
---|
| 696 | self.logDebug("Nothing to do, sleeping a bit...") |
---|
| 697 | time.sleep(0.01) # give some time to the system |
---|
| 698 | else : |
---|
| 699 | for (fd, mask) in availablefds : |
---|
| 700 | try : |
---|
| 701 | if mask & select.POLLOUT : |
---|
| 702 | # We can write |
---|
| 703 | if fd == tocfno : |
---|
| 704 | if indata : |
---|
| 705 | try : |
---|
| 706 | nbwritten = os.write(fd, indata) |
---|
| 707 | except (OSError, IOError), msg : |
---|
| 708 | self.logDebug("Error while writing to original backend's stdin %s : %s" % (fd, msg)) |
---|
| 709 | else : |
---|
| 710 | if len(indata) != nbwritten : |
---|
| 711 | self.logDebug("Short write to original backend's input !") |
---|
| 712 | totaltochild += nbwritten |
---|
| 713 | self.logDebug("%s bytes sent to original backend so far..." % totaltochild) |
---|
| 714 | indata = indata[nbwritten:] |
---|
| 715 | else : |
---|
| 716 | self.logDebug("No data to send to original backend yet, sleeping a bit...") |
---|
| 717 | time.sleep(0.01) |
---|
| 718 | |
---|
| 719 | if endinput : |
---|
| 720 | self.unregisterFileNo(pollster, tocfno) |
---|
| 721 | self.logDebug("Closing original backend's stdin.") |
---|
| 722 | os.close(tocfno) |
---|
| 723 | inputclosed = 1 |
---|
| 724 | elif fd == stderrfno : |
---|
| 725 | if outdata : |
---|
| 726 | try : |
---|
| 727 | nbwritten = os.write(fd, outdata) |
---|
| 728 | except (OSError, IOError), msg : |
---|
| 729 | self.logDebug("Error while writing to CUPS back channel (stderr) %s : %s" % (fd, msg)) |
---|
| 730 | else : |
---|
| 731 | if len(outdata) != nbwritten : |
---|
| 732 | self.logDebug("Short write to stderr (CUPS) !") |
---|
| 733 | totaltocups += nbwritten |
---|
| 734 | self.logDebug("%s bytes sent back to CUPS so far..." % totaltocups) |
---|
| 735 | outdata = outdata[nbwritten:] |
---|
| 736 | else : |
---|
| 737 | # self.logDebug("No data to send back to CUPS yet, sleeping a bit...") # Uncommenting this fills your logs |
---|
| 738 | time.sleep(0.01) # Give some time to the system, stderr is ALWAYS writeable it seems. |
---|
| 739 | |
---|
| 740 | if endoutput : |
---|
| 741 | self.unregisterFileNo(pollster, stderrfno) |
---|
| 742 | outputclosed = 1 |
---|
| 743 | else : |
---|
| 744 | self.logDebug("Unexpected : %s - Sleeping a bit..." % self.formatFileEvent(fd, mask)) |
---|
| 745 | time.sleep(0.01) |
---|
| 746 | |
---|
| 747 | if mask & (select.POLLIN | select.POLLPRI) : |
---|
| 748 | # We have something to read |
---|
| 749 | try : |
---|
| 750 | data = os.read(fd, MEGABYTE) |
---|
| 751 | except (IOError, OSError), msg : |
---|
| 752 | self.logDebug("Error while reading file %s : %s" % (fd, msg)) |
---|
| 753 | else : |
---|
| 754 | if fd == infno : |
---|
| 755 | if not data : # If yes, then no more input data |
---|
| 756 | self.unregisterFileNo(pollster, infno) |
---|
| 757 | self.logDebug("Input data ends.") |
---|
| 758 | endinput = 1 # this happens with real files. |
---|
| 759 | else : |
---|
| 760 | indata += data |
---|
| 761 | totalfromcups += len(data) |
---|
| 762 | self.logDebug("%s bytes read from CUPS so far..." % totalfromcups) |
---|
| 763 | elif fd == fromcfno : |
---|
| 764 | if not data : |
---|
| 765 | self.logDebug("No back channel data to read from original backend yet, sleeping a bit...") |
---|
| 766 | time.sleep(0.01) |
---|
| 767 | else : |
---|
| 768 | outdata += data |
---|
| 769 | totalfromchild += len(data) |
---|
| 770 | self.logDebug("%s bytes read from original backend so far..." % totalfromchild) |
---|
| 771 | else : |
---|
| 772 | self.logDebug("Unexpected : %s - Sleeping a bit..." % self.formatFileEvent(fd, mask)) |
---|
| 773 | time.sleep(0.01) |
---|
| 774 | |
---|
| 775 | if mask & (select.POLLHUP | select.POLLERR) : |
---|
| 776 | # Treat POLLERR as an EOF. |
---|
| 777 | # Some standard I/O stream has no more datas |
---|
| 778 | self.unregisterFileNo(pollster, fd) |
---|
| 779 | if fd == infno : |
---|
| 780 | # Here we are in the case where the input file is stdin. |
---|
| 781 | # which has no more data to be read. |
---|
| 782 | self.logDebug("Input data ends.") |
---|
| 783 | endinput = 1 |
---|
| 784 | elif fd == fromcfno : |
---|
| 785 | # We are no more interested in this file descriptor |
---|
| 786 | self.logDebug("Closing original backend's stdout+stderr.") |
---|
| 787 | os.close(fromcfno) |
---|
| 788 | endoutput = 1 |
---|
| 789 | else : |
---|
| 790 | self.logDebug("Unexpected : %s - Sleeping a bit..." % self.formatFileEvent(fd, mask)) |
---|
| 791 | time.sleep(0.01) |
---|
| 792 | |
---|
| 793 | if mask & select.POLLNVAL : |
---|
| 794 | self.logDebug("File %s was closed. Unregistering from polling object." % fd) |
---|
| 795 | self.unregisterFileNo(pollster, fd) |
---|
| 796 | except IOError, msg : |
---|
| 797 | self.logDebug("Got an IOError : %s" % msg) # we got signalled during an I/O |
---|
| 798 | |
---|
| 799 | # We must close the original backend's input stream |
---|
| 800 | if killed and not inputclosed : |
---|
| 801 | self.logDebug("Forcing close of original backend's stdin.") |
---|
| 802 | os.close(tocfno) |
---|
| 803 | |
---|
| 804 | self.logDebug("Exiting streams polling loop...") |
---|
| 805 | |
---|
| 806 | self.logDebug("input data's final length : %s" % len(indata)) |
---|
| 807 | self.logDebug("back-channel data's final length : %s" % len(outdata)) |
---|
| 808 | |
---|
| 809 | self.logDebug("Total bytes read from CUPS (job's datas) : %s" % totalfromcups) |
---|
| 810 | self.logDebug("Total bytes sent to original backend (job's datas) : %s" % totaltochild) |
---|
| 811 | |
---|
| 812 | self.logDebug("Total bytes read from original backend (back-channel datas) : %s" % totalfromchild) |
---|
| 813 | self.logDebug("Total bytes sent back to CUPS (back-channel datas) : %s" % totaltocups) |
---|
| 814 | |
---|
| 815 | # Check exit code of original CUPS backend. |
---|
| 816 | if status == -1 : |
---|
| 817 | # we exited the loop before the original backend exited |
---|
| 818 | # now we have to wait for it to finish and get its status |
---|
| 819 | self.logDebug("Waiting for original backend to exit...") |
---|
| 820 | try : |
---|
| 821 | status = subprocess.wait() |
---|
| 822 | except OSError : # already dead : TODO : detect when abnormal |
---|
| 823 | status = 0 |
---|
[587] | 824 | if os.WIFEXITED(status) : |
---|
| 825 | return os.WEXITSTATUS(status) |
---|
[588] | 826 | elif not killed : |
---|
| 827 | self.logInfo("CUPS backend %s died abnormally." % originalbackend, "error") |
---|
| 828 | return -1 |
---|
[587] | 829 | else : |
---|
| 830 | return 1 |
---|
| 831 | |
---|
[565] | 832 | if __name__ == "__main__" : |
---|
| 833 | # This is a CUPS backend, we should act and die like a CUPS backend |
---|
[568] | 834 | wrapper = CupsBackend() |
---|
[565] | 835 | if len(sys.argv) == 1 : |
---|
[568] | 836 | print "\n".join(wrapper.discoverOtherBackends()) |
---|
| 837 | sys.exit(0) |
---|
[565] | 838 | elif len(sys.argv) not in (6, 7) : |
---|
[568] | 839 | sys.stderr.write("ERROR: %s job-id user title copies options [file]\n"\ |
---|
| 840 | % sys.argv[0]) |
---|
| 841 | sys.exit(1) |
---|
[565] | 842 | else : |
---|
[585] | 843 | try : |
---|
| 844 | wrapper.initBackend() |
---|
| 845 | wrapper.saveDatasAndCheckSum() |
---|
| 846 | wrapper.exportAttributes() |
---|
| 847 | retcode = wrapper.runBranches() |
---|
| 848 | wrapper.cleanUp() |
---|
| 849 | except SystemExit, e : |
---|
| 850 | retcode = e.code |
---|
| 851 | except : |
---|
| 852 | import traceback |
---|
| 853 | lines = [] |
---|
| 854 | for line in traceback.format_exception(*sys.exc_info()) : |
---|
| 855 | lines.extend([l for l in line.split("\n") if l]) |
---|
| 856 | msg = "ERROR: ".join(["%s (PID %s) : %s\n" % (wrapper.MyName, wrapper.pid, l) for l in (["ERROR: Tea4CUPS v%s" % version] + lines)]) |
---|
| 857 | sys.stderr.write(msg) |
---|
| 858 | sys.stderr.flush() |
---|
| 859 | retcode = 1 |
---|
[579] | 860 | sys.exit(retcode) |
---|