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