776 | | |
777 | | # then deal with signals |
778 | | # CUPS backends always ignore SIGPIPE and ignore SIGTERM |
779 | | # when printing from their stdin |
780 | | # Here we have to catch them correctly, and pass |
781 | | # SIGTERM to any child if needed. |
782 | | self.gotSigTerm = self.gotSigChld = self.gotSigPipe = 0 |
783 | | signal.signal(signal.SIGTERM, self.sigterm_handler) |
784 | | signal.signal(signal.SIGCHLD, self.sigchld_handler) |
785 | | signal.signal(signal.SIGPIPE, self.sigpipe_handler) |
786 | | |
| 786 | self.jobdatastream = self.openJobDataStream() |
| 787 | self.softwareJobSize = self.precomputeJobSize() |
| 788 | self.logdebug("Precomputed job's size is : %s pages" % self.softwareJobSize) |
| 789 | |
| 790 | def openJobDataStream(self) : |
| 791 | """Opens the file which contains the job's datas.""" |
| 792 | if self.preserveinputfile is None : |
| 793 | # Job comes from sys.stdin, but this is not |
| 794 | # seekable and complexifies our task, so create |
| 795 | # a temporary file and use it instead |
| 796 | self.logdebug("Duplicating data stream from stdin to temporary file") |
| 797 | MEGABYTE = 1024*1024 |
| 798 | infile = tempfile.TemporaryFile() |
| 799 | while 1 : |
| 800 | data = sys.stdin.read(MEGABYTE) |
| 801 | if not data : |
| 802 | break |
| 803 | infile.write(data) |
| 804 | infile.flush() |
| 805 | infile.seek(0) |
| 806 | return infile |
| 807 | else : |
| 808 | # real file, just open it |
| 809 | self.logdebug("Opening data stream %s" % self.preserveinputfile) |
| 810 | return open(self.preserveinputfile, "rb") |
| 811 | |
| 812 | def closeJobDataStream(self) : |
| 813 | """Closes the file which contains the job's datas.""" |
| 814 | self.logdebug("Closing data stream.") |
| 815 | try : |
| 816 | self.jobdatastream.close() |
| 817 | except : |
| 818 | pass |
| 819 | |
| 820 | def precomputeJobSize(self) : |
| 821 | """Computes the job size with a software method.""" |
| 822 | try : |
| 823 | parser = pdlanalyzer.PDLAnalyzer(self.jobdatastream) |
| 824 | return parser.getJobSize() |
| 825 | except pdlanalyzer.PDLAnalyzerError, msg : |
| 826 | # Here we just log the failure, but |
| 827 | # we finally ignore it and return 0 since this |
| 828 | # computation is just an indication of what the |
| 829 | # job's size MAY be. |
| 830 | self.logger.log_message(_("Unable to precompute the job's size with the generic PDL analyzer."), "warn") |
| 831 | return 0 |
| 832 | |