root / pykota / trunk / bin / warnpykota @ 1597

Revision 1546, 9.2 kB (checked in by jalet, 20 years ago)

Now all tracebacks include PyKota's version number

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