[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 | # |
---|
[1257] | 8 | # (c) 2003-2004 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 | # |
---|
| 25 | # $Log$ |
---|
[1808] | 26 | # Revision 1.35 2004/10/12 09:14:03 jalet |
---|
| 27 | # Ensures that warnpykota still test other quota entries for each user if |
---|
| 28 | # he has not been warned, even if limited by his account balance. |
---|
| 29 | # |
---|
[1807] | 30 | # Revision 1.34 2004/10/12 09:09:04 jalet |
---|
| 31 | # Now warnpykota warns users limited by balance only once per launch. |
---|
| 32 | # |
---|
[1806] | 33 | # Revision 1.33 2004/10/12 08:58:53 jalet |
---|
| 34 | # Now warnpykota only warns users who have already printed, to not confuse |
---|
| 35 | # users who have just opened their account. |
---|
| 36 | # |
---|
[1803] | 37 | # Revision 1.32 2004/10/11 22:53:05 jalet |
---|
| 38 | # Postponed string interpolation to help message's output method |
---|
| 39 | # |
---|
[1796] | 40 | # Revision 1.31 2004/10/11 12:49:06 jalet |
---|
| 41 | # Renders help translatable |
---|
| 42 | # |
---|
[1785] | 43 | # Revision 1.30 2004/10/06 10:05:47 jalet |
---|
| 44 | # Minor changes to allow any PyKota administrator to launch enhanced versions |
---|
| 45 | # of the commands, and not only the root user. |
---|
| 46 | # |
---|
[1546] | 47 | # Revision 1.29 2004/06/18 13:34:49 jalet |
---|
| 48 | # Now all tracebacks include PyKota's version number |
---|
| 49 | # |
---|
[1526] | 50 | # Revision 1.28 2004/06/07 18:43:41 jalet |
---|
| 51 | # Fixed over-verbose exits when displaying help or version number |
---|
| 52 | # |
---|
[1517] | 53 | # Revision 1.27 2004/06/03 21:50:34 jalet |
---|
| 54 | # Improved error logging. |
---|
| 55 | # crashrecipient directive added. |
---|
| 56 | # Now exports the job's size in bytes too. |
---|
| 57 | # |
---|
[1257] | 58 | # Revision 1.26 2004/01/08 14:10:32 jalet |
---|
| 59 | # Copyright year changed. |
---|
| 60 | # |
---|
[1156] | 61 | # Revision 1.25 2003/10/09 21:25:26 jalet |
---|
| 62 | # Multiple printer names or wildcards can be passed on the command line |
---|
| 63 | # separated with commas. |
---|
| 64 | # Beta phase. |
---|
| 65 | # |
---|
[1144] | 66 | # Revision 1.24 2003/10/07 09:07:28 jalet |
---|
| 67 | # Character encoding added to please latest version of Python |
---|
| 68 | # |
---|
[1117] | 69 | # Revision 1.23 2003/08/18 16:35:28 jalet |
---|
| 70 | # New pychecker pass, on the tools this time. |
---|
| 71 | # |
---|
[1113] | 72 | # Revision 1.22 2003/07/29 20:55:17 jalet |
---|
| 73 | # 1.14 is out ! |
---|
| 74 | # |
---|
[1071] | 75 | # Revision 1.21 2003/07/07 12:19:52 jalet |
---|
| 76 | # Now repykota and warnpykota --groups check the groups the user is a member of |
---|
| 77 | # in the print quota database, not in the system passwd/group files |
---|
| 78 | # |
---|
[1041] | 79 | # Revision 1.20 2003/06/25 14:10:01 jalet |
---|
| 80 | # Hey, it may work (edpykota --reset excepted) ! |
---|
| 81 | # |
---|
[975] | 82 | # Revision 1.19 2003/04/29 22:03:38 jalet |
---|
| 83 | # Better error handling. |
---|
| 84 | # |
---|
[952] | 85 | # Revision 1.18 2003/04/23 22:13:56 jalet |
---|
| 86 | # Preliminary support for LPRng added BUT STILL UNTESTED. |
---|
| 87 | # |
---|
[934] | 88 | # Revision 1.17 2003/04/17 13:32:17 jalet |
---|
| 89 | # bad documentation string |
---|
| 90 | # |
---|
[927] | 91 | # Revision 1.16 2003/04/16 12:35:49 jalet |
---|
| 92 | # Groups quota work now ! |
---|
| 93 | # |
---|
[900] | 94 | # Revision 1.15 2003/04/10 21:47:20 jalet |
---|
| 95 | # Job history added. Upgrade script neutralized for now ! |
---|
| 96 | # |
---|
[895] | 97 | # Revision 1.14 2003/04/08 21:31:39 jalet |
---|
| 98 | # (anything or 0) = anything !!! Go back to school Jerome ! |
---|
| 99 | # |
---|
[891] | 100 | # Revision 1.13 2003/04/08 21:13:44 jalet |
---|
| 101 | # Prepare --groups option to work. |
---|
| 102 | # |
---|
[890] | 103 | # Revision 1.12 2003/04/08 21:10:18 jalet |
---|
| 104 | # Checks --groups option presence instead of --users because --users is the default. |
---|
| 105 | # |
---|
[873] | 106 | # Revision 1.11 2003/03/29 13:45:27 jalet |
---|
| 107 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 108 | # Two README files were added. |
---|
| 109 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 110 | # |
---|
[866] | 111 | # Revision 1.10 2003/03/25 11:45:32 jalet |
---|
| 112 | # Clearer help. |
---|
| 113 | # |
---|
[842] | 114 | # Revision 1.9 2003/03/09 23:39:14 jalet |
---|
| 115 | # Simplified translations. |
---|
| 116 | # |
---|
[791] | 117 | # Revision 1.8 2003/02/10 12:07:30 jalet |
---|
| 118 | # Now repykota should output the recorded total page number for each printer too. |
---|
| 119 | # |
---|
[775] | 120 | # Revision 1.7 2003/02/09 13:40:29 jalet |
---|
| 121 | # typo |
---|
| 122 | # |
---|
[772] | 123 | # Revision 1.6 2003/02/09 12:56:53 jalet |
---|
| 124 | # Internationalization begins... |
---|
| 125 | # |
---|
[751] | 126 | # Revision 1.5 2003/02/07 23:24:38 jalet |
---|
| 127 | # Empty line deleted |
---|
| 128 | # |
---|
[730] | 129 | # Revision 1.4 2003/02/06 23:25:40 jalet |
---|
| 130 | # Cleaner docstring |
---|
| 131 | # |
---|
[729] | 132 | # Revision 1.3 2003/02/06 23:20:02 jalet |
---|
| 133 | # warnpykota doesn't need any user/group name argument, mimicing the |
---|
| 134 | # warnquota disk quota tool. |
---|
| 135 | # |
---|
[728] | 136 | # Revision 1.2 2003/02/06 22:54:33 jalet |
---|
| 137 | # warnpykota should be ok |
---|
[696] | 138 | # |
---|
| 139 | # |
---|
| 140 | # |
---|
| 141 | |
---|
[728] | 142 | import sys |
---|
[1041] | 143 | import os |
---|
| 144 | import pwd |
---|
[728] | 145 | |
---|
[1796] | 146 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
[975] | 147 | from pykota.config import PyKotaConfigError |
---|
| 148 | from pykota.storage import PyKotaStorageError |
---|
[728] | 149 | |
---|
[1796] | 150 | __doc__ = N_("""warnpykota v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
[728] | 151 | |
---|
[730] | 152 | Sends mail to users over print quota. |
---|
| 153 | |
---|
[728] | 154 | command line usage : |
---|
| 155 | |
---|
[1041] | 156 | warnpykota [options] [names] |
---|
[728] | 157 | |
---|
| 158 | options : |
---|
| 159 | |
---|
| 160 | -v | --version Prints warnpykota's version number then exits. |
---|
| 161 | -h | --help Prints this message then exits. |
---|
| 162 | |
---|
[866] | 163 | -u | --users Warns users over their print quota, this is the |
---|
| 164 | default. |
---|
[728] | 165 | |
---|
[934] | 166 | -g | --groups Warns users whose groups quota are over limit. |
---|
[728] | 167 | |
---|
| 168 | -P | --printer p Verify quotas on this printer only. Actually p can |
---|
| 169 | use wildcards characters to select only |
---|
| 170 | some printers. The default value is *, meaning |
---|
| 171 | all printers. |
---|
[1156] | 172 | You can specify several names or wildcards, |
---|
| 173 | by separating them with commas. |
---|
[728] | 174 | |
---|
| 175 | examples : |
---|
| 176 | |
---|
| 177 | $ warnpykota --printer lp |
---|
| 178 | |
---|
| 179 | This will warn all users of the lp printer who have exceeded their |
---|
| 180 | print quota. |
---|
| 181 | |
---|
| 182 | $ warnpykota |
---|
| 183 | |
---|
| 184 | This will warn all users who have exceeded their print quota on |
---|
| 185 | any printer. |
---|
| 186 | |
---|
[1041] | 187 | $ warnpykota --groups --printer "laserjet*" "dev*" |
---|
[728] | 188 | |
---|
[1041] | 189 | This will warn all users of groups which names begins with "dev" and |
---|
| 190 | who have exceeded their print quota on any printer which name begins |
---|
| 191 | with "laserjet" |
---|
| 192 | |
---|
[1785] | 193 | If launched by an user who is not a PyKota administrator, additionnal |
---|
| 194 | arguments representing users or groups names are ignored, and only the |
---|
| 195 | current user/group is reported. |
---|
[728] | 196 | |
---|
| 197 | This program is free software; you can redistribute it and/or modify |
---|
| 198 | it under the terms of the GNU General Public License as published by |
---|
| 199 | the Free Software Foundation; either version 2 of the License, or |
---|
| 200 | (at your option) any later version. |
---|
| 201 | |
---|
| 202 | This program is distributed in the hope that it will be useful, |
---|
| 203 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 204 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 205 | GNU General Public License for more details. |
---|
| 206 | |
---|
| 207 | You should have received a copy of the GNU General Public License |
---|
| 208 | along with this program; if not, write to the Free Software |
---|
| 209 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 210 | |
---|
[1803] | 211 | Please e-mail bugs to: %s""") |
---|
[728] | 212 | |
---|
| 213 | class WarnPyKota(PyKotaTool) : |
---|
| 214 | """A class for warnpykota.""" |
---|
[1041] | 215 | def main(self, ugnames, options) : |
---|
[728] | 216 | """Warn users or groups over print quota.""" |
---|
[1785] | 217 | if self.config.isAdmin : |
---|
| 218 | # PyKota administrator |
---|
[1041] | 219 | if not ugnames : |
---|
| 220 | # no username, means all usernames |
---|
| 221 | ugnames = [ "*" ] |
---|
| 222 | else : |
---|
[1785] | 223 | # not a PyKota administrator |
---|
[1041] | 224 | # warns only the current user |
---|
| 225 | # the utility of this is discutable, but at least it |
---|
| 226 | # protects other users from mail bombing if they are |
---|
| 227 | # over quota. |
---|
[1785] | 228 | username = pwd.getpwuid(os.geteuid())[0] |
---|
[1041] | 229 | if options["groups"] : |
---|
[1071] | 230 | user = self.storage.getUser(username) |
---|
| 231 | if user.Exists : |
---|
| 232 | ugnames = [ g.Name for g in self.storage.getUserGroups(user) ] |
---|
| 233 | else : |
---|
| 234 | ugnames = [ ] |
---|
[1041] | 235 | else : |
---|
[1071] | 236 | ugnames = [ username ] |
---|
[1041] | 237 | |
---|
[900] | 238 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 239 | if not printers : |
---|
[772] | 240 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
[1041] | 241 | for printer in printers : |
---|
[890] | 242 | if options["groups"] : |
---|
[1041] | 243 | for (group, grouppquota) in self.storage.getPrinterGroupsAndQuotas(printer, ugnames) : |
---|
| 244 | self.warnGroupPQuota(grouppquota) |
---|
[890] | 245 | else : |
---|
[1807] | 246 | alreadydone = {} |
---|
[1041] | 247 | for (user, userpquota) in self.storage.getPrinterUsersAndQuotas(printer, ugnames) : |
---|
[1806] | 248 | # we only want to warn users who have ever printed something |
---|
| 249 | # and don't want to warn users who have never printed |
---|
| 250 | if (user.AccountBalance and (user.AccountBalance != user.LifeTimePaid)) or \ |
---|
| 251 | userpquota.PageCounter or userpquota.LifePageCounter or \ |
---|
| 252 | self.storage.getUserNbJobsFromHistory(user) : |
---|
[1808] | 253 | done = alreadydone.get(user.Name) |
---|
| 254 | if (user.LimitBy.lower() != 'balance') or not done : |
---|
| 255 | action = self.warnUserPQuota(userpquota) |
---|
| 256 | if not done : |
---|
| 257 | alreadydone[user.Name] = (action in ('WARN', 'DENY')) |
---|
[728] | 258 | |
---|
| 259 | if __name__ == "__main__" : |
---|
[1113] | 260 | retcode = 0 |
---|
[728] | 261 | try : |
---|
| 262 | defaults = { \ |
---|
| 263 | "printer" : "*", \ |
---|
| 264 | } |
---|
| 265 | short_options = "vhugP:" |
---|
| 266 | long_options = ["help", "version", "users", "groups", "printer="] |
---|
| 267 | |
---|
| 268 | # Initializes the command line tool |
---|
| 269 | sender = WarnPyKota(doc=__doc__) |
---|
| 270 | |
---|
| 271 | # parse and checks the command line |
---|
[729] | 272 | (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
[728] | 273 | |
---|
| 274 | # sets long options |
---|
| 275 | options["help"] = options["h"] or options["help"] |
---|
| 276 | options["version"] = options["v"] or options["version"] |
---|
[895] | 277 | options["users"] = options["u"] or options["users"] |
---|
| 278 | options["groups"] = options["g"] or options["groups"] |
---|
[728] | 279 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
| 280 | |
---|
| 281 | if options["help"] : |
---|
| 282 | sender.display_usage_and_quit() |
---|
| 283 | elif options["version"] : |
---|
| 284 | sender.display_version_and_quit() |
---|
| 285 | elif options["users"] and options["groups"] : |
---|
[842] | 286 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
[728] | 287 | else : |
---|
[1113] | 288 | retcode = sender.main(args, options) |
---|
[1526] | 289 | except SystemExit : |
---|
| 290 | pass |
---|
[1517] | 291 | except : |
---|
| 292 | try : |
---|
| 293 | sender.crashed("warnpykota failed") |
---|
| 294 | except : |
---|
[1546] | 295 | crashed("warnpykota failed") |
---|
[1113] | 296 | retcode = -1 |
---|
| 297 | |
---|
| 298 | try : |
---|
| 299 | sender.storage.close() |
---|
| 300 | except (TypeError, NameError, AttributeError) : |
---|
| 301 | pass |
---|
| 302 | |
---|
| 303 | sys.exit(retcode) |
---|