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