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