root / pykota / tags / 1.22 / bin / warnpykota

Revision 2216, 7.2 kB (checked in by jerome, 20 years ago)

Now exits with no traceback in case of Ctrl+C

  • 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, 2005 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#
26
27import sys
28import os
29import pwd
30
31from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_
32from pykota.config import PyKotaConfigError
33from pykota.storage import PyKotaStorageError
34
35__doc__ = N_("""warnpykota v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres
36
37Sends mail to users over print quota.
38
39command line usage :
40
41  warnpykota  [options]  [names]
42
43options :
44
45  -v | --version       Prints warnpykota's version number then exits.
46  -h | --help          Prints this message then exits.
47 
48  -u | --users         Warns users over their print quota, this is the
49                       default.
50 
51  -g | --groups        Warns users whose groups quota are over limit.
52 
53  -P | --printer p     Verify quotas on this printer only. Actually p can
54                       use wildcards characters to select only
55                       some printers. The default value is *, meaning
56                       all printers.
57                       You can specify several names or wildcards,
58                       by separating them with commas.
59 
60examples :                             
61
62  $ warnpykota --printer lp
63 
64  This will warn all users of the lp printer who have exceeded their
65  print quota.
66
67  $ warnpykota
68 
69  This will warn all users  who have exceeded their print quota on
70  any printer.
71
72  $ warnpykota --groups --printer "laserjet*" "dev*"
73 
74  This will warn all users of groups which names begins with "dev" and
75  who have exceeded their print quota on any printer which name begins
76  with "laserjet"
77 
78  If launched by an user who is not a PyKota administrator, additionnal
79  arguments representing users or groups names are ignored, and only the
80  current user/group is reported.
81
82This program is free software; you can redistribute it and/or modify
83it under the terms of the GNU General Public License as published by
84the Free Software Foundation; either version 2 of the License, or
85(at your option) any later version.
86
87This program is distributed in the hope that it will be useful,
88but WITHOUT ANY WARRANTY; without even the implied warranty of
89MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
90GNU General Public License for more details.
91
92You should have received a copy of the GNU General Public License
93along with this program; if not, write to the Free Software
94Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
95
96Please e-mail bugs to: %s""")
97       
98class WarnPyKota(PyKotaTool) :       
99    """A class for warnpykota."""
100    def main(self, ugnames, options) :
101        """Warn users or groups over print quota."""
102        if self.config.isAdmin :
103            # PyKota administrator
104            if not ugnames :
105                # no username, means all usernames
106                ugnames = [ "*" ]
107        else :       
108            # not a PyKota administrator
109            # warns only the current user
110            # the utility of this is discutable, but at least it
111            # protects other users from mail bombing if they are
112            # over quota.
113            username = pwd.getpwuid(os.geteuid())[0]
114            if options["groups"] :
115                user = self.storage.getUser(username)
116                if user.Exists :
117                    ugnames = [ g.Name for g in self.storage.getUserGroups(user) ]
118                else :   
119                    ugnames = [ ]
120            else :
121                ugnames = [ username ]
122       
123        printers = self.storage.getMatchingPrinters(options["printer"])
124        if not printers :
125            raise PyKotaToolError, _("There's no printer matching %s") % options["printer"]
126        alreadydone = {}
127        for printer in printers :
128            if options["groups"] :
129                for (group, grouppquota) in self.storage.getPrinterGroupsAndQuotas(printer, ugnames) :
130                    self.warnGroupPQuota(grouppquota)
131            else :
132                for (user, userpquota) in self.storage.getPrinterUsersAndQuotas(printer, ugnames) :
133                    # we only want to warn users who have ever printed something
134                    # and don't want to warn users who have never printed
135                    if (user.AccountBalance and (user.AccountBalance != user.LifeTimePaid)) or \
136                       userpquota.PageCounter or userpquota.LifePageCounter or \
137                       self.storage.getUserNbJobsFromHistory(user) :
138                        done = alreadydone.get(user.Name)
139                        if (user.LimitBy.lower() != 'balance') or not done :
140                            action = self.warnUserPQuota(userpquota)
141                            if not done :
142                                alreadydone[user.Name] = (action in ('WARN', 'DENY'))
143                     
144if __name__ == "__main__" : 
145    retcode = 0
146    try :
147        defaults = { \
148                     "printer" : "*", \
149                   }
150        short_options = "vhugP:"
151        long_options = ["help", "version", "users", "groups", "printer="]
152       
153        # Initializes the command line tool
154        sender = WarnPyKota(doc=__doc__)
155        sender.deferredInit()
156       
157        # parse and checks the command line
158        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
159       
160        # sets long options
161        options["help"] = options["h"] or options["help"]
162        options["version"] = options["v"] or options["version"]
163        options["users"] = options["u"] or options["users"]
164        options["groups"] = options["g"] or options["groups"]
165        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
166       
167        if options["help"] :
168            sender.display_usage_and_quit()
169        elif options["version"] :
170            sender.display_version_and_quit()
171        elif options["users"] and options["groups"] :   
172            raise PyKotaToolError, _("incompatible options, see help.")
173        else :
174            retcode = sender.main(args, options)
175    except KeyboardInterrupt :       
176        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
177    except SystemExit :       
178        pass
179    except :
180        try :
181            sender.crashed("warnpykota failed")
182        except :   
183            crashed("warnpykota failed")
184        retcode = -1
185       
186    try :
187        sender.storage.close()
188    except (TypeError, NameError, AttributeError) :   
189        pass
190       
191    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.