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