root / pykota / trunk / bin / warnpykota @ 1517

Revision 1517, 8.9 kB (checked in by jalet, 20 years ago)

Improved error logging.
crashrecipient directive added.
Now exports the job's size in bytes too.

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