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