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