[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 | # |
---|
[2028] | 8 | # (c) 2003, 2004, 2005 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 |
---|
| 21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
[696] | 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
[2028] | 25 | # |
---|
[696] | 26 | |
---|
[728] | 27 | import sys |
---|
[1041] | 28 | import os |
---|
| 29 | import pwd |
---|
[728] | 30 | |
---|
[1796] | 31 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
[975] | 32 | from pykota.config import PyKotaConfigError |
---|
| 33 | from pykota.storage import PyKotaStorageError |
---|
[728] | 34 | |
---|
[2028] | 35 | __doc__ = N_("""warnpykota v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres |
---|
[728] | 36 | |
---|
[730] | 37 | Sends mail to users over print quota. |
---|
| 38 | |
---|
[728] | 39 | command line usage : |
---|
| 40 | |
---|
[1041] | 41 | warnpykota [options] [names] |
---|
[728] | 42 | |
---|
| 43 | options : |
---|
| 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 | |
---|
| 60 | examples : |
---|
| 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. |
---|
[728] | 81 | |
---|
| 82 | This program is free software; you can redistribute it and/or modify |
---|
| 83 | it under the terms of the GNU General Public License as published by |
---|
| 84 | the Free Software Foundation; either version 2 of the License, or |
---|
| 85 | (at your option) any later version. |
---|
| 86 | |
---|
| 87 | This program is distributed in the hope that it will be useful, |
---|
| 88 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 89 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 90 | GNU General Public License for more details. |
---|
| 91 | |
---|
| 92 | You should have received a copy of the GNU General Public License |
---|
| 93 | along with this program; if not, write to the Free Software |
---|
| 94 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 95 | |
---|
[1803] | 96 | Please e-mail bugs to: %s""") |
---|
[728] | 97 | |
---|
| 98 | class WarnPyKota(PyKotaTool) : |
---|
| 99 | """A class for warnpykota.""" |
---|
[1041] | 100 | def main(self, ugnames, options) : |
---|
[728] | 101 | """Warn users or groups over print quota.""" |
---|
[1785] | 102 | if self.config.isAdmin : |
---|
| 103 | # PyKota administrator |
---|
[1041] | 104 | if not ugnames : |
---|
| 105 | # no username, means all usernames |
---|
| 106 | ugnames = [ "*" ] |
---|
| 107 | else : |
---|
[1785] | 108 | # not a PyKota administrator |
---|
[1041] | 109 | # warns only the current user |
---|
| 110 | # the utility of this is discutable, but at least it |
---|
| 111 | # protects other users from mail bombing if they are |
---|
| 112 | # over quota. |
---|
[1785] | 113 | username = pwd.getpwuid(os.geteuid())[0] |
---|
[1041] | 114 | if options["groups"] : |
---|
[1071] | 115 | user = self.storage.getUser(username) |
---|
| 116 | if user.Exists : |
---|
| 117 | ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] |
---|
| 118 | else : |
---|
| 119 | ugnames = [ ] |
---|
[1041] | 120 | else : |
---|
[1071] | 121 | ugnames = [ username ] |
---|
[1041] | 122 | |
---|
[900] | 123 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 124 | if not printers : |
---|
[772] | 125 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
[1810] | 126 | alreadydone = {} |
---|
[1041] | 127 | for printer in printers : |
---|
[890] | 128 | if options["groups"] : |
---|
[1041] | 129 | for (group, grouppquota) in self.storage.getPrinterGroupsAndQuotas(printer, ugnames) : |
---|
| 130 | self.warnGroupPQuota(grouppquota) |
---|
[890] | 131 | else : |
---|
[1041] | 132 | for (user, userpquota) in self.storage.getPrinterUsersAndQuotas(printer, ugnames) : |
---|
[1806] | 133 | # we only want to warn users who have ever printed something |
---|
| 134 | # and don't want to warn users who have never printed |
---|
| 135 | if (user.AccountBalance and (user.AccountBalance != user.LifeTimePaid)) or \ |
---|
| 136 | userpquota.PageCounter or userpquota.LifePageCounter or \ |
---|
| 137 | self.storage.getUserNbJobsFromHistory(user) : |
---|
[1808] | 138 | done = alreadydone.get(user.Name) |
---|
| 139 | if (user.LimitBy.lower() != 'balance') or not done : |
---|
| 140 | action = self.warnUserPQuota(userpquota) |
---|
| 141 | if not done : |
---|
| 142 | alreadydone[user.Name] = (action in ('WARN', 'DENY')) |
---|
[728] | 143 | |
---|
| 144 | if __name__ == "__main__" : |
---|
[1113] | 145 | retcode = 0 |
---|
[728] | 146 | try : |
---|
| 147 | defaults = { \ |
---|
| 148 | "printer" : "*", \ |
---|
| 149 | } |
---|
| 150 | short_options = "vhugP:" |
---|
| 151 | long_options = ["help", "version", "users", "groups", "printer="] |
---|
| 152 | |
---|
| 153 | # Initializes the command line tool |
---|
| 154 | sender = WarnPyKota(doc=__doc__) |
---|
| 155 | |
---|
| 156 | # parse and checks the command line |
---|
[729] | 157 | (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
[728] | 158 | |
---|
| 159 | # sets long options |
---|
| 160 | options["help"] = options["h"] or options["help"] |
---|
| 161 | options["version"] = options["v"] or options["version"] |
---|
[895] | 162 | options["users"] = options["u"] or options["users"] |
---|
| 163 | options["groups"] = options["g"] or options["groups"] |
---|
[728] | 164 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
| 165 | |
---|
| 166 | if options["help"] : |
---|
| 167 | sender.display_usage_and_quit() |
---|
| 168 | elif options["version"] : |
---|
| 169 | sender.display_version_and_quit() |
---|
| 170 | elif options["users"] and options["groups"] : |
---|
[842] | 171 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
[728] | 172 | else : |
---|
[1113] | 173 | retcode = sender.main(args, options) |
---|
[1526] | 174 | except SystemExit : |
---|
| 175 | pass |
---|
[1517] | 176 | except : |
---|
| 177 | try : |
---|
| 178 | sender.crashed("warnpykota failed") |
---|
| 179 | except : |
---|
[1546] | 180 | crashed("warnpykota failed") |
---|
[1113] | 181 | retcode = -1 |
---|
| 182 | |
---|
| 183 | try : |
---|
| 184 | sender.storage.close() |
---|
| 185 | except (TypeError, NameError, AttributeError) : |
---|
| 186 | pass |
---|
| 187 | |
---|
| 188 | sys.exit(retcode) |
---|