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