root / pykota / trunk / bin / repykota @ 768

Revision 752, 6.4 kB (checked in by jalet, 22 years ago)

Typos

  • 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.6  2003/02/07 23:39:16  jalet
20# Typos
21#
22# Revision 1.5  2003/02/07 08:38:36  jalet
23# Missing conversion.
24# empty line between two printers
25#
26# Revision 1.4  2003/02/07 08:34:15  jalet
27# Test wrt date limit was wrong
28#
29# Revision 1.3  2003/02/07 00:08:52  jalet
30# Typos
31#
32# Revision 1.2  2003/02/06 23:58:05  jalet
33# repykota should be ok
34#
35#
36#
37
38import sys
39
40from mx import DateTime
41
42from pykota import version
43from pykota.tool import PyKotaTool, PyKotaToolError
44
45__doc__ = """repykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres
46
47Generates print quota reports.
48
49command line usage :
50
51  repykota [options]
52
53options :
54
55  -v | --version       Prints repykota's version number then exits.
56  -h | --help          Prints this message then exits.
57 
58  -u | --users         Generates a report on users quota, this is
59                       the default.
60 
61  -g | --groups        Generates a report on group quota instead of users.
62 
63  -P | --printer p     Report quotas on this printer only. Actually p can
64                       use wildcards characters to select only
65                       some printers. The default value is *, meaning
66                       all printers.
67 
68examples :                             
69
70  $ repykota --printer lp
71 
72  This will print the quota status for all users who use the lp printer.
73
74  $ repykota
75 
76  This will print the quota status for all users on all printers.
77
78This program is free software; you can redistribute it and/or modify
79it under the terms of the GNU General Public License as published by
80the Free Software Foundation; either version 2 of the License, or
81(at your option) any later version.
82
83This program is distributed in the hope that it will be useful,
84but WITHOUT ANY WARRANTY; without even the implied warranty of
85MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
86GNU General Public License for more details.
87
88You should have received a copy of the GNU General Public License
89along with this program; if not, write to the Free Software
90Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
91
92Please e-mail bugs to: %s""" % (version.__version__, version.__author__)
93       
94class RePyKota(PyKotaTool) :       
95    """A class for repykota."""
96    def main(self, options) :
97        """Print Quota reports generator."""
98        printernames = self.storage.getMatchingPrinters(options["printer"])
99        if not printernames :
100            raise PyKotaToolError, "There's no printer matching %s" % options["printer"]
101        for printer in printernames :
102            print "*** Report for %s quota on printer %s" % ((options["users"] and "user") or "group", printer)
103            print "Pages grace time: %idays" % self.config.getGraceDelay()
104            if options["users"] :
105                print "User            used    soft    hard  grace"
106                print "-----------------------------------------------"
107                for name in self.storage.getPrinterUsers(printer) :
108                    quota = self.storage.getUserPQuota(name, printer)
109                    self.printQuota(name, quota)
110            else :
111                print "Group           used    soft    hard  grace"
112                print "-----------------------------------------------"
113                for name in self.storage.getPrinterGroups(printer) :
114                    quota = self.storage.getGroupPQuota(name, printer) 
115                    self.printQuota(name, quota)
116            print       
117                       
118    def printQuota(self, name, quota) :
119        """Prints the quota information."""
120        if quota is not None :
121            pagecounter = quota["pagecounter"]
122            softlimit = quota["softlimit"] or 0
123            hardlimit = quota["hardlimit"] or 0
124            datelimit = quota["datelimit"]
125            if datelimit is not None :
126                now = DateTime.now()
127                datelimit = DateTime.ISO.ParseDateTime(datelimit)
128                if now >= datelimit :
129                    datelimit = "DENY"
130            else :   
131                datelimit = ""
132            reached = ((pagecounter >= softlimit) and "+") or "-"
133            print "%-10.10s %c %7i %7i %7i %s" % (name, reached, pagecounter, softlimit, hardlimit, str(datelimit)[:10])
134       
135                   
136if __name__ == "__main__" : 
137    try :
138        defaults = { \
139                     "users"  : 1, \
140                     "groups" : 0, \
141                     "printer" : "*", \
142                   }
143        short_options = "vhugP:"
144        long_options = ["help", "version", "users", "groups", "printer="]
145       
146        # Initializes the command line tool
147        reporter = RePyKota(doc=__doc__)
148       
149        # parse and checks the command line
150        (options, args) = reporter.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
151       
152        # sets long options
153        options["help"] = options["h"] or options["help"]
154        options["version"] = options["v"] or options["version"]
155        options["users"] = options["u"] or options["users"] or defaults["users"]
156        options["groups"] = options["g"] or options["groups"] or defaults["groups"]
157        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
158       
159        if options["help"] :
160            reporter.display_usage_and_quit()
161        elif options["version"] :
162            reporter.display_version_and_quit()
163        elif options["users"] and options["groups"] :   
164            raise PyKotaToolError, "repykota: options --users and --groups are incompatible."
165        elif options["groups"] :   
166            raise PyKotaToolError, "repykota: options --groups is currently not implemented."
167        elif args :   
168            raise PyKotaToolError, "repykota: unused arguments [%s]. Aborting." % ", ".join(args)
169        else :
170            sys.exit(reporter.main(options))
171    except PyKotaToolError, msg :           
172        sys.stderr.write("%s\n" % msg)
173        sys.stderr.flush()
174        sys.exit(-1)
Note: See TracBrowser for help on using the browser.