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