Show
Ignore:
Timestamp:
06/25/03 16:10:01 (21 years ago)
Author:
jalet
Message:

Hey, it may work (edpykota --reset excepted) !

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/repykota

    r1030 r1041  
    2323# 
    2424# $Log$ 
     25# Revision 1.39  2003/06/25 14:10:01  jalet 
     26# Hey, it may work (edpykota --reset excepted) ! 
     27# 
    2528# Revision 1.38  2003/06/15 22:26:52  jalet 
    2629# More work on LDAP 
     
    147150 
    148151import sys 
     152import os 
     153import pwd 
     154import grp 
    149155 
    150156from mx import DateTime 
     
    187193   
    188194  This will print the quota status for all users on all printers. 
     195   
     196  $ repykota --printer "laser*" jerome "jo*" 
     197   
     198  This will print the quota status for user jerome and all users 
     199  whose name begins with "jo" on all printers which name begin 
     200  with "laser" 
     201   
     202  If launched by a non-root user, additionnal arguments representing 
     203  users or groups names are ignored, and only the current user/group 
     204  is reported. 
    189205 
    190206This program is free software; you can redistribute it and/or modify 
     
    206222class RePyKota(PyKotaTool) :         
    207223    """A class for repykota.""" 
    208     def main(self, options) : 
     224    def main(self, ugnames, options) : 
    209225        """Print Quota reports generator.""" 
     226        uid = os.geteuid() 
     227        if not uid : 
     228            # root user 
     229            if not ugnames : 
     230                # no username, means all usernames 
     231                ugnames = [ "*" ] 
     232        else :         
     233            # not the root user 
     234            # reports only the current user 
     235            if options["groups"] : 
     236                ugnames = [ grp.getgrgid(pwd.getpwuid(uid)[3])[0] ] 
     237            else : 
     238                ugnames = [ pwd.getpwuid(uid)[0] ] 
     239         
    210240        printers = self.storage.getMatchingPrinters(options["printer"]) 
    211241        if not printers : 
    212242            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] 
    213         for (printerid, printer) in printers : 
    214             print _("*** Report for %s quota on printer %s") % ((options["groups"] and "group") or "user", printer) 
    215             print _("Pages grace time: %i days") % self.config.getGraceDelay(printer) 
    216             prices = self.storage.getPrinterPrices(printerid) 
    217             if prices is None : 
    218                 (perpage, perjob) = (None, None) 
    219             else :     
    220                 (perpage, perjob) = prices 
    221             if perjob is not None : 
    222                 print _("Price per job: %.3f") % perjob 
    223             if perpage is not None :     
    224                 print _("Price per page: %.3f") % perpage 
     243        for printer in printers : 
     244            print _("*** Report for %s quota on printer %s") % ((options["groups"] and "group") or "user", printer.Name) 
     245            print _("Pages grace time: %i days") % self.config.getGraceDelay(printer.Name) 
     246            if printer.PricePerJob is not None : 
     247                print _("Price per job: %.3f") % printer.PricePerJob 
     248            if printer.PricePerPage is not None :     
     249                print _("Price per page: %.3f") % printer.PricePerPage 
    225250            total = 0 
    226251            totalmoney = 0.0 
     
    228253                print _("Group           used    soft    hard    balance grace         total       paid")  
    229254                print "------------------------------------------------------------------------------" 
    230                 for (ident, name) in (self.storage.getPrinterGroups(printerid) or []) : 
    231                     quota = self.storage.getGroupPQuota(ident, printerid)  
    232                     balance = self.storage.getGroupBalance(ident) 
    233                     limitby = self.storage.getGroupLimitBy(ident) 
    234                     (pages, money) = self.printQuota(name, quota, balance, limitby) 
     255                for (group, grouppquota) in self.storage.getPrinterGroupsAndQuotas(printer, ugnames) : 
     256                    (pages, money) = self.printQuota(group, grouppquota) 
    235257                    total += pages 
    236258                    totalmoney += money 
     
    239261                print _("User            used    soft    hard    balance grace         total       paid")  
    240262                print "------------------------------------------------------------------------------" 
    241                 for (ident, name) in (self.storage.getPrinterUsers(printerid) or []) : 
    242                     quota = self.storage.getUserPQuota(ident, printerid) 
    243                     balance = self.storage.getUserBalance(ident) 
    244                     limitby = self.storage.getUserLimitBy(ident) 
    245                     (pages, money) = self.printQuota(name, quota, balance, limitby) 
     263                for (user, userpquota) in self.storage.getPrinterUsersAndQuotas(printer, ugnames) : 
     264                    (pages, money) = self.printQuota(user, userpquota) 
    246265                    total += pages 
    247266                    totalmoney += money 
    248267            if total or totalmoney :         
    249268                print (" " * 50) + (_("Total : %9i") % total) + ("%11s" % ("%7.2f" % totalmoney)[:11]) 
    250             printerpagecounter = self.storage.getPrinterPageCounter(printerid) 
    251269            try : 
    252                 msg = "%9i" % printerpagecounter["pagecounter"] 
     270                msg = "%9i" % printer.LastJob.PrinterPageCounter 
    253271            except TypeError :      
    254272                msg = _("unknown") 
     
    258276            print _("Totals may be inaccurate if some users are members of several groups.") 
    259277                         
    260     def printQuota(self, name, quota, balance, limitby) : 
     278    def printQuota(self, entry, quota) : 
    261279        """Prints the quota information.""" 
    262         if quota is not None : 
    263             lifepagecounter = quota["lifepagecounter"] or 0 
    264             pagecounter = quota["pagecounter"] or 0 
    265             softlimit = quota["softlimit"] 
    266             hardlimit = quota["hardlimit"] 
    267             datelimit = quota["datelimit"] 
    268             if balance is not None : 
    269                 (balance, lifetimepaid) = balance 
    270             else :     
    271                 (balance, lifetimepaid) = (0.0, 0.0) 
    272             if datelimit is not None : 
    273                 now = DateTime.now() 
    274                 datelimit = DateTime.ISO.ParseDateTime(datelimit) 
    275                 if now >= datelimit : 
    276                     datelimit = "DENY" 
    277             else :     
    278                 datelimit = "" 
    279             if limitby == "balance" :     
    280                 reached = (((balance <= 0) and "+") or "-") + "B" 
    281             else : 
    282                 reached = (((softlimit is not None) and (pagecounter >= softlimit) and "+") or "-") + "Q" 
    283             balance = balance or 0.0     
    284             lifetimepaid = lifetimepaid or 0.0 
    285             strbalance = ("%5.2f" % balance)[:10] 
    286             strlifetimepaid = ("%6.2f" % lifetimepaid)[:10] 
    287             print "%-9.9s %s %7i %7s %7s %10s %-10.10s %8i %10s" % (name, reached, pagecounter, str(softlimit), str(hardlimit), strbalance, str(datelimit)[:10], lifepagecounter, strlifetimepaid) 
    288             return (lifepagecounter, lifetimepaid) 
     280        lifepagecounter = int(quota.LifePageCounter or 0) 
     281        pagecounter = int(quota.PageCounter or 0) 
     282        balance = float(entry.AccountBalance or 0.0) 
     283        lifetimepaid = float(entry.LifeTimePaid or 0.0) 
     284         
     285        if quota.DateLimit is not None : 
     286            now = DateTime.now() 
     287            datelimit = DateTime.ISO.ParseDateTime(quota.DateLimit) 
     288            if now >= datelimit : 
     289                datelimit = "DENY" 
     290        elif (quota.HardLimit is not None) and (pagecounter >= quota.HardLimit) :     
     291            datelimit = "DENY" 
     292        elif (quota.HardLimit is None) and (quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) : 
     293            datelimit = "DENY" 
    289294        else :     
    290             return (0, 0) 
    291          
     295            datelimit = "" 
     296             
     297        if entry.LimitBy.lower() == "balance" :     
     298            reached = (((balance <= 0) and "+") or "-") + "B" 
     299        else : 
     300            reached = (((quota.SoftLimit is not None) and (pagecounter >= quota.SoftLimit) and "+") or "-") + "Q" 
     301             
     302        strbalance = ("%5.2f" % balance)[:10] 
     303        strlifetimepaid = ("%6.2f" % lifetimepaid)[:10] 
     304        print "%-9.9s %s %7i %7s %7s %10s %-10.10s %8i %10s" % (entry.Name, reached, pagecounter, str(quota.SoftLimit), str(quota.HardLimit), strbalance, str(datelimit)[:10], lifepagecounter, strlifetimepaid) 
     305        return (lifepagecounter, lifetimepaid) 
    292306                     
    293307if __name__ == "__main__" :  
     
    318332        elif options["users"] and options["groups"] :     
    319333            raise PyKotaToolError, _("incompatible options, see help.") 
    320         elif args :     
    321             raise PyKotaToolError, _("unused arguments [%s]. Aborting.") % ", ".join(args) 
    322334        else : 
    323             sys.exit(reporter.main(options)) 
     335            sys.exit(reporter.main(args, options)) 
    324336    except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError), msg :             
    325337        sys.stderr.write("%s\n" % msg)