root / pykota / trunk / bin / repykota @ 733

Revision 733, 6.2 kB (checked in by jalet, 21 years ago)

Test wrt date limit was wrong

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