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