root / pykota / trunk / bin / repykota @ 900

Revision 900, 9.0 kB (checked in by jalet, 21 years ago)

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