#! /usr/bin/env python # -*- coding: utf-8 -*- # # PyKota : Print Quotas for CUPS # # (c) 2003-2013 Jerome Alet # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # # $Id$ # # import sys import os import fcntl import time import errno import tempfile import popen2 import cStringIO import shlex import signal import md5 import fnmatch import pwd import socket import smtplib from email.MIMEText import MIMEText from email.Header import Header import email.Utils from mx import DateTime import pykota.appinit from pykota.utils import * from pykota.errors import PyKotaToolError from pykota.tool import PyKotaTool from pykota.accounter import openAccounter from pykota import cups class FakeObject : """Fake object.""" def __init__(self, name) : """Fake init.""" self.Name = name class FakePrinter(FakeObject) : """Fake printer instance.""" pass class FakeUser(FakeObject) : """Fake user instance.""" def __init__(self, name) : """Fake init.""" self.Email = name FakeObject.__init__(self, name) class CUPSBackend(PyKotaTool) : """Base class for tools with no database access.""" def __init__(self) : """Initializes the CUPS backend wrapper.""" PyKotaTool.__init__(self) signal.signal(signal.SIGTERM, signal.SIG_IGN) signal.signal(signal.SIGPIPE, signal.SIG_IGN) self.oldSigIntHandler = signal.signal(signal.SIGINT, signal.SIG_IGN) self.MyName = "PyKota" self.myname = "cupspykota" self.pid = os.getpid() self.DataFile = None self.lockfilename = None self.lockfile = None def deferredInit(self) : """Deferred initialization.""" PyKotaTool.deferredInit(self) if not self.config.isAdmin : from pykota import config username = self.effectiveUserName raise config.PyKotaConfigError, _("User %(username)s is not allowed to read ~pykota/pykotadmin.conf, you must check the permissions.") % locals() def enableSigInt(self) : """Enables the SIGINT signal (which raises KeyboardInterrupt).""" signal.signal(signal.SIGINT, self.oldSigIntHandler) def waitForLock(self) : """Waits until we can acquire the lock file.""" self.logdebug("Waiting for lock %s to become available..." % self.lockfilename) haslock = False while not haslock : try : # open the lock file, optionally creating it if needed. self.lockfile = open(self.lockfilename, "a+") # we wait indefinitely for the lock to become available. # works over NFS too. fcntl.lockf(self.lockfile, fcntl.LOCK_EX) haslock = True self.logdebug("Lock %s acquired." % self.lockfilename) # Here we save the PID in the lock file, but we don't use # it, because the lock file may be in a directory shared # over NFS between two (or more) print servers, so the PID # has no meaning in this case. self.lockfile.truncate(0) self.lockfile.seek(0, 0) self.lockfile.write(str(self.pid)) self.lockfile.flush() except IOError, msg : self.logdebug("I/O Error while waiting for lock %s : %s" % (self.lockfilename, msg)) time.sleep(0.25) def discoverOtherBackends(self) : """Discovers the other CUPS backends. Executes each existing backend in turn in device enumeration mode. Returns the list of available backends. Unfortunately this method can't output any debug information to stdout or stderr, else CUPS considers that the device is not available. """ available = [] (directory, myname) = os.path.split(sys.argv[0]) if not directory : directory = "./" lockfilename = os.path.join(tempfile.gettempdir(), "%s..LCK" % myname) if os.path.exists(lockfilename) : lockfile = open(lockfilename, "r") pid = int(lockfile.read()) lockfile.close() try : # see if the pid contained in the lock file is still running os.kill(pid, 0) except OSError, err : if err.errno != errno.EPERM : # process doesn't exist anymore os.remove(lockfilename) if not os.path.exists(lockfilename) : lockfile = open(lockfilename, "w") lockfile.write("%i" % self.pid) lockfile.close() allbackends = [ os.path.join(directory, b) \ for b in os.listdir(directory) \ if os.access(os.path.join(directory, b), os.X_OK) \ and (b != myname)] for backend in allbackends : answer = os.popen(backend, "r") try : devices = [line.strip() for line in answer.readlines()] except : devices = [] status = answer.close() if status is None : for d in devices : # each line is of the form : # 'xxxx xxxx "xxxx xxx" "xxxx xxx"' # so we have to decompose it carefully fdevice = cStringIO.StringIO(d) tokenizer = shlex.shlex(fdevice) tokenizer.wordchars = tokenizer.wordchars + \ r".:,?!~/\_$*-+={}[]()#" arguments = [] while 1 : token = tokenizer.get_token() if token : arguments.append(token) else : break fdevice.close() try : (devicetype, device, name, fullname) = arguments except ValueError : pass # ignore this 'bizarre' device else : if name.startswith('"') and name.endswith('"') : name = name[1:-1] if fullname.startswith('"') and fullname.endswith('"') : fullname = fullname[1:-1] available.append('%s %s:%s "%s+%s" "%s managed %s"' \ % (devicetype, self.myname, \ device, self.MyName, \ name, self.MyName, \ fullname)) os.remove(lockfilename) available.append('direct %s:// "%s+Nothing" "%s managed Virtual Printer"' \ % (self.myname, self.MyName, self.MyName)) return available def checkCUPSVersion(self) : """Checks if CUPS is not v1.3.4 or higher.""" fullversion = os.environ.get("SOFTWARE", "") if fullversion.startswith("CUPS/") : vnum = fullversion.split("/")[1] try : (major, minor, release) = [int(p) for p in vnum.split(".")] except ValueError : pass else : return (major > 1) \ or ((major == 1) and (minor > 3)) \ or ((major == 1) and (minor == 3) and (release >= 4)) return False def initBackendParameters(self) : """Initializes the backend's attributes.""" # check that the DEVICE_URI environment variable's value is # prefixed with self.myname otherwise don't touch it. # If this is the case, we have to remove the prefix from # the environment before launching the real backend self.logdebug("Initializing backend...") self.softwareJobSize = None if not self.checkCUPSVersion() : self.printInfo("BEWARE : CUPS is too old. You should use CUPS v1.3.4 or higher.", "error") self.PrinterName = os.environ.get("PRINTER", "") directories = [ self.config.getPrinterDirectory(self.PrinterName), tempfile.gettempdir() ] self.Directory = None for direc in directories : if os.access(direc, os.O_RDWR) : self.Directory = direc break else : self.printInfo("Insufficient permissions to access to temporary directory %s" % direc, "warn") self.Action = "ALLOW" # job allowed by default self.Reason = None try : copies = int(sys.argv[4].strip()) if copies < 1 : raise ValueError except (ValueError, TypeError) : self.logdebug("Invalid number of copies '%s', using 1 instead." % sys.argv[4]) copies = 1 if len(sys.argv) == 7 : fname = sys.argv[6] # read job's datas from file else : fname = None # read job's datas from stdin self.Ticket = cups.JobTicket(sys.argv[1].strip(), self.PrinterName, \ copies, fname, sys.argv[5].strip()) self.UserName = self.Ticket.OriginatingUserName self.DataFile = (os.path.join(self.Directory, "%s-%s-%s-%s" % \ (self.myname, self.PrinterName, self.UserName, self.Ticket.JobId))).encode(sys.getfilesystemencoding(), "replace") muststartwith = "%s:" % self.myname device_uri = os.environ.get("DEVICE_URI", "") if device_uri.startswith(muststartwith) : fulldevice_uri = device_uri[:] device_uri = fulldevice_uri[len(muststartwith):] for i in range(2) : if device_uri.startswith("/") : device_uri = device_uri[1:] try : (backend, destination) = device_uri.split(":", 1) except ValueError : if not device_uri : self.logdebug("Not attached to an existing print queue.") backend = "" printerhostname = "" else : raise PyKotaToolError, "Invalid DEVICE_URI : %s\n" % device_uri else : if backend == "hp" : try : printerhostname = destination.split("=")[1] # hp:/net/HP_LaserJet_8000_Series?ip=192.168.100.100 except IndexError : self.logdebug("Unsupported hplip URI %s" % device_uri) printerhostname = "" else : while destination.startswith("/") : destination = destination[1:] checkauth = destination.split("@", 1) if len(checkauth) == 2 : destination = checkauth[1] printerhostname = destination.split("/")[0].split(":")[0] self.PrinterHostName = printerhostname self.RealBackend = backend self.DeviceURI = device_uri if self.Ticket.BillingCode is None : self.OriginalJobBillingCode = None else : self.OriginalJobBillingCode = self.Ticket.BillingCode[:] baselockfilename = self.DeviceURI.replace("/", ".") baselockfilename = baselockfilename.replace(":", ".") baselockfilename = baselockfilename.replace("?", ".") baselockfilename = baselockfilename.replace("&", ".") baselockfilename = baselockfilename.replace("@", ".") self.lockfilename = os.path.join(self.Directory, "%s-%s..LCK" % (self.myname, baselockfilename)) self.logdebug("Backend : %s" % self.RealBackend) self.logdebug("DeviceURI : %s" % self.DeviceURI) self.logdebug("Printername : %s" % self.PrinterName) self.logdebug("Username : %s" % self.UserName) self.logdebug("JobId : %s" % self.Ticket.JobId) self.logdebug("Title : %s" % self.Ticket.Title) self.logdebug("Filename : %s" % self.Ticket.FileName) self.logdebug("Copies : %s" % self.Ticket.Copies) self.logdebug("Options : %s" % self.Ticket.Options) self.logdebug("Directory : %s" % self.Directory) self.logdebug("DataFile : %s" % self.DataFile) self.logdebug("JobBillingCode : %s" % self.Ticket.BillingCode) self.logdebug("JobOriginatingHostName : %s" % self.Ticket.OriginatingHostName) # fakes some entries to allow for external mailto # before real entries are extracted from the database. self.User = FakeUser(self.UserName) self.Printer = FakePrinter(self.PrinterName) self.enableSigInt() self.logdebug("Backend initialized.") def overwriteJobAttributes(self) : """Overwrites some of the job's attributes if needed.""" self.logdebug("Sanitizing job's attributes...") # First overwrite the job ticket self.overwriteJobTicket() # do we want to strip out the Samba/Winbind domain name ? separator = self.config.getWinbindSeparator() if separator is not None : self.UserName = self.UserName.split(separator)[-1] # this option is deprecated, and we want to tell people # this is the case. tolower = self.config.getUserNameToLower() if tolower is not None : self.printInfo("Option 'utolower' is deprecated. Please use the 'usernamecase' option instead. Syntax is 'usernamecase: lower|upper|native' and defaults to 'native'.", "error") # We apply it anyway if needed, to not break existing # configurations. TODO : make this a fatal failure in v1.27 if self.config.isTrue(tolower) : self.UserName = self.UserName.lower() # Now use the newer and more complete 'usernamecase' directive. casechange = self.config.getUserNameCase() if casechange != "native" : self.UserName = getattr(self.UserName, casechange)() # do we want to strip some prefix off of titles ? stripprefix = self.config.getStripTitle(self.PrinterName) if stripprefix : if fnmatch.fnmatch(self.Ticket.Title[:len(stripprefix)], stripprefix) : self.logdebug("Prefix [%s] removed from job's title [%s]." \ % (stripprefix, self.Ticket.Title)) self.Ticket.Title = self.Ticket.Title[len(stripprefix):] self.logdebug("Username : %s" % self.UserName) self.logdebug("BillingCode : %s" % self.Ticket.BillingCode) self.logdebug("Title : %s" % self.Ticket.Title) self.logdebug("Job's attributes sanitizing done.") def didUserConfirm(self) : """Asks for user confirmation through an external script. returns False if the end user wants to cancel the job, else True. """ self.logdebug("Checking if we have to ask for user's confirmation...") answer = None confirmationcommand = self.config.getAskConfirmation(self.PrinterName) if confirmationcommand : self.logdebug("Launching subprocess [%s] to ask for user confirmation." \ % confirmationcommand) inputfile = os.popen(confirmationcommand.encode(self.charset, "replace"), "r") try : for answer in inputfile.xreadlines() : answer = answer.strip().upper() if answer == "CANCEL" : break except IOError, msg : self.logdebug("IOError while reading subprocess' output : %s" % msg) inputfile.close() self.logdebug("User's confirmation received : %s" % (((answer == "CANCEL") and "CANCEL") or "CONTINUE")) else : self.logdebug("No need to ask for user's confirmation, job processing will continue.") return (answer != "CANCEL") def overwriteJobTicket(self) : """Should we overwrite the job's ticket (username and billingcode) ?""" self.logdebug("Checking if we need to overwrite the job ticket...") jobticketcommand = self.config.getOverwriteJobTicket(self.PrinterName) if jobticketcommand : username = billingcode = action = reason = None self.logdebug("Launching subprocess [%s] to overwrite the job ticket." \ % jobticketcommand) inputfile = os.popen(jobticketcommand.encode(self.charset, "replace"), "r") try : for line in inputfile.xreadlines() : line = line.strip() if line in ("DENY", "AUTH=NO", "AUTH=IMPOSSIBLE") : self.logdebug("Seen %s command." % line) action = "DENY" elif line == "CANCEL" : self.logdebug("Seen CANCEL command.") action = "CANCEL" elif line.startswith("USERNAME=") : username = line.split("=", 1)[1].strip().decode(self.charset, "replace") self.logdebug("Seen new username [%s]" % username) elif line.startswith("BILLINGCODE=") : billingcode = line.split("=", 1)[1].strip().decode(self.charset, "replace") self.logdebug("Seen new billing code [%s]" % billingcode) elif line.startswith("REASON=") : reason = line.split("=", 1)[1].strip().decode(self.charset, "replace") self.logdebug("Seen new reason [%s]" % reason) except IOError, msg : self.logdebug("IOError while reading subprocess' output : %s" % msg) inputfile.close() # now overwrite the job's ticket if new data was supplied if action == "DENY" : self.Action = action self.Reason = (reason or _("You are not allowed to print at this time.")) elif action == "CANCEL" : self.Action = action self.Reason = (reason or _("Print job cancelled.")) setenv("PYKOTASTATUS", "CANCELLED", self.charset) else : # don't overwrite anything unless job authorized # to continue to the physical printer. if username and username.strip() : self.UserName = username if billingcode is not None : self.Ticket.BillingCode = billingcode self.logdebug("Job ticket overwriting done.") def saveDatasAndCheckSum(self) : """Saves the input datas into a static file.""" self.logdebug("Duplicating data stream into %s" % self.DataFile) mustclose = 0 outfile = open(self.DataFile, "wb") if self.Ticket.FileName is not None : infile = open(self.Ticket.FileName, "rb") self.logdebug("Reading input datas from %s" % self.Ticket.FileName) mustclose = 1 else : infile = sys.stdin self.logdebug("Reading input datas from stdin") CHUNK = 64*1024 # read 64 Kb at a time dummy = 0 sizeread = 0 checksum = md5.new() while 1 : data = infile.read(CHUNK) if not data : break sizeread += len(data) outfile.write(data) checksum.update(data) if not (dummy % 32) : # Only display every 2 Mb self.logdebug("%s bytes saved..." % sizeread) dummy += 1 if mustclose : infile.close() outfile.close() self.JobSizeBytes = sizeread self.JobMD5Sum = checksum.hexdigest() self.logdebug("JobSizeBytes : %s" % self.JobSizeBytes) self.logdebug("JobMD5Sum : %s" % self.JobMD5Sum) self.logdebug("Data stream duplicated into %s" % self.DataFile) def clean(self) : """Cleans up the place.""" self.logdebug("Cleaning up...") if (self.DataFile is not None) and os.path.exists(self.DataFile) : try : keep = self.config.getPrinterKeepFiles(self.PrinterName) except AttributeError : keep = False if not keep : self.logdebug("Work file %s will be deleted." % self.DataFile) try : os.remove(self.DataFile) except OSError, msg : self.logdebug("Problem while deleting work file %s : %s" % (self.DataFile, msg)) else : self.logdebug("Work file %s has been deleted." % self.DataFile) else : self.logdebug("Work file %s will be kept." % self.DataFile) PyKotaTool.clean(self) if self.lockfile is not None : self.logdebug("Unlocking %s..." % self.lockfilename) try : fcntl.lockf(self.lockfile, fcntl.LOCK_UN) self.lockfile.close() except : self.printInfo("Problem while unlocking %s" % self.lockfilename, "error") else : self.logdebug("%s unlocked." % self.lockfilename) self.logdebug("Clean.") def precomputeJobSize(self) : """Computes the job size with a software method.""" self.logdebug("Precomputing job's size...") self.preaccounter.beginJob(None) self.preaccounter.endJob(None) self.softwareJobSize = self.preaccounter.getJobSize(None) self.logdebug("Precomputed job's size is %s pages." % self.softwareJobSize) def precomputeJobPrice(self) : """Precomputes the job price with a software method.""" self.logdebug("Precomputing job's price...") self.softwareJobPrice = self.UserPQuota.computeJobPrice(self.softwareJobSize, self.preaccounter.inkUsage) self.logdebug("Precomputed job's price is %.3f credits." \ % self.softwareJobPrice) def exportJobInfo(self) : """Exports the actual job's attributes to the environment.""" self.logdebug("Exporting job information to the environment...") setenv("DEVICE_URI", self.DeviceURI, self.charset) # WARNING ! setenv("PYKOTACHARSET", self.charset, self.charset) setenv("PYKOTALANG", self.language, self.charset) setenv("PYKOTAPRINTERNAME", self.PrinterName, self.charset) setenv("PYKOTADIRECTORY", self.Directory, self.charset) setenv("PYKOTADATAFILE", self.DataFile.decode(sys.getfilesystemencoding()), self.charset) setenv("PYKOTAJOBSIZEBYTES", str(self.JobSizeBytes), self.charset) setenv("PYKOTAMD5SUM", self.JobMD5Sum, self.charset) setenv("PYKOTAJOBORIGINATINGHOSTNAME", self.Ticket.OriginatingHostName or "", self.charset) setenv("PYKOTAJOBID", self.Ticket.JobId, self.charset) setenv("PYKOTAUSERNAME", self.UserName, self.charset) setenv("PYKOTAORIGINALUSERNAME", self.Ticket.OriginalUserName, self.charset) setenv("PYKOTATITLE", self.Ticket.Title, self.charset) setenv("PYKOTACOPIES", str(self.Ticket.Copies), self.charset) setenv("PYKOTAOPTIONS", self.Ticket.Options, self.charset) setenv("PYKOTAFILENAME", self.Ticket.FileName or "", self.charset) setenv("PYKOTAJOBBILLING", self.Ticket.BillingCode or "", self.charset) setenv("PYKOTAORIGINALJOBBILLING", self.OriginalJobBillingCode or "", self.charset) setenv("PYKOTAPRINTERHOSTNAME", self.PrinterHostName, self.charset) setenv("PYKOTAPRECOMPUTEDJOBSIZE", str(self.softwareJobSize), self.charset) self.logdebug("Environment updated.") def exportUserInfo(self) : """Exports user information to the environment.""" self.logdebug("Exporting user information to the environment...") setenv("PYKOTAOVERCHARGE", str(self.User.OverCharge), self.charset) setenv("PYKOTALIMITBY", str(self.User.LimitBy), self.charset) setenv("PYKOTABALANCE", str(self.User.AccountBalance or 0.0), self.charset) setenv("PYKOTALIFETIMEPAID", str(self.User.LifeTimePaid or 0.0), self.charset) setenv("PYKOTAUSERDESCRIPTION", self.User.Description or "", self.charset) setenv("PYKOTAMAXJOBSIZE", ((self.UserPQuota.MaxJobSize is None) and _("Unlimited")) or str(self.UserPQuota.MaxJobSize), self.charset) setenv("PYKOTAPAGECOUNTER", str(self.UserPQuota.PageCounter or 0), self.charset) setenv("PYKOTALIFEPAGECOUNTER", str(self.UserPQuota.LifePageCounter or 0), self.charset) setenv("PYKOTASOFTLIMIT", str(self.UserPQuota.SoftLimit), self.charset) setenv("PYKOTAHARDLIMIT", str(self.UserPQuota.HardLimit), self.charset) setenv("PYKOTADATELIMIT", str(self.UserPQuota.DateLimit), self.charset) setenv("PYKOTAWARNCOUNT", str(self.UserPQuota.WarnCount), self.charset) # TODO : move this elsewhere once software accounting is done only once. setenv("PYKOTAPRECOMPUTEDJOBPRICE", str(self.softwareJobPrice), self.charset) self.logdebug("Environment updated.") def exportPrinterInfo(self) : """Exports printer information to the environment.""" self.logdebug("Exporting printer information to the environment...") # exports the list of printers groups the current # printer is a member of setenv("PYKOTAPGROUPS", ",".join([p.Name for p in self.storage.getParentPrinters(self.Printer)]), self.charset) setenv("PYKOTAPRINTERDESCRIPTION", self.Printer.Description or "", self.charset) setenv("PYKOTAPRINTERMAXJOBSIZE", ((self.Printer.MaxJobSize is None) and _("Unlimited")) or str(self.Printer.MaxJobSize), self.charset) setenv("PYKOTAPRINTERPASSTHROUGHMODE", (self.Printer.PassThrough and _("ON")) or _("OFF"), self.charset) setenv("PYKOTAPRICEPERPAGE", str(self.Printer.PricePerPage or 0), self.charset) setenv("PYKOTAPRICEPERJOB", str(self.Printer.PricePerJob or 0), self.charset) self.logdebug("Environment updated.") def exportPhaseInfo(self, phase) : """Exports phase information to the environment.""" self.logdebug("Exporting phase information [%s] to the environment..." % phase) setenv("PYKOTAPHASE", phase, self.charset) self.logdebug("Environment updated.") def exportJobSizeAndPrice(self) : """Exports job's size and price information to the environment.""" self.logdebug("Exporting job's size and price information to the environment...") setenv("PYKOTAJOBSIZE", str(self.JobSize), self.charset) setenv("PYKOTAJOBPRICE", str(self.JobPrice), self.charset) self.logdebug("Environment updated.") def exportReason(self) : """Exports the job's action status and optional reason.""" self.logdebug("Exporting job's action status...") setenv("PYKOTAACTION", self.Action or "", self.charset) if self.Reason : setenv("PYKOTAREASON", self.Reason or "", self.charset) self.logdebug("Environment updated.") def acceptJob(self) : """Returns the appropriate exit code to tell CUPS all is OK.""" return 0 def removeJob(self) : """Returns the appropriate exit code to let CUPS think all is OK. Returning 0 (success) prevents CUPS from stopping the print queue. """ return 0 def launchPreHook(self) : """Allows plugging of an external hook before the job gets printed.""" prehook = self.config.getPreHook(self.PrinterName) if prehook : self.logdebug("Executing pre-hook [%s]..." % prehook) retcode = os.system(prehook.encode(self.charset, "replace")) self.logdebug("pre-hook exited with status %s." % retcode) def launchPostHook(self) : """Allows plugging of an external hook after the job gets printed and/or denied.""" posthook = self.config.getPostHook(self.PrinterName) if posthook : self.logdebug("Executing post-hook [%s]..." % posthook) retcode = os.system(posthook.encode(self.charset, "replace")) self.logdebug("post-hook exited with status %s." % retcode) def improveMessage(self, message) : """Improves a message by adding more informations in it if possible.""" try : return "%s@%s(%s) => %s" % (self.UserName, \ self.PrinterName, \ self.Ticket.JobId, \ message) except : return message def logdebug(self, message) : """Improves the debug message before outputting it.""" PyKotaTool.logdebug(self, self.improveMessage(message)) def printInfo(self, message, level="info") : """Improves the informational message before outputting it.""" self.logger.log_message(self.improveMessage(message), level) def startingBanner(self, withaccounting) : """Retrieves a starting banner for current printer and returns its content.""" self.logdebug("Retrieving starting banner...") self.printBanner(self.config.getStartingBanner(self.PrinterName), withaccounting) self.logdebug("Starting banner retrieved.") def endingBanner(self, withaccounting) : """Retrieves an ending banner for current printer and returns its content.""" self.logdebug("Retrieving ending banner...") self.printBanner(self.config.getEndingBanner(self.PrinterName), withaccounting) self.logdebug("Ending banner retrieved.") def printBanner(self, bannerfileorcommand, withaccounting) : """Reads a banner or generates one through an external command. Returns the banner's content in a format which MUST be accepted by the printer. """ self.logdebug("Printing banner...") if bannerfileorcommand : if os.access(bannerfileorcommand, os.X_OK) or \ not os.path.isfile(bannerfileorcommand) : self.logdebug("Launching %s to generate a banner." % bannerfileorcommand) child = popen2.Popen3(bannerfileorcommand.encode(self.charset, "replace"), capturestderr=1) self.runOriginalBackend(child.fromchild, isBanner=1) child.tochild.close() child.childerr.close() child.fromchild.close() status = child.wait() if os.WIFEXITED(status) : status = os.WEXITSTATUS(status) self.printInfo(_("Banner generator %s exit code is %s") \ % (bannerfileorcommand, str(status))) if withaccounting : if self.accounter.isSoftware : self.BannerSize += 1 # TODO : fix this by passing the banner's content through software accounting else : self.logdebug("Using %s as the banner." % bannerfileorcommand) try : fh = open(bannerfileorcommand, 'rb') except IOError, msg : self.printInfo("Impossible to open %s : %s" \ % (bannerfileorcommand, msg), "error") else : self.runOriginalBackend(fh, isBanner=1) fh.close() if withaccounting : if self.accounter.isSoftware : self.BannerSize += 1 # TODO : fix this by passing the banner's content through software accounting self.logdebug("Banner printed...") def handleBanner(self, bannertype, withaccounting) : """Handles the banner with or without accounting.""" if withaccounting : acc = "with" else : acc = "without" self.logdebug("Handling %s banner %s accounting..." % (bannertype, acc)) if (self.Action == 'DENY') and \ (self.UserPQuota.WarnCount >= \ self.config.getMaxDenyBanners(self.PrinterName)) : self.printInfo(_("Banner won't be printed : maximum number of deny banners reached."), \ "warn") else : if self.Action == 'DENY' : self.logdebug("Incrementing the number of deny banners for user %s on printer %s" \ % (self.UserName, self.PrinterName)) self.UserPQuota.incDenyBannerCounter() # increments the warning counter self.exportUserInfo() if ((self.Action == 'CANCEL') and not self.config.getPrintCancelledBanners()) : self.logdebug("Print job cancelled, not printing a banner.", "warn") else : self.logdebug("Checking if job owner printed the last job and if another banner is needed...") # Print the banner by default printbanner = True avoidduplicatebanners = self.config.getAvoidDuplicateBanners(self.PrinterName) if ((avoidduplicatebanners == "NO") or (avoidduplicatebanners == 0)): self.logdebug("We want all banners to be printed.") else : # Check if we should deny the banner or not if self.Printer.LastJob.Exists \ and (self.Printer.LastJob.UserName == self.UserName) : if (avoidduplicatebanners == "YES") : printbanner = False else : # avoidduplicatebanners is an integer, since NO, # YES and 0 are already handled now = DateTime.now() try : previous = DateTime.ISO.ParseDateTime(str(self.Printer.LastJob.JobDate)[:19]) except : previous = now difference = (now - previous).seconds self.logdebug("Difference with previous job : %.2f seconds. Try to avoid banners for : %.2f seconds." % (difference, avoidduplicatebanners)) if difference < avoidduplicatebanners : self.logdebug("Duplicate banner avoided because previous banner is less than %.2f seconds old." % avoidduplicatebanners) printbanner = False else : printbanner = True if printbanner : getattr(self, "%sBanner" % bannertype)(withaccounting) self.logdebug("%s banner done." % bannertype.title()) def sanitizeJobSize(self) : """Sanitizes the job's size if needed.""" # TODO : there's a difficult to see bug here when banner accounting is activated and hardware accounting is used. self.logdebug("Sanitizing job's size...") if self.softwareJobSize and (self.JobSize != self.softwareJobSize) : self.printInfo(_("Beware : computed job size (%s) != precomputed job size (%s)") % \ (self.JobSize, self.softwareJobSize), \ "error") (limit, replacement) = self.config.getTrustJobSize(self.PrinterName) if limit is None : self.printInfo(_("The job size will be trusted anyway according to the 'trustjobsize' directive"), "warn") else : if self.JobSize <= limit : self.printInfo(_("The job size will be trusted because it is inferior to the 'trustjobsize' directive's limit %s") % limit, "warn") else : self.printInfo(_("The job size will be modified according to the 'trustjobsize' directive : %s") % replacement, "warn") if replacement == "PRECOMPUTED" : self.JobSize = self.softwareJobSize else : self.JobSize = replacement self.logdebug("Job's size sanitized.") def getPrinterUserAndUserPQuota(self) : """Returns a tuple (policy, printer, user, and user print quota) on this printer. "OK" is returned in the policy if both printer, user and user print quota exist in the Quota Storage. Otherwise, the policy as defined for this printer in pykota.conf is returned. If policy was set to "EXTERNAL" and one of printer, user, or user print quota doesn't exist in the Quota Storage, then an external command is launched, as defined in the external policy for this printer in pykota.conf This external command can do anything, like automatically adding printers or users, for example, and finally extracting printer, user and user print quota from the Quota Storage is tried a second time. "EXTERNALERROR" is returned in case policy was "EXTERNAL" and an error status was returned by the external command. """ self.logdebug("Retrieving printer, user, and user print quota entry from database...") for passnumber in range(1, 3) : printer = self.storage.getPrinter(self.PrinterName) user = self.storage.getUser(self.UserName) userpquota = self.storage.getUserPQuota(user, printer) if printer.Exists and user.Exists and userpquota.Exists : policy = "OK" break (policy, args) = self.config.getPrinterPolicy(self.PrinterName) if policy == "EXTERNAL" : commandline = self.formatCommandLine(args, user, printer) if not printer.Exists : self.printInfo(_("Printer %s not registered in the PyKota system, applying external policy (%s) for printer %s") % (self.PrinterName, commandline, self.PrinterName)) if not user.Exists : self.printInfo(_("User %s not registered in the PyKota system, applying external policy (%s) for printer %s") % (self.UserName, commandline, self.PrinterName)) if not userpquota.Exists : self.printInfo(_("User %s doesn't have quota on printer %s in the PyKota system, applying external policy (%s) for printer %s") % (self.UserName, self.PrinterName, commandline, self.PrinterName)) if os.system(commandline.encode(self.charset, "replace")) : self.printInfo(_("External policy %s for printer %s produced an error. Job rejected. Please check PyKota's configuration files.") % (commandline, self.PrinterName), "error") policy = "EXTERNALERROR" break else : if not printer.Exists : self.printInfo(_("Printer %s not registered in the PyKota system, applying default policy (%s)") % (self.PrinterName, policy)) if not user.Exists : self.printInfo(_("User %s not registered in the PyKota system, applying default policy (%s) for printer %s") % (self.UserName, policy, self.PrinterName)) if not userpquota.Exists : self.printInfo(_("User %s doesn't have quota on printer %s in the PyKota system, applying default policy (%s)") % (self.UserName, self.PrinterName, policy)) break if policy == "EXTERNAL" : if not printer.Exists : self.printInfo(_("Printer %s still not registered in the PyKota system, job will be rejected") % self.PrinterName) if not user.Exists : self.printInfo(_("User %s still not registered in the PyKota system, job will be rejected on printer %s") % (self.UserName, self.PrinterName)) if not userpquota.Exists : self.printInfo(_("User %s still doesn't have quota on printer %s in the PyKota system, job will be rejected") % (self.UserName, self.PrinterName)) self.Policy = policy self.Printer = printer self.User = user self.UserPQuota = userpquota self.logdebug("Retrieval of printer, user and user print quota entry done.") def getBillingCode(self) : """Extracts the billing code from the database. An optional script is launched to notify the user when the billing code is unknown and PyKota was configured to deny printing in this case. """ self.logdebug("Retrieving billing code information from the database...") self.BillingCode = None if self.Ticket.BillingCode : self.BillingCode = self.storage.getBillingCode(self.Ticket.BillingCode) if self.BillingCode.Exists : self.logdebug("Billing code [%s] found in database." % self.Ticket.BillingCode) else : msg = "Unknown billing code [%s] : " % self.Ticket.BillingCode (newaction, script) = self.config.getUnknownBillingCode(self.PrinterName) if newaction == "CREATE" : self.logdebug(msg + "will be created.") self.storage.addBillingCode(self.BillingCode) self.BillingCode = self.storage.getBillingCode(self.Ticket.BillingCode) if self.BillingCode.Exists : self.logdebug(msg + "has been created.") else : self.printInfo(msg + "couldn't be created.", "error") else : self.logdebug(msg + "job will be denied.") self.Action = newaction if script is not None : self.logdebug(msg + "launching subprocess [%s] to notify user." % script) os.system(script.encode(self.charset, "replace")) self.logdebug("Retrieval of billing code information done.") def checkIfDupe(self) : """Checks if the job is a duplicate, and handles the situation.""" self.logdebug("Checking if the job is a duplicate...") denyduplicates = self.config.getDenyDuplicates(self.PrinterName) if not denyduplicates : self.logdebug("We don't care about duplicate jobs after all.") else : if self.Printer.LastJob.Exists \ and (self.Printer.LastJob.UserName == self.UserName) \ and (self.Printer.LastJob.JobMD5Sum == self.JobMD5Sum) : now = DateTime.now() try : previous = DateTime.ISO.ParseDateTime(str(self.Printer.LastJob.JobDate)[:19]) except : previous = now difference = (now - previous).seconds duplicatesdelay = self.config.getDuplicatesDelay(self.PrinterName) self.logdebug("Difference with previous job : %.2f seconds. Duplicates delay : %.2f seconds." % (difference, duplicatesdelay)) if difference > duplicatesdelay : self.logdebug("Duplicate job allowed because previous one is more than %.2f seconds old." % duplicatesdelay) else : # TODO : use the current user's last job instead of # TODO : the current printer's last job. This would be # TODO : better but requires an additional database query # TODO : with SQL, and is much more complex with the # TODO : actual LDAP schema. Maybe this is not very # TODO : important, because usually duplicate jobs are sucessive. msg = _("Job is a dupe") if denyduplicates is True : self.printInfo("%s : %s." % (msg, _("Printing is denied by configuration")), "warn") self.Action = "DENY" self.Reason = _("Duplicate print jobs are not allowed on printer %s.") % self.PrinterName else : self.logdebug("Launching subprocess [%s] to see if duplicate jobs should be allowed or not." % denyduplicates) fanswer = os.popen(denyduplicates.encode(self.charset, "replace"), "r") self.Action = fanswer.read().strip().upper() fanswer.close() if self.Action == "DENY" : self.printInfo("%s : %s." % (msg, _("Subprocess denied printing of a dupe")), "warn") self.Reason = _("Duplicate print jobs are not allowed on printer %s at this time.") % self.PrinterName else : self.printInfo("%s : %s." % (msg, _("Subprocess allowed printing of a dupe")), "warn") else : self.logdebug("Job doesn't seem to be a duplicate.") self.logdebug("Checking if the job is a duplicate done.") def tellUser(self) : """Sends a message to an user.""" self.logdebug("Sending some feedback to user %s..." % self.UserName) if not self.Reason : self.logdebug("No feedback to send to user %s." % self.UserName) else : (mailto, arguments) = self.config.getMailTo(self.PrinterName) if mailto == "EXTERNAL" : # TODO : clean this again self.externalMailTo(arguments, self.Action, self.User, self.Printer, self.Reason) else : # TODO : clean this again admin = self.config.getAdmin(self.PrinterName) adminmail = self.config.getAdminMail(self.PrinterName) usermail = self.User.Email or self.User.Name if "@" not in usermail : usermail = "%s@%s" % (usermail, self.maildomain or self.smtpserver) destination = [] if mailto in ("BOTH", "ADMIN") : destination.append(adminmail) if mailto in ("BOTH", "USER") : destination.append(usermail) fullmessage = self.Reason + (_("\n\nYour system administrator :\n\n\t%s - <%s>\n") % (admin, adminmail)) try : server = smtplib.SMTP(self.smtpserver) except socket.error, msg : self.printInfo(_("Impossible to connect to SMTP server : %s") % msg, "error") else : try : msg = MIMEText(fullmessage.encode(self.charset, "replace"), _charset=self.charset) subject = _("Print Quota") msg["Subject"] = Header(subject.encode(self.charset, "replace"), charset=self.charset, errors="replace") msg["From"] = adminmail if mailto in ("BOTH", "USER") : msg["To"] = usermail if mailto == "BOTH" : msg["Cc"] = adminmail else : msg["To"] = adminmail msg["Date"] = email.Utils.formatdate(localtime=True) server.sendmail(adminmail, destination, msg.as_string()) except smtplib.SMTPException, answer : try : for (k, v) in answer.recipients.items() : self.printInfo(_("Impossible to send mail to %s, error %s : %s") % (k, v[0], v[1]), "error") except AttributeError : self.printInfo(_("Problem when sending mail : %s") % str(answer), "error") server.quit() self.logdebug("Feedback sent to user %s." % self.UserName) def mainWork(self) : """Main work is done here.""" if not self.JobSizeBytes : # if no data to pass to real backend, probably a filter # higher in the chain failed because of a misconfiguration. # we deny the job in this case (nothing to print anyway) self.Reason = _("Job contains no data. Printing is denied.") self.printInfo(self.Reason, "error") self.tellUser() return self.removeJob() self.getPrinterUserAndUserPQuota() if self.Policy == "EXTERNALERROR" : # Policy was 'EXTERNAL' and the external command returned an error code self.Reason = _("Error in external policy script. Printing is denied.") self.printInfo(self.Reason, "error") self.tellUser() return self.removeJob() elif self.Policy == "EXTERNAL" : # Policy was 'EXTERNAL' and the external command wasn't able # to add either the printer, user or user print quota self.Reason = _("Still no print quota entry for user %s on printer %s after external policy. Printing is denied.") % (self.UserName, self.PrinterName) self.printInfo(self.Reason, "warn") self.tellUser() return self.removeJob() elif self.Policy == "DENY" : # Either printer, user or user print quota doesn't exist, # and the job should be rejected. self.Reason = _("Printing is denied by printer policy.") self.printInfo(self.Reason, "warn") self.tellUser() return self.removeJob() elif self.Policy == "ALLOW" : # ALLOW means : Either printer, user or user print quota doesn't exist, # but the job should be allowed anyway. self.Reason = _("Job allowed by printer policy. No accounting will be done.") self.printInfo(self.Reason, "warn") self.tellUser() return self.printJobDatas() elif self.Policy == "OK" : # OK means : Both printer, user and user print quota exist, job should # be allowed if current user is allowed to print on this printer return self.doWork() else : self.Reason = _("Invalid policy %s for printer %s") % (self.Policy, self.PrinterName) self.printInfo(self.Reason, "error") self.tellUser() return self.removeJob() def doWork(self) : """The accounting work is done here.""" self.precomputeJobPrice() self.exportUserInfo() self.exportPrinterInfo() self.exportPhaseInfo("BEFORE") if self.Action not in ("DENY", "CANCEL") : if self.Printer.MaxJobSize and (self.softwareJobSize > self.Printer.MaxJobSize) : # This printer was set to refuse jobs this large. self.printInfo(_("Precomputed job size (%s pages) too large for printer %s.") % (self.softwareJobSize, self.PrinterName), "warn") self.Action = "DENY" # here we don't put the precomputed job size in the message # because in case of error the user could complain :-) self.Reason = _("You are not allowed to print so many pages on printer %s at this time.") % self.PrinterName if self.Action not in ("DENY", "CANCEL") : if (self.UserPQuota.MaxJobSize is not None) and (self.softwareJobSize > self.UserPQuota.MaxJobSize) : # This user print quota entry was set to refuse jobs this large. self.printInfo(_("Precomputed job size (%s pages) too large for user %s on printer %s.") % (self.softwareJobSize, self.UserName, self.PrinterName), "warn") self.Action = "DENY" # here we don't put the precomputed job size in the message # because in case of error the user could complain :-) self.Reason = _("You are not allowed to print so many pages on printer %s at this time.") % self.PrinterName if self.Action not in ("DENY", "CANCEL") : if self.User.LimitBy == "noprint" : self.printInfo(_("User %s is not allowed to print at this time.") % self.UserName, "warn") self.Action = "DENY" self.Reason = _("Your account settings forbid you to print at this time.") if self.Action not in ("DENY", "CANCEL") : # If printing is still allowed at this time, we # need to extract the billing code information from the database. # No need to do this if the job is denied, this way we # save some database queries. self.getBillingCode() if self.Action not in ("DENY", "CANCEL") : # If printing is still allowed at this time, we # need to check if the job is a dupe or not, and what to do then. # No need to do this if the job is denied, this way we # save some database queries. self.checkIfDupe() if self.Action not in ("DENY", "CANCEL") : # If printing is still allowed at this time, we # need to check the user's print quota on the current printer. # No need to do this if the job is denied, this way we # save some database queries. if self.User.LimitBy in ('noquota', 'nochange') : self.logdebug("User %s is allowed to print with no limit, no need to check quota." % self.UserName) elif self.Printer.PassThrough : self.logdebug("Printer %s is in PassThrough mode, no need to check quota." % self.PrinterName) else : self.logdebug("Checking user %s print quota entry on printer %s" \ % (self.UserName, self.PrinterName)) self.Action = self.checkUserPQuota(self.UserPQuota) if self.Action.startswith("POLICY_") : self.Action = self.Action[7:] if self.Action == "DENY" : self.printInfo(_("Print Quota exceeded for user %s on printer %s") % (self.UserName, self.PrinterName)) self.Reason = self.config.getHardWarn(self.PrinterName) elif self.Action == "WARN" : self.printInfo(_("Print Quota low for user %s on printer %s") % (self.UserName, self.PrinterName)) if self.User.LimitBy and (self.User.LimitBy.lower() == "balance") : self.Reason = self.config.getPoorWarn() else : self.Reason = self.config.getSoftWarn(self.PrinterName) # If job still allowed to print, should we ask for confirmation ? if self.Action not in ("DENY", "CANCEL") : if not self.didUserConfirm() : self.Action = "CANCEL" self.Reason = _("Print job cancelled.") setenv("PYKOTASTATUS", "CANCELLED", self.charset) # exports some new environment variables self.exportReason() # now tell the user if he needs to know something self.tellUser() # launches the pre hook self.launchPreHook() # handle starting banner pages without accounting self.BannerSize = 0 accountbanner = self.config.getAccountBanner(self.PrinterName) if (self.Action != "CANCEL") and accountbanner in ["ENDING", "NONE"] : self.handleBanner("starting", 0) if self.Action == "DENY" : self.printInfo(_("Job denied, no accounting will be done.")) elif self.Action == "CANCEL" : self.printInfo(_("Job cancelled, no accounting will be done.")) else : self.printInfo(_("Job accounting begins.")) self.accounter.beginJob(self.Printer) # handle starting banner pages with accounting if (self.Action != "CANCEL") and accountbanner in ["STARTING", "BOTH"] : self.handleBanner("starting", 1) # pass the job's data to the real backend if needed if self.Action in ("ALLOW", "WARN") : retcode = self.printJobDatas() else : retcode = self.removeJob() # indicate phase change self.exportPhaseInfo("AFTER") # handle ending banner pages with accounting if (self.Action != "CANCEL") and accountbanner in ["ENDING", "BOTH"] : self.handleBanner("ending", 1) # stops accounting if self.Action == "DENY" : self.printInfo(_("Job denied, no accounting has been done.")) elif self.Action == "CANCEL" : self.printInfo(_("Job cancelled, no accounting has been done.")) else : self.accounter.endJob(self.Printer) self.printInfo(_("Job accounting ends.")) # Do all these database changes within a single transaction # NB : we don't enclose ALL the changes within a single transaction # because while waiting for the printer to answer its internal page # counter, we would open the door to accounting problems for other # jobs launched by the same user at the same time on other printers. # All the code below doesn't take much time, so it's fine. self.storage.beginTransaction() try : onbackenderror = self.config.getPrinterOnBackendError(self.PrinterName) if retcode : # NB : We don't send any feedback to the end user. Only the admin # has to know that the real CUPS backend failed. self.Action = "PROBLEM" self.exportReason() if "NOCHARGE" in onbackenderror : self.JobSize = 0 self.printInfo(_("Job size forced to 0 because the real CUPS backend failed. No accounting will be done."), "warn") else : self.printInfo(_("The real CUPS backend failed, but the job will be accounted for anyway."), "warn") # retrieve the job size if self.Action == "DENY" : self.JobSize = 0 self.printInfo(_("Job size forced to 0 because printing is denied.")) elif self.Action == "CANCEL" : self.JobSize = 0 self.printInfo(_("Job size forced to 0 because printing was cancelled.")) else : self.UserPQuota.resetDenyBannerCounter() if (self.Action != "PROBLEM") or ("CHARGE" in onbackenderror) : self.JobSize = self.accounter.getJobSize(self.Printer) self.sanitizeJobSize() self.JobSize += self.BannerSize else : self.JobSize = 0 self.printInfo(_("Job size : %i") % self.JobSize) if ((self.Action == "PROBLEM") and ("NOCHARGE" in onbackenderror)) or \ (self.Action in ("DENY", "CANCEL")) : self.JobPrice = 0.0 elif (self.User.LimitBy == "nochange") or self.Printer.PassThrough : # no need to update the quota for the current user on this printer self.printInfo(_("User %s's quota on printer %s won't be modified") % (self.UserName, self.PrinterName)) self.JobPrice = 0.0 else : # update the quota for the current user on this printer self.printInfo(_("Updating user %s's quota on printer %s") % (self.UserName, self.PrinterName)) self.JobPrice = self.UserPQuota.increasePagesUsage(self.JobSize, self.accounter.inkUsage) # adds the current job to history self.Printer.addJobToHistory(self.Ticket.JobId, self.User, self.accounter.getLastPageCounter(), \ self.Action, self.JobSize, self.JobPrice, self.Ticket.FileName, \ self.Ticket.Title, self.Ticket.Copies, self.Ticket.Options, self.Ticket.OriginatingHostName, \ self.JobSizeBytes, self.JobMD5Sum, None, self.Ticket.BillingCode, \ self.softwareJobSize, self.softwareJobPrice) self.printInfo(_("Job added to history.")) if hasattr(self, "BillingCode") and self.BillingCode and self.BillingCode.Exists : if (self.Action in ("ALLOW", "WARN")) or \ ((self.Action == "PROBLEM") and ("CHARGE" in onbackenderror)) : self.BillingCode.consume(self.JobSize, self.JobPrice) self.printInfo(_("Billing code %s was updated.") % self.BillingCode.BillingCode) except : self.storage.rollbackTransaction() raise else : self.storage.commitTransaction() # exports some new environment variables self.exportJobSizeAndPrice() # then re-export user information with new values self.exportUserInfo() # handle ending banner pages without accounting if (self.Action != "CANCEL") and accountbanner in ["STARTING", "NONE"] : self.handleBanner("ending", 0) self.launchPostHook() return retcode def printJobDatas(self) : """Sends the job's datas to the real backend.""" self.logdebug("Sending job's datas to real backend...") delay = 0 number = 1 for onb in self.config.getPrinterOnBackendError(self.PrinterName) : if onb.startswith("RETRY:") : try : (number, delay) = [int(p) for p in onb[6:].split(":", 2)] if (number < 0) or (delay < 0) : raise ValueError except ValueError : self.printInfo(_("Incorrect value for the 'onbackenderror' directive in section [%s]") % self.PrinterName, "error") delay = 0 number = 1 else : break loopcnt = 1 while True : if self.Ticket.FileName is None : infile = open(self.DataFile, "rb") else : infile = None retcode = self.runOriginalBackend(infile) if self.Ticket.FileName is None : infile.close() if not retcode : break else : if (not number) or (loopcnt < number) : self.logdebug(_("The real backend produced an error, we will try again in %s seconds.") % delay) time.sleep(delay) loopcnt += 1 else : break self.logdebug("Job's datas sent to real backend.") return retcode def runOriginalBackend(self, filehandle=None, isBanner=0) : """Launches the original backend.""" originalbackend = os.path.join(os.path.split(sys.argv[0])[0], self.RealBackend) if not isBanner : arguments = [os.environ["DEVICE_URI"]] + [a.encode("UTF-8") for a in sys.argv[1:]] else : # For banners, we absolutely WANT # to remove any filename from the command line ! self.logdebug("It looks like we try to print a banner.") arguments = [os.environ["DEVICE_URI"]] + [a.encode("UTF-8") for a in sys.argv[1:6]] arguments[2] = self.UserName # in case it was overwritten by external script # TODO : do something about job-billing option, in case it was overwritten as well... # TODO : do something about the job title : if we are printing a banner and the backend # TODO : uses the job's title to name an output file (cups-pdf:// for example), we're stuck ! self.logdebug("Starting original backend %s with args %s" % (originalbackend, " ".join(['"%s"' % a.decode("UTF-8") for a in arguments]))) pid = os.fork() self.logdebug("Forked !") if pid == 0 : if filehandle is not None : self.logdebug("Redirecting file handle to real backend's stdin") os.dup2(filehandle.fileno(), 0) try : self.logdebug("Calling execve...") os.execve(originalbackend, arguments, os.environ) except OSError, msg : self.logdebug("execve() failed: %s" % msg) self.logdebug("We shouldn't be there !!!") os._exit(-1) self.logdebug("Waiting for original backend to exit...") killed = False status = -1 while status == -1 : try : status = os.waitpid(pid, 0)[1] except OSError, (err, msg) : if err == 4 : killed = True if os.WIFEXITED(status) : status = os.WEXITSTATUS(status) message = "CUPS backend %s returned %d." % \ (originalbackend, status) if status : level = "error" self.Reason = message else : level = "info" self.printInfo(message, level) return status elif not killed : self.Reason = "CUPS backend %s died abnormally." % originalbackend self.printInfo(self.Reason, "error") return 1 else : self.Reason = "CUPS backend %s was killed." % originalbackend self.printInfo(self.Reason, "warn") return 1 if __name__ == "__main__" : # This is a CUPS backend, we should act and die like a CUPS backend wrapper = CUPSBackend() if len(sys.argv) == 1 : print "\n".join(wrapper.discoverOtherBackends()) sys.exit(0) elif len(sys.argv) not in (6, 7) : logerr("ERROR: %s job-id user title copies options [file]\n"\ % sys.argv[0]) sys.exit(1) else : os.environ["PATH"] = "%s:/bin:/usr/bin:/usr/local/bin:/opt/bin:/sbin:/usr/sbin" % os.environ.get("PATH", "") try : try : wrapper.deferredInit() wrapper.initBackendParameters() wrapper.waitForLock() if os.environ.get("PYKOTASTATUS") == "CANCELLED" : raise KeyboardInterrupt wrapper.saveDatasAndCheckSum() wrapper.exportJobInfo() # exports a first time to give hints to external scripts wrapper.preaccounter = openAccounter(wrapper, ispreaccounter=1) wrapper.accounter = openAccounter(wrapper) wrapper.precomputeJobSize() wrapper.exportJobInfo() # exports a second time, now that we know the job's size. TODO : don't reexport all wrapper.overwriteJobAttributes() wrapper.exportJobInfo() # re-exports in case it was overwritten. TODO : don't reexport all. retcode = wrapper.mainWork() except KeyboardInterrupt : wrapper.printInfo(_("Job %s interrupted by the administrator !") % wrapper.Ticket.JobId, "warn") retcode = 0 except SystemExit, err : retcode = err.code except : try : wrapper.crashed("cupspykota backend failed") except : crashed("cupspykota backend failed") retcode = 1 finally : wrapper.clean() sys.exit(retcode)