root / pykota / trunk / bin / repykota @ 927

Revision 927, 9.4 kB (checked in by jalet, 21 years ago)

Groups quota work now !

  • 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# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21#
22# $Id$
23#
24# $Log$
25# Revision 1.26  2003/04/16 12:35:49  jalet
26# Groups quota work now !
27#
28# Revision 1.25  2003/04/15 11:30:57  jalet
29# More work done on money print charging.
30# Minor bugs corrected.
31# All tools now access to the storage as priviledged users, repykota excepted.
32#
33# Revision 1.24  2003/04/14 20:05:20  jalet
34# Reversed test.
35#
36# Revision 1.23  2003/04/11 14:42:54  jalet
37# Translations
38#
39# Revision 1.21  2003/04/10 21:47:20  jalet
40# Job history added. Upgrade script neutralized for now !
41#
42# Revision 1.20  2003/04/08 21:31:39  jalet
43# (anything or 0) = anything !!! Go back to school Jerome !
44#
45# Revision 1.19  2003/04/08 21:13:44  jalet
46# Prepare --groups option to work.
47#
48# Revision 1.18  2003/04/08 21:10:18  jalet
49# Checks --groups option presence instead of --users because --users is the default.
50#
51# Revision 1.17  2003/03/29 13:45:27  jalet
52# GPL paragraphs were incorrectly (from memory) copied into the sources.
53# Two README files were added.
54# Upgrade script for PostgreSQL pre 1.01 schema was added.
55#
56# Revision 1.16  2003/03/09 23:56:21  jalet
57# Option noquota added to do accounting only.
58#
59# Revision 1.15  2003/03/09 23:39:14  jalet
60# Simplified translations.
61#
62# Revision 1.14  2003/02/27 09:04:02  jalet
63# Missing translation
64#
65# Revision 1.13  2003/02/27 08:44:01  jalet
66# Check to see if the printer was ever used at all, and displays "unknown"
67# as the pagecounter value in this casCheck to see if the printer was ever used at all, and displays "unknown"
68# as the pagecounter value in this case.
69#
70# Revision 1.12  2003/02/17 23:02:23  jalet
71# getGraceDelay for printer
72#
73# Revision 1.11  2003/02/10 12:12:34  jalet
74# Translations.
75#
76# Revision 1.10  2003/02/10 12:07:30  jalet
77# Now repykota should output the recorded total page number for each printer too.
78#
79# Revision 1.9  2003/02/09 13:40:29  jalet
80# typo
81#
82# Revision 1.8  2003/02/09 12:56:53  jalet
83# Internationalization begins...
84#
85# Revision 1.7  2003/02/08 23:17:20  jalet
86# repykota now outputs life time page counters and the total pages printed by
87# all users/groups on each printer.
88#
89# Revision 1.6  2003/02/07 23:39:16  jalet
90# Typos
91#
92# Revision 1.5  2003/02/07 08:38:36  jalet
93# Missing conversion.
94# empty line between two printers
95#
96# Revision 1.4  2003/02/07 08:34:15  jalet
97# Test wrt date limit was wrong
98#
99# Revision 1.3  2003/02/07 00:08:52  jalet
100# Typos
101#
102# Revision 1.2  2003/02/06 23:58:05  jalet
103# repykota should be ok
104#
105#
106#
107
108import sys
109
110from mx import DateTime
111
112from pykota import version
113from pykota.tool import PyKotaTool, PyKotaToolError
114
115__doc__ = """repykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres
116
117Generates print quota reports.
118
119command line usage :
120
121  repykota [options]
122
123options :
124
125  -v | --version       Prints repykota's version number then exits.
126  -h | --help          Prints this message then exits.
127 
128  -u | --users         Generates a report on users quota, this is
129                       the default.
130 
131  -g | --groups        Generates a report on group quota instead of users.
132 
133  -P | --printer p     Report quotas on this printer only. Actually p can
134                       use wildcards characters to select only
135                       some printers. The default value is *, meaning
136                       all printers.
137 
138examples :                             
139
140  $ repykota --printer lp
141 
142  This will print the quota status for all users who use the lp printer.
143
144  $ repykota
145 
146  This will print the quota status for all users on all printers.
147
148This program is free software; you can redistribute it and/or modify
149it under the terms of the GNU General Public License as published by
150the Free Software Foundation; either version 2 of the License, or
151(at your option) any later version.
152
153This program is distributed in the hope that it will be useful,
154but WITHOUT ANY WARRANTY; without even the implied warranty of
155MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
156GNU General Public License for more details.
157
158You should have received a copy of the GNU General Public License
159along with this program; if not, write to the Free Software
160Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
161
162Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
163       
164class RePyKota(PyKotaTool) :       
165    """A class for repykota."""
166    def __init__(self, doc) :
167        """Initializes the tool as a non-priviledged storage user."""
168        PyKotaTool.__init__(self, asadmin=0, doc=doc)
169       
170    def main(self, options) :
171        """Print Quota reports generator."""
172        printers = self.storage.getMatchingPrinters(options["printer"])
173        if not printers :
174            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
175        for (printerid, printer) in printers :
176            print _("*** Report for %s quota on printer %s") % ((options["groups"] and "group") or "user", printer)
177            print _("Pages grace time: %idays") % self.config.getGraceDelay(printer)
178            total = 0
179            if options["groups"] :
180                print _("Group            used     soft     hard   grace        total")
181                print "------------------------------------------------------------"
182                for (ident, name) in self.storage.getPrinterGroups(printerid) :
183                    quota = self.storage.getGroupPQuota(ident, printerid) 
184                    total += self.printQuota(name, quota)
185            else :
186                # default is user quota report
187                print _("User             used     soft     hard   grace        total")
188                print "------------------------------------------------------------"
189                for (ident, name) in self.storage.getPrinterUsers(printerid) :
190                    quota = self.storage.getUserPQuota(ident, printerid)
191                    total += self.printQuota(name, quota)
192            if total :       
193                print (" " * 43) + (_("Total : %9i") % total)
194            printerpagecounter = self.storage.getPrinterPageCounter(printerid)
195            try :
196                msg = "%9i" % printerpagecounter["pagecounter"]
197            except TypeError :     
198                msg = _("unknown")
199            print (" " * 44) + (_("Real : %s") % msg)
200            print       
201                       
202    def printQuota(self, name, quota) :
203        """Prints the quota information."""
204        if quota is not None :
205            lifepagecounter = quota["lifepagecounter"] or 0
206            pagecounter = quota["pagecounter"] or 0
207            softlimit = quota["softlimit"]
208            hardlimit = quota["hardlimit"]
209            datelimit = quota["datelimit"]
210            if datelimit is not None :
211                now = DateTime.now()
212                datelimit = DateTime.ISO.ParseDateTime(datelimit)
213                if now >= datelimit :
214                    datelimit = "DENY"
215            else :   
216                datelimit = ""
217            reached = ((softlimit is not None) and (pagecounter >= softlimit) and "+") or "-"
218            print "%-10.10s %c %8i %8s %8s %10s %9i" % (name, reached, pagecounter, str(softlimit), str(hardlimit), str(datelimit)[:10], lifepagecounter)
219            return lifepagecounter
220       
221                   
222if __name__ == "__main__" : 
223    try :
224        defaults = { \
225                     "printer" : "*", \
226                   }
227        short_options = "vhugP:"
228        long_options = ["help", "version", "users", "groups", "printer="]
229       
230        # Initializes the command line tool
231        reporter = RePyKota(doc=__doc__)
232       
233        # parse and checks the command line
234        (options, args) = reporter.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
235       
236        # sets long options
237        options["help"] = options["h"] or options["help"]
238        options["version"] = options["v"] or options["version"]
239        options["users"] = options["u"] or options["users"]
240        options["groups"] = options["g"] or options["groups"]
241        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
242       
243        if options["help"] :
244            reporter.display_usage_and_quit()
245        elif options["version"] :
246            reporter.display_version_and_quit()
247        elif options["users"] and options["groups"] :   
248            raise PyKotaToolError, _("incompatible options, see help.")
249        elif args :   
250            raise PyKotaToolError, _("unused arguments [%s]. Aborting.") % ", ".join(args)
251        else :
252            sys.exit(reporter.main(options))
253    except PyKotaToolError, msg :           
254        sys.stderr.write("%s\n" % msg)
255        sys.stderr.flush()
256        sys.exit(-1)
Note: See TracBrowser for help on using the browser.