root / pykota / trunk / bin / warnpykota @ 1257

Revision 1257, 8.8 kB (checked in by jalet, 20 years ago)

Copyright year changed.

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