[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$ |
---|
[1483] | 24 | # Revision 1.2 2004/05/18 14:49:23 jalet |
---|
| 25 | # Big code changes to completely remove the need for "requester" directives, |
---|
| 26 | # jsut use "hardware(... your previous requester directive's content ...)" |
---|
| 27 | # |
---|
[1475] | 28 | # Revision 1.1 2004/05/13 13:59:30 jalet |
---|
| 29 | # Code simplifications |
---|
| 30 | # |
---|
| 31 | # |
---|
| 32 | # |
---|
| 33 | |
---|
| 34 | import sys |
---|
| 35 | import os |
---|
| 36 | import popen2 |
---|
| 37 | import tempfile |
---|
| 38 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
| 39 | |
---|
| 40 | class Accounter(AccounterBase) : |
---|
| 41 | def computeJobSize(self) : |
---|
| 42 | """Feeds an external command with our datas to let it compute the job size, and return its value.""" |
---|
| 43 | temporary = None |
---|
| 44 | if self.filter.inputfile is None : |
---|
| 45 | infile = sys.stdin |
---|
| 46 | # we will have to duplicate our standard input |
---|
| 47 | temporary = tempfile.TemporaryFile() |
---|
| 48 | else : |
---|
| 49 | infile = open(self.filter.inputfile, "rb") |
---|
| 50 | |
---|
[1483] | 51 | # launches software accounter |
---|
[1475] | 52 | # TODO : USE tempfile.mkstemp() instead ! Needs some work ! |
---|
| 53 | infilename = tempfile.mktemp() |
---|
| 54 | outfilename = tempfile.mktemp() |
---|
| 55 | errfilename = tempfile.mktemp() |
---|
| 56 | |
---|
| 57 | try : |
---|
| 58 | # feed it with our data |
---|
| 59 | fakeinput = open(infilename, "wb") |
---|
| 60 | data = infile.read(256*1024) |
---|
| 61 | while data : |
---|
| 62 | fakeinput.write(data) |
---|
| 63 | if temporary is not None : |
---|
| 64 | temporary.write(data) |
---|
| 65 | data = infile.read(256*1024) |
---|
| 66 | fakeinput.close() |
---|
| 67 | |
---|
| 68 | # launches child process |
---|
| 69 | command = "%s <%s >%s 2>%s" % (self.arguments, infilename, outfilename, errfilename) |
---|
| 70 | retcode = os.system(command) |
---|
| 71 | |
---|
| 72 | # check exit status |
---|
| 73 | if (os.WIFEXITED(retcode) and not os.WEXITSTATUS(retcode)) or os.stat(errfilename) : |
---|
[1483] | 74 | # tries to extract the job size from the software accounter's |
---|
[1475] | 75 | # standard output |
---|
| 76 | childoutput = open(outfilename, "r") |
---|
| 77 | try : |
---|
| 78 | pagecount = int(childoutput.readline().strip()) |
---|
| 79 | except (AttributeError, ValueError) : |
---|
[1483] | 80 | self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % self.arguments) |
---|
[1475] | 81 | pagecount = 0 |
---|
| 82 | childoutput.close() |
---|
| 83 | else : |
---|
[1483] | 84 | self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % self.arguments) |
---|
[1475] | 85 | pagecount = 0 |
---|
| 86 | os.remove(infilename) |
---|
| 87 | os.remove(outfilename) |
---|
| 88 | os.remove(errfilename) |
---|
| 89 | except IOError, msg : |
---|
| 90 | # TODO : temporary files may remain on the filesystem... |
---|
| 91 | msg = "%s : %s" % (self.arguments, msg) |
---|
[1483] | 92 | self.filter.logger.log_message(_("Unable to compute job size with accounter %s") % msg) |
---|
[1475] | 93 | pagecount = 0 |
---|
| 94 | |
---|
| 95 | if temporary is not None : |
---|
| 96 | # this is a copy of our previous standard input |
---|
| 97 | # flush, then rewind |
---|
| 98 | temporary.flush() |
---|
| 99 | temporary.seek(0, 0) |
---|
| 100 | # our temporary file will be used later if the |
---|
| 101 | # job is allowed. |
---|
| 102 | self.filter.inputfile = temporary |
---|
| 103 | else : |
---|
| 104 | infile.close() |
---|
| 105 | return pagecount |
---|
| 106 | |
---|