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