root / pykota / trunk / bin / repykota @ 843

Revision 843, 7.9 kB (checked in by jalet, 21 years ago)

Option noquota added to do accounting only.

  • 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
2
[731]3# PyKota Print Quota Reports generator
[696]4#
5# PyKota - Print Quotas for CUPS
6#
7# (c) 2003 Jerome Alet <alet@librelogiciel.com>
8# You're welcome to redistribute this software under the
9# terms of the GNU General Public Licence version 2.0
10# or, at your option, any higher version.
11#
12# You can read the complete GNU GPL in the file COPYING
13# which should come along with this software, or visit
14# the Free Software Foundation's WEB site http://www.fsf.org
15#
16# $Id$
17#
18# $Log$
[843]19# Revision 1.16  2003/03/09 23:56:21  jalet
20# Option noquota added to do accounting only.
21#
[842]22# Revision 1.15  2003/03/09 23:39:14  jalet
23# Simplified translations.
24#
[814]25# Revision 1.14  2003/02/27 09:04:02  jalet
26# Missing translation
27#
[813]28# Revision 1.13  2003/02/27 08:44:01  jalet
29# Check to see if the printer was ever used at all, and displays "unknown"
30# as the pagecounter value in this casCheck to see if the printer was ever used at all, and displays "unknown"
31# as the pagecounter value in this case.
32#
[805]33# Revision 1.12  2003/02/17 23:02:23  jalet
34# getGraceDelay for printer
35#
[793]36# Revision 1.11  2003/02/10 12:12:34  jalet
37# Translations.
38#
[791]39# Revision 1.10  2003/02/10 12:07:30  jalet
40# Now repykota should output the recorded total page number for each printer too.
41#
[775]42# Revision 1.9  2003/02/09 13:40:29  jalet
43# typo
44#
[772]45# Revision 1.8  2003/02/09 12:56:53  jalet
46# Internationalization begins...
47#
[770]48# Revision 1.7  2003/02/08 23:17:20  jalet
49# repykota now outputs life time page counters and the total pages printed by
50# all users/groups on each printer.
51#
[752]52# Revision 1.6  2003/02/07 23:39:16  jalet
53# Typos
54#
[734]55# Revision 1.5  2003/02/07 08:38:36  jalet
56# Missing conversion.
57# empty line between two printers
58#
[733]59# Revision 1.4  2003/02/07 08:34:15  jalet
60# Test wrt date limit was wrong
61#
[732]62# Revision 1.3  2003/02/07 00:08:52  jalet
63# Typos
64#
[731]65# Revision 1.2  2003/02/06 23:58:05  jalet
66# repykota should be ok
[696]67#
68#
69#
70
[731]71import sys
72
73from mx import DateTime
74
75from pykota import version
76from pykota.tool import PyKotaTool, PyKotaToolError
77
78__doc__ = """repykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres
79
80Generates print quota reports.
81
82command line usage :
83
84  repykota [options]
85
86options :
87
88  -v | --version       Prints repykota's version number then exits.
89  -h | --help          Prints this message then exits.
90 
91  -u | --users         Generates a report on users quota, this is
92                       the default.
93 
94  -g | --groups        Generates a report on group quota instead of users.
95 
96  -P | --printer p     Report quotas on this printer only. Actually p can
97                       use wildcards characters to select only
98                       some printers. The default value is *, meaning
99                       all printers.
100 
101examples :                             
102
103  $ repykota --printer lp
104 
[752]105  This will print the quota status for all users who use the lp printer.
[731]106
107  $ repykota
108 
109  This will print the quota status for all users on all printers.
110
111This program is free software; you can redistribute it and/or modify
112it under the terms of the GNU General Public License as published by
113the Free Software Foundation; either version 2 of the License, or
114(at your option) any later version.
115
116This program is distributed in the hope that it will be useful,
117but WITHOUT ANY WARRANTY; without even the implied warranty of
118MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
119GNU General Public License for more details.
120
121You should have received a copy of the GNU General Public License
122along with this program; if not, write to the Free Software
123Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
124
125Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
126       
127class RePyKota(PyKotaTool) :       
128    """A class for repykota."""
129    def main(self, options) :
130        """Print Quota reports generator."""
131        printernames = self.storage.getMatchingPrinters(options["printer"])
132        if not printernames :
[772]133            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
[791]134        for (printer, printerpagecounter) in printernames :
[772]135            print _("*** Report for %s quota on printer %s") % ((options["users"] and "user") or "group", printer)
[805]136            print _("Pages grace time: %idays") % self.config.getGraceDelay(printer)
[770]137            total = 0
[731]138            if options["users"] :
[772]139                print _("User             used     soft     hard   grace        total")
[770]140                print "------------------------------------------------------------"
[731]141                for name in self.storage.getPrinterUsers(printer) :
142                    quota = self.storage.getUserPQuota(name, printer)
[770]143                    total += self.printQuota(name, quota)
[731]144            else :
[772]145                print _("Group            used     soft     hard   grace        total")
[770]146                print "------------------------------------------------------------"
[731]147                for name in self.storage.getPrinterGroups(printer) :
148                    quota = self.storage.getGroupPQuota(name, printer) 
[770]149                    total += self.printQuota(name, quota)
150            if total :       
[793]151                print (" " * 43) + (_("Total : %9i") % total)
[813]152            if printerpagecounter is None :   
[814]153                msg = _("unknown")
[813]154            else :
155                msg = "%9i" % printerpagecounter
156            print (" " * 44) + (_("Real : %s") % msg)
[734]157            print       
[731]158                       
159    def printQuota(self, name, quota) :
160        """Prints the quota information."""
161        if quota is not None :
[770]162            lifepagecounter = quota["lifepagecounter"]
[731]163            pagecounter = quota["pagecounter"]
[843]164            softlimit = quota["softlimit"]
165            hardlimit = quota["hardlimit"]
[731]166            datelimit = quota["datelimit"]
167            if datelimit is not None :
168                now = DateTime.now()
169                datelimit = DateTime.ISO.ParseDateTime(datelimit)
[733]170                if now >= datelimit :
[731]171                    datelimit = "DENY"
172            else :   
173                datelimit = ""
[843]174            reached = ((softlimit is not None) and (pagecounter >= softlimit) and "+") or "-"
175            print "%-10.10s %c %8i %8s %8s %10s %9i" % (name, reached, pagecounter, str(softlimit), str(hardlimit), str(datelimit)[:10], lifepagecounter)
[770]176            return lifepagecounter
[731]177       
178                   
179if __name__ == "__main__" : 
180    try :
181        defaults = { \
182                     "users"  : 1, \
183                     "groups" : 0, \
184                     "printer" : "*", \
185                   }
186        short_options = "vhugP:"
187        long_options = ["help", "version", "users", "groups", "printer="]
188       
189        # Initializes the command line tool
190        reporter = RePyKota(doc=__doc__)
191       
192        # parse and checks the command line
193        (options, args) = reporter.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
194       
195        # sets long options
196        options["help"] = options["h"] or options["help"]
197        options["version"] = options["v"] or options["version"]
198        options["users"] = options["u"] or options["users"] or defaults["users"]
199        options["groups"] = options["g"] or options["groups"] or defaults["groups"]
200        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
201       
202        if options["help"] :
203            reporter.display_usage_and_quit()
204        elif options["version"] :
205            reporter.display_version_and_quit()
206        elif options["users"] and options["groups"] :   
[842]207            raise PyKotaToolError, _("incompatible options, see help.")
[731]208        elif options["groups"] :   
[842]209            raise PyKotaToolError, _("option --groups is currently not implemented.")
[731]210        elif args :   
[842]211            raise PyKotaToolError, _("unused arguments [%s]. Aborting.") % ", ".join(args)
[731]212        else :
213            sys.exit(reporter.main(options))
214    except PyKotaToolError, msg :           
215        sys.stderr.write("%s\n" % msg)
216        sys.stderr.flush()
217        sys.exit(-1)
Note: See TracBrowser for help on using the browser.