root / pykota / trunk / bin / edpykota @ 1418

Revision 1418, 28.9 kB (checked in by jalet, 20 years ago)

Began integration of Henrik Janhagen's work on quota-then-balance
and balance-then-quota

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