[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$ |
---|
[1665] | 24 | # Revision 1.7 2004/08/06 13:45:51 jalet |
---|
| 25 | # Fixed french translation problem. |
---|
| 26 | # Fixed problem with group quotas and strict enforcement. |
---|
| 27 | # |
---|
[1584] | 28 | # Revision 1.6 2004/07/01 19:56:43 jalet |
---|
| 29 | # Better dispatching of error messages |
---|
| 30 | # |
---|
[1536] | 31 | # Revision 1.5 2004/06/10 22:42:06 jalet |
---|
| 32 | # Better messages in logs |
---|
| 33 | # |
---|
[1514] | 34 | # Revision 1.4 2004/06/02 21:51:14 jalet |
---|
| 35 | # Moved the sigterm capturing elsewhere |
---|
| 36 | # |
---|
[1495] | 37 | # Revision 1.3 2004/05/24 22:45:49 jalet |
---|
| 38 | # New 'enforcement' directive added |
---|
| 39 | # Polling loop improvements |
---|
| 40 | # |
---|
[1483] | 41 | # Revision 1.2 2004/05/18 14:49:23 jalet |
---|
| 42 | # Big code changes to completely remove the need for "requester" directives, |
---|
| 43 | # jsut use "hardware(... your previous requester directive's content ...)" |
---|
| 44 | # |
---|
[1475] | 45 | # Revision 1.1 2004/05/13 13:59:30 jalet |
---|
| 46 | # Code simplifications |
---|
| 47 | # |
---|
| 48 | # |
---|
| 49 | |
---|
| 50 | import sys |
---|
| 51 | import os |
---|
| 52 | import popen2 |
---|
| 53 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
| 54 | |
---|
| 55 | class Accounter(AccounterBase) : |
---|
| 56 | def computeJobSize(self) : |
---|
| 57 | """Feeds an external command with our datas to let it compute the job size, and return its value.""" |
---|
[1584] | 58 | self.filter.printInfo(_("Launching SOFTWARE(%s)...") % self.arguments) |
---|
[1495] | 59 | MEGABYTE = 1024*1024 |
---|
| 60 | self.filter.jobdatastream.seek(0) |
---|
| 61 | child = popen2.Popen4(self.arguments) |
---|
[1475] | 62 | try : |
---|
[1495] | 63 | data = self.filter.jobdatastream.read(MEGABYTE) |
---|
[1475] | 64 | while data : |
---|
[1495] | 65 | child.tochild.write(data) |
---|
| 66 | data = self.filter.jobdatastream.read(MEGABYTE) |
---|
| 67 | child.tochild.flush() |
---|
| 68 | child.tochild.close() |
---|
| 69 | except (IOError, OSError), msg : |
---|
| 70 | msg = "%s : %s" % (self.arguments, msg) |
---|
[1584] | 71 | self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg) |
---|
[1475] | 72 | |
---|
[1495] | 73 | pagecount = 0 |
---|
| 74 | try : |
---|
| 75 | pagecount = int(child.fromchild.readline().strip()) |
---|
| 76 | except (AttributeError, ValueError) : |
---|
[1584] | 77 | self.filter.printInfo(_("Unable to compute job size with accounter %s") % self.arguments) |
---|
[1495] | 78 | except (IOError, OSError), msg : |
---|
[1475] | 79 | msg = "%s : %s" % (self.arguments, msg) |
---|
[1584] | 80 | self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg) |
---|
[1495] | 81 | child.fromchild.close() |
---|
| 82 | |
---|
| 83 | try : |
---|
| 84 | retcode = child.wait() |
---|
| 85 | except OSError, msg : |
---|
[1584] | 86 | self.filter.printInfo(_("Problem while waiting for software accounter pid %s to exit : %s") % (child.pid, msg)) |
---|
[1495] | 87 | else : |
---|
| 88 | if os.WIFEXITED(retcode) : |
---|
| 89 | status = os.WEXITSTATUS(retcode) |
---|
| 90 | else : |
---|
| 91 | status = retcode |
---|
[1665] | 92 | self.filter.printInfo(_("Software accounter %s exit code is %s") % (self.arguments, str(status))) |
---|
[1495] | 93 | self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, pagecount)) |
---|
[1475] | 94 | return pagecount |
---|
| 95 | |
---|