root / pykota / trunk / bin / warnpykota @ 1806

Revision 1806, 10.1 kB (checked in by jalet, 20 years ago)

Now warnpykota only warns users who have already printed, to not confuse
users who have just opened their account.

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