root / pykota / trunk / bin / warnpykota @ 1785

Revision 1785, 9.4 kB (checked in by jalet, 20 years ago)

Minor changes to allow any PyKota administrator to launch enhanced versions
of the commands, and not only the root user.

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