[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$ |
---|
[1680] | 24 | # Revision 1.9 2004/08/25 22:34:39 jalet |
---|
| 25 | # Now both software and hardware accounting raise an exception when no valid |
---|
| 26 | # result can be extracted from the subprocess' output. |
---|
| 27 | # Hardware accounting now reads subprocess' output until an integer is read |
---|
| 28 | # or data is exhausted : it now behaves just like software accounting in this |
---|
| 29 | # aspect. |
---|
| 30 | # |
---|
[1678] | 31 | # Revision 1.8 2004/08/22 14:04:47 jalet |
---|
| 32 | # Tries to fix problem with subprocesses outputting more datas than needed |
---|
| 33 | # |
---|
[1665] | 34 | # Revision 1.7 2004/08/06 13:45:51 jalet |
---|
| 35 | # Fixed french translation problem. |
---|
| 36 | # Fixed problem with group quotas and strict enforcement. |
---|
| 37 | # |
---|
[1584] | 38 | # Revision 1.6 2004/07/01 19:56:43 jalet |
---|
| 39 | # Better dispatching of error messages |
---|
| 40 | # |
---|
[1536] | 41 | # Revision 1.5 2004/06/10 22:42:06 jalet |
---|
| 42 | # Better messages in logs |
---|
| 43 | # |
---|
[1514] | 44 | # Revision 1.4 2004/06/02 21:51:14 jalet |
---|
| 45 | # Moved the sigterm capturing elsewhere |
---|
| 46 | # |
---|
[1495] | 47 | # Revision 1.3 2004/05/24 22:45:49 jalet |
---|
| 48 | # New 'enforcement' directive added |
---|
| 49 | # Polling loop improvements |
---|
| 50 | # |
---|
[1483] | 51 | # Revision 1.2 2004/05/18 14:49:23 jalet |
---|
| 52 | # Big code changes to completely remove the need for "requester" directives, |
---|
| 53 | # jsut use "hardware(... your previous requester directive's content ...)" |
---|
| 54 | # |
---|
[1475] | 55 | # Revision 1.1 2004/05/13 13:59:30 jalet |
---|
| 56 | # Code simplifications |
---|
| 57 | # |
---|
| 58 | # |
---|
| 59 | |
---|
| 60 | import sys |
---|
| 61 | import os |
---|
| 62 | import popen2 |
---|
| 63 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
| 64 | |
---|
| 65 | class Accounter(AccounterBase) : |
---|
| 66 | def computeJobSize(self) : |
---|
| 67 | """Feeds an external command with our datas to let it compute the job size, and return its value.""" |
---|
[1584] | 68 | self.filter.printInfo(_("Launching SOFTWARE(%s)...") % self.arguments) |
---|
[1495] | 69 | MEGABYTE = 1024*1024 |
---|
| 70 | self.filter.jobdatastream.seek(0) |
---|
| 71 | child = popen2.Popen4(self.arguments) |
---|
[1475] | 72 | try : |
---|
[1495] | 73 | data = self.filter.jobdatastream.read(MEGABYTE) |
---|
[1475] | 74 | while data : |
---|
[1495] | 75 | child.tochild.write(data) |
---|
| 76 | data = self.filter.jobdatastream.read(MEGABYTE) |
---|
| 77 | child.tochild.flush() |
---|
| 78 | child.tochild.close() |
---|
| 79 | except (IOError, OSError), msg : |
---|
| 80 | msg = "%s : %s" % (self.arguments, msg) |
---|
[1584] | 81 | self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg) |
---|
[1475] | 82 | |
---|
[1680] | 83 | pagecounter = None |
---|
[1495] | 84 | try : |
---|
[1678] | 85 | answer = child.fromchild.read() |
---|
[1495] | 86 | except (IOError, OSError), msg : |
---|
[1475] | 87 | msg = "%s : %s" % (self.arguments, msg) |
---|
[1584] | 88 | self.filter.printInfo(_("Unable to compute job size with accounter %s") % msg) |
---|
[1678] | 89 | else : |
---|
| 90 | lines = [l.strip() for l in answer.split("\n")] |
---|
| 91 | for i in range(len(lines)) : |
---|
| 92 | try : |
---|
[1680] | 93 | pagecounter = int(lines[i]) |
---|
[1678] | 94 | except (AttributeError, ValueError) : |
---|
[1680] | 95 | self.filter.printInfo(_("Line [%s] skipped in accounter's output. Trying again...") % lines[i]) |
---|
[1678] | 96 | else : |
---|
| 97 | break |
---|
[1495] | 98 | child.fromchild.close() |
---|
| 99 | |
---|
| 100 | try : |
---|
| 101 | retcode = child.wait() |
---|
| 102 | except OSError, msg : |
---|
[1584] | 103 | self.filter.printInfo(_("Problem while waiting for software accounter pid %s to exit : %s") % (child.pid, msg)) |
---|
[1495] | 104 | else : |
---|
| 105 | if os.WIFEXITED(retcode) : |
---|
| 106 | status = os.WEXITSTATUS(retcode) |
---|
| 107 | else : |
---|
| 108 | status = retcode |
---|
[1665] | 109 | self.filter.printInfo(_("Software accounter %s exit code is %s") % (self.arguments, str(status))) |
---|
[1475] | 110 | |
---|
[1680] | 111 | if pagecounter is None : |
---|
| 112 | raise PyKotaAccounterError, _("Unable to compute job size with accounter %s") % self.arguments |
---|
| 113 | |
---|
| 114 | self.filter.logdebug("Software accounter %s said job is %s pages long." % (self.arguments, pagecounter)) |
---|
| 115 | return pagecounter |
---|
| 116 | |
---|