root / pykota / trunk / bin / warnpykota @ 3260

Revision 3260, 6.5 kB (checked in by jerome, 16 years ago)

Changed license to GNU GPL v3 or later.
Changed Python source encoding from ISO-8859-15 to UTF-8 (only ASCII
was used anyway).

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