Changeset 676

Show
Ignore:
Timestamp:
06/27/06 14:28:16 (18 years ago)
Author:
jerome
Message:

Added support for the 'retry' directive.
Fixed an IPP parsing problem.
Fixed the filename argument to the original backend : if a tea4cups filter was applied,
and the file was printed in raw mode, the filtered datas were not printed, but the raw
ones were.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • tea4cups/trunk/tea4cups

    r674 r676  
    2626import sys 
    2727import os 
     28import time 
    2829import pwd 
    2930import errno 
     
    123124        self.tags[0x05] = "unsupported-attributes-tag" 
    124125        self.tags[0x06] = "subscription-attributes-tag" 
    125         self.tags[0x07] = "event-notification-attributes-tag" 
     126        self.tags[0x07] = "event_notification-attributes-tag" 
    126127         
    127128        # out of band values 
     
    531532        self.DeviceURI = device_uri 
    532533        self.PrinterName = os.environ.get("PRINTER", "") 
    533         self.Directory = self.getPrintQueueOption(self.PrinterName, "directory") 
     534        self.Directory = self.getPrintQueueOption(self.PrinterName, "directory", ignore=1) or tempfile.gettempdir() 
    534535        self.DataFile = os.path.join(self.Directory, "%s-%s-%s-%s" % (self.myname, self.PrinterName, self.UserName, self.JobId)) 
    535536        (ippfilename, ippmessage) = self.parseIPPRequestFile() 
     
    675676    def cleanUp(self) : 
    676677        """Cleans up the place.""" 
    677         if not self.isTrue(self.getPrintQueueOption(self.PrinterName, "keepfiles", ignore=1)) : 
    678             os.remove(self.DataFile) 
     678        if (not self.isTrue(self.getPrintQueueOption(self.PrinterName, "keepfiles", ignore=1))) \ 
     679            and os.path.exists(self.DataFile) : 
     680            try : 
     681                os.remove(self.DataFile) 
     682            except OSError, msg :     
     683                self.logInfo("Problem when removing %s : %s" % (self.DataFile, msg), "error") 
    679684 
    680685    def sigtermHandler(self, signum, frame) : 
     
    698703        if not self.isCancelled and not self.gotSigTerm : 
    699704            if self.RealBackend : 
    700                 retcode = self.runOriginalBackend() 
     705                retcode = self.launchOriginalBackend() 
    701706                if retcode : 
    702707                    onfail = self.getPrintQueueOption(self.PrinterName, \ 
     
    802807        return exitcode 
    803808 
     809    def launchOriginalBackend(self) : 
     810        """Launches the original backend, optionally retrying if needed.""" 
     811        number = 1 
     812        delay = 0 
     813        retry = self.getPrintQueueOption(self.PrinterName, "retry", ignore=1) 
     814        if retry is not None : 
     815            try : 
     816                (number, delay) = [int(p) for p in retry.strip().split(",")] 
     817            except (ValueError, AttributeError, TypeError) :     
     818                self.logInfo("Invalid value '%s' for the 'retry' directive for printer %s in %s." % (retry, self.PrinterName, self.conffile), "error") 
     819                number = 1 
     820                delay = 0 
     821                 
     822        loopcount = 1  
     823        while 1 :             
     824            retcode = self.runOriginalBackend() 
     825            if not retcode : 
     826                break 
     827            else : 
     828                if (not number) or (loopcount < number) : 
     829                    self.logInfo(_("The real backend produced an error, we will try again in %s seconds.") % delay, "warn") 
     830                    time.sleep(delay) 
     831                    loopcount += 1 
     832                else :     
     833                    break 
     834        return retcode             
     835         
    804836    def runOriginalBackend(self) : 
    805837        """Launches the original backend.""" 
     
    814846                os.dup2(f.fileno(), 0) 
    815847                f.close() 
     848            else :     
     849                arguments[6] = self.DataFile # in case a tea4cups filter was applied 
    816850            try : 
    817851                os.execve(originalbackend, arguments, os.environ) 
     
    831865            status = os.WEXITSTATUS(status) 
    832866            if status : 
    833               self.logInfo("CUPS backend %s returned %d." % (originalbackend,\ 
     867                self.logInfo("CUPS backend %s returned %d." % (originalbackend,\ 
    834868                                                             status), "error") 
    835869            return status 
     
    852886        sys.exit(1) 
    853887    else : 
     888        retcode = 1 
    854889        try : 
    855             wrapper.readConfig() 
    856             wrapper.initBackend() 
    857             wrapper.saveDatasAndCheckSum() 
    858             wrapper.exportAttributes() 
    859             retcode = wrapper.runBranches() 
     890            try : 
     891                wrapper.readConfig() 
     892                wrapper.initBackend() 
     893                wrapper.saveDatasAndCheckSum() 
     894                wrapper.exportAttributes() 
     895                retcode = wrapper.runBranches() 
     896            except SystemExit, e : 
     897                retcode = e.code 
     898            except : 
     899                import traceback 
     900                lines = [] 
     901                for line in traceback.format_exception(*sys.exc_info()) : 
     902                    lines.extend([l for l in line.split("\n") if l]) 
     903                msg = "ERROR: ".join(["%s (PID %s) : %s\n" % (wrapper.MyName, \ 
     904                                                              wrapper.pid, l) \ 
     905                            for l in (["ERROR: Tea4CUPS v%s" % __version__] + lines)]) 
     906                sys.stderr.write(msg) 
     907                sys.stderr.flush() 
     908                retcode = 1 
     909        finally :         
    860910            wrapper.cleanUp() 
    861         except SystemExit, e : 
    862             retcode = e.code 
    863         except : 
    864             import traceback 
    865             lines = [] 
    866             for line in traceback.format_exception(*sys.exc_info()) : 
    867                 lines.extend([l for l in line.split("\n") if l]) 
    868             msg = "ERROR: ".join(["%s (PID %s) : %s\n" % (wrapper.MyName, \ 
    869                                                           wrapper.pid, l) \ 
    870                         for l in (["ERROR: Tea4CUPS v%s" % __version__] + lines)]) 
    871             sys.stderr.write(msg) 
    872             sys.stderr.flush() 
    873             retcode = 1 
    874911        sys.exit(retcode)