root / pykota / trunk / bin / warnpykota @ 2060

Revision 2028, 10.8 kB (checked in by jalet, 20 years ago)

Modified copyright years

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
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# $Log$
26# Revision 1.37  2005/01/17 08:44:24  jalet
27# Modified copyright years
28#
29# Revision 1.36  2004/10/12 17:08:32  jalet
30# alreadydone mapping was reset in each iteration, this is fixed.
31#
32# Revision 1.35  2004/10/12 09:14:03  jalet
33# Ensures that warnpykota still test other quota entries for each user if
34# he has not been warned, even if limited by his account balance.
35#
36# Revision 1.34  2004/10/12 09:09:04  jalet
37# Now warnpykota warns users limited by balance only once per launch.
38#
39# Revision 1.33  2004/10/12 08:58:53  jalet
40# Now warnpykota only warns users who have already printed, to not confuse
41# users who have just opened their account.
42#
43# Revision 1.32  2004/10/11 22:53:05  jalet
44# Postponed string interpolation to help message's output method
45#
46# Revision 1.31  2004/10/11 12:49:06  jalet
47# Renders help translatable
48#
49# Revision 1.30  2004/10/06 10:05:47  jalet
50# Minor changes to allow any PyKota administrator to launch enhanced versions
51# of the commands, and not only the root user.
52#
53# Revision 1.29  2004/06/18 13:34:49  jalet
54# Now all tracebacks include PyKota's version number
55#
56# Revision 1.28  2004/06/07 18:43:41  jalet
57# Fixed over-verbose exits when displaying help or version number
58#
59# Revision 1.27  2004/06/03 21:50:34  jalet
60# Improved error logging.
61# crashrecipient directive added.
62# Now exports the job's size in bytes too.
63#
64# Revision 1.26  2004/01/08 14:10:32  jalet
65# Copyright year changed.
66#
67# Revision 1.25  2003/10/09 21:25:26  jalet
68# Multiple printer names or wildcards can be passed on the command line
69# separated with commas.
70# Beta phase.
71#
72# Revision 1.24  2003/10/07 09:07:28  jalet
73# Character encoding added to please latest version of Python
74#
75# Revision 1.23  2003/08/18 16:35:28  jalet
76# New pychecker pass, on the tools this time.
77#
78# Revision 1.22  2003/07/29 20:55:17  jalet
79# 1.14 is out !
80#
81# Revision 1.21  2003/07/07 12:19:52  jalet
82# Now repykota and warnpykota --groups check the groups the user is a member of
83# in the print quota database, not in the system passwd/group files
84#
85# Revision 1.20  2003/06/25 14:10:01  jalet
86# Hey, it may work (edpykota --reset excepted) !
87#
88# Revision 1.19  2003/04/29 22:03:38  jalet
89# Better error handling.
90#
91# Revision 1.18  2003/04/23 22:13:56  jalet
92# Preliminary support for LPRng added BUT STILL UNTESTED.
93#
94# Revision 1.17  2003/04/17 13:32:17  jalet
95# bad documentation string
96#
97# Revision 1.16  2003/04/16 12:35:49  jalet
98# Groups quota work now !
99#
100# Revision 1.15  2003/04/10 21:47:20  jalet
101# Job history added. Upgrade script neutralized for now !
102#
103# Revision 1.14  2003/04/08 21:31:39  jalet
104# (anything or 0) = anything !!! Go back to school Jerome !
105#
106# Revision 1.13  2003/04/08 21:13:44  jalet
107# Prepare --groups option to work.
108#
109# Revision 1.12  2003/04/08 21:10:18  jalet
110# Checks --groups option presence instead of --users because --users is the default.
111#
112# Revision 1.11  2003/03/29 13:45:27  jalet
113# GPL paragraphs were incorrectly (from memory) copied into the sources.
114# Two README files were added.
115# Upgrade script for PostgreSQL pre 1.01 schema was added.
116#
117# Revision 1.10  2003/03/25 11:45:32  jalet
118# Clearer help.
119#
120# Revision 1.9  2003/03/09 23:39:14  jalet
121# Simplified translations.
122#
123# Revision 1.8  2003/02/10 12:07:30  jalet
124# Now repykota should output the recorded total page number for each printer too.
125#
126# Revision 1.7  2003/02/09 13:40:29  jalet
127# typo
128#
129# Revision 1.6  2003/02/09 12:56:53  jalet
130# Internationalization begins...
131#
132# Revision 1.5  2003/02/07 23:24:38  jalet
133# Empty line deleted
134#
135# Revision 1.4  2003/02/06 23:25:40  jalet
136# Cleaner docstring
137#
138# Revision 1.3  2003/02/06 23:20:02  jalet
139# warnpykota doesn't need any user/group name argument, mimicing the
140# warnquota disk quota tool.
141#
142# Revision 1.2  2003/02/06 22:54:33  jalet
143# warnpykota should be ok
144#
145#
146#
147
148import sys
149import os
150import pwd
151
152from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_
153from pykota.config import PyKotaConfigError
154from pykota.storage import PyKotaStorageError
155
156__doc__ = N_("""warnpykota v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres
157
158Sends mail to users over print quota.
159
160command line usage :
161
162  warnpykota  [options]  [names]
163
164options :
165
166  -v | --version       Prints warnpykota's version number then exits.
167  -h | --help          Prints this message then exits.
168 
169  -u | --users         Warns users over their print quota, this is the
170                       default.
171 
172  -g | --groups        Warns users whose groups quota are over limit.
173 
174  -P | --printer p     Verify quotas on this printer only. Actually p can
175                       use wildcards characters to select only
176                       some printers. The default value is *, meaning
177                       all printers.
178                       You can specify several names or wildcards,
179                       by separating them with commas.
180 
181examples :                             
182
183  $ warnpykota --printer lp
184 
185  This will warn all users of the lp printer who have exceeded their
186  print quota.
187
188  $ warnpykota
189 
190  This will warn all users  who have exceeded their print quota on
191  any printer.
192
193  $ warnpykota --groups --printer "laserjet*" "dev*"
194 
195  This will warn all users of groups which names begins with "dev" and
196  who have exceeded their print quota on any printer which name begins
197  with "laserjet"
198 
199  If launched by an user who is not a PyKota administrator, additionnal
200  arguments representing users or groups names are ignored, and only the
201  current user/group is reported.
202
203This program is free software; you can redistribute it and/or modify
204it under the terms of the GNU General Public License as published by
205the Free Software Foundation; either version 2 of the License, or
206(at your option) any later version.
207
208This program is distributed in the hope that it will be useful,
209but WITHOUT ANY WARRANTY; without even the implied warranty of
210MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
211GNU General Public License for more details.
212
213You should have received a copy of the GNU General Public License
214along with this program; if not, write to the Free Software
215Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
216
217Please e-mail bugs to: %s""")
218       
219class WarnPyKota(PyKotaTool) :       
220    """A class for warnpykota."""
221    def main(self, ugnames, options) :
222        """Warn users or groups over print quota."""
223        if self.config.isAdmin :
224            # PyKota administrator
225            if not ugnames :
226                # no username, means all usernames
227                ugnames = [ "*" ]
228        else :       
229            # not a PyKota administrator
230            # warns only the current user
231            # the utility of this is discutable, but at least it
232            # protects other users from mail bombing if they are
233            # over quota.
234            username = pwd.getpwuid(os.geteuid())[0]
235            if options["groups"] :
236                user = self.storage.getUser(username)
237                if user.Exists :
238                    ugnames = [ g.Name for g in self.storage.getUserGroups(user) ]
239                else :   
240                    ugnames = [ ]
241            else :
242                ugnames = [ username ]
243       
244        printers = self.storage.getMatchingPrinters(options["printer"])
245        if not printers :
246            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
247        alreadydone = {}
248        for printer in printers :
249            if options["groups"] :
250                for (group, grouppquota) in self.storage.getPrinterGroupsAndQuotas(printer, ugnames) :
251                    self.warnGroupPQuota(grouppquota)
252            else :
253                for (user, userpquota) in self.storage.getPrinterUsersAndQuotas(printer, ugnames) :
254                    # we only want to warn users who have ever printed something
255                    # and don't want to warn users who have never printed
256                    if (user.AccountBalance and (user.AccountBalance != user.LifeTimePaid)) or \
257                       userpquota.PageCounter or userpquota.LifePageCounter or \
258                       self.storage.getUserNbJobsFromHistory(user) :
259                        done = alreadydone.get(user.Name)
260                        if (user.LimitBy.lower() != 'balance') or not done :
261                            action = self.warnUserPQuota(userpquota)
262                            if not done :
263                                alreadydone[user.Name] = (action in ('WARN', 'DENY'))
264                     
265if __name__ == "__main__" : 
266    retcode = 0
267    try :
268        defaults = { \
269                     "printer" : "*", \
270                   }
271        short_options = "vhugP:"
272        long_options = ["help", "version", "users", "groups", "printer="]
273       
274        # Initializes the command line tool
275        sender = WarnPyKota(doc=__doc__)
276       
277        # parse and checks the command line
278        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
279       
280        # sets long options
281        options["help"] = options["h"] or options["help"]
282        options["version"] = options["v"] or options["version"]
283        options["users"] = options["u"] or options["users"]
284        options["groups"] = options["g"] or options["groups"]
285        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
286       
287        if options["help"] :
288            sender.display_usage_and_quit()
289        elif options["version"] :
290            sender.display_version_and_quit()
291        elif options["users"] and options["groups"] :   
292            raise PyKotaToolError, _("incompatible options, see help.")
293        else :
294            retcode = sender.main(args, options)
295    except SystemExit :       
296        pass
297    except :
298        try :
299            sender.crashed("warnpykota failed")
300        except :   
301            crashed("warnpykota failed")
302        retcode = -1
303       
304    try :
305        sender.storage.close()
306    except (TypeError, NameError, AttributeError) :   
307        pass
308       
309    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.