root / pykota / trunk / bin / edpykota @ 1870

Revision 1809, 30.8 kB (checked in by jalet, 20 years ago)

Now outputs the name of the offending user if a mere mortal tries to use
one of these commands !!!

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