root / pykota / trunk / bin / warnpykota @ 1204

Revision 1156, 8.7 kB (checked in by jalet, 21 years ago)

Multiple printer names or wildcards can be passed on the command line
separated with commas.
Beta phase.

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