root / pykota / trunk / bin / repykota @ 855

Revision 843, 7.9 kB (checked in by jalet, 22 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
Line 
1#! /usr/bin/env python
2
3# PyKota Print Quota Reports generator
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$
19# Revision 1.16  2003/03/09 23:56:21  jalet
20# Option noquota added to do accounting only.
21#
22# Revision 1.15  2003/03/09 23:39:14  jalet
23# Simplified translations.
24#
25# Revision 1.14  2003/02/27 09:04:02  jalet
26# Missing translation
27#
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#
33# Revision 1.12  2003/02/17 23:02:23  jalet
34# getGraceDelay for printer
35#
36# Revision 1.11  2003/02/10 12:12:34  jalet
37# Translations.
38#
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#
42# Revision 1.9  2003/02/09 13:40:29  jalet
43# typo
44#
45# Revision 1.8  2003/02/09 12:56:53  jalet
46# Internationalization begins...
47#
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#
52# Revision 1.6  2003/02/07 23:39:16  jalet
53# Typos
54#
55# Revision 1.5  2003/02/07 08:38:36  jalet
56# Missing conversion.
57# empty line between two printers
58#
59# Revision 1.4  2003/02/07 08:34:15  jalet
60# Test wrt date limit was wrong
61#
62# Revision 1.3  2003/02/07 00:08:52  jalet
63# Typos
64#
65# Revision 1.2  2003/02/06 23:58:05  jalet
66# repykota should be ok
67#
68#
69#
70
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 
105  This will print the quota status for all users who use the lp printer.
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 :
133            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
134        for (printer, printerpagecounter) in printernames :
135            print _("*** Report for %s quota on printer %s") % ((options["users"] and "user") or "group", printer)
136            print _("Pages grace time: %idays") % self.config.getGraceDelay(printer)
137            total = 0
138            if options["users"] :
139                print _("User             used     soft     hard   grace        total")
140                print "------------------------------------------------------------"
141                for name in self.storage.getPrinterUsers(printer) :
142                    quota = self.storage.getUserPQuota(name, printer)
143                    total += self.printQuota(name, quota)
144            else :
145                print _("Group            used     soft     hard   grace        total")
146                print "------------------------------------------------------------"
147                for name in self.storage.getPrinterGroups(printer) :
148                    quota = self.storage.getGroupPQuota(name, printer) 
149                    total += self.printQuota(name, quota)
150            if total :       
151                print (" " * 43) + (_("Total : %9i") % total)
152            if printerpagecounter is None :   
153                msg = _("unknown")
154            else :
155                msg = "%9i" % printerpagecounter
156            print (" " * 44) + (_("Real : %s") % msg)
157            print       
158                       
159    def printQuota(self, name, quota) :
160        """Prints the quota information."""
161        if quota is not None :
162            lifepagecounter = quota["lifepagecounter"]
163            pagecounter = quota["pagecounter"]
164            softlimit = quota["softlimit"]
165            hardlimit = quota["hardlimit"]
166            datelimit = quota["datelimit"]
167            if datelimit is not None :
168                now = DateTime.now()
169                datelimit = DateTime.ISO.ParseDateTime(datelimit)
170                if now >= datelimit :
171                    datelimit = "DENY"
172            else :   
173                datelimit = ""
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)
176            return lifepagecounter
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"] :   
207            raise PyKotaToolError, _("incompatible options, see help.")
208        elif options["groups"] :   
209            raise PyKotaToolError, _("option --groups is currently not implemented.")
210        elif args :   
211            raise PyKotaToolError, _("unused arguments [%s]. Aborting.") % ", ".join(args)
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.