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