root / pykota / trunk / bin / repykota @ 873

Revision 873, 8.5 kB (checked in by jalet, 21 years ago)

GPL paragraphs were incorrectly (from memory) copied into the sources.
Two README files were added.
Upgrade script for PostgreSQL pre 1.01 schema was added.

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