root / pykota / trunk / bin / warnpykota @ 1113

Revision 1113, 8.2 kB (checked in by jalet, 21 years ago)

1.14 is out !

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