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