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