1 | # PyKota |
---|
2 | # |
---|
3 | # PyKota - Print Quotas for CUPS and LPRng |
---|
4 | # |
---|
5 | # (c) 2003 Jerome Alet <alet@librelogiciel.com> |
---|
6 | # This program is free software; you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation; either version 2 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program; if not, write to the Free Software |
---|
18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | # $Log$ |
---|
23 | # Revision 1.2 2003/05/13 13:54:20 jalet |
---|
24 | # Better handling of broken pipes |
---|
25 | # |
---|
26 | # Revision 1.1 2003/05/07 19:47:06 jalet |
---|
27 | # v1.07 Release of the Shame is out ! |
---|
28 | # |
---|
29 | # |
---|
30 | # |
---|
31 | |
---|
32 | import sys |
---|
33 | import os |
---|
34 | import popen2 |
---|
35 | import tempfile |
---|
36 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
37 | |
---|
38 | class Accounter(AccounterBase) : |
---|
39 | def doAccounting(self, printerid, userid) : |
---|
40 | """Deletgates the computation of the job size to an external command. |
---|
41 | |
---|
42 | The command must print the job size on its standard output and exit successfully. |
---|
43 | """ |
---|
44 | # get the job size |
---|
45 | jobsize = self.getJobSize() |
---|
46 | |
---|
47 | # get last job information for this printer |
---|
48 | pgc = self.filter.storage.getPrinterPageCounter(printerid) |
---|
49 | if pgc is None : |
---|
50 | # The printer hasn't been used yet, from PyKota's point of view |
---|
51 | counterbeforejob = 0 |
---|
52 | else : |
---|
53 | # get last job size and page counter from Quota Storage |
---|
54 | # Last lifetime page counter before actual job is |
---|
55 | # last page counter + last job size |
---|
56 | counterbeforejob = (pgc["pagecounter"] or 0) + (pgc["jobsize"] or 0) |
---|
57 | |
---|
58 | # Is the current user allowed to print at all ? |
---|
59 | action = self.filter.warnUserPQuota(self.filter.username, self.filter.printername) |
---|
60 | |
---|
61 | # update the quota for the current user on this printer, if allowed to print |
---|
62 | if action == "DENY" : |
---|
63 | jobsize = 0 |
---|
64 | else : |
---|
65 | self.filter.storage.updateUserPQuota(userid, printerid, jobsize) |
---|
66 | |
---|
67 | # adds the current job to history |
---|
68 | self.filter.storage.addJobToHistory(self.filter.jobid, self.filter.storage.getUserId(self.filter.username), printerid, counterbeforejob, action, jobsize) |
---|
69 | |
---|
70 | return action |
---|
71 | |
---|
72 | def getJobSize(self) : |
---|
73 | """Feeds an external command with our datas to let it compute the job size, and return its value.""" |
---|
74 | temporary = None |
---|
75 | if self.filter.inputfile is None : |
---|
76 | infile = sys.stdin |
---|
77 | # we will have to duplicate our standard input |
---|
78 | temporary = tempfile.TemporaryFile() |
---|
79 | else : |
---|
80 | infile = open(self.filter.inputfile, "rb") |
---|
81 | |
---|
82 | # launches external accounter |
---|
83 | try : |
---|
84 | process = popen2.Popen3("%s" % self.arguments) |
---|
85 | |
---|
86 | # feed it with our data |
---|
87 | data = infile.read(256*1024) |
---|
88 | while data : |
---|
89 | process.tochild.write(data) |
---|
90 | temporary.write(data) |
---|
91 | data = infile.read(256*1024) |
---|
92 | process.tochild.close() |
---|
93 | |
---|
94 | # wait for child process to exit (or die) |
---|
95 | retcode = process.wait() |
---|
96 | |
---|
97 | # check exit status |
---|
98 | if os.WIFEXITED(retcode) and not os.WEXITSTATUS(retcode) : |
---|
99 | # tries to extract the job size from the external accounter's |
---|
100 | # standard output |
---|
101 | try : |
---|
102 | pagecount = int(process.fromchild.readline().strip()) |
---|
103 | except (AttributeError, ValueError) : |
---|
104 | self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments) |
---|
105 | pagecount = 0 |
---|
106 | else : |
---|
107 | self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments) |
---|
108 | pagecount = 0 |
---|
109 | process.fromchild.close() |
---|
110 | except IOError, msg : |
---|
111 | msg = "%s : %s" % (self.arguments, msg) |
---|
112 | self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % msg) |
---|
113 | pagecount = 0 |
---|
114 | |
---|
115 | if temporary is not None : |
---|
116 | # this is a copy of our previous standard input |
---|
117 | # flush, then rewind |
---|
118 | temporary.flush() |
---|
119 | temporary.seek(0, 0) |
---|
120 | # our temporary file will be used later if the |
---|
121 | # job is allowed. |
---|
122 | self.filter.inputfile = temporary |
---|
123 | else : |
---|
124 | infile.close() |
---|
125 | |
---|
126 | return pagecount |
---|
127 | |
---|