root / pykota / trunk / bin / edpykota @ 1526

Revision 1526, 29.4 kB (checked in by jalet, 20 years ago)

Fixed over-verbose exits when displaying help or version number

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