Changeset 2712

Show
Ignore:
Timestamp:
02/20/06 21:17:43 (18 years ago)
Author:
jerome
Message:

First pass of code removal.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/edpykota

    r2708 r2712  
    4848  -h | --help          Prints this message then exits. 
    4949   
    50   -a | --add           Adds users and/or printers if they don't  
    51                        exist on the Quota Storage Server. 
     50  -a | --add           Adds users or groups print quota entries if 
     51                       they don't exist in database. 
    5252                        
    53   -d | --delete        Deletes users/groups from the quota storage. 
    54                        Printers are never deleted. 
    55                         
    56   -c | --charge p[,j]  Sets the price per page and per job to charge 
    57                        for a particular printer. Job price is optional. 
    58                        If both are to be set, separate them with a comma. 
    59                        Floating point values are allowed. 
    60                         
    61   -o | --overcharge f  Sets the overcharging factor applied to the user  
    62                        when computing the cost of a print job. Positive or  
    63                        negative floating point values are allowed, 
    64                        this allows you to do some really creative 
    65                        things like giving money to an user whenever 
    66                        he prints. The number of pages in a print job 
    67                        is not modified by this coefficient, only the 
    68                        cost of the job for a particular user. 
    69                        Only users have a coefficient. 
    70    
    71   -i | --ingroups g1[,g2...]  Puts the users into each of the groups 
    72                               listed, separated by commas. The groups 
    73                               must already exist in the Quota Storage. 
    74    
    75   -u | --users         Edit users print quotas, this is the default. 
     53  -d | --delete        Deletes users or groups print quota entries. 
     54                       Users or groups are never deleted, you have 
     55                       to use the pkusers command to delete them. 
    7656   
    7757  -P | --printer p     Edit quotas on printer p only. Actually p can 
     
    8262                       by separating them with commas. 
    8363   
    84   -G | --pgroups pg1[,pg2...] Adds the printer(s) to the printer groups 
    85                        pg1, pg2, etc... which must already exist. 
    86                        A printer group is just like a normal printer, 
    87                        only that it is usually unknown from the printing 
    88                        system. Create printer groups exactly the same 
    89                        way that you create printers, then add other  
    90                        printers to them with this option. 
    91                        Accounting is done on a printer and on all 
    92                        the printer groups it belongs to, quota checking 
    93                        is done on a printer and on all the printer groups 
    94                        it belongs to. 
    95    
    96   -g | --groups        Edit users groups print quotas instead of users. 
     64  -g | --groups        Edit groups print quota entries instead of  
     65                       users print quota entries. 
    9766                           
    98   -p | --prototype u|g Uses user u or group g as a prototype to set 
    99                        print quotas 
    100                         
    101   -n | --noquota       Sets both soft and hard limits to None for a 
    102                        particular print quota entry. 
    103                        This is NOT the same as --limitby noquota 
    104                        which acts on ALL print quota entries for a 
    105                        particular user. 
     67  -n | --noquota       Sets both soft and hard limits to None for users 
     68                       or groups print quota entries. 
    10669   
    10770  -r | --reset         Resets the actual page counter for the user 
     
    11376                       printers. This is a shortcut for '--used 0'. 
    11477                        
    115   -l | --limitby l     Choose if the user/group is limited in printing                      
    116                        by its account balance or by its page quota. 
    117                        The default value is 'quota'. Allowed values 
    118                        are 'quota' 'balance' 'noquota' 'noprint'  
    119                        and 'nochange' : 
    120                         
    121                          - quota : limit by number of pages per printer. 
    122                          - balance : limit by number of credits in account. 
    123                          - noquota : no limit, accounting still done. 
    124                          - nochange : no limit, accounting not done.  
    125                          - noprint : printing is denied.  
    126                        NB : nochange and noprint are not supported for groups. 
    127                         
    128   -b | --balance b     Sets the user's account balance to b.                      
    129                        Account balance may be increase or decreased 
    130                        if b is prefixed with + or -. 
    131                        WARNING : when decreasing account balance, 
    132                        the total paid so far by the user is decreased 
    133                        too. 
    134                        Groups don't have a real balance, but the 
    135                        sum of their users' account balance. 
    136                         
    137   -C | --comment txt   Defines some informational text to be associated 
    138                        with a change to an user's account balance. 
    139                        Only meaningful if -b | --balance is also used. 
    140                         
    14178  -S | --softlimit sl  Sets the quota soft limit to sl pages.                        
    14279   
    14380  -H | --hardlimit hl  Sets the quota hard limit to hl pages. 
    14481   
    145   -I | --increase v    Increase both Soft and Hard limits by the value 
     82  -I | --increase v    Increase existing Soft and Hard limits by the value 
    14683                       of v. You can prefix v with + or -, if no sign is 
    14784                       used, + is assumed. 
    14885 
    149   -U | --used usage    Sets the pagecounters for the user to usage pages; 
    150                        useful for migrating users from a different system 
     86  -U | --used u        Sets the page counters for the user u pages on 
     87                       the selected printers. Doesn't work for groups, since 
     88                       their page counters are the sum of all their members' 
     89                       page counters. 
     90                       Useful for migrating users from a different system 
    15191                       where they have already used some pages. Actual 
    15292                       and Life Time page counters may be increased or decreased 
    153                        if usage is prefixed with + or -. 
     93                       if u is prefixed with + or -. 
    15494                       WARNING : BOTH page counters are modified in all cases, 
    15595                       so be careful. 
    156                        NB : if 'usage' equals '0', then the action taken is 
     96                       NB : if u equals '0', then the action taken is 
    15797                       the same as if --hardreset was used. 
    15898 
     
    162102examples :                               
    163103 
    164   $ edpykota --add -p jerome john paul george ringo/ringo@example.com 
    165    
    166   This will add users john, paul, george and ringo to the quota 
    167   database, and set their print quotas to the same values than user  
    168   jerome. User jerome must already exist. 
    169   User ringo's email address will also be set to 'ringo@example.com' 
     104  $ edpykota --add john paul george ringo 
     105   
     106  This will create print quota entries for users john, paul, george 
     107  and ringo on all printers. These print quota entries will have no 
     108  limit set. 
    170109   
    171110  $ edpykota --printer lp -S 50 -H 60 jerome 
    172111   
    173112  This will set jerome's print quota on the lp printer to a soft limit 
    174   of 50 pages, and a hard limit of 60 pages. If either user jerome or 
    175   printer lp doesn't exist on the Quota Storage Server then nothing is done. 
    176  
    177   $ edpykota --add --printer lp --ingroups coders,it -S 50 -H 60 jerome 
    178    
    179   Same as above, but if either user jerome or printer lp doesn't exist  
    180   on the Quota Storage Server they are automatically added. Also 
    181   user jerome is put into the groups "coders" and "it" which must 
    182   already exist in the Quota Storage. 
    183              
     113  of 50 pages, and a hard limit of 60 pages. Both user jerome and 
     114  printer lp have been previously created with the pkusers and pkprinters 
     115  commands, respectively. 
     116 
    184117  $ edpykota -g -S 500 -H 550 financial support             
    185118   
     
    202135  Print Quotas for jerome on other printers are unchanged. 
    203136   
    204   $ edpykota --limitby balance jerome 
    205    
    206   This will tell PyKota to limit jerome by his account's balance 
    207   when printing. 
    208    
    209   $ edpykota --balance +10.0 jerome 
    210    
    211   This will increase jerome's account balance by 10.0 (in your 
    212   own currency). You can decrease the account balance with a 
    213   dash prefix, and set it to a fixed amount with no prefix. 
    214    
    215   $ edpykota --delete jerome rachel 
    216    
    217   This will completely delete jerome and rachel from the Quota Storage 
    218   database. All their quotas and jobs will be deleted too. 
    219    
    220   $ edpykota --printer lp --charge 0.1 
    221    
    222   This will set the page price for printer lp to 0.1. Job price 
    223   will not be changed. 
    224    
    225   $ edpykota --printer hplj1,hplj2 --pgroups Laser,HP 
    226    
    227   This will put printers hplj1 and hplj2 in printers groups Laser and HP. 
    228   When printing either on hplj1 or hplj2, print quota will also be  
    229   checked and accounted for on virtual printers Laser and HP. 
    230    
    231   $ edpykota --overcharge 2.5 poorstudent 
    232    
    233   This will overcharge the poorstudent user by a factor of 2.5. 
    234    
    235   $ edpykota --overcharge -1 jerome 
    236    
    237   User jerome will actually earn money whenever he prints. 
    238    
    239   $ edpykota --overcharge 0 boss 
    240    
    241   User boss can print at will, it won't cost him anything because the 
    242   cost of each print job will be multiplied by zero before charging 
    243   his account. 
     137  $ edpykota --delete --printer "HP*,XER*" jerome rachel 
     138   
     139  This will delete users jerome and rachel's print quota 
     140  entries on all printers which name begin with 'HP' or 
     141  'XER'. The jobs printed by these users on these printers 
     142  will be deleted from the history. 
    244143""")  
    245144         
     
    265164        else : 
    266165            softlimit = hardlimit = None 
    267              
    268             limitby = options["limitby"] 
    269             if limitby : 
    270                 limitby = limitby.strip().lower() 
    271             if limitby : 
    272                 if limitby not in ('quota', 'balance', 'noquota', \ 
    273                                             'noprint', 'nochange') : 
    274                     raise PyKotaCommandLineError, _("Invalid limitby value %s") % options["limitby"] 
    275                 if limitby in ('noquota', 'nochange') :     
    276                     options["noquota"] = 1 
    277                 if (limitby in ('nochange', 'noprint')) and options["groups"] :     
    278                     raise PyKotaCommandLineError, _("Invalid limitby value %s") % options["limitby"] 
    279166             
    280167            used = options["used"] 
     
    313200                    (softlimit, hardlimit) = (hardlimit, softlimit) 
    314201                 
    315             overcharge = options["overcharge"] 
    316             if overcharge : 
    317                 try : 
    318                     overcharge = float(overcharge.strip()) 
    319                 except (ValueError, AttributeError) :     
    320                     raise PyKotaCommandLineError, _("Invalid overcharge value %s") % options["overcharge"] 
    321                      
    322             balance = options["balance"] 
    323             if balance : 
    324                 balance = balance.strip() 
    325                 try : 
    326                     balancevalue = float(balance) 
    327                 except ValueError :     
    328                     raise PyKotaCommandLineError, _("Invalid balance value %s") % options["balance"] 
    329                  
    330             if options["charge"] : 
    331                 try : 
    332                     charges = [float(part) for part in options["charge"].split(',', 1)] 
    333                 except ValueError :     
    334                     raise PyKotaCommandLineError, _("Invalid charge amount value %s") % options["charge"] 
    335                 else :     
    336                     if len(charges) > 2 : 
    337                         charges = charges[:2] 
    338                     if len(charges) != 2 : 
    339                         charges = [charges[0], None] 
    340                          
    341             if options["ingroups"] :     
    342                 groupnames = [gname.strip() for gname in options["ingroups"].split(',')] 
    343             else :     
    344                 groupnames = [] 
    345                  
    346             rejectunknown = self.config.getRejectUnknown()     
    347             printeradded = 0 
    348             printers = self.storage.getMatchingPrinters(options["printer"]) 
    349             if not printers : 
    350                 pname = options["printer"] 
    351                 if options["add"] and pname : 
    352                     if self.isValidName(pname) : 
    353                         printers = [ self.storage.addPrinter(pname) ] 
    354                         if printers[0].Exists : 
    355                             printeradded = 1 
    356                         else :     
    357                             raise PyKotaToolError, _("Impossible to add printer %s") % pname 
    358                     else :     
    359                         raise PyKotaCommandLineError, _("Invalid printer name %s") % pname 
    360                 else : 
    361                     raise PyKotaCommandLineError, _("There's no printer matching %s") % pname 
    362             if not names :     
    363                 names = getattr(self.storage, "getAll%ssNames" % suffix)() # all users or groups 
    364                      
    365             printersgroups = []         
    366             if options["pgroups"] :         
    367                 printersgroups = self.storage.getMatchingPrinters(options["pgroups"]) 
    368                  
    369             if options["prototype"] :     
    370                 protoentry = getattr(self.storage, "get%s" % suffix)(options["prototype"]) 
    371                 if not protoentry.Exists : 
    372                     raise PyKotaCommandLineError, _("Prototype object %s not found in Quota Storage.") % protoentry.Name 
    373                 else :     
    374                     limitby = protoentry.LimitBy 
    375                     balancevalue = protoentry.AccountBalance 
    376                     if balancevalue is not None : 
    377                         balance = str(abs(balancevalue)) 
    378                     else :     
    379                         balance = None 
    380                     overcharge = getattr(protoentry, "OverCharge", None) 
    381                  
    382202            sys.stdout.write(_("Managing print quotas... (this may take a lot of time)")) 
    383203            sys.stdout.flush() 
     
    386206            changed = {} # tracks changes made at the user/group level 
    387207            for printer in printers : 
    388                 for pgroup in printersgroups : 
    389                     pgroup.addPrinterToGroup(printer)     
    390                      
    391                 if options["charge"] : 
    392                     (perpage, perjob) = charges 
    393                     printer.setPrices(perpage, perjob)     
    394                     printer.save() 
    395                      
    396                 if options["prototype"] : 
    397                     protoquota = getattr(self.storage, "get%sPQuota" % suffix)(protoentry, printer) 
    398                     if not protoquota.Exists : 
    399                         self.printInfo(_("Prototype %s not found in Quota Storage for printer %s.") % (protoentry.Name, printer.Name)) 
    400                     else :     
    401                         (softlimit, hardlimit) = (protoquota.SoftLimit, protoquota.HardLimit) 
    402                          
    403208                if not options["noquota"] :     
    404209                    if hardlimit is None :     
     
    431236                    allentries = getattr(self.storage, "getPrinter%ssAndQuotas" % suffix)(printer, names) 
    432237                     
    433                 # TODO : do this only once !!!     
    434                 allnames = [entry.Name for (entry, dummy) in allentries] 
    435                 for name in names :     
    436                     if not self.matchString(name, allnames) : 
    437                         if options["groups"] : 
    438                             missinggroups[name] = 1 
    439                         else :     
    440                             missingusers[name] = 1 
    441                      
    442238                for (entry, entrypquota) in allentries : 
    443239                    if not changed.has_key(entry.Name) : 
     
    446242                            changed[entry.Name]["ingroups"] = [] 
    447243                             
    448                     if not entry.Exists :         
    449                         # not found 
    450                         if options["add"] : 
    451                             # In case we want to add something, it is crucial 
    452                             # that we DON'T check with the system accounts files 
    453                             # like /etc/passwd because users may be defined  
    454                             # only remotely 
    455                             if self.isValidName(entry.Name) : 
    456                                 reject = 0 
    457                                 if rejectunknown : 
    458                                     if options["groups"] : 
    459                                         try : 
    460                                             grp.getgrnam(entry.Name) 
    461                                         except KeyError :     
    462                                             self.printInfo(_("Unknown group %s") % entry.Name, "error") 
    463                                             reject = 1 
    464                                     else :     
    465                                         try : 
    466                                             pwd.getpwnam(entry.Name) 
    467                                         except KeyError :     
    468                                             self.printInfo(_("Unknown user %s") % entry.Name, "error") 
    469                                             reject = 1 
    470                                 if not reject :         
    471                                     entry = getattr(self.storage, "add%s" % suffix)(entry) 
    472                             else :     
    473                                 if options["groups"] : 
    474                                     self.printInfo(_("Invalid group name %s") % entry.Name) 
    475                                 else :     
    476                                     self.printInfo(_("Invalid user name %s") % entry.Name) 
    477                         else : 
    478                             if options["groups"] : 
    479                                 missinggroups[entry.Name] = 1 
    480                             else :     
    481                                 missingusers[entry.Name] = 1 
    482                                      
    483244                    if entry.Exists and (not entrypquota.Exists) : 
    484245                        # not found 
     
    489250                        self.printInfo(_("Quota not found for object %s on printer %s.") % (entry.Name, printer.Name)) 
    490251                    else :     
    491                         if options["noquota"] or options["prototype"] \ 
     252                        if options["noquota"] \ 
    492253                           or ((softlimit is not None) and (hardlimit is not None)) : 
    493254                            entrypquota.setLimits(softlimit, hardlimit) 
     255                             
    494256                        if increase : 
    495257                           if (entrypquota.SoftLimit is None) \ 
     
    503265                               else :     
    504266                                   self.printInfo(_("You can't set negative limits."), "error") 
    505                         if limitby : 
    506                             if changed[entry.Name].get("limitby") is None : 
    507                                 entry.setLimitBy(limitby) 
    508                                 changed[entry.Name]["limitby"] = limitby 
    509267                         
    510268                        if options["reset"] : 
     
    518276                                entrypquota.setUsage(used) 
    519277                                 
    520                             if overcharge is not None :     
    521                                 if changed[entry.Name].get("overcharge") is None : 
    522                                     entry.setOverChargeFactor(overcharge) 
    523                                     changed[entry.Name]["overcharge"] = overcharge 
    524                                      
    525                             if balance : 
    526                                 if changed[entry.Name].get("balance") is None : 
    527                                     if balance.startswith("+") or balance.startswith("-") : 
    528                                         newbalance = float(entry.AccountBalance or 0.0) + balancevalue 
    529                                         newlifetimepaid = float(entry.LifeTimePaid or 0.0) + balancevalue 
    530                                         entry.setAccountBalance(newbalance, newlifetimepaid, options["comment"]) 
    531                                     else : 
    532                                         diff = balancevalue - float(entry.AccountBalance or 0.0) 
    533                                         newlifetimepaid = float(entry.LifeTimePaid or 0.0) + diff 
    534                                         entry.setAccountBalance(balancevalue, newlifetimepaid, options["comment"]) 
    535                                     changed[entry.Name]["balance"] = balance 
    536                                      
    537                             for groupname in groupnames :         
    538                                 # not executed if option --ingroups is not used 
    539                                 if groupname not in changed[entry.Name]["ingroups"] : 
    540                                     group = self.storage.getGroup(groupname) 
    541                                     if group.Exists : 
    542                                         self.storage.addUserToGroup(entry, group) 
    543                                         changed[entry.Name]["ingroups"].append(groupname) 
    544                                     else : 
    545                                         self.printInfo(_("Group %s not found in the PyKota Storage.") % groupname) 
    546                              
    547             # Now outputs the list of nonexistent users and groups                 
    548             for name in missingusers.keys() : 
    549                 self.printInfo(_("Nonexistent user %s or missing print quota entry.") % name, level="warn") 
    550             for name in missinggroups.keys() : 
    551                 self.printInfo(_("Nonexistent group %s or missing print quota entry.") % name, level="warn") 
    552              
    553278            sys.stdout.write("\nDone.\n")     
    554279                      
     
    559284        defaults = { \ 
    560285                     "printer" : "*", \ 
    561                      "comment" : "", \ 
    562286                   } 
    563         short_options = "vhdo:c:C:l:b:i:naugrp:P:S:H:G:RU:I:" 
    564         long_options = ["help", "version", "comment=", \ 
    565                         "overcharge=", "charge=", "delete", "limitby=", \ 
    566                         "balance=", "ingroups=", "noquota", "add", "users", \ 
    567                         "groups", "reset", "hardreset", "prototype=", \ 
    568                         "printer=", "softlimit=", "hardlimit=", "pgroups=", \ 
     287        short_options = "vhdnagrP:S:H:G:RU:I:" 
     288        long_options = ["help", "version", \ 
     289                        "delete", \ 
     290                        "noquota", "add", \ 
     291                        "groups", "reset", "hardreset", \ 
     292                        "printer=", "softlimit=", "hardlimit=", \ 
    569293                        "increase=", "used="] 
    570294         
    571295        # Initializes the command line tool 
    572         editor = EdPyKota(doc=__doc__) 
    573         editor.deferredInit() 
     296        manager = EdPyKota(doc=__doc__) 
     297        manager.deferredInit() 
    574298         
    575299        # parse and checks the command line 
    576         (options, args) = editor.parseCommandline(sys.argv[1:], short_options, long_options) 
     300        (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) 
    577301         
    578302        # sets long options 
     
    580304        options["version"] = options["v"] or options["version"] 
    581305        options["add"] = options["a"] or options["add"] 
    582         options["users"] = options["u"] or options["users"] 
    583306        options["groups"] = options["g"] or options["groups"] 
    584         options["prototype"] = options["p"] or options["prototype"] 
    585307        options["printer"] = options["P"] or options["printer"] or defaults["printer"] 
    586308        options["softlimit"] = options["S"] or options["softlimit"] 
     
    588310        options["reset"] = options["r"] or options["reset"]  
    589311        options["noquota"] = options["n"] or options["noquota"] 
    590         options["limitby"] = options["l"] or options["limitby"] 
    591         options["balance"] = options["b"] or options["balance"]  
    592312        options["delete"] = options["d"] or options["delete"]  
    593         options["ingroups"] = options["i"] or options["ingroups"] 
    594         options["charge"] = options["c"] or options["charge"] 
    595         options["pgroups"] = options["G"] or options["pgroups"] 
    596313        options["hardreset"] = options["R"] or options["hardreset"]  
    597314        options["used"] = options["U"] or options["used"] 
    598         options["overcharge"] = options["o"] or options["overcharge"] 
    599         options["comment"] = options["C"] or options["comment"] or defaults["comment"] 
    600315        options["increase"] = options["I"] or options["increase"] 
    601316         
    602317        if options["help"] : 
    603             editor.display_usage_and_quit() 
     318            manager.display_usage_and_quit() 
    604319        elif options["version"] : 
    605             editor.display_version_and_quit() 
    606         elif options["users"] and options["groups"] :     
     320            manager.display_version_and_quit() 
     321        elif options["add"] and options["delete"] :     
    607322            raise PyKotaCommandLineError, _("incompatible options, see help.") 
    608         elif (options["add"] or options["prototype"]) and options["delete"] :     
     323        elif options["noquota"] and (options["hardlimit"] or options["softlimit"]) : 
    609324            raise PyKotaCommandLineError, _("incompatible options, see help.") 
    610         elif (options["reset"] or options["hardreset"] or options["limitby"] or options["used"] or options["balance"] or options["overcharge"] or options["softlimit"] or options["hardlimit"]) and options["prototype"] : 
    611             raise PyKotaCommandLineError, _("incompatible options, see help.") 
    612         elif options["noquota"] and (options["prototype"] or options["hardlimit"] or options["softlimit"]) : 
    613             raise PyKotaCommandLineError, _("incompatible options, see help.") 
    614         elif options["groups"] and (options["balance"] or options["ingroups"] or options["used"] or options["overcharge"]) : 
    615             raise PyKotaCommandLineError, _("incompatible options, see help.") 
    616         elif options["comment"] and not options["balance"] :     
     325        elif options["groups"] and options["used"] : 
    617326            raise PyKotaCommandLineError, _("incompatible options, see help.") 
    618327        elif options["delete"] and not args : 
    619328            raise PyKotaCommandLineError, _("You have to pass user or group names on the command line") 
    620329        else : 
    621             retcode = editor.main(args, options) 
     330            retcode = manager.main(args, options) 
    622331    except KeyboardInterrupt :         
    623332        sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     
    630339    except : 
    631340        try : 
    632             editor.crashed("edpykota failed") 
     341            manager.crashed("edpykota failed") 
    633342        except :     
    634343            crashed("edpykota failed") 
     
    636345 
    637346    try : 
    638         editor.storage.close() 
     347        manager.storage.close() 
    639348    except (TypeError, NameError, AttributeError) :     
    640349        pass