root / pykota / trunk / bin / repykota @ 890

Revision 890, 8.7 kB (checked in by jalet, 21 years ago)

Checks --groups option presence instead of --users because --users is the default.

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