root / pykota / trunk / bin / warnpykota @ 1796

Revision 1796, 9.5 kB (checked in by jalet, 20 years ago)

Renders help translatable

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