[1475] | 1 | # PyKota |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
[3133] | 6 | # (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com> |
---|
[1475] | 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
[2302] | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[1475] | 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
[2074] | 23 | # |
---|
[1475] | 24 | |
---|
| 25 | import os |
---|
| 26 | import popen2 |
---|
| 27 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
| 28 | |
---|
| 29 | class Accounter(AccounterBase) : |
---|
| 30 | def computeJobSize(self) : |
---|
| 31 | """Feeds an external command with our datas to let it compute the job size, and return its value.""" |
---|
[2635] | 32 | if (not self.isPreAccounter) and \ |
---|
| 33 | (self.filter.accounter.arguments == self.filter.preaccounter.arguments) : |
---|
| 34 | # if precomputing has been done and both accounter and preaccounter are |
---|
| 35 | # configured the same, no need to launch a second pass since we already |
---|
| 36 | # know the result. |
---|
| 37 | self.filter.logdebug("Precomputing pass told us that job is %s pages long." % self.filter.softwareJobSize) |
---|
| 38 | return self.filter.softwareJobSize # Optimize : already computed ! |
---|
[1680] | 39 | |
---|
[2635] | 40 | if self.arguments : |
---|
| 41 | self.filter.logdebug("Using external script %s to compute job's size." % self.arguments) |
---|
| 42 | return self.withExternalScript() |
---|
| 43 | else : |
---|
| 44 | self.filter.logdebug("Using internal parser to compute job's size.") |
---|
| 45 | return self.withInternalParser() |
---|
| 46 | |
---|
| 47 | def withInternalParser(self) : |
---|
| 48 | """Does software accounting through an external script.""" |
---|
| 49 | jobsize = 0 |
---|
| 50 | if self.filter.JobSizeBytes : |
---|
[2074] | 51 | try : |
---|
[2635] | 52 | from pkpgpdls import analyzer, pdlparser |
---|
| 53 | except ImportError : |
---|
[2909] | 54 | self.filter.printInfo("pkpgcounter is now distributed separately, please grab it from http://www.pykota.com/software/pkpgcounter", "error") |
---|
[2635] | 55 | self.filter.printInfo("Precomputed job size will be forced to 0 pages.", "error") |
---|
| 56 | else : |
---|
| 57 | infile = open(self.filter.DataFile, "rb") |
---|
| 58 | try : |
---|
| 59 | parser = analyzer.PDLAnalyzer(infile) |
---|
| 60 | jobsize = parser.getJobSize() |
---|
| 61 | except pdlparser.PDLParserError, msg : |
---|
| 62 | # Here we just log the failure, but |
---|
| 63 | # we finally ignore it and return 0 since this |
---|
| 64 | # computation is just an indication of what the |
---|
| 65 | # job's size MAY be. |
---|
| 66 | self.filter.printInfo(_("Unable to precompute the job's size with the generic PDL analyzer : %s") % msg, "warn") |
---|
| 67 | else : |
---|
| 68 | if self.filter.InputFile is not None : |
---|
| 69 | # when a filename is passed as an argument, the backend |
---|
| 70 | # must generate the correct number of copies. |
---|
| 71 | jobsize *= self.filter.Copies |
---|
| 72 | infile.close() |
---|
| 73 | return jobsize |
---|
[2074] | 74 | |
---|
[2635] | 75 | def withExternalScript(self) : |
---|
| 76 | """Does software accounting through an external script.""" |
---|
| 77 | self.filter.printInfo(_("Launching SOFTWARE(%s)...") % self.arguments) |
---|
| 78 | MEGABYTE = 1024*1024 |
---|
| 79 | infile = open(self.filter.DataFile, "rb") |
---|
| 80 | child = popen2.Popen4(self.arguments) |
---|
| 81 | try : |
---|
| 82 | data = infile.read(MEGABYTE) |
---|
| 83 | while data : |
---|
| 84 | child.tochild.write(data) |
---|
| 85 | data = infile.read(MEGABYTE) |
---|
| 86 | child.tochild.flush() |
---|
| 87 | child.tochild.close() |
---|
| 88 | except (IOError, OSError), msg : |
---|
| 89 | msg = "%s : %s" % (self.arguments, msg) |
---|
| 90 | self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg) |
---|
| 91 | infile.close() |
---|
| 92 | pagecounter = None |
---|
| 93 | try : |
---|
| 94 | answer = child.fromchild.read() |
---|
| 95 | except (IOError, OSError), msg : |
---|
| 96 | msg = "%s : %s" % (self.arguments, msg) |
---|
| 97 | self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg) |
---|
| 98 | else : |
---|
| 99 | lines = [l.strip() for l in answer.split("\n")] |
---|
| 100 | for i in range(len(lines)) : |
---|
| 101 | try : |
---|
| 102 | pagecounter = int(lines[i]) |
---|
| 103 | except (AttributeError, ValueError) : |
---|
| 104 | self.filter.printInfo(_("Line [%s] skipped in accounter's output. Trying again...") % lines[i]) |
---|
| 105 | else : |
---|
| 106 | break |
---|
| 107 | child.fromchild.close() |
---|
| 108 | |
---|
| 109 | try : |
---|
| 110 | status = child.wait() |
---|
| 111 | except OSError, msg : |
---|
| 112 | self.filter.printInfo(_("Problem while waiting for software accounter pid %s to exit : %s") % (child.pid, msg)) |
---|
| 113 | else : |
---|
| 114 | if os.WIFEXITED(status) : |
---|
| 115 | status = os.WEXITSTATUS(status) |
---|
| 116 | self.filter.printInfo(_("Software accounter %s exit code is %s") % (self.arguments, str(status))) |
---|
[2074] | 117 | |
---|
[2635] | 118 | if pagecounter is None : |
---|
| 119 | message = _("Unable to compute job size with accounter %s") % self.arguments |
---|
| 120 | if self.onerror == "CONTINUE" : |
---|
| 121 | self.filter.printInfo(message, "error") |
---|
| 122 | else : |
---|
| 123 | raise PyKotaAccounterError, message |
---|
| 124 | self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, repr(pagecounter))) |
---|
| 125 | |
---|
[3000] | 126 | pagecounter = pagecounter or 0 |
---|
| 127 | if self.filter.InputFile is not None : |
---|
| 128 | # when a filename is passed as an argument, the backend |
---|
| 129 | # must generate the correct number of copies. |
---|
| 130 | pagecounter *= self.filter.Copies |
---|
| 131 | |
---|
| 132 | return pagecounter |
---|