Changeset 639
- Timestamp:
- 06/07/05 10:58:06 (19 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
tea4cups/trunk/tea4cups
r638 r639 5 5 # 6 6 # (c) 2005 Jerome Alet <alet@librelogiciel.com> 7 # (c) 2005 Peter Stuge <stuge-tea4cups@cdy.org> 7 8 # This program is free software; you can redistribute it and/or modify 8 9 # it under the terms of the GNU General Public License as published by … … 38 39 from struct import unpack 39 40 40 version = "2.12alpha 2_unofficial"41 version = "2.12alpha3_unofficial" 41 42 42 43 class TeeError(Exception): … … 320 321 branchbasename = "%s_" % branchtype.lower() 321 322 try : 322 # globalbranches = [ (k, v) for (k, v) in self.config.items("global") if k.startswith(branchbasename) ]323 323 globalbranches = [ (k, self.config.get("global", k)) for k in self.config.options("global") if k.startswith(branchbasename) ] 324 324 except ConfigParser.NoSectionError, msg : 325 325 raise ConfigError, "Invalid configuration file : %s" % msg 326 326 try : 327 # sectionbranches = [ (k, v) for (k, v) in self.config.items(printqueuename) if k.startswith(branchbasename) ]328 327 sectionbranches = [ (k, self.config.get(printqueuename, k)) for k in self.config.options(printqueuename) if k.startswith(branchbasename) ] 329 328 except ConfigParser.NoSectionError, msg : … … 581 580 signal.signal(signal.SIGTERM, self.sigtermHandler) 582 581 serialize = self.isTrue(self.getPrintQueueOption(self.PrinterName, "serialize", ignore=1)) 583 self.pipe = os.pipe()582 self.pipes = { 0: os.pipe() } 584 583 for branchtype in ["prehook", "tee", "posthook"] : 585 584 if branchtype == "posthook" : 586 os.close(self.pipe [1])585 os.close(self.pipes[0][1]) 587 586 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]) 588 594 status = self.runCommands(branchtype, branches, serialize) 589 595 if status : … … 596 602 break # We don't want to execute tees or posthooks in this case 597 603 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 603 610 if not exitcode : 604 611 self.logInfo("OK") … … 607 614 return exitcode 608 615 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 2005616 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 612 619 pid = os.fork() 613 620 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) 618 632 return os.waitpid(pid, 0)[1] 619 633 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 620 646 def runCommands(self, btype, branches, serialize) : 621 647 """Runs the commands for a particular branch type.""" … … 638 664 if self.gotSigTerm : 639 665 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) 645 667 self.logDebug("Exit code for %s %s on printer %s is %s" % (btype, branch, self.PrinterName, retcode)) 646 668 if os.WIFEXITED(retcode) : … … 672 694 sys.exit(self.runOriginalBackend()) 673 695 else : 674 self.logDebug("Launching %s : %s" % (branch, command)) 675 retcode = os.system(command) 696 retcode = self.runCommand(branch, command) 676 697 if os.WIFEXITED(retcode) : 677 698 retcode = os.WEXITSTATUS(retcode)