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.5 2003/07/07 11:49:24 jalet |
---|
24 | # Lots of small fixes with the help of PyChecker |
---|
25 | # |
---|
26 | # Revision 1.4 2003/06/25 14:10:01 jalet |
---|
27 | # Hey, it may work (edpykota --reset excepted) ! |
---|
28 | # |
---|
29 | # Revision 1.3 2003/05/27 23:00:21 jalet |
---|
30 | # Big rewrite of external accounting methods. |
---|
31 | # Should work well now. |
---|
32 | # |
---|
33 | # Revision 1.2 2003/05/13 13:54:20 jalet |
---|
34 | # Better handling of broken pipes |
---|
35 | # |
---|
36 | # Revision 1.1 2003/05/07 19:47:06 jalet |
---|
37 | # v1.07 Release of the Shame is out ! |
---|
38 | # |
---|
39 | # |
---|
40 | # |
---|
41 | |
---|
42 | import sys |
---|
43 | import os |
---|
44 | import popen2 |
---|
45 | import tempfile |
---|
46 | from pykota.accounter import AccounterBase, PyKotaAccounterError |
---|
47 | |
---|
48 | class Accounter(AccounterBase) : |
---|
49 | def doAccounting(self, printer, user) : |
---|
50 | """Deletgates the computation of the job size to an external command. |
---|
51 | |
---|
52 | The command must print the job size on its standard output and exit successfully. |
---|
53 | """ |
---|
54 | # get the job size, which is real job size * number of copies. |
---|
55 | jobsize = self.getJobSize() * self.filter.copies |
---|
56 | |
---|
57 | # get last job information for this printer |
---|
58 | if not printer.LastJob.Exists : |
---|
59 | # The printer hasn't been used yet, from PyKota's point of view |
---|
60 | counterbeforejob = 0 |
---|
61 | else : |
---|
62 | # get last job size and page counter from Quota Storage |
---|
63 | # Last lifetime page counter before actual job is |
---|
64 | # last page counter + last job size |
---|
65 | counterbeforejob = int(printer.LastJob.PrinterPageCounter or 0) + int(printer.LastJob.JobSize or 0) |
---|
66 | |
---|
67 | # Is the current user allowed to print at all ? |
---|
68 | userpquota = self.filter.storage.getUserPQuota(user, printer) |
---|
69 | action = self.filter.warnUserPQuota(userpquota) |
---|
70 | |
---|
71 | # update the quota for the current user on this printer, if allowed to print |
---|
72 | if action == "DENY" : |
---|
73 | jobsize = 0 |
---|
74 | else : |
---|
75 | userpquota.increasePagesUsage(jobsize) |
---|
76 | |
---|
77 | # adds the current job to history |
---|
78 | printer.addJobToHistory(self.filter.jobid, user, counterbeforejob, action, jobsize) |
---|
79 | |
---|
80 | return action |
---|
81 | |
---|
82 | def getJobSize(self) : |
---|
83 | """Feeds an external command with our datas to let it compute the job size, and return its value.""" |
---|
84 | temporary = None |
---|
85 | if self.filter.inputfile is None : |
---|
86 | infile = sys.stdin |
---|
87 | # we will have to duplicate our standard input |
---|
88 | temporary = tempfile.TemporaryFile() |
---|
89 | else : |
---|
90 | infile = open(self.filter.inputfile, "rb") |
---|
91 | |
---|
92 | # launches external accounter |
---|
93 | try : |
---|
94 | infilename = tempfile.mkstemp() # Python 2.3 |
---|
95 | except AttributeError : |
---|
96 | infilename = tempfile.mktemp() |
---|
97 | try : |
---|
98 | outfilename = tempfile.mkstemp() # Python 2.3 |
---|
99 | except AttributeError : |
---|
100 | outfilename = tempfile.mktemp() |
---|
101 | try : |
---|
102 | errfilename = tempfile.mkstemp() # Python 2.3 |
---|
103 | except AttributeError : |
---|
104 | errfilename = tempfile.mktemp() |
---|
105 | |
---|
106 | try : |
---|
107 | # feed it with our data |
---|
108 | fakeinput = open(infilename, "wb") |
---|
109 | data = infile.read(256*1024) |
---|
110 | while data : |
---|
111 | fakeinput.write(data) |
---|
112 | if temporary is not None : |
---|
113 | temporary.write(data) |
---|
114 | data = infile.read(256*1024) |
---|
115 | fakeinput.close() |
---|
116 | |
---|
117 | # launches child process |
---|
118 | command = "%s <%s >%s 2>%s" % (self.arguments, infilename, outfilename, errfilename) |
---|
119 | retcode = os.system(command) |
---|
120 | |
---|
121 | # check exit status |
---|
122 | if (os.WIFEXITED(retcode) and not os.WEXITSTATUS(retcode)) or os.stat(errfilename) : |
---|
123 | # tries to extract the job size from the external accounter's |
---|
124 | # standard output |
---|
125 | childoutput = open(outfilename, "r") |
---|
126 | try : |
---|
127 | pagecount = int(childoutput.readline().strip()) |
---|
128 | except (AttributeError, ValueError) : |
---|
129 | self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments) |
---|
130 | pagecount = 0 |
---|
131 | childoutput.close() |
---|
132 | else : |
---|
133 | self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % self.arguments) |
---|
134 | pagecount = 0 |
---|
135 | os.remove(infilename) |
---|
136 | os.remove(outfilename) |
---|
137 | os.remove(errfilename) |
---|
138 | except IOError, msg : |
---|
139 | # TODO : temporary files may remain on the filesystem... |
---|
140 | msg = "%s : %s" % (self.arguments, msg) |
---|
141 | self.filter.logger.log_message(_("Unable to compute job size with external accounter %s") % msg) |
---|
142 | pagecount = 0 |
---|
143 | |
---|
144 | if temporary is not None : |
---|
145 | # this is a copy of our previous standard input |
---|
146 | # flush, then rewind |
---|
147 | temporary.flush() |
---|
148 | temporary.seek(0, 0) |
---|
149 | # our temporary file will be used later if the |
---|
150 | # job is allowed. |
---|
151 | self.filter.inputfile = temporary |
---|
152 | else : |
---|
153 | infile.close() |
---|
154 | |
---|
155 | return pagecount |
---|
156 | |
---|