[1475] | 1 | # PyKota |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | # |
---|
| 4 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 5 | # |
---|
| 6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
| 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 |
---|
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 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.""" |
---|
[1584] | 32 | self.filter.printInfo(_("Launching SOFTWARE(%s)...") % self.arguments) |
---|
[2074] | 33 | if not self.arguments : |
---|
| 34 | pagecounter = self.filter.softwareJobSize # Optimize : already computed ! |
---|
| 35 | self.filter.logdebug("Internal software accounter said job is %s pages long." % repr(pagecounter)) |
---|
| 36 | else : |
---|
| 37 | MEGABYTE = 1024*1024 |
---|
| 38 | self.filter.jobdatastream.seek(0) |
---|
| 39 | child = popen2.Popen4(self.arguments) |
---|
| 40 | try : |
---|
| 41 | data = self.filter.jobdatastream.read(MEGABYTE) |
---|
| 42 | while data : |
---|
| 43 | child.tochild.write(data) |
---|
| 44 | data = self.filter.jobdatastream.read(MEGABYTE) |
---|
| 45 | child.tochild.flush() |
---|
| 46 | child.tochild.close() |
---|
| 47 | except (IOError, OSError), msg : |
---|
| 48 | msg = "%s : %s" % (self.arguments, msg) |
---|
| 49 | self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg) |
---|
[1475] | 50 | |
---|
[2074] | 51 | pagecounter = None |
---|
| 52 | try : |
---|
| 53 | answer = child.fromchild.read() |
---|
| 54 | except (IOError, OSError), msg : |
---|
| 55 | msg = "%s : %s" % (self.arguments, msg) |
---|
| 56 | self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg) |
---|
| 57 | else : |
---|
| 58 | lines = [l.strip() for l in answer.split("\n")] |
---|
| 59 | for i in range(len(lines)) : |
---|
| 60 | try : |
---|
| 61 | pagecounter = int(lines[i]) |
---|
| 62 | except (AttributeError, ValueError) : |
---|
| 63 | self.filter.printInfo(_("Line [%s] skipped in accounter's output. Trying again...") % lines[i]) |
---|
| 64 | else : |
---|
| 65 | break |
---|
| 66 | child.fromchild.close() |
---|
[1680] | 67 | |
---|
[2074] | 68 | try : |
---|
| 69 | status = child.wait() |
---|
| 70 | except OSError, msg : |
---|
| 71 | self.filter.printInfo(_("Problem while waiting for software accounter pid %s to exit : %s") % (child.pid, msg)) |
---|
| 72 | else : |
---|
| 73 | if os.WIFEXITED(status) : |
---|
| 74 | status = os.WEXITSTATUS(status) |
---|
| 75 | self.filter.printInfo(_("Software accounter %s exit code is %s") % (self.arguments, str(status))) |
---|
| 76 | |
---|
| 77 | if pagecounter is None : |
---|
| 78 | message = _("Unable to compute job size with accounter %s") % self.arguments |
---|
| 79 | if self.onerror == "CONTINUE" : |
---|
| 80 | self.filter.printInfo(message, "error") |
---|
| 81 | else : |
---|
| 82 | raise PyKotaAccounterError, message |
---|
| 83 | self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, repr(pagecounter))) |
---|
| 84 | |
---|
[1687] | 85 | return pagecounter or 0 |
---|