1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | |
---|
4 | # PyKota Print Quota Warning sender |
---|
5 | # |
---|
6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
7 | # |
---|
8 | # (c) 2003, 2004, 2005 Jerome Alet <alet@librelogiciel.com> |
---|
9 | # This program is free software; you can redistribute it and/or modify |
---|
10 | # it under the terms of the GNU General Public License as published by |
---|
11 | # the Free Software Foundation; either version 2 of the License, or |
---|
12 | # (at your option) any later version. |
---|
13 | # |
---|
14 | # This program is distributed in the hope that it will be useful, |
---|
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | # GNU General Public License for more details. |
---|
18 | # |
---|
19 | # You should have received a copy of the GNU General Public License |
---|
20 | # along with this program; if not, write to the Free Software |
---|
21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | # |
---|
23 | # $Id$ |
---|
24 | # |
---|
25 | # |
---|
26 | |
---|
27 | import sys |
---|
28 | import os |
---|
29 | import pwd |
---|
30 | |
---|
31 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
32 | from pykota.config import PyKotaConfigError |
---|
33 | from pykota.storage import PyKotaStorageError |
---|
34 | |
---|
35 | __doc__ = N_("""warnpykota v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres |
---|
36 | |
---|
37 | Sends mail to users over print quota. |
---|
38 | |
---|
39 | command line usage : |
---|
40 | |
---|
41 | warnpykota [options] [names] |
---|
42 | |
---|
43 | options : |
---|
44 | |
---|
45 | -v | --version Prints warnpykota's version number then exits. |
---|
46 | -h | --help Prints this message then exits. |
---|
47 | |
---|
48 | -u | --users Warns users over their print quota, this is the |
---|
49 | default. |
---|
50 | |
---|
51 | -g | --groups Warns users whose groups quota are over limit. |
---|
52 | |
---|
53 | -P | --printer p Verify quotas on this printer only. Actually p can |
---|
54 | use wildcards characters to select only |
---|
55 | some printers. The default value is *, meaning |
---|
56 | all printers. |
---|
57 | You can specify several names or wildcards, |
---|
58 | by separating them with commas. |
---|
59 | |
---|
60 | examples : |
---|
61 | |
---|
62 | $ warnpykota --printer lp |
---|
63 | |
---|
64 | This will warn all users of the lp printer who have exceeded their |
---|
65 | print quota. |
---|
66 | |
---|
67 | $ warnpykota |
---|
68 | |
---|
69 | This will warn all users who have exceeded their print quota on |
---|
70 | any printer. |
---|
71 | |
---|
72 | $ warnpykota --groups --printer "laserjet*" "dev*" |
---|
73 | |
---|
74 | This will warn all users of groups which names begins with "dev" and |
---|
75 | who have exceeded their print quota on any printer which name begins |
---|
76 | with "laserjet" |
---|
77 | |
---|
78 | If launched by an user who is not a PyKota administrator, additionnal |
---|
79 | arguments representing users or groups names are ignored, and only the |
---|
80 | current user/group is reported. |
---|
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""") |
---|
97 | |
---|
98 | class WarnPyKota(PyKotaTool) : |
---|
99 | """A class for warnpykota.""" |
---|
100 | def main(self, ugnames, options) : |
---|
101 | """Warn users or groups over print quota.""" |
---|
102 | if self.config.isAdmin : |
---|
103 | # PyKota administrator |
---|
104 | if not ugnames : |
---|
105 | # no username, means all usernames |
---|
106 | ugnames = [ "*" ] |
---|
107 | else : |
---|
108 | # not a PyKota administrator |
---|
109 | # warns only the current user |
---|
110 | # the utility of this is discutable, but at least it |
---|
111 | # protects other users from mail bombing if they are |
---|
112 | # over quota. |
---|
113 | username = pwd.getpwuid(os.geteuid())[0] |
---|
114 | if options["groups"] : |
---|
115 | user = self.storage.getUser(username) |
---|
116 | if user.Exists : |
---|
117 | ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] |
---|
118 | else : |
---|
119 | ugnames = [ ] |
---|
120 | else : |
---|
121 | ugnames = [ username ] |
---|
122 | |
---|
123 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
124 | if not printers : |
---|
125 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
126 | alreadydone = {} |
---|
127 | for printer in printers : |
---|
128 | if options["groups"] : |
---|
129 | for (group, grouppquota) in self.storage.getPrinterGroupsAndQuotas(printer, ugnames) : |
---|
130 | self.warnGroupPQuota(grouppquota) |
---|
131 | else : |
---|
132 | for (user, userpquota) in self.storage.getPrinterUsersAndQuotas(printer, ugnames) : |
---|
133 | # we only want to warn users who have ever printed something |
---|
134 | # and don't want to warn users who have never printed |
---|
135 | if (user.AccountBalance and (user.AccountBalance != user.LifeTimePaid)) or \ |
---|
136 | userpquota.PageCounter or userpquota.LifePageCounter or \ |
---|
137 | self.storage.getUserNbJobsFromHistory(user) : |
---|
138 | done = alreadydone.get(user.Name) |
---|
139 | if (user.LimitBy.lower() != 'balance') or not done : |
---|
140 | action = self.warnUserPQuota(userpquota) |
---|
141 | if not done : |
---|
142 | alreadydone[user.Name] = (action in ('WARN', 'DENY')) |
---|
143 | |
---|
144 | if __name__ == "__main__" : |
---|
145 | retcode = 0 |
---|
146 | try : |
---|
147 | defaults = { \ |
---|
148 | "printer" : "*", \ |
---|
149 | } |
---|
150 | short_options = "vhugP:" |
---|
151 | long_options = ["help", "version", "users", "groups", "printer="] |
---|
152 | |
---|
153 | # Initializes the command line tool |
---|
154 | sender = WarnPyKota(doc=__doc__) |
---|
155 | |
---|
156 | # parse and checks the command line |
---|
157 | (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
158 | |
---|
159 | # sets long options |
---|
160 | options["help"] = options["h"] or options["help"] |
---|
161 | options["version"] = options["v"] or options["version"] |
---|
162 | options["users"] = options["u"] or options["users"] |
---|
163 | options["groups"] = options["g"] or options["groups"] |
---|
164 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
165 | |
---|
166 | if options["help"] : |
---|
167 | sender.display_usage_and_quit() |
---|
168 | elif options["version"] : |
---|
169 | sender.display_version_and_quit() |
---|
170 | elif options["users"] and options["groups"] : |
---|
171 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
172 | else : |
---|
173 | retcode = sender.main(args, options) |
---|
174 | except SystemExit : |
---|
175 | pass |
---|
176 | except : |
---|
177 | try : |
---|
178 | sender.crashed("warnpykota failed") |
---|
179 | except : |
---|
180 | crashed("warnpykota failed") |
---|
181 | retcode = -1 |
---|
182 | |
---|
183 | try : |
---|
184 | sender.storage.close() |
---|
185 | except (TypeError, NameError, AttributeError) : |
---|
186 | pass |
---|
187 | |
---|
188 | sys.exit(retcode) |
---|