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 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 | # $Log$ |
---|
26 | # Revision 1.24 2003/10/07 09:07:28 jalet |
---|
27 | # Character encoding added to please latest version of Python |
---|
28 | # |
---|
29 | # Revision 1.23 2003/08/18 16:35:28 jalet |
---|
30 | # New pychecker pass, on the tools this time. |
---|
31 | # |
---|
32 | # Revision 1.22 2003/07/29 20:55:17 jalet |
---|
33 | # 1.14 is out ! |
---|
34 | # |
---|
35 | # Revision 1.21 2003/07/07 12:19:52 jalet |
---|
36 | # Now repykota and warnpykota --groups check the groups the user is a member of |
---|
37 | # in the print quota database, not in the system passwd/group files |
---|
38 | # |
---|
39 | # Revision 1.20 2003/06/25 14:10:01 jalet |
---|
40 | # Hey, it may work (edpykota --reset excepted) ! |
---|
41 | # |
---|
42 | # Revision 1.19 2003/04/29 22:03:38 jalet |
---|
43 | # Better error handling. |
---|
44 | # |
---|
45 | # Revision 1.18 2003/04/23 22:13:56 jalet |
---|
46 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
47 | # |
---|
48 | # Revision 1.17 2003/04/17 13:32:17 jalet |
---|
49 | # bad documentation string |
---|
50 | # |
---|
51 | # Revision 1.16 2003/04/16 12:35:49 jalet |
---|
52 | # Groups quota work now ! |
---|
53 | # |
---|
54 | # Revision 1.15 2003/04/10 21:47:20 jalet |
---|
55 | # Job history added. Upgrade script neutralized for now ! |
---|
56 | # |
---|
57 | # Revision 1.14 2003/04/08 21:31:39 jalet |
---|
58 | # (anything or 0) = anything !!! Go back to school Jerome ! |
---|
59 | # |
---|
60 | # Revision 1.13 2003/04/08 21:13:44 jalet |
---|
61 | # Prepare --groups option to work. |
---|
62 | # |
---|
63 | # Revision 1.12 2003/04/08 21:10:18 jalet |
---|
64 | # Checks --groups option presence instead of --users because --users is the default. |
---|
65 | # |
---|
66 | # Revision 1.11 2003/03/29 13:45:27 jalet |
---|
67 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
68 | # Two README files were added. |
---|
69 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
70 | # |
---|
71 | # Revision 1.10 2003/03/25 11:45:32 jalet |
---|
72 | # Clearer help. |
---|
73 | # |
---|
74 | # Revision 1.9 2003/03/09 23:39:14 jalet |
---|
75 | # Simplified translations. |
---|
76 | # |
---|
77 | # Revision 1.8 2003/02/10 12:07:30 jalet |
---|
78 | # Now repykota should output the recorded total page number for each printer too. |
---|
79 | # |
---|
80 | # Revision 1.7 2003/02/09 13:40:29 jalet |
---|
81 | # typo |
---|
82 | # |
---|
83 | # Revision 1.6 2003/02/09 12:56:53 jalet |
---|
84 | # Internationalization begins... |
---|
85 | # |
---|
86 | # Revision 1.5 2003/02/07 23:24:38 jalet |
---|
87 | # Empty line deleted |
---|
88 | # |
---|
89 | # Revision 1.4 2003/02/06 23:25:40 jalet |
---|
90 | # Cleaner docstring |
---|
91 | # |
---|
92 | # Revision 1.3 2003/02/06 23:20:02 jalet |
---|
93 | # warnpykota doesn't need any user/group name argument, mimicing the |
---|
94 | # warnquota disk quota tool. |
---|
95 | # |
---|
96 | # Revision 1.2 2003/02/06 22:54:33 jalet |
---|
97 | # warnpykota should be ok |
---|
98 | # |
---|
99 | # |
---|
100 | # |
---|
101 | |
---|
102 | import sys |
---|
103 | import os |
---|
104 | import pwd |
---|
105 | |
---|
106 | from pykota import version |
---|
107 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
108 | from pykota.config import PyKotaConfigError |
---|
109 | from pykota.storage import PyKotaStorageError |
---|
110 | |
---|
111 | __doc__ = """warnpykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres |
---|
112 | |
---|
113 | Sends mail to users over print quota. |
---|
114 | |
---|
115 | command line usage : |
---|
116 | |
---|
117 | warnpykota [options] [names] |
---|
118 | |
---|
119 | options : |
---|
120 | |
---|
121 | -v | --version Prints warnpykota's version number then exits. |
---|
122 | -h | --help Prints this message then exits. |
---|
123 | |
---|
124 | -u | --users Warns users over their print quota, this is the |
---|
125 | default. |
---|
126 | |
---|
127 | -g | --groups Warns users whose groups quota are over limit. |
---|
128 | |
---|
129 | -P | --printer p Verify quotas on this printer only. Actually p can |
---|
130 | use wildcards characters to select only |
---|
131 | some printers. The default value is *, meaning |
---|
132 | all printers. |
---|
133 | |
---|
134 | examples : |
---|
135 | |
---|
136 | $ warnpykota --printer lp |
---|
137 | |
---|
138 | This will warn all users of the lp printer who have exceeded their |
---|
139 | print quota. |
---|
140 | |
---|
141 | $ warnpykota |
---|
142 | |
---|
143 | This will warn all users who have exceeded their print quota on |
---|
144 | any printer. |
---|
145 | |
---|
146 | $ warnpykota --groups --printer "laserjet*" "dev*" |
---|
147 | |
---|
148 | This will warn all users of groups which names begins with "dev" and |
---|
149 | who have exceeded their print quota on any printer which name begins |
---|
150 | with "laserjet" |
---|
151 | |
---|
152 | If launched by a non-root user, additionnal arguments representing |
---|
153 | users or groups names are ignored, and only the current user/group |
---|
154 | is warned. |
---|
155 | |
---|
156 | This program is free software; you can redistribute it and/or modify |
---|
157 | it under the terms of the GNU General Public License as published by |
---|
158 | the Free Software Foundation; either version 2 of the License, or |
---|
159 | (at your option) any later version. |
---|
160 | |
---|
161 | This program is distributed in the hope that it will be useful, |
---|
162 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
163 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
164 | GNU General Public License for more details. |
---|
165 | |
---|
166 | You should have received a copy of the GNU General Public License |
---|
167 | along with this program; if not, write to the Free Software |
---|
168 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
169 | |
---|
170 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
171 | |
---|
172 | class WarnPyKota(PyKotaTool) : |
---|
173 | """A class for warnpykota.""" |
---|
174 | def main(self, ugnames, options) : |
---|
175 | """Warn users or groups over print quota.""" |
---|
176 | uid = os.geteuid() |
---|
177 | if not uid : |
---|
178 | # root user |
---|
179 | if not ugnames : |
---|
180 | # no username, means all usernames |
---|
181 | ugnames = [ "*" ] |
---|
182 | else : |
---|
183 | # not the root user |
---|
184 | # warns only the current user |
---|
185 | # the utility of this is discutable, but at least it |
---|
186 | # protects other users from mail bombing if they are |
---|
187 | # over quota. |
---|
188 | username = pwd.getpwuid(uid)[0] |
---|
189 | if options["groups"] : |
---|
190 | user = self.storage.getUser(username) |
---|
191 | if user.Exists : |
---|
192 | ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] |
---|
193 | else : |
---|
194 | ugnames = [ ] |
---|
195 | else : |
---|
196 | ugnames = [ username ] |
---|
197 | |
---|
198 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
199 | if not printers : |
---|
200 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
201 | for printer in printers : |
---|
202 | if options["groups"] : |
---|
203 | for (group, grouppquota) in self.storage.getPrinterGroupsAndQuotas(printer, ugnames) : |
---|
204 | self.warnGroupPQuota(grouppquota) |
---|
205 | else : |
---|
206 | for (user, userpquota) in self.storage.getPrinterUsersAndQuotas(printer, ugnames) : |
---|
207 | self.warnUserPQuota(userpquota) |
---|
208 | |
---|
209 | if __name__ == "__main__" : |
---|
210 | retcode = 0 |
---|
211 | try : |
---|
212 | defaults = { \ |
---|
213 | "printer" : "*", \ |
---|
214 | } |
---|
215 | short_options = "vhugP:" |
---|
216 | long_options = ["help", "version", "users", "groups", "printer="] |
---|
217 | |
---|
218 | # Initializes the command line tool |
---|
219 | sender = WarnPyKota(doc=__doc__) |
---|
220 | |
---|
221 | # parse and checks the command line |
---|
222 | (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
223 | |
---|
224 | # sets long options |
---|
225 | options["help"] = options["h"] or options["help"] |
---|
226 | options["version"] = options["v"] or options["version"] |
---|
227 | options["users"] = options["u"] or options["users"] |
---|
228 | options["groups"] = options["g"] or options["groups"] |
---|
229 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
230 | |
---|
231 | if options["help"] : |
---|
232 | sender.display_usage_and_quit() |
---|
233 | elif options["version"] : |
---|
234 | sender.display_version_and_quit() |
---|
235 | elif options["users"] and options["groups"] : |
---|
236 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
237 | else : |
---|
238 | retcode = sender.main(args, options) |
---|
239 | except (PyKotaToolError, PyKotaConfigError, PyKotaStorageError), msg : |
---|
240 | sys.stderr.write("%s\n" % msg) |
---|
241 | sys.stderr.flush() |
---|
242 | retcode = -1 |
---|
243 | |
---|
244 | try : |
---|
245 | sender.storage.close() |
---|
246 | except (TypeError, NameError, AttributeError) : |
---|
247 | pass |
---|
248 | |
---|
249 | sys.exit(retcode) |
---|