Changeset 587

Show
Ignore:
Timestamp:
03/14/05 17:02:18 (19 years ago)
Author:
jerome
Message:

Added (buggy) code to handle the transmission to the real backend

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • tea4cups/trunk/tea4cups

    r585 r587  
    3232import tempfile 
    3333import ConfigParser 
     34import threading 
    3435from struct import unpack 
    3536 
     
    518519        if self.isTrue(self.getPrintQueueOption(self.PrinterName, "serialize", ignore=1)) : 
    519520            self.logDebug("Serialized Tees") 
     521            if self.RealBackend : 
     522                self.logDebug("Launching original backend %s for printer %s" % (self.RealBackend, self.PrinterName)) 
     523                exitcode = self.runOriginalBackend() 
    520524            for (branch, command) in branches.items() : 
    521525                self.logDebug("Launching %s : %s" % (branch, command)) 
     
    530534            self.logDebug("Forked Tees") 
    531535            pids = {} 
     536            if self.RealBackend : 
     537                branches["Original backend"] = None     # Fakes a tee to launch one more child 
    532538            for (branch, command) in branches.items() : 
    533539                pid = os.fork() 
     
    535541                    pids[branch] = pid 
    536542                else :     
    537                     self.logDebug("Launching %s : %s" % (branch, command)) 
    538                     retcode = os.system(command) 
    539                     if os.WIFEXITED(retcode) : 
    540                         retcode = os.WEXITSTATUS(retcode) 
    541                     else :     
    542                         retcode = -1 
    543                     sys.exit(retcode) 
     543                    if branch == "Original backend" : 
     544                        self.logDebug("Launching original backend %s for printer %s" % (self.RealBackend, self.PrinterName)) 
     545                        sys.exit(self.runOriginalBackend()) 
     546                    else : 
     547                        self.logDebug("Launching %s : %s" % (branch, command)) 
     548                        retcode = os.system(command) 
     549                        if os.WIFEXITED(retcode) : 
     550                            retcode = os.WEXITSTATUS(retcode) 
     551                        else :     
     552                            retcode = -1 
     553                        sys.exit(retcode) 
    544554            for (branch, pid) in pids.items() : 
    545555                (childpid, retcode) = os.waitpid(pid, 0) 
     
    551561                    exitcode = 1 
    552562        return exitcode 
     563         
     564    def runOriginalBackend(self) :     
     565        """Launches the original backend.""" 
     566        originalbackend = os.path.join(os.path.split(sys.argv[0])[0], self.RealBackend) 
     567        arguments = sys.argv 
     568        self.logDebug("Starting original backend %s with args %s" % (originalbackend, " ".join(['"%s"' % a for a in ([os.environ["DEVICE_URI"]] + arguments[1:])]))) 
     569        subprocess = Popen4ForCUPS([originalbackend] + arguments[1:], bufsize=0, arg0=os.environ["DEVICE_URI"]) 
     570        rendezvous = threading.Event() 
     571        if self.InputFile is None : 
     572           self.logDebug("Launching data thread.") 
     573           infile = open(self.DataFile, "rb") 
     574           iothread = threading.Thread(target = self.handleOriginalBackendIO, kwargs = { "parent" : threading.currentThread(), "event" : rendezvous, "inf": infile, "outf" : subprocess.tochild}) 
     575           iothread.start() 
     576            
     577        # here we only want to read its output and send it on our stderr 
     578        while (self.InputFile is not None) or iothread.isAlive() : # until all data transmitted to original backend 
     579            data = subprocess.fromchild.readline() 
     580            if (not data) or rendezvous.isSet() : 
     581                break 
     582            sys.stderr.write(data)     
     583            sys.stderr.flush() 
     584             
     585        if not rendezvous.isSet() :     
     586            rendezvous.set()     
     587        if self.InputFile is None : 
     588            infile.close() 
     589            iothread.join() 
     590        subprocess.fromchild.close() 
     591        subprocess.tochild.close() 
     592        status = subprocess.wait() 
     593        if os.WIFEXITED(status) : 
     594            return os.WEXITSTATUS(status) 
     595        else :     
     596            return 1 
     597         
     598    def handleOriginalBackendIO(self, parent, event, inf, outf) :     
     599        """Thread to handles the original backend's I/O.""" 
     600        dummy = 0 
     601        totalsent = 0 
     602        while 1 : 
     603            if not parent.isAlive() : 
     604                self.logInfo("Parent died unexpectedly.", level = "warn") 
     605                break 
     606            if event.isSet() :     
     607                self.logInfo("Parent said work is finished.") 
     608                break 
     609            try :     
     610                data = inf.readline() 
     611                if not data : 
     612                    break 
     613                outf.write(data)     
     614                totalsent += len(data) 
     615                dummy += 1 
     616                if not (dummy % 10) : 
     617                    self.logDebug("Sent %i bytes to original backend." % totalsent) 
     618                try : 
     619                    outf.flush() 
     620                except :     
     621                    pass 
     622            except IOError, msg : 
     623                self.logInfo("I/O Error : %s" % msg, level = "warn") 
     624                break 
     625        event.set()         
     626        sys.exit(0) 
    553627         
    554628if __name__ == "__main__" :