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