[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] | 27 | import sys |
---|
[1041] | 28 | import os |
---|
| 29 | import pwd |
---|
[728] | 30 | |
---|
[2829] | 31 | from pykota.tool import PyKotaTool, PyKotaCommandLineError, crashed, N_ |
---|
[728] | 32 | |
---|
[2344] | 33 | __doc__ = N_("""warnpykota v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[728] | 34 | |
---|
[730] | 35 | Sends mail to users over print quota. |
---|
| 36 | |
---|
[728] | 37 | command line usage : |
---|
| 38 | |
---|
[1041] | 39 | warnpykota [options] [names] |
---|
[728] | 40 | |
---|
| 41 | options : |
---|
| 42 | |
---|
| 43 | -v | --version Prints warnpykota's version number then exits. |
---|
| 44 | -h | --help Prints this message then exits. |
---|
| 45 | |
---|
[866] | 46 | -u | --users Warns users over their print quota, this is the |
---|
| 47 | default. |
---|
[728] | 48 | |
---|
[934] | 49 | -g | --groups Warns users whose groups quota are over limit. |
---|
[728] | 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. |
---|
[1156] | 55 | You can specify several names or wildcards, |
---|
| 56 | by separating them with commas. |
---|
[728] | 57 | |
---|
| 58 | examples : |
---|
| 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 | |
---|
[1041] | 70 | $ warnpykota --groups --printer "laserjet*" "dev*" |
---|
[728] | 71 | |
---|
[1041] | 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 | |
---|
[1785] | 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. |
---|
[2344] | 79 | """) |
---|
[728] | 80 | |
---|
| 81 | class WarnPyKota(PyKotaTool) : |
---|
| 82 | """A class for warnpykota.""" |
---|
[1041] | 83 | def main(self, ugnames, options) : |
---|
[728] | 84 | """Warn users or groups over print quota.""" |
---|
[1785] | 85 | if self.config.isAdmin : |
---|
| 86 | # PyKota administrator |
---|
[1041] | 87 | if not ugnames : |
---|
| 88 | # no username, means all usernames |
---|
| 89 | ugnames = [ "*" ] |
---|
| 90 | else : |
---|
[1785] | 91 | # not a PyKota administrator |
---|
[1041] | 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. |
---|
[1785] | 96 | username = pwd.getpwuid(os.geteuid())[0] |
---|
[1041] | 97 | if options["groups"] : |
---|
[1071] | 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 = [ ] |
---|
[1041] | 103 | else : |
---|
[1071] | 104 | ugnames = [ username ] |
---|
[1041] | 105 | |
---|
[900] | 106 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 107 | if not printers : |
---|
[2512] | 108 | raise PyKotaCommandLineError, _("There's no printer matching %s") % options["printer"] |
---|
[1810] | 109 | alreadydone = {} |
---|
[1041] | 110 | for printer in printers : |
---|
[890] | 111 | if options["groups"] : |
---|
[1041] | 112 | for (group, grouppquota) in self.storage.getPrinterGroupsAndQuotas(printer, ugnames) : |
---|
| 113 | self.warnGroupPQuota(grouppquota) |
---|
[890] | 114 | else : |
---|
[1041] | 115 | for (user, userpquota) in self.storage.getPrinterUsersAndQuotas(printer, ugnames) : |
---|
[1806] | 116 | # we only want to warn users who have ever printed something |
---|
| 117 | # and don't want to warn users who have never printed |
---|
[2692] | 118 | if ((user.AccountBalance > self.config.getBalanceZero()) and \ |
---|
| 119 | (user.AccountBalance != user.LifeTimePaid)) or \ |
---|
[1806] | 120 | userpquota.PageCounter or userpquota.LifePageCounter or \ |
---|
| 121 | self.storage.getUserNbJobsFromHistory(user) : |
---|
[1808] | 122 | done = alreadydone.get(user.Name) |
---|
[2547] | 123 | if (user.LimitBy == 'quota') or not done : |
---|
[1808] | 124 | action = self.warnUserPQuota(userpquota) |
---|
| 125 | if not done : |
---|
| 126 | alreadydone[user.Name] = (action in ('WARN', 'DENY')) |
---|
[728] | 127 | |
---|
| 128 | if __name__ == "__main__" : |
---|
[1113] | 129 | retcode = 0 |
---|
[728] | 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__) |
---|
[2210] | 139 | sender.deferredInit() |
---|
[728] | 140 | |
---|
| 141 | # parse and checks the command line |
---|
[729] | 142 | (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
[728] | 143 | |
---|
| 144 | # sets long options |
---|
| 145 | options["help"] = options["h"] or options["help"] |
---|
| 146 | options["version"] = options["v"] or options["version"] |
---|
[895] | 147 | options["users"] = options["u"] or options["users"] |
---|
| 148 | options["groups"] = options["g"] or options["groups"] |
---|
[728] | 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"] : |
---|
[2512] | 156 | raise PyKotaCommandLineError, _("incompatible options, see help.") |
---|
[728] | 157 | else : |
---|
[1113] | 158 | retcode = sender.main(args, options) |
---|
[2216] | 159 | except KeyboardInterrupt : |
---|
| 160 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 161 | retcode = -3 |
---|
[2512] | 162 | except PyKotaCommandLineError, msg : |
---|
| 163 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 164 | retcode = -2 |
---|
[1526] | 165 | except SystemExit : |
---|
| 166 | pass |
---|
[1517] | 167 | except : |
---|
| 168 | try : |
---|
| 169 | sender.crashed("warnpykota failed") |
---|
| 170 | except : |
---|
[1546] | 171 | crashed("warnpykota failed") |
---|
[1113] | 172 | retcode = -1 |
---|
| 173 | |
---|
| 174 | try : |
---|
| 175 | sender.storage.close() |
---|
| 176 | except (TypeError, NameError, AttributeError) : |
---|
| 177 | pass |
---|
| 178 | |
---|
| 179 | sys.exit(retcode) |
---|