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