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