Changeset 584

Show
Ignore:
Timestamp:
03/13/05 18:22:22 (19 years ago)
Author:
jerome
Message:

Forked tees work fine now

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • tea4cups/trunk/tea4cups

    r583 r584  
    2525import sys 
    2626import os 
     27import popen2 
    2728import errno 
    2829import md5 
     
    5051    pass  
    5152     
     53class Popen4ForCUPS(popen2.Popen4) : 
     54    """Our own class to execute real backends. 
     55     
     56       Their first argument is different from their path so using 
     57       native popen2.Popen3 would not be feasible. 
     58    """ 
     59    def __init__(self, cmd, bufsize=-1, arg0=None) : 
     60        self.arg0 = arg0 
     61        popen2.Popen4.__init__(self, cmd, bufsize) 
     62         
     63    def _run_child(self, cmd): 
     64        try : 
     65            MAXFD = os.sysconf("SC_OPEN_MAX") 
     66        except (AttributeError, ValueError) :     
     67            MAXFD = 256 
     68        for i in range(3, MAXFD) :  
     69            try: 
     70                os.close(i) 
     71            except OSError: 
     72                pass 
     73        try: 
     74            os.execvpe(cmd[0], [self.arg0 or cmd[0]] + cmd[1:], os.environ) 
     75        finally: 
     76            os._exit(1) 
     77     
    5278# Some IPP constants     
    5379OPERATION_ATTRIBUTES_TAG = 0x01 
     
    211237        """Logs something to debug output if debug is enabled.""" 
    212238        if self.debug : 
    213             sys.stderr.write("DEBUG: %s (PID %i) : %s\n" % (self.MyName, self.pid, message)) 
     239            sys.stderr.write("DEBUG: %s (PID %i) : %s\n" % (self.MyName, os.getpid(), message)) 
    214240            sys.stderr.flush() 
    215241             
    216242    def logInfo(self, message, level="info") :         
    217243        """Logs a message to CUPS' error_log file.""" 
    218         sys.stderr.write("%s: %s (PID %i) : %s\n" % (level.upper(), self.MyName, self.pid, message)) 
     244        sys.stderr.write("%s: %s (PID %i) : %s\n" % (level.upper(), self.MyName, os.getpid(), message)) 
    219245        sys.stderr.flush() 
    220246         
     
    487513    def runBranches(self) :          
    488514        """Launches each tee defined for the current print queue.""" 
     515        exitcode = 0 
    489516        branches = self.enumTeeBranches(self.PrinterName) 
    490517        if self.isTrue(self.getPrintQueueOption(self.PrinterName, "serialize", ignore=1)) : 
     518            self.logDebug("Serialized Tees") 
    491519            for (branch, command) in branches.items() : 
    492520                self.logDebug("Launching %s : %s" % (branch, command)) 
    493                 os.system(command) 
     521                retcode = os.system(command) 
     522                self.logDebug("Exit code for tee %s on printer %s is %s" % (branch, self.PrinterName, retcode)) 
     523                if os.WIFEXITED(retcode) : 
     524                    retcode = os.WEXITSTATUS(retcode) 
     525                if retcode :     
     526                    self.logInfo("Tee %s on printer %s didn't exit successfully." % (branch, self.PrinterName), "error") 
     527                    exitcode = 1 
    494528        else :         
    495             raise TeeError, "Forking tees not yet implemented." 
     529            self.logDebug("Forked Tees") 
     530            pids = {} 
     531            for (branch, command) in branches.items() : 
     532                pid = os.fork() 
     533                if pid : 
     534                    pids[branch] = pid 
     535                else :     
     536                    self.logDebug("Launching %s : %s" % (branch, command)) 
     537                    retcode = os.system(command) 
     538                    if os.WIFEXITED(retcode) : 
     539                        retcode = os.WEXITSTATUS(retcode) 
     540                    else :     
     541                        retcode = -1 
     542                    sys.exit(retcode) 
     543            for (branch, pid) in pids.items() : 
     544                (childpid, retcode) = os.waitpid(pid, 0) 
     545                self.logDebug("Exit code for tee %s (PID %s) on printer %s is %s" % (branch, childpid, self.PrinterName, retcode)) 
     546                if os.WIFEXITED(retcode) : 
     547                    retcode = os.WEXITSTATUS(retcode) 
     548                if retcode :     
     549                    self.logInfo("Tee %s (PID %s) on printer %s didn't exit successfully." % (branch, childpid, self.PrinterName), "error") 
     550                    exitcode = 1 
     551        return exitcode 
    496552         
    497553if __name__ == "__main__" :