Changeset 598

Show
Ignore:
Timestamp:
03/19/05 11:36:04 (20 years ago)
Author:
jerome
Message:

More work on prehook and posthook. Needs testing.
Now sorts tees before launching them, may be useful.

Location:
tea4cups/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • tea4cups/trunk/README

    r592 r598  
    3333message, all of this by printing it a single time. 
    3434 
     35Tea4CUPS provides three ways to launch commands : 
     36 
     37        - prehooks : these are guaranteed to be launched before the  
     38                     print job is sent to the real printer. 
     39                      
     40        - tees : these are launched at the same time the job is being              
     41                 sent to the real printer. 
     42                  
     43        - posthooks : these are guaranteed to be launched after the 
     44                      print job has been sent to the real printer. 
     45                  
    3546To help your own commands, Tea4CUPS makes available as part of the 
    3647environment several variables which can be used as part of the 
     
    4960        TEAOPTIONS : Options of the print job. 
    5061        TEAINPUTFILE : Print job's data file or empty when job read from stdin. 
     62        TEASTATUS : Original CUPS backend's exit code : ONLY AVAILABLE FROM  
     63                    post hooks, obviously. 
    5164         
    5265In the case you want to use both Tea4CUPS and PyKota, you MUST install  
  • tea4cups/trunk/tea4cups

    r597 r598  
    527527         
    528528    def runBranches(self) :          
    529         """Launches each tee defined for the current print queue.""" 
     529        """Launches each hook or tee defined for the current print queue.""" 
    530530        exitcode = 0 
    531531        signal.signal(signal.SIGTERM, self.sigtermHandler) 
    532         prehooks = self.enumBranches(self.PrinterName, "prehook") 
    533         branches = self.enumBranches(self.PrinterName, "tee") 
    534         prehooks = self.enumBranches(self.PrinterName, "posthook") 
    535         if self.isTrue(self.getPrintQueueOption(self.PrinterName, "serialize", ignore=1)) : 
    536             self.logDebug("Serialized Tees") 
    537             if self.RealBackend : 
     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 : 
    538551                self.logDebug("Launching original backend %s for printer %s" % (self.RealBackend, self.PrinterName)) 
    539552                exitcode = self.runOriginalBackend() 
    540             for (branch, command) in branches.items() : 
     553            for branch in branchlist : 
     554                command = branches[branch] 
    541555                if self.gotSigTerm : 
    542556                    break 
    543557                self.logDebug("Launching %s : %s" % (branch, command)) 
    544558                retcode = os.system(command) 
    545                 self.logDebug("Exit code for tee %s on printer %s is %s" % (branch, self.PrinterName, retcode)) 
     559                self.logDebug("Exit code for %s %s on printer %s is %s" % (btype, branch, self.PrinterName, retcode)) 
    546560                if os.WIFEXITED(retcode) : 
    547561                    retcode = os.WEXITSTATUS(retcode) 
    548562                if retcode :     
    549                     self.logInfo("Tee %s on printer %s didn't exit successfully." % (branch, self.PrinterName), "error") 
     563                    self.logInfo("%s %s on printer %s didn't exit successfully." % (btypetitle, branch, self.PrinterName), "error") 
    550564                    exitcode = 1 
    551565        else :         
    552             self.logDebug("Forked Tees") 
     566            self.logDebug("Forked %ss" % btypetitle) 
    553567            pids = {} 
    554             if self.RealBackend : 
     568            if (btype == "tee") and self.RealBackend : 
    555569                branches["Original backend"] = None     # Fakes a tee to launch one more child 
    556             for (branch, command) in branches.items() : 
     570                branchlist = ["Original backend"] + branchlist 
     571            for branch in branchlist : 
     572                command = branches[branch] 
    557573                if self.gotSigTerm : 
    558574                    break 
     
    574590            for (branch, pid) in pids.items() : 
    575591                (childpid, retcode) = os.waitpid(pid, 0) 
    576                 self.logDebug("Exit code for tee %s (PID %s) on printer %s is %s" % (branch, childpid, self.PrinterName, retcode)) 
     592                self.logDebug("Exit code for %s %s (PID %s) on printer %s is %s" % (btype, branch, childpid, self.PrinterName, retcode)) 
    577593                if os.WIFEXITED(retcode) : 
    578594                    retcode = os.WEXITSTATUS(retcode) 
    579595                if retcode :     
    580                     self.logInfo("Tee %s (PID %s) on printer %s didn't exit successfully." % (branch, childpid, self.PrinterName), "error") 
     596                    self.logInfo("%s %s (PID %s) on printer %s didn't exit successfully." % (btypetitle, branch, childpid, self.PrinterName), "error") 
    581597                    exitcode = 1 
    582         signal.signal(signal.SIGTERM, signal.SIG_IGN) 
     598                if branch == "Original backend" :     
     599                    os.environ["TEASTATUS"] = str(retcode) 
    583600        return exitcode 
    584601         
  • tea4cups/trunk/tea4cups.conf

    r597 r598  
    3131# The value defined in a print queue section takes precedence over the 
    3232# value defined in the [global] section. 
     33# directory : /var/spool/tea4cups/ 
    3334directory : /var/spool/cups/ 
    3435 
     
    4445# Should we serialize the launch of all tees : launch one tee after 
    4546# the other to save some system resources. 
    46 # Defaults to No if unset, meaning that all tees are launched in 
     47# Defaults to No if unset, meaning that all tees (or hooks) are launched in 
    4748# parallel. 
     49# 
     50# NB : in any case, hooks or tees names are sorted alphabetically and  
     51# are launched in this sort order (obviously when launched in parallel 
     52# this is unnoticeable). 
     53#  
    4854# Can be set either in the [global] section or any print queue section. 
    4955# The value defined in a print queue section takes precedence over the 
     
    100106#  
    101107# prehook_0 : echo "Your print job has been accepted" | smbclient -M $TEAUSERNAME 
    102 # posthook_0 : echo "Your print job has been printed" | smbclient -M $TEAUSERNAME 
     108# posthook_0 : echo "Your print job has been printed with status $TEASTATUS" | smbclient -M $TEAUSERNAME 
    103109 
    104110 
     
    111117# value takes precedence 
    112118#tee_0 : cat $TEADATAFILE >~$TEAUSERNAME/savejobs/$TEAJOBID.prn 
     119 
    113120# An empty value deletes a value defined in the [global] section 
    114121# so this particular tee doesn't get executed. 
    115122#tee_pdf :  
     123 
    116124# A reflector which produces 4 copies each time : 
    117125#tee_4copies : lp -dotherprinter -n4 $CUPSDATAFILE  
     126 
    118127# A simple accounting mechanism  
    119128#tee_accounting : echo $TEAPRINTERNAME $TEAJOBID $TEAUSERNAME `pkpgcounter $TEADATAFILE` >/var/log/printaccounting.log 
     129 
     130# An additionnal posthook : 
     131#posthook_dummy : echo "$TEAJOBID : $TEAMD5SUM" >>/tmp/jobmd5sums