root / pykota / trunk / bin / warnpykota @ 1117

Revision 1117, 8.3 kB (checked in by jalet, 21 years ago)

New pychecker pass, on the tools this time.

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