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