root / pykota / trunk / bin / warnpykota @ 2692

Revision 2692, 6.7 kB (checked in by jerome, 18 years ago)

Added the 'duplicatesdelay' and 'balancezero' directives.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[696]1#! /usr/bin/env python
[1144]2# -*- coding: ISO-8859-15 -*-
[696]3
[728]4# PyKota Print Quota Warning sender
[696]5#
[952]6# PyKota - Print Quotas for CUPS and LPRng
[696]7#
[2622]8# (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com>
[873]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.
[696]13#
[873]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
[2303]21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
[696]22#
23# $Id$
24#
[2028]25#
[696]26
[728]27import sys
[1041]28import os
29import pwd
[728]30
[2512]31from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_
[975]32from pykota.config import PyKotaConfigError
33from pykota.storage import PyKotaStorageError
[728]34
[2344]35__doc__ = N_("""warnpykota v%(__version__)s (c) %(__years__)s %(__author__)s
[728]36
[730]37Sends mail to users over print quota.
38
[728]39command line usage :
40
[1041]41  warnpykota  [options]  [names]
[728]42
43options :
44
45  -v | --version       Prints warnpykota's version number then exits.
46  -h | --help          Prints this message then exits.
47 
[866]48  -u | --users         Warns users over their print quota, this is the
49                       default.
[728]50 
[934]51  -g | --groups        Warns users whose groups quota are over limit.
[728]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.
[1156]57                       You can specify several names or wildcards,
58                       by separating them with commas.
[728]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
[1041]72  $ warnpykota --groups --printer "laserjet*" "dev*"
[728]73 
[1041]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 
[1785]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.
[2344]81""")
[728]82       
83class WarnPyKota(PyKotaTool) :       
84    """A class for warnpykota."""
[1041]85    def main(self, ugnames, options) :
[728]86        """Warn users or groups over print quota."""
[1785]87        if self.config.isAdmin :
88            # PyKota administrator
[1041]89            if not ugnames :
90                # no username, means all usernames
91                ugnames = [ "*" ]
92        else :       
[1785]93            # not a PyKota administrator
[1041]94            # warns only the current user
95            # the utility of this is discutable, but at least it
96            # protects other users from mail bombing if they are
97            # over quota.
[1785]98            username = pwd.getpwuid(os.geteuid())[0]
[1041]99            if options["groups"] :
[1071]100                user = self.storage.getUser(username)
101                if user.Exists :
102                    ugnames = [ g.Name for g in self.storage.getUserGroups(user) ]
103                else :   
104                    ugnames = [ ]
[1041]105            else :
[1071]106                ugnames = [ username ]
[1041]107       
[900]108        printers = self.storage.getMatchingPrinters(options["printer"])
109        if not printers :
[2512]110            raise PyKotaCommandLineError, _("There's no printer matching %s") % options["printer"]
[1810]111        alreadydone = {}
[1041]112        for printer in printers :
[890]113            if options["groups"] :
[1041]114                for (group, grouppquota) in self.storage.getPrinterGroupsAndQuotas(printer, ugnames) :
115                    self.warnGroupPQuota(grouppquota)
[890]116            else :
[1041]117                for (user, userpquota) in self.storage.getPrinterUsersAndQuotas(printer, ugnames) :
[1806]118                    # we only want to warn users who have ever printed something
119                    # and don't want to warn users who have never printed
[2692]120                    if ((user.AccountBalance > self.config.getBalanceZero()) and \
121                       (user.AccountBalance != user.LifeTimePaid)) or \
[1806]122                       userpquota.PageCounter or userpquota.LifePageCounter or \
123                       self.storage.getUserNbJobsFromHistory(user) :
[1808]124                        done = alreadydone.get(user.Name)
[2547]125                        if (user.LimitBy == 'quota') or not done :
[1808]126                            action = self.warnUserPQuota(userpquota)
127                            if not done :
128                                alreadydone[user.Name] = (action in ('WARN', 'DENY'))
[728]129                     
130if __name__ == "__main__" : 
[1113]131    retcode = 0
[728]132    try :
133        defaults = { \
134                     "printer" : "*", \
135                   }
136        short_options = "vhugP:"
137        long_options = ["help", "version", "users", "groups", "printer="]
138       
139        # Initializes the command line tool
140        sender = WarnPyKota(doc=__doc__)
[2210]141        sender.deferredInit()
[728]142       
143        # parse and checks the command line
[729]144        (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
[728]145       
146        # sets long options
147        options["help"] = options["h"] or options["help"]
148        options["version"] = options["v"] or options["version"]
[895]149        options["users"] = options["u"] or options["users"]
150        options["groups"] = options["g"] or options["groups"]
[728]151        options["printer"] = options["P"] or options["printer"] or defaults["printer"]
152       
153        if options["help"] :
154            sender.display_usage_and_quit()
155        elif options["version"] :
156            sender.display_version_and_quit()
157        elif options["users"] and options["groups"] :   
[2512]158            raise PyKotaCommandLineError, _("incompatible options, see help.")
[728]159        else :
[1113]160            retcode = sender.main(args, options)
[2216]161    except KeyboardInterrupt :       
162        sys.stderr.write("\nInterrupted with Ctrl+C !\n")
[2609]163        retcode = -3
[2512]164    except PyKotaCommandLineError, msg :   
165        sys.stderr.write("%s : %s\n" % (sys.argv[0], msg))
[2609]166        retcode = -2
[1526]167    except SystemExit :       
168        pass
[1517]169    except :
170        try :
171            sender.crashed("warnpykota failed")
172        except :   
[1546]173            crashed("warnpykota failed")
[1113]174        retcode = -1
175       
176    try :
177        sender.storage.close()
178    except (TypeError, NameError, AttributeError) :   
179        pass
180       
181    sys.exit(retcode)   
Note: See TracBrowser for help on using the browser.