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