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

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

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • 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