root / pykota / branches / specialauth / bin / warnpykota @ 3398

Revision 3133, 6.6 kB (checked in by jerome, 18 years ago)

Changed copyright years.

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