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