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