root / pykota / trunk / bin / warnpykota @ 1539

Revision 1526, 9.0 kB (checked in by jalet, 20 years ago)

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