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