root / pykota / trunk / bin / edpykota @ 1803

Revision 1803, 30.6 kB (checked in by jalet, 20 years ago)

Postponed string interpolation to help message's output method

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