1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # PyKota Print Quota Reports generator |
---|
5 | # |
---|
6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
7 | # |
---|
8 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
9 | # This program is free software; you can redistribute it and/or modify |
---|
10 | # it under the terms of the GNU General Public License as published by |
---|
11 | # the Free Software Foundation; either version 2 of the License, or |
---|
12 | # (at your option) any later version. |
---|
13 | # |
---|
14 | # This program is distributed in the hope that it will be useful, |
---|
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | # GNU General Public License for more details. |
---|
18 | # |
---|
19 | # You should have received a copy of the GNU General Public License |
---|
20 | # along with this program; if not, write to the Free Software |
---|
21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | # |
---|
23 | # $Id$ |
---|
24 | # |
---|
25 | # $Log$ |
---|
26 | # Revision 1.52 2004/10/11 22:53:05 jalet |
---|
27 | # Postponed string interpolation to help message's output method |
---|
28 | # |
---|
29 | # Revision 1.51 2004/10/11 12:49:06 jalet |
---|
30 | # Renders help translatable |
---|
31 | # |
---|
32 | # Revision 1.50 2004/10/06 10:05:47 jalet |
---|
33 | # Minor changes to allow any PyKota administrator to launch enhanced versions |
---|
34 | # of the commands, and not only the root user. |
---|
35 | # |
---|
36 | # Revision 1.49 2004/06/18 13:34:49 jalet |
---|
37 | # Now all tracebacks include PyKota's version number |
---|
38 | # |
---|
39 | # Revision 1.48 2004/06/07 18:43:41 jalet |
---|
40 | # Fixed over-verbose exits when displaying help or version number |
---|
41 | # |
---|
42 | # Revision 1.47 2004/06/03 21:50:34 jalet |
---|
43 | # Improved error logging. |
---|
44 | # crashrecipient directive added. |
---|
45 | # Now exports the job's size in bytes too. |
---|
46 | # |
---|
47 | # Revision 1.46 2004/01/08 14:10:32 jalet |
---|
48 | # Copyright year changed. |
---|
49 | # |
---|
50 | # Revision 1.45 2003/10/09 21:25:25 jalet |
---|
51 | # Multiple printer names or wildcards can be passed on the command line |
---|
52 | # separated with commas. |
---|
53 | # Beta phase. |
---|
54 | # |
---|
55 | # Revision 1.44 2003/10/07 09:07:27 jalet |
---|
56 | # Character encoding added to please latest version of Python |
---|
57 | # |
---|
58 | # Revision 1.43 2003/08/18 16:35:28 jalet |
---|
59 | # New pychecker pass, on the tools this time. |
---|
60 | # |
---|
61 | # Revision 1.42 2003/07/29 20:55:17 jalet |
---|
62 | # 1.14 is out ! |
---|
63 | # |
---|
64 | # Revision 1.41 2003/07/07 12:19:52 jalet |
---|
65 | # Now repykota and warnpykota --groups check the groups the user is a member of |
---|
66 | # in the print quota database, not in the system passwd/group files |
---|
67 | # |
---|
68 | # Revision 1.40 2003/06/30 12:46:15 jalet |
---|
69 | # Extracted reporting code. |
---|
70 | # |
---|
71 | # Revision 1.39 2003/06/25 14:10:01 jalet |
---|
72 | # Hey, it may work (edpykota --reset excepted) ! |
---|
73 | # |
---|
74 | # Revision 1.38 2003/06/15 22:26:52 jalet |
---|
75 | # More work on LDAP |
---|
76 | # |
---|
77 | # Revision 1.37 2003/06/10 16:37:54 jalet |
---|
78 | # Deletion of the second user which is not needed anymore. |
---|
79 | # Added a debug configuration field in /etc/pykota.conf |
---|
80 | # All queries can now be sent to the logger in debug mode, this will |
---|
81 | # greatly help improve performance when time for this will come. |
---|
82 | # |
---|
83 | # Revision 1.36 2003/06/06 14:21:08 jalet |
---|
84 | # New LDAP schema. |
---|
85 | # Small bug fixes. |
---|
86 | # |
---|
87 | # Revision 1.35 2003/05/28 13:56:24 jalet |
---|
88 | # Unneeded code. |
---|
89 | # |
---|
90 | # Revision 1.34 2003/04/29 22:03:38 jalet |
---|
91 | # Better error handling. |
---|
92 | # |
---|
93 | # Revision 1.33 2003/04/28 08:25:48 jalet |
---|
94 | # Small display bug in repykota was fixed. |
---|
95 | # |
---|
96 | # Revision 1.32 2003/04/23 22:13:56 jalet |
---|
97 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
98 | # |
---|
99 | # Revision 1.31 2003/04/23 09:58:17 jalet |
---|
100 | # Prices per page/job are now displayed |
---|
101 | # |
---|
102 | # Revision 1.30 2003/04/18 08:34:51 jalet |
---|
103 | # Minor fix. |
---|
104 | # |
---|
105 | # Revision 1.29 2003/04/18 08:29:30 jalet |
---|
106 | # Minor bug fix |
---|
107 | # |
---|
108 | # Revision 1.28 2003/04/17 09:36:30 jalet |
---|
109 | # Bad alignment in output |
---|
110 | # |
---|
111 | # Revision 1.27 2003/04/17 09:26:21 jalet |
---|
112 | # repykota now reports account balances too. |
---|
113 | # |
---|
114 | # Revision 1.26 2003/04/16 12:35:49 jalet |
---|
115 | # Groups quota work now ! |
---|
116 | # |
---|
117 | # Revision 1.25 2003/04/15 11:30:57 jalet |
---|
118 | # More work done on money print charging. |
---|
119 | # Minor bugs corrected. |
---|
120 | # All tools now access to the storage as priviledged users, repykota excepted. |
---|
121 | # |
---|
122 | # Revision 1.24 2003/04/14 20:05:20 jalet |
---|
123 | # Reversed test. |
---|
124 | # |
---|
125 | # Revision 1.23 2003/04/11 14:42:54 jalet |
---|
126 | # Translations |
---|
127 | # |
---|
128 | # Revision 1.21 2003/04/10 21:47:20 jalet |
---|
129 | # Job history added. Upgrade script neutralized for now ! |
---|
130 | # |
---|
131 | # Revision 1.20 2003/04/08 21:31:39 jalet |
---|
132 | # (anything or 0) = anything !!! Go back to school Jerome ! |
---|
133 | # |
---|
134 | # Revision 1.19 2003/04/08 21:13:44 jalet |
---|
135 | # Prepare --groups option to work. |
---|
136 | # |
---|
137 | # Revision 1.18 2003/04/08 21:10:18 jalet |
---|
138 | # Checks --groups option presence instead of --users because --users is the default. |
---|
139 | # |
---|
140 | # Revision 1.17 2003/03/29 13:45:27 jalet |
---|
141 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
142 | # Two README files were added. |
---|
143 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
144 | # |
---|
145 | # Revision 1.16 2003/03/09 23:56:21 jalet |
---|
146 | # Option noquota added to do accounting only. |
---|
147 | # |
---|
148 | # Revision 1.15 2003/03/09 23:39:14 jalet |
---|
149 | # Simplified translations. |
---|
150 | # |
---|
151 | # Revision 1.14 2003/02/27 09:04:02 jalet |
---|
152 | # Missing translation |
---|
153 | # |
---|
154 | # Revision 1.13 2003/02/27 08:44:01 jalet |
---|
155 | # Check to see if the printer was ever used at all, and displays "unknown" |
---|
156 | # as the pagecounter value in this casCheck to see if the printer was ever used at all, and displays "unknown" |
---|
157 | # as the pagecounter value in this case. |
---|
158 | # |
---|
159 | # Revision 1.12 2003/02/17 23:02:23 jalet |
---|
160 | # getGraceDelay for printer |
---|
161 | # |
---|
162 | # Revision 1.11 2003/02/10 12:12:34 jalet |
---|
163 | # Translations. |
---|
164 | # |
---|
165 | # Revision 1.10 2003/02/10 12:07:30 jalet |
---|
166 | # Now repykota should output the recorded total page number for each printer too. |
---|
167 | # |
---|
168 | # Revision 1.9 2003/02/09 13:40:29 jalet |
---|
169 | # typo |
---|
170 | # |
---|
171 | # Revision 1.8 2003/02/09 12:56:53 jalet |
---|
172 | # Internationalization begins... |
---|
173 | # |
---|
174 | # Revision 1.7 2003/02/08 23:17:20 jalet |
---|
175 | # repykota now outputs life time page counters and the total pages printed by |
---|
176 | # all users/groups on each printer. |
---|
177 | # |
---|
178 | # Revision 1.6 2003/02/07 23:39:16 jalet |
---|
179 | # Typos |
---|
180 | # |
---|
181 | # Revision 1.5 2003/02/07 08:38:36 jalet |
---|
182 | # Missing conversion. |
---|
183 | # empty line between two printers |
---|
184 | # |
---|
185 | # Revision 1.4 2003/02/07 08:34:15 jalet |
---|
186 | # Test wrt date limit was wrong |
---|
187 | # |
---|
188 | # Revision 1.3 2003/02/07 00:08:52 jalet |
---|
189 | # Typos |
---|
190 | # |
---|
191 | # Revision 1.2 2003/02/06 23:58:05 jalet |
---|
192 | # repykota should be ok |
---|
193 | # |
---|
194 | # |
---|
195 | # |
---|
196 | |
---|
197 | import sys |
---|
198 | import os |
---|
199 | import pwd |
---|
200 | |
---|
201 | from mx import DateTime |
---|
202 | |
---|
203 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
204 | from pykota.config import PyKotaConfigError |
---|
205 | from pykota.storage import PyKotaStorageError |
---|
206 | from pykota.reporter import PyKotaReporterError |
---|
207 | from pykota import reporter |
---|
208 | |
---|
209 | __doc__ = N_("""repykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
210 | |
---|
211 | Generates print quota reports. |
---|
212 | |
---|
213 | command line usage : |
---|
214 | |
---|
215 | repykota [options] |
---|
216 | |
---|
217 | options : |
---|
218 | |
---|
219 | -v | --version Prints repykota's version number then exits. |
---|
220 | -h | --help Prints this message then exits. |
---|
221 | |
---|
222 | -u | --users Generates a report on users quota, this is |
---|
223 | the default. |
---|
224 | |
---|
225 | -g | --groups Generates a report on group quota instead of users. |
---|
226 | |
---|
227 | -P | --printer p Report quotas on this printer only. Actually p can |
---|
228 | use wildcards characters to select only |
---|
229 | some printers. The default value is *, meaning |
---|
230 | all printers. |
---|
231 | You can specify several names or wildcards, |
---|
232 | by separating them with commas. |
---|
233 | |
---|
234 | examples : |
---|
235 | |
---|
236 | $ repykota --printer lp |
---|
237 | |
---|
238 | This will print the quota status for all users who use the lp printer. |
---|
239 | |
---|
240 | $ repykota |
---|
241 | |
---|
242 | This will print the quota status for all users on all printers. |
---|
243 | |
---|
244 | $ repykota --printer "laser*,*pson" jerome "jo*" |
---|
245 | |
---|
246 | This will print the quota status for user jerome and all users |
---|
247 | whose name begins with "jo" on all printers which name begins |
---|
248 | with "laser" or ends with "pson". |
---|
249 | |
---|
250 | If launched by an user who is not a PyKota administrator, additionnal |
---|
251 | arguments representing users or groups names are ignored, and only the |
---|
252 | current user/group is reported. |
---|
253 | |
---|
254 | This program is free software; you can redistribute it and/or modify |
---|
255 | it under the terms of the GNU General Public License as published by |
---|
256 | the Free Software Foundation; either version 2 of the License, or |
---|
257 | (at your option) any later version. |
---|
258 | |
---|
259 | This program is distributed in the hope that it will be useful, |
---|
260 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
261 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
262 | GNU General Public License for more details. |
---|
263 | |
---|
264 | You should have received a copy of the GNU General Public License |
---|
265 | along with this program; if not, write to the Free Software |
---|
266 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
267 | |
---|
268 | Please e-mail bugs to: %s""") |
---|
269 | |
---|
270 | class RePyKota(PyKotaTool) : |
---|
271 | """A class for repykota.""" |
---|
272 | def main(self, ugnames, options) : |
---|
273 | """Print Quota reports generator.""" |
---|
274 | if self.config.isAdmin : |
---|
275 | # PyKota administrator |
---|
276 | if not ugnames : |
---|
277 | # no username, means all usernames |
---|
278 | ugnames = [ "*" ] |
---|
279 | else : |
---|
280 | # not the root user |
---|
281 | # reports only the current user |
---|
282 | username = pwd.getpwuid(os.geteuid())[0] |
---|
283 | if options["groups"] : |
---|
284 | user = self.storage.getUser(username) |
---|
285 | if user.Exists : |
---|
286 | ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] |
---|
287 | else : |
---|
288 | ugnames = [ ] |
---|
289 | else : |
---|
290 | ugnames = [ username ] |
---|
291 | |
---|
292 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
293 | if not printers : |
---|
294 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
295 | |
---|
296 | self.reportingtool = reporter.openReporter(self, "text", printers, ugnames, (options["groups"] and 1) or 0) |
---|
297 | print self.reportingtool.generateReport() |
---|
298 | |
---|
299 | if __name__ == "__main__" : |
---|
300 | retcode = 0 |
---|
301 | try : |
---|
302 | defaults = { \ |
---|
303 | "printer" : "*", \ |
---|
304 | } |
---|
305 | short_options = "vhugP:" |
---|
306 | long_options = ["help", "version", "users", "groups", "printer="] |
---|
307 | |
---|
308 | # Initializes the command line tool |
---|
309 | reportTool = RePyKota(doc=__doc__) |
---|
310 | |
---|
311 | # parse and checks the command line |
---|
312 | (options, args) = reportTool.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
313 | |
---|
314 | # sets long options |
---|
315 | options["help"] = options["h"] or options["help"] |
---|
316 | options["version"] = options["v"] or options["version"] |
---|
317 | options["users"] = options["u"] or options["users"] |
---|
318 | options["groups"] = options["g"] or options["groups"] |
---|
319 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
320 | |
---|
321 | if options["help"] : |
---|
322 | reportTool.display_usage_and_quit() |
---|
323 | elif options["version"] : |
---|
324 | reportTool.display_version_and_quit() |
---|
325 | elif options["users"] and options["groups"] : |
---|
326 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
327 | else : |
---|
328 | retcode = reportTool.main(args, options) |
---|
329 | except SystemExit : |
---|
330 | pass |
---|
331 | except : |
---|
332 | try : |
---|
333 | reportTool.crashed("repykota failed") |
---|
334 | except : |
---|
335 | crashed("repykota failed") |
---|
336 | retcode = -1 |
---|
337 | |
---|
338 | try : |
---|
339 | reportTool.storage.close() |
---|
340 | except (TypeError, NameError, AttributeError) : |
---|
341 | pass |
---|
342 | |
---|
343 | sys.exit(retcode) |
---|