[696] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
[728] | 3 | # PyKota Print Quota Warning sender |
---|
[696] | 4 | # |
---|
| 5 | # PyKota - Print Quotas for CUPS |
---|
| 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$ |
---|
[927] | 25 | # Revision 1.16 2003/04/16 12:35:49 jalet |
---|
| 26 | # Groups quota work now ! |
---|
| 27 | # |
---|
[900] | 28 | # Revision 1.15 2003/04/10 21:47:20 jalet |
---|
| 29 | # Job history added. Upgrade script neutralized for now ! |
---|
| 30 | # |
---|
[895] | 31 | # Revision 1.14 2003/04/08 21:31:39 jalet |
---|
| 32 | # (anything or 0) = anything !!! Go back to school Jerome ! |
---|
| 33 | # |
---|
[891] | 34 | # Revision 1.13 2003/04/08 21:13:44 jalet |
---|
| 35 | # Prepare --groups option to work. |
---|
| 36 | # |
---|
[890] | 37 | # Revision 1.12 2003/04/08 21:10:18 jalet |
---|
| 38 | # Checks --groups option presence instead of --users because --users is the default. |
---|
| 39 | # |
---|
[873] | 40 | # Revision 1.11 2003/03/29 13:45:27 jalet |
---|
| 41 | # GPL paragraphs were incorrectly (from memory) copied into the sources. |
---|
| 42 | # Two README files were added. |
---|
| 43 | # Upgrade script for PostgreSQL pre 1.01 schema was added. |
---|
| 44 | # |
---|
[866] | 45 | # Revision 1.10 2003/03/25 11:45:32 jalet |
---|
| 46 | # Clearer help. |
---|
| 47 | # |
---|
[842] | 48 | # Revision 1.9 2003/03/09 23:39:14 jalet |
---|
| 49 | # Simplified translations. |
---|
| 50 | # |
---|
[791] | 51 | # Revision 1.8 2003/02/10 12:07:30 jalet |
---|
| 52 | # Now repykota should output the recorded total page number for each printer too. |
---|
| 53 | # |
---|
[775] | 54 | # Revision 1.7 2003/02/09 13:40:29 jalet |
---|
| 55 | # typo |
---|
| 56 | # |
---|
[772] | 57 | # Revision 1.6 2003/02/09 12:56:53 jalet |
---|
| 58 | # Internationalization begins... |
---|
| 59 | # |
---|
[751] | 60 | # Revision 1.5 2003/02/07 23:24:38 jalet |
---|
| 61 | # Empty line deleted |
---|
| 62 | # |
---|
[730] | 63 | # Revision 1.4 2003/02/06 23:25:40 jalet |
---|
| 64 | # Cleaner docstring |
---|
| 65 | # |
---|
[729] | 66 | # Revision 1.3 2003/02/06 23:20:02 jalet |
---|
| 67 | # warnpykota doesn't need any user/group name argument, mimicing the |
---|
| 68 | # warnquota disk quota tool. |
---|
| 69 | # |
---|
[728] | 70 | # Revision 1.2 2003/02/06 22:54:33 jalet |
---|
| 71 | # warnpykota should be ok |
---|
[696] | 72 | # |
---|
| 73 | # |
---|
| 74 | # |
---|
| 75 | |
---|
[728] | 76 | import sys |
---|
| 77 | |
---|
| 78 | from pykota import version |
---|
| 79 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
| 80 | |
---|
| 81 | __doc__ = """warnpykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres |
---|
| 82 | |
---|
[730] | 83 | Sends mail to users over print quota. |
---|
| 84 | |
---|
[728] | 85 | command line usage : |
---|
| 86 | |
---|
[729] | 87 | warnpykota [options] |
---|
[728] | 88 | |
---|
| 89 | options : |
---|
| 90 | |
---|
| 91 | -v | --version Prints warnpykota's version number then exits. |
---|
| 92 | -h | --help Prints this message then exits. |
---|
| 93 | |
---|
[866] | 94 | -u | --users Warns users over their print quota, this is the |
---|
| 95 | default. |
---|
[728] | 96 | |
---|
[866] | 97 | -g | --groups Warns group administrators instead of users, |
---|
| 98 | for each group over its print quota. |
---|
[728] | 99 | |
---|
| 100 | -P | --printer p Verify quotas on this printer only. Actually p can |
---|
| 101 | use wildcards characters to select only |
---|
| 102 | some printers. The default value is *, meaning |
---|
| 103 | all printers. |
---|
| 104 | |
---|
| 105 | examples : |
---|
| 106 | |
---|
| 107 | $ warnpykota --printer lp |
---|
| 108 | |
---|
| 109 | This will warn all users of the lp printer who have exceeded their |
---|
| 110 | print quota. |
---|
| 111 | |
---|
| 112 | $ warnpykota |
---|
| 113 | |
---|
| 114 | This will warn all users who have exceeded their print quota on |
---|
| 115 | any printer. |
---|
| 116 | |
---|
| 117 | $ warnpykota --groups --printer "laserjet*" |
---|
| 118 | |
---|
| 119 | This will warn all group administrators of groups which have exceeded |
---|
| 120 | their print quota on any printer which name begins with "laserjet" |
---|
| 121 | |
---|
| 122 | This program is free software; you can redistribute it and/or modify |
---|
| 123 | it under the terms of the GNU General Public License as published by |
---|
| 124 | the Free Software Foundation; either version 2 of the License, or |
---|
| 125 | (at your option) any later version. |
---|
| 126 | |
---|
| 127 | This program is distributed in the hope that it will be useful, |
---|
| 128 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 129 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 130 | GNU General Public License for more details. |
---|
| 131 | |
---|
| 132 | You should have received a copy of the GNU General Public License |
---|
| 133 | along with this program; if not, write to the Free Software |
---|
| 134 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 135 | |
---|
| 136 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
| 137 | |
---|
| 138 | class WarnPyKota(PyKotaTool) : |
---|
| 139 | """A class for warnpykota.""" |
---|
[729] | 140 | def main(self, options) : |
---|
[728] | 141 | """Warn users or groups over print quota.""" |
---|
[900] | 142 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 143 | if not printers : |
---|
[772] | 144 | raise PyKotaToolError, _("There's no printer matching %s") % options["printer"] |
---|
[900] | 145 | for (printerid, printer) in printers : |
---|
[890] | 146 | if options["groups"] : |
---|
[900] | 147 | for (ident, name) in self.storage.getPrinterGroups(printerid) : |
---|
[890] | 148 | self.warnGroupPQuota(name, printer) |
---|
| 149 | else : |
---|
[900] | 150 | for (ident, name) in self.storage.getPrinterUsers(printerid) : |
---|
[728] | 151 | self.warnUserPQuota(name, printer) |
---|
| 152 | |
---|
| 153 | if __name__ == "__main__" : |
---|
| 154 | try : |
---|
| 155 | defaults = { \ |
---|
| 156 | "printer" : "*", \ |
---|
| 157 | } |
---|
| 158 | short_options = "vhugP:" |
---|
| 159 | long_options = ["help", "version", "users", "groups", "printer="] |
---|
| 160 | |
---|
| 161 | # Initializes the command line tool |
---|
| 162 | sender = WarnPyKota(doc=__doc__) |
---|
| 163 | |
---|
| 164 | # parse and checks the command line |
---|
[729] | 165 | (options, args) = sender.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1) |
---|
[728] | 166 | |
---|
| 167 | # sets long options |
---|
| 168 | options["help"] = options["h"] or options["help"] |
---|
| 169 | options["version"] = options["v"] or options["version"] |
---|
[895] | 170 | options["users"] = options["u"] or options["users"] |
---|
| 171 | options["groups"] = options["g"] or options["groups"] |
---|
[728] | 172 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
| 173 | |
---|
| 174 | if options["help"] : |
---|
| 175 | sender.display_usage_and_quit() |
---|
| 176 | elif options["version"] : |
---|
| 177 | sender.display_version_and_quit() |
---|
| 178 | elif options["users"] and options["groups"] : |
---|
[842] | 179 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
[729] | 180 | elif args : |
---|
[842] | 181 | raise PyKotaToolError, _("unused arguments [%s]. Aborting.") % ", ".join(args) |
---|
[728] | 182 | else : |
---|
[729] | 183 | sys.exit(sender.main(options)) |
---|
[728] | 184 | except PyKotaToolError, msg : |
---|
| 185 | sys.stderr.write("%s\n" % msg) |
---|
| 186 | sys.stderr.flush() |
---|
| 187 | sys.exit(-1) |
---|