1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # PyKota Print Quota Editor |
---|
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.76 2004/07/01 19:56:40 jalet |
---|
27 | # Better dispatching of error messages |
---|
28 | # |
---|
29 | # Revision 1.75 2004/06/18 13:34:47 jalet |
---|
30 | # Now all tracebacks include PyKota's version number |
---|
31 | # |
---|
32 | # Revision 1.74 2004/06/07 18:43:40 jalet |
---|
33 | # Fixed over-verbose exits when displaying help or version number |
---|
34 | # |
---|
35 | # Revision 1.73 2004/06/03 21:50:34 jalet |
---|
36 | # Improved error logging. |
---|
37 | # crashrecipient directive added. |
---|
38 | # Now exports the job's size in bytes too. |
---|
39 | # |
---|
40 | # Revision 1.72 2004/04/16 16:20:19 jalet |
---|
41 | # Note about not implemented limitby values |
---|
42 | # |
---|
43 | # Revision 1.71 2004/03/24 15:15:24 jalet |
---|
44 | # Began integration of Henrik Janhagen's work on quota-then-balance |
---|
45 | # and balance-then-quota |
---|
46 | # |
---|
47 | # Revision 1.70 2004/02/27 09:23:21 jalet |
---|
48 | # Minor code reorganisation |
---|
49 | # |
---|
50 | # Revision 1.69 2004/02/19 15:05:56 jalet |
---|
51 | # domain names changed to example.com in the doc, according to RFC2606 |
---|
52 | # |
---|
53 | # Revision 1.68 2004/01/28 10:05:22 jalet |
---|
54 | # New user/group deletion code |
---|
55 | # |
---|
56 | # Revision 1.67 2004/01/15 11:19:07 jalet |
---|
57 | # Typos in messages wrt gettext. |
---|
58 | # |
---|
59 | # Revision 1.66 2004/01/12 21:54:36 jalet |
---|
60 | # User's email address can now be set at user's creation time. |
---|
61 | # |
---|
62 | # Revision 1.65 2004/01/08 16:24:49 jalet |
---|
63 | # edpykota now supports adding printers to printer groups. |
---|
64 | # |
---|
65 | # Revision 1.64 2004/01/08 14:10:32 jalet |
---|
66 | # Copyright year changed. |
---|
67 | # |
---|
68 | # Revision 1.63 2003/11/24 16:50:58 jalet |
---|
69 | # Old help message deletedd |
---|
70 | # |
---|
71 | # Revision 1.62 2003/11/12 23:28:38 jalet |
---|
72 | # More work on new backend. This commit may be unstable. |
---|
73 | # |
---|
74 | # Revision 1.61 2003/11/12 13:06:35 jalet |
---|
75 | # Bug fix wrt no user/group name command line argument to edpykota |
---|
76 | # |
---|
77 | # Revision 1.60 2003/10/09 21:25:24 jalet |
---|
78 | # Multiple printer names or wildcards can be passed on the command line |
---|
79 | # separated with commas. |
---|
80 | # Beta phase. |
---|
81 | # |
---|
82 | # Revision 1.59 2003/10/07 09:07:27 jalet |
---|
83 | # Character encoding added to please latest version of Python |
---|
84 | # |
---|
85 | # Revision 1.58 2003/10/03 12:27:01 jalet |
---|
86 | # Several optimizations, especially with LDAP backend |
---|
87 | # |
---|
88 | # Revision 1.57 2003/08/20 16:01:19 jalet |
---|
89 | # Comment added. |
---|
90 | # |
---|
91 | # Revision 1.56 2003/07/29 20:55:17 jalet |
---|
92 | # 1.14 is out ! |
---|
93 | # |
---|
94 | # Revision 1.55 2003/07/28 09:11:12 jalet |
---|
95 | # PyKota now tries to add its attributes intelligently in existing LDAP |
---|
96 | # directories. |
---|
97 | # |
---|
98 | # Revision 1.54 2003/07/21 06:32:42 jalet |
---|
99 | # Prevents email messages to be sent at modification/creation time for |
---|
100 | # a user/group quota |
---|
101 | # |
---|
102 | # Revision 1.53 2003/07/09 06:03:41 jalet |
---|
103 | # Fixed typo when using edpykota --prototype |
---|
104 | # |
---|
105 | # Revision 1.52 2003/07/07 12:11:13 jalet |
---|
106 | # Small fix |
---|
107 | # |
---|
108 | # Revision 1.51 2003/07/07 11:55:50 jalet |
---|
109 | # Small fix |
---|
110 | # |
---|
111 | # Revision 1.50 2003/07/05 12:33:53 jalet |
---|
112 | # More on previous fix. |
---|
113 | # |
---|
114 | # Revision 1.49 2003/07/05 12:32:07 jalet |
---|
115 | # Ensure that the user don't pass more than two prices for a printer. |
---|
116 | # |
---|
117 | # Revision 1.48 2003/06/25 19:52:30 jalet |
---|
118 | # Should be ready for testing :-) |
---|
119 | # |
---|
120 | # Revision 1.47 2003/06/25 14:10:01 jalet |
---|
121 | # Hey, it may work (edpykota --reset excepted) ! |
---|
122 | # |
---|
123 | # Revision 1.46 2003/06/16 11:59:09 jalet |
---|
124 | # More work on LDAP |
---|
125 | # |
---|
126 | # Revision 1.45 2003/06/11 19:32:00 jalet |
---|
127 | # Severe bug wrt account balance setting should be corrected. |
---|
128 | # |
---|
129 | # Revision 1.44 2003/04/29 22:03:38 jalet |
---|
130 | # Better error handling. |
---|
131 | # |
---|
132 | # Revision 1.43 2003/04/23 22:13:56 jalet |
---|
133 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
134 | # |
---|
135 | # Revision 1.42 2003/04/17 13:38:47 jalet |
---|
136 | # Docstring corrected for better manual page |
---|
137 | # |
---|
138 | # Revision 1.41 2003/04/16 12:35:49 jalet |
---|
139 | # Groups quota work now ! |
---|
140 | # |
---|
141 | # Revision 1.40 2003/04/16 08:22:09 jalet |
---|
142 | # More strict error detection. |
---|
143 | # Minor code rewrite to avoid some repetitive tests. |
---|
144 | # |
---|
145 | # Revision 1.39 2003/04/16 08:01:53 jalet |
---|
146 | # edpykota --charge command line option works now. |
---|
147 | # |
---|
148 | # Revision 1.38 2003/04/15 22:02:43 jalet |
---|
149 | # More complete docstring |
---|
150 | # |
---|
151 | # Revision 1.37 2003/04/15 21:58:33 jalet |
---|
152 | # edpykota now accepts a --delete option. |
---|
153 | # Preparation to allow edpykota to accept much more command line options |
---|
154 | # (WARNING : docstring is OK, but code isn't !) |
---|
155 | # |
---|
156 | # Revision 1.36 2003/04/15 13:55:28 jalet |
---|
157 | # Options --limitby and --balance added to edpykota |
---|
158 | # |
---|
159 | # Revision 1.35 2003/04/15 13:06:39 jalet |
---|
160 | # Allow to add a printer without any user |
---|
161 | # |
---|
162 | # Revision 1.34 2003/04/11 16:51:11 jalet |
---|
163 | # Bug fix for edpykota --add with users who already had a quota on the printer. |
---|
164 | # |
---|
165 | # Revision 1.33 2003/04/10 21:47:20 jalet |
---|
166 | # Job history added. Upgrade script neutralized for now ! |
---|
167 | # |
---|
168 | # Revision 1.32 2003/04/08 21:31:39 jalet |
---|
169 | # (anything or 0) = anything !!! Go back to school Jerome ! |
---|
170 | # |
---|
171 | # Revision 1.31 2003/04/08 21:13:44 jalet |
---|
172 | # Prepare --groups option to work. |
---|
173 | # |
---|
174 | # Revision 1.30 2003/04/08 21:10:18 jalet |
---|
175 | # Checks --groups option presence instead of --users because --users is the default. |
---|
176 | # |
---|
177 | # Revision 1.29 2003/04/05 09:28:56 jalet |
---|
178 | # Unnecessary message was logged |
---|
179 | # |
---|
180 | # Revision 1.28 2003/03/29 13:45:26 jalet |
---|
181 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
182 | # Two README files were added. |
---|
183 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
184 | # |
---|
185 | # Revision 1.27 2003/03/10 00:23:04 jalet |
---|
186 | # Bad english |
---|
187 | # |
---|
188 | # Revision 1.26 2003/03/10 00:11:27 jalet |
---|
189 | # Cleaner example. |
---|
190 | # |
---|
191 | # Revision 1.25 2003/03/09 23:56:21 jalet |
---|
192 | # Option noquota added to do accounting only. |
---|
193 | # |
---|
194 | # Revision 1.24 2003/02/27 23:48:41 jalet |
---|
195 | # Correctly maps PyKota's log levels to syslog log levels |
---|
196 | # |
---|
197 | # Revision 1.23 2003/02/27 22:55:20 jalet |
---|
198 | # WARN log priority doesn't exist. |
---|
199 | # |
---|
200 | # Revision 1.22 2003/02/27 09:37:02 jalet |
---|
201 | # Wildcards seem to work now |
---|
202 | # |
---|
203 | # Revision 1.21 2003/02/27 09:04:46 jalet |
---|
204 | # user and group names can be passed as wildcards if the --add option |
---|
205 | # is not set. The default is to act on all users or groups. |
---|
206 | # |
---|
207 | # Revision 1.20 2003/02/10 12:07:30 jalet |
---|
208 | # Now repykota should output the recorded total page number for each printer too. |
---|
209 | # |
---|
210 | # Revision 1.19 2003/02/09 13:40:29 jalet |
---|
211 | # typo |
---|
212 | # |
---|
213 | # Revision 1.18 2003/02/09 12:56:53 jalet |
---|
214 | # Internationalization begins... |
---|
215 | # |
---|
216 | # Revision 1.17 2003/02/08 22:47:23 jalet |
---|
217 | # Option --reset can now be used without having to use soft and hard limits |
---|
218 | # on the command line. |
---|
219 | # |
---|
220 | # Revision 1.16 2003/02/08 22:39:46 jalet |
---|
221 | # --reset command line option added |
---|
222 | # |
---|
223 | # Revision 1.15 2003/02/08 22:20:01 jalet |
---|
224 | # Clarification on why we don't check with /etc/passwd to see if the user |
---|
225 | # name is valid or not. |
---|
226 | # |
---|
227 | # Revision 1.14 2003/02/08 22:18:15 jalet |
---|
228 | # Now checks user and group names for validity before adding them |
---|
229 | # |
---|
230 | # Revision 1.13 2003/02/08 22:09:02 jalet |
---|
231 | # Only printer was added the first time. |
---|
232 | # |
---|
233 | # Revision 1.12 2003/02/08 21:44:49 jalet |
---|
234 | # Python 2.1 string module doesn't define ascii_letters |
---|
235 | # |
---|
236 | # Revision 1.11 2003/02/08 09:42:44 jalet |
---|
237 | # Better handle wrong or bad command line arguments |
---|
238 | # |
---|
239 | # Revision 1.10 2003/02/08 09:39:20 jalet |
---|
240 | # typos |
---|
241 | # |
---|
242 | # Revision 1.9 2003/02/08 09:38:06 jalet |
---|
243 | # Badly placed test |
---|
244 | # |
---|
245 | # Revision 1.8 2003/02/07 22:53:57 jalet |
---|
246 | # Checks if printer name is valid before adding it |
---|
247 | # |
---|
248 | # Revision 1.7 2003/02/07 22:17:58 jalet |
---|
249 | # Incomplete test |
---|
250 | # |
---|
251 | # Revision 1.6 2003/02/07 22:13:13 jalet |
---|
252 | # Perhaps edpykota is now able to add printers !!! Oh, stupid me ! |
---|
253 | # |
---|
254 | # Revision 1.5 2003/02/06 14:49:04 jalet |
---|
255 | # edpykota should be ok now |
---|
256 | # |
---|
257 | # Revision 1.4 2003/02/06 14:28:59 jalet |
---|
258 | # edpykota should be ok, minus some typos |
---|
259 | # |
---|
260 | # Revision 1.3 2003/02/06 10:47:21 jalet |
---|
261 | # Documentation string and command line options didn't match. |
---|
262 | # |
---|
263 | # Revision 1.2 2003/02/06 10:39:23 jalet |
---|
264 | # Preliminary edpykota work. |
---|
265 | # |
---|
266 | # Revision 1.1 2003/02/05 21:41:09 jalet |
---|
267 | # Skeletons added for all command line tools |
---|
268 | # |
---|
269 | # |
---|
270 | # |
---|
271 | |
---|
272 | import sys |
---|
273 | |
---|
274 | from pykota import version |
---|
275 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed |
---|
276 | from pykota.config import PyKotaConfigError |
---|
277 | from pykota.storage import PyKotaStorageError |
---|
278 | |
---|
279 | __doc__ = """edpykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
280 | A Print Quota editor for PyKota. |
---|
281 | |
---|
282 | command line usage : |
---|
283 | |
---|
284 | edpykota [options] user1 user2 ... userN |
---|
285 | |
---|
286 | edpykota [options] group1 group2 ... groupN |
---|
287 | |
---|
288 | options : |
---|
289 | |
---|
290 | -v | --version Prints edpykota's version number then exits. |
---|
291 | -h | --help Prints this message then exits. |
---|
292 | |
---|
293 | -a | --add Adds users and/or printers if they don't |
---|
294 | exist on the Quota Storage Server. |
---|
295 | |
---|
296 | -d | --delete Deletes users/groups from the quota storage. |
---|
297 | Printers are never deleted. |
---|
298 | |
---|
299 | -c | --charge p[,j] Sets the price per page and per job to charge |
---|
300 | for a particular printer. Job price is optional. |
---|
301 | If both are to be set, separate them with a comma. |
---|
302 | Floating point values are allowed. |
---|
303 | |
---|
304 | -i | --ingroups g1[,g2...] Puts the users into each of the groups |
---|
305 | listed, separated by commas. The groups |
---|
306 | must already exist in the Quota Storage. |
---|
307 | |
---|
308 | -u | --users Edit users print quotas, this is the default. |
---|
309 | |
---|
310 | -P | --printer p Edit quotas on printer p only. Actually p can |
---|
311 | use wildcards characters to select only |
---|
312 | some printers. The default value is *, meaning |
---|
313 | all printers. |
---|
314 | You can specify several names or wildcards, |
---|
315 | by separating them with commas. |
---|
316 | |
---|
317 | -G | --pgroups pg1[,pg2...] Adds the printer(s) to the printer groups |
---|
318 | pg1, pg2, etc... which must already exist. |
---|
319 | A printer group is just like a normal printer, |
---|
320 | only that it is usually unknown from the printing |
---|
321 | system. Create printer groups exactly the same |
---|
322 | way that you create printers, then add other |
---|
323 | printers to them with this option. |
---|
324 | Accounting is done on a printer and on all |
---|
325 | the printer groups it belongs to, quota checking |
---|
326 | is done on a printer and on all the printer groups |
---|
327 | it belongs to. |
---|
328 | |
---|
329 | -g | --groups Edit users groups print quotas instead of users. |
---|
330 | |
---|
331 | -p | --prototype u|g Uses user u or group g as a prototype to set |
---|
332 | print quotas |
---|
333 | |
---|
334 | -n | --noquota Doesn't set a quota but only does accounting. |
---|
335 | |
---|
336 | -r | --reset Resets the printed page counter for the user |
---|
337 | or group to zero. The life time page counter |
---|
338 | is kept unchanged. |
---|
339 | |
---|
340 | -l | --limitby l Choose if the user/group is limited in printing |
---|
341 | by its account balance or by its page quota. |
---|
342 | The default value is 'quota'. Allowed values |
---|
343 | are 'quota' 'balance' 'quota-then-balance' and |
---|
344 | 'balance-then-quota'. |
---|
345 | WARNING : quota-then-balance and balance-then-quota |
---|
346 | are not yet implemented. |
---|
347 | |
---|
348 | -b | --balance b Sets the user's account balance to b. |
---|
349 | Account balance may be increase or decreased |
---|
350 | if b is prefixed with + or -. |
---|
351 | WARNING : when decreasing account balance, |
---|
352 | the total paid so far by the user is decreased |
---|
353 | too. |
---|
354 | Groups don't have a real balance, but the |
---|
355 | sum of their users' account balance. |
---|
356 | |
---|
357 | -S | --softlimit sl Sets the quota soft limit to sl pages. |
---|
358 | |
---|
359 | -H | --hardlimit hl Sets the quota hard limit to hl pages. |
---|
360 | |
---|
361 | user1 through userN and group1 through groupN can use wildcards |
---|
362 | if the --add option is not set. |
---|
363 | |
---|
364 | examples : |
---|
365 | |
---|
366 | $ edpykota --add -p jerome john paul george ringo/ringo@example.com |
---|
367 | |
---|
368 | This will add users john, paul, george and ringo to the quota |
---|
369 | database, and set their print quotas to the same values than user |
---|
370 | jerome. User jerome must already exist. |
---|
371 | User ringo's email address will also be set to 'ringo@example.com' |
---|
372 | |
---|
373 | $ edpykota --printer lp -S 50 -H 60 jerome |
---|
374 | |
---|
375 | This will set jerome's print quota on the lp printer to a soft limit |
---|
376 | of 50 pages, and a hard limit of 60 pages. If either user jerome or |
---|
377 | printer lp doesn't exist on the Quota Storage Server then nothing is done. |
---|
378 | |
---|
379 | $ edpykota --add --printer lp --ingroups coders,it -S 50 -H 60 jerome |
---|
380 | |
---|
381 | Same as above, but if either user jerome or printer lp doesn't exist |
---|
382 | on the Quota Storage Server they are automatically added. Also |
---|
383 | user jerome is put into the groups "coders" and "it" which must |
---|
384 | already exist in the Quota Storage. |
---|
385 | |
---|
386 | $ edpykota -g -S 500 -H 550 financial support |
---|
387 | |
---|
388 | This will set print quota soft limit to 500 pages and hard limit |
---|
389 | to 550 pages for groups financial and support on all printers. |
---|
390 | |
---|
391 | $ edpykota --reset jerome "jo*" |
---|
392 | |
---|
393 | This will reset jerome's page counter to zero on all printers, as |
---|
394 | well as every user whose name begins with 'jo'. |
---|
395 | Their life time page counter on each printer will be kept unchanged. |
---|
396 | |
---|
397 | $ edpykota --printer hpcolor --noquota jerome |
---|
398 | |
---|
399 | This will tell PyKota to not limit jerome when printing on the |
---|
400 | hpcolor printer. All his jobs will be allowed on this printer, but |
---|
401 | accounting of the pages he prints will still be kept. |
---|
402 | Print Quotas for jerome on other printers are unchanged. |
---|
403 | |
---|
404 | $ edpykota --limitby balance jerome |
---|
405 | |
---|
406 | This will tell PyKota to limit jerome by his account's balance |
---|
407 | when printing. |
---|
408 | |
---|
409 | $ edpykota --balance +10.0 jerome |
---|
410 | |
---|
411 | This will increase jerome's account balance by 10.0 (in your |
---|
412 | own currency). You can decrease the account balance with a |
---|
413 | dash prefix, and set it to a fixed amount with no prefix. |
---|
414 | |
---|
415 | $ edpykota --delete jerome rachel |
---|
416 | |
---|
417 | This will completely delete jerome and rachel from the Quota Storage |
---|
418 | database. All their quotas and jobs will be deleted too. |
---|
419 | |
---|
420 | $ edpykota --printer lp --charge 0.1 |
---|
421 | |
---|
422 | This will set the page price for printer lp to 0.1. Job price |
---|
423 | will not be changed. |
---|
424 | |
---|
425 | $ edpykota --printer hplj1,hplj2 --pgroups Laser,HP |
---|
426 | |
---|
427 | This will put printers hplj1 and hplj2 in printers groups Laser and HP. |
---|
428 | When printing either on hplj1 or hplj2, print quota will also be |
---|
429 | checked and accounted for on virtual printers Laser and HP. |
---|
430 | |
---|
431 | This program is free software; you can redistribute it and/or modify |
---|
432 | it under the terms of the GNU General Public License as published by |
---|
433 | the Free Software Foundation; either version 2 of the License, or |
---|
434 | (at your option) any later version. |
---|
435 | |
---|
436 | This program is distributed in the hope that it will be useful, |
---|
437 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
438 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
439 | GNU General Public License for more details. |
---|
440 | |
---|
441 | You should have received a copy of the GNU General Public License |
---|
442 | along with this program; if not, write to the Free Software |
---|
443 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
444 | |
---|
445 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
446 | |
---|
447 | class EdPyKota(PyKotaTool) : |
---|
448 | """A class for edpykota.""" |
---|
449 | def main(self, names, options) : |
---|
450 | """Edit user or group quotas.""" |
---|
451 | |
---|
452 | suffix = (options["groups"] and "Group") or "User" |
---|
453 | |
---|
454 | softlimit = hardlimit = None |
---|
455 | if not options["noquota"] : |
---|
456 | if options["softlimit"] : |
---|
457 | try : |
---|
458 | softlimit = int(options["softlimit"].strip()) |
---|
459 | except ValueError : |
---|
460 | raise PyKotaToolError, _("Invalid softlimit value %s.") % options["softlimit"] |
---|
461 | if options["hardlimit"] : |
---|
462 | try : |
---|
463 | hardlimit = int(options["hardlimit"].strip()) |
---|
464 | except ValueError : |
---|
465 | raise PyKotaToolError, _("Invalid hardlimit value %s.") % options["hardlimit"] |
---|
466 | if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) : |
---|
467 | # error, exchange them |
---|
468 | self.printInfo(_("Hard limit %i is less than soft limit %i, values will be exchanged.") % (hardlimit, softlimit)) |
---|
469 | (softlimit, hardlimit) = (hardlimit, softlimit) |
---|
470 | |
---|
471 | balance = options["balance"] |
---|
472 | if balance : |
---|
473 | balance = balance.strip() |
---|
474 | try : |
---|
475 | balancevalue = float(balance) |
---|
476 | except ValueError : |
---|
477 | raise PyKotaToolError, _("Invalid balance value %s") % options["balance"] |
---|
478 | |
---|
479 | if options["charge"] : |
---|
480 | try : |
---|
481 | charges = [float(part) for part in options["charge"].split(',', 1)] |
---|
482 | except ValueError : |
---|
483 | raise PyKotaToolError, _("Invalid charge amount value %s") % options["charge"] |
---|
484 | else : |
---|
485 | if len(charges) > 2 : |
---|
486 | charges = charges[:2] |
---|
487 | if len(charges) != 2 : |
---|
488 | charges = [charges[0], None] |
---|
489 | |
---|
490 | limitby = options["limitby"] |
---|
491 | if limitby : |
---|
492 | limitby = limitby.strip().lower() |
---|
493 | if limitby and (limitby not in ('quota', 'balance', 'quota-then-balance', 'balance-then-quota')) : |
---|
494 | raise PyKotaToolError, _("Invalid limitby value %s") % options["limitby"] |
---|
495 | |
---|
496 | if options["ingroups"] : |
---|
497 | groupnames = [gname.strip() for gname in options["ingroups"].split(',')] |
---|
498 | else : |
---|
499 | groupnames = [] |
---|
500 | |
---|
501 | if options["prototype"] : |
---|
502 | protoentry = getattr(self.storage, "get%s" % suffix)(options["prototype"]) |
---|
503 | |
---|
504 | printeradded = 0 |
---|
505 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
506 | if not printers : |
---|
507 | pname = options["printer"] |
---|
508 | if options["add"] and pname : |
---|
509 | if self.isValidName(pname) : |
---|
510 | printers = [ self.storage.addPrinter(pname) ] |
---|
511 | if printers[0].Exists : |
---|
512 | printeradded = 1 |
---|
513 | else : |
---|
514 | raise PyKotaToolError, _("Impossible to add printer %s") % pname |
---|
515 | else : |
---|
516 | raise PyKotaToolError, _("Invalid printer name %s") % pname |
---|
517 | else : |
---|
518 | raise PyKotaToolError, _("There's no printer matching %s") % pname |
---|
519 | if not names : |
---|
520 | if options["add"] : |
---|
521 | if not printeradded : |
---|
522 | raise PyKotaToolError, _("You have to pass user or group names on the command line") |
---|
523 | else : |
---|
524 | names = getattr(self.storage, "getAll%ssNames" % suffix)() |
---|
525 | else : |
---|
526 | names = [ "*" ] # all users |
---|
527 | |
---|
528 | printersgroups = [] |
---|
529 | if options["pgroups"] : |
---|
530 | printersgroups = self.storage.getMatchingPrinters(options["pgroups"]) |
---|
531 | |
---|
532 | todelete = {} |
---|
533 | changed = {} # tracks changes made at the user/group level |
---|
534 | for printer in printers : |
---|
535 | for pgroup in printersgroups : |
---|
536 | pgroup.addPrinterToGroup(printer) |
---|
537 | |
---|
538 | if options["charge"] : |
---|
539 | (perpage, perjob) = charges |
---|
540 | printer.setPrices(perpage, perjob) |
---|
541 | |
---|
542 | if options["prototype"] : |
---|
543 | if protoentry.Exists : |
---|
544 | protoquota = getattr(self.storage, "get%sPQuota" % suffix)(protoentry, printer) |
---|
545 | if not protoquota.Exists : |
---|
546 | self.printInfo(_("Prototype %s not found in Quota Storage for printer %s.") % (protoentry.Name, printer.Name)) |
---|
547 | continue # skip this printer |
---|
548 | else : |
---|
549 | (softlimit, hardlimit) = (protoquota.SoftLimit, protoquota.HardLimit) |
---|
550 | else : |
---|
551 | self.printInfo(_("Prototype object %s not found in Quota Storage.") % protoentry.Name) |
---|
552 | |
---|
553 | if not options["noquota"] : |
---|
554 | if hardlimit is None : |
---|
555 | hardlimit = softlimit |
---|
556 | if hardlimit is not None : |
---|
557 | self.printInfo(_("Undefined hard limit set to soft limit (%s) on printer %s.") % (str(hardlimit), printer.Name)) |
---|
558 | if softlimit is None : |
---|
559 | softlimit = hardlimit |
---|
560 | if softlimit is not None : |
---|
561 | self.printInfo(_("Undefined soft limit set to hard limit (%s) on printer %s.") % (str(softlimit), printer.Name)) |
---|
562 | |
---|
563 | if options["add"] : |
---|
564 | allentries = [] |
---|
565 | for name in names : |
---|
566 | email = "" |
---|
567 | if not options["groups"] : |
---|
568 | splitname = name.split('/', 1) # username/email |
---|
569 | if len(splitname) == 1 : |
---|
570 | splitname.append("") |
---|
571 | (name, email) = splitname |
---|
572 | if email and (email.count('@') != 1) : |
---|
573 | self.printInfo(_("Invalid email address %s") % email) |
---|
574 | email = "" |
---|
575 | entry = getattr(self.storage, "get%s" % suffix)(name) |
---|
576 | if email and not options["groups"] : |
---|
577 | entry.Email = email |
---|
578 | entrypquota = getattr(self.storage, "get%sPQuota" % suffix)(entry, printer) |
---|
579 | allentries.append((entry, entrypquota)) |
---|
580 | else : |
---|
581 | allentries = getattr(self.storage, "getPrinter%ssAndQuotas" % suffix)(printer, names) |
---|
582 | |
---|
583 | for (entry, entrypquota) in allentries : |
---|
584 | if not changed.has_key(entry.Name) : |
---|
585 | changed[entry.Name] = {} |
---|
586 | if not options["groups"] : |
---|
587 | changed[entry.Name]["ingroups"] = [] |
---|
588 | |
---|
589 | if not entry.Exists : |
---|
590 | # not found |
---|
591 | if options["add"] : |
---|
592 | # In case we want to add something, it is crucial |
---|
593 | # that we DON'T check with the system accounts files |
---|
594 | # like /etc/passwd because users may be defined |
---|
595 | # only remotely |
---|
596 | if self.isValidName(entry.Name) : |
---|
597 | entry = getattr(self.storage, "add%s" % suffix)(entry) |
---|
598 | else : |
---|
599 | if options["groups"] : |
---|
600 | self.printInfo(_("Invalid group name %s") % entry.Name) |
---|
601 | else : |
---|
602 | self.printInfo(_("Invalid user name %s") % entry.Name) |
---|
603 | elif options["delete"] : |
---|
604 | todelete[entry.Name] = entry |
---|
605 | |
---|
606 | if entry.Exists and (not entrypquota.Exists) : |
---|
607 | # not found |
---|
608 | if options["add"] : |
---|
609 | entrypquota = getattr(self.storage, "add%sPQuota" % suffix)(entry, printer) |
---|
610 | |
---|
611 | if not entrypquota.Exists : |
---|
612 | self.printInfo(_("Quota not found for object %s on printer %s.") % (entry.Name, printer.Name)) |
---|
613 | else : |
---|
614 | if options["noquota"] or options["prototype"] or ((softlimit is not None) and (hardlimit is not None)) : |
---|
615 | entrypquota.setLimits(softlimit, hardlimit) |
---|
616 | if limitby : |
---|
617 | if changed[entry.Name].get("limitby") is None : |
---|
618 | entry.setLimitBy(limitby) |
---|
619 | changed[entry.Name]["limitby"] = limitby |
---|
620 | |
---|
621 | if not options["groups"] : |
---|
622 | if options["reset"] : |
---|
623 | entrypquota.reset() |
---|
624 | |
---|
625 | if balance : |
---|
626 | if changed[entry.Name].get("balance") is None : |
---|
627 | if balance.startswith("+") or balance.startswith("-") : |
---|
628 | newbalance = float(entry.AccountBalance or 0.0) + balancevalue |
---|
629 | newlifetimepaid = float(entry.LifeTimePaid or 0.0) + balancevalue |
---|
630 | entry.setAccountBalance(newbalance, newlifetimepaid) |
---|
631 | else : |
---|
632 | diff = balancevalue - float(entry.AccountBalance or 0.0) |
---|
633 | newlifetimepaid = float(entry.LifeTimePaid or 0.0) + diff |
---|
634 | entry.setAccountBalance(balancevalue, newlifetimepaid) |
---|
635 | changed[entry.Name]["balance"] = balance |
---|
636 | |
---|
637 | for groupname in groupnames : |
---|
638 | # not executed if option --ingroups is not used |
---|
639 | if groupname not in changed[entry.Name]["ingroups"] : |
---|
640 | group = self.storage.getGroup(groupname) |
---|
641 | if group.Exists : |
---|
642 | self.storage.addUserToGroup(entry, group) |
---|
643 | changed[entry.Name]["ingroups"].append(groupname) |
---|
644 | else : |
---|
645 | self.printInfo(_("Group %s not found in the PyKota Storage.") % groupname) |
---|
646 | |
---|
647 | # This line disabled to prevent sending of unwanted email |
---|
648 | # messages if quota is reached at creation/modification time. |
---|
649 | # The check will be done at print time anyway. |
---|
650 | # getattr(self, "warn%sPQuota" % suffix)(entrypquota) |
---|
651 | |
---|
652 | # Now delete what has to be deleted |
---|
653 | for (name, entry) in todelete.items() : |
---|
654 | entry.delete() |
---|
655 | |
---|
656 | if __name__ == "__main__" : |
---|
657 | retcode = 0 |
---|
658 | try : |
---|
659 | defaults = { \ |
---|
660 | "printer" : "*", \ |
---|
661 | } |
---|
662 | short_options = "vhdc:l:b:i:naugrp:P:S:H:G:" |
---|
663 | long_options = ["help", "version", "charge=", "delete", "limitby=", "balance=", "ingroups=", "noquota", "add", "users", "groups", "reset", "prototype=", "printer=", "softlimit=", "hardlimit=", "pgroups="] |
---|
664 | |
---|
665 | # Initializes the command line tool |
---|
666 | editor = EdPyKota(doc=__doc__) |
---|
667 | |
---|
668 | # parse and checks the command line |
---|
669 | (options, args) = editor.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
670 | |
---|
671 | # sets long options |
---|
672 | options["help"] = options["h"] or options["help"] |
---|
673 | options["version"] = options["v"] or options["version"] |
---|
674 | options["add"] = options["a"] or options["add"] |
---|
675 | options["users"] = options["u"] or options["users"] |
---|
676 | options["groups"] = options["g"] or options["groups"] |
---|
677 | options["prototype"] = options["p"] or options["prototype"] |
---|
678 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
679 | options["softlimit"] = options["S"] or options["softlimit"] |
---|
680 | options["hardlimit"] = options["H"] or options["hardlimit"] |
---|
681 | options["reset"] = options["r"] or options["reset"] |
---|
682 | options["noquota"] = options["n"] or options["noquota"] |
---|
683 | options["limitby"] = options["l"] or options["limitby"] |
---|
684 | options["balance"] = options["b"] or options["balance"] |
---|
685 | options["delete"] = options["d"] or options["delete"] |
---|
686 | options["ingroups"] = options["i"] or options["ingroups"] |
---|
687 | options["charge"] = options["c"] or options["charge"] |
---|
688 | options["pgroups"] = options["G"] or options["pgroups"] |
---|
689 | |
---|
690 | if options["help"] : |
---|
691 | editor.display_usage_and_quit() |
---|
692 | elif options["version"] : |
---|
693 | editor.display_version_and_quit() |
---|
694 | elif options["users"] and options["groups"] : |
---|
695 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
696 | elif (options["add"] or options["prototype"]) and options["delete"] : |
---|
697 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
698 | elif (options["softlimit"] or options["hardlimit"]) and options["prototype"] : |
---|
699 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
700 | elif options["noquota"] and (options["prototype"] or options["hardlimit"] or options["softlimit"]) : |
---|
701 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
702 | elif options["groups"] and (options["balance"] or options["ingroups"]) : |
---|
703 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
704 | else : |
---|
705 | retcode = editor.main(args, options) |
---|
706 | except SystemExit : |
---|
707 | pass |
---|
708 | except : |
---|
709 | try : |
---|
710 | editor.crashed("edpykota failed") |
---|
711 | except : |
---|
712 | crashed("edpykota failed") |
---|
713 | retcode = -1 |
---|
714 | |
---|
715 | try : |
---|
716 | editor.storage.close() |
---|
717 | except (TypeError, NameError, AttributeError) : |
---|
718 | pass |
---|
719 | |
---|
720 | sys.exit(retcode) |
---|