Changeset 639

Show
Ignore:
Timestamp:
06/07/05 10:58:06 (19 years ago)
Author:
jerome
Message:

Integrated Peter Stuge's second patch

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • tea4cups/trunk/tea4cups

    r638 r639  
    55# 
    66# (c) 2005 Jerome Alet <alet@librelogiciel.com> 
     7# (c) 2005 Peter Stuge <stuge-tea4cups@cdy.org> 
    78# This program is free software; you can redistribute it and/or modify 
    89# it under the terms of the GNU General Public License as published by 
     
    3839from struct import unpack 
    3940 
    40 version = "2.12alpha2_unofficial" 
     41version = "2.12alpha3_unofficial" 
    4142 
    4243class TeeError(Exception): 
     
    320321        branchbasename = "%s_" % branchtype.lower() 
    321322        try : 
    322             # globalbranches = [ (k, v) for (k, v) in self.config.items("global") if k.startswith(branchbasename) ] 
    323323            globalbranches = [ (k, self.config.get("global", k)) for k in self.config.options("global") if k.startswith(branchbasename) ] 
    324324        except ConfigParser.NoSectionError, msg :     
    325325            raise ConfigError, "Invalid configuration file : %s" % msg 
    326326        try : 
    327             # sectionbranches = [ (k, v) for (k, v) in self.config.items(printqueuename) if k.startswith(branchbasename) ] 
    328327            sectionbranches = [ (k, self.config.get(printqueuename, k)) for k in self.config.options(printqueuename) if k.startswith(branchbasename) ] 
    329328        except ConfigParser.NoSectionError, msg :     
     
    581580        signal.signal(signal.SIGTERM, self.sigtermHandler) 
    582581        serialize = self.isTrue(self.getPrintQueueOption(self.PrinterName, "serialize", ignore=1)) 
    583         self.pipe = os.pipe() 
     582        self.pipes = { 0: os.pipe() } 
    584583        for branchtype in ["prehook", "tee", "posthook"] : 
    585584            if branchtype == "posthook" : 
    586                 os.close(self.pipe[1]) 
     585                os.close(self.pipes[0][1]) 
    587586            branches = self.enumBranches(self.PrinterName, branchtype) 
     587            for b in branches : 
     588                bname = b.split("_", 1)[1] 
     589                if branchtype != "posthook" and bname not in self.pipes : 
     590                    self.pipes[bname] = os.pipe() 
     591                else : 
     592                    if bname in self.pipes : 
     593                        os.close(self.pipes[bname][1]) 
    588594            status = self.runCommands(branchtype, branches, serialize) 
    589595            if status : 
     
    596602                break # We don't want to execute tees or posthooks in this case 
    597603        signal.signal(signal.SIGTERM, signal.SIG_IGN) 
    598         try : 
    599             os.close(self.pipe[1]) 
    600         except OSError : 
    601             pass 
    602         os.close(self.pipe[0]) 
     604        for p in self.pipes : 
     605            os.close(self.pipes[p][0]) 
     606            try : 
     607                os.close(self.pipes[p][1]) 
     608            except OSError : 
     609                pass 
    603610        if not exitcode : 
    604611            self.logInfo("OK") 
     
    607614        return exitcode 
    608615         
    609     def pipedSystem(self, cmd, stdin=0, stdout=1) : 
    610         """Launches a command making a pipe available to it.""" 
    611         # Code contributed by Peter Stuge on May 23rd 2005 
     616    def stdioRedirSystem(self, cmd, stdin=0, stdout=1) : 
     617        """Launches a command with stdio redirected.""" 
     618        # Code contributed by Peter Stuge on May 23rd and June 7th 2005 
    612619        pid = os.fork() 
    613620        if pid == 0 : 
    614             os.dup2(stdin, 0) 
    615             os.dup2(stdout, 1) 
    616             os.execl("/bin/sh", "sh", "-c", cmd) 
    617             os.exit(1) 
     621            if stdin != 0 : 
     622                os.dup2(stdin, 0) 
     623                os.close(stdin) 
     624            if stdout != 1 : 
     625                os.dup2(stdout, 1) 
     626                os.close(stdout) 
     627            try : 
     628                os.execl("/bin/sh", "sh", "-c", cmd) 
     629            except OSError , msg : 
     630                self.logDebug("execl() failed: %s" % msg) 
     631            os._exit(1) 
    618632        return os.waitpid(pid, 0)[1] 
    619633         
     634    def runCommand(self, branch, command) : 
     635        """Runs a particular branch command.""" 
     636        # Code contributed by Peter Stuge on June 7th 2005 
     637        self.logDebug("Launching %s : %s" % (branch, command)) 
     638        btype, bname = branch.split("_", 1) 
     639        if bname not in self.pipes : 
     640          bname = 0 
     641        if btype != "posthook" : 
     642            return self.stdioRedirSystem(command, 0, self.pipes[bname][1]) 
     643        else : 
     644            return self.stdioRedirSystem(command, self.pipes[bname][0]) 
     645 
    620646    def runCommands(self, btype, branches, serialize) :     
    621647        """Runs the commands for a particular branch type.""" 
     
    638664                if self.gotSigTerm : 
    639665                    break 
    640                 self.logDebug("Launching %s : %s" % (branch, command)) 
    641                 if btype != "posthook" : 
    642                     retcode = self.pipedSystem(command, 0, self.pipe[1]) 
    643                 else : 
    644                     retcode = self.pipedSystem(command, self.pipe[0]) 
     666                retcode = self.runCommand(branch, command) 
    645667                self.logDebug("Exit code for %s %s on printer %s is %s" % (btype, branch, self.PrinterName, retcode)) 
    646668                if os.WIFEXITED(retcode) : 
     
    672694                        sys.exit(self.runOriginalBackend()) 
    673695                    else : 
    674                         self.logDebug("Launching %s : %s" % (branch, command)) 
    675                         retcode = os.system(command) 
     696                        retcode = self.runCommand(branch, command) 
    676697                        if os.WIFEXITED(retcode) : 
    677698                            retcode = os.WEXITSTATUS(retcode)