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