[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 | # |
---|
| 23 | # $Log$ |
---|
[1495] | 24 | # Revision 1.3 2004/05/24 22:45:49 jalet |
---|
| 25 | # New 'enforcement' directive added |
---|
| 26 | # Polling loop improvements |
---|
| 27 | # |
---|
[1483] | 28 | # Revision 1.2 2004/05/18 14:49:23 jalet |
---|
| 29 | # Big code changes to completely remove the need for "requester" directives, |
---|
| 30 | # jsut use "hardware(... your previous requester directive's content ...)" |
---|
| 31 | # |
---|
[1475] | 32 | # Revision 1.1 2004/05/13 13:59:30 jalet |
---|
| 33 | # Code simplifications |
---|
| 34 | # |
---|
| 35 | # |
---|
| 36 | |
---|
| 37 | import sys |
---|
| 38 | import os |
---|
| 39 | import popen2 |
---|
| 40 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
| 41 | |
---|
| 42 | class Accounter(AccounterBase) : |
---|
| 43 | def computeJobSize(self) : |
---|
| 44 | """Feeds an external command with our datas to let it compute the job size, and return its value.""" |
---|
[1495] | 45 | self.filter.logdebug("Launching software accounter %s" % self.arguments) |
---|
| 46 | MEGABYTE = 1024*1024 |
---|
| 47 | self.filter.jobdatastream.seek(0) |
---|
| 48 | child = popen2.Popen4(self.arguments) |
---|
[1475] | 49 | try : |
---|
[1495] | 50 | data = self.filter.jobdatastream.read(MEGABYTE) |
---|
[1475] | 51 | while data : |
---|
[1495] | 52 | child.tochild.write(data) |
---|
| 53 | data = self.filter.jobdatastream.read(MEGABYTE) |
---|
| 54 | child.tochild.flush() |
---|
| 55 | child.tochild.close() |
---|
| 56 | except (IOError, OSError), msg : |
---|
| 57 | msg = "%s : %s" % (self.arguments, msg) |
---|
| 58 | self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % msg) |
---|
[1475] | 59 | |
---|
[1495] | 60 | pagecount = 0 |
---|
| 61 | try : |
---|
| 62 | pagecount = int(child.fromchild.readline().strip()) |
---|
| 63 | except (AttributeError, ValueError) : |
---|
| 64 | self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % self.arguments) |
---|
| 65 | except (IOError, OSError), msg : |
---|
[1475] | 66 | msg = "%s : %s" % (self.arguments, msg) |
---|
[1483] | 67 | self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % msg) |
---|
[1495] | 68 | child.fromchild.close() |
---|
| 69 | |
---|
| 70 | try : |
---|
| 71 | retcode = child.wait() |
---|
| 72 | except OSError, msg : |
---|
| 73 | self.filter.logger.log_message(_("Problem while waiting for software accounter pid %s to exit") % child.pid) |
---|
| 74 | else : |
---|
| 75 | if os.WIFEXITED(retcode) : |
---|
| 76 | status = os.WEXITSTATUS(retcode) |
---|
| 77 | else : |
---|
| 78 | status = retcode |
---|
| 79 | self.filter.logger.log_message(_("Software accounter %s exit code is %s") % (self.arguments, repr(retcode))) |
---|
| 80 | self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, pagecount)) |
---|
[1475] | 81 | return pagecount |
---|
| 82 | |
---|