[696] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | # PyKota Print Quota Editor |
---|
| 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$ |
---|
[748] | 19 | # Revision 1.6 2003/02/07 22:13:13 jalet |
---|
| 20 | # Perhaps edpykota is now able to add printers !!! Oh, stupid me ! |
---|
| 21 | # |
---|
[720] | 22 | # Revision 1.5 2003/02/06 14:49:04 jalet |
---|
| 23 | # edpykota should be ok now |
---|
| 24 | # |
---|
[719] | 25 | # Revision 1.4 2003/02/06 14:28:59 jalet |
---|
| 26 | # edpykota should be ok, minus some typos |
---|
| 27 | # |
---|
[717] | 28 | # Revision 1.3 2003/02/06 10:47:21 jalet |
---|
| 29 | # Documentation string and command line options didn't match. |
---|
| 30 | # |
---|
[715] | 31 | # Revision 1.2 2003/02/06 10:39:23 jalet |
---|
| 32 | # Preliminary edpykota work. |
---|
| 33 | # |
---|
[696] | 34 | # Revision 1.1 2003/02/05 21:41:09 jalet |
---|
| 35 | # Skeletons added for all command line tools |
---|
| 36 | # |
---|
| 37 | # |
---|
| 38 | # |
---|
| 39 | |
---|
[715] | 40 | import sys |
---|
| 41 | |
---|
| 42 | from pykota import version |
---|
| 43 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
| 44 | |
---|
| 45 | __doc__ = """edpykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres |
---|
| 46 | A Print Quota editor for PyKota. |
---|
| 47 | |
---|
| 48 | command line usage : |
---|
| 49 | |
---|
[717] | 50 | edpykota [options] user1 user2 ... userN |
---|
| 51 | edpykota [options] group1 group2 ... groupN |
---|
[715] | 52 | |
---|
| 53 | options : |
---|
| 54 | |
---|
| 55 | -v | --version Prints edpykota's version number then exits. |
---|
| 56 | -h | --help Prints this message then exits. |
---|
| 57 | |
---|
| 58 | -a | --add Adds users and/or printers if they don't |
---|
| 59 | exist on the Quota Storage Server. |
---|
| 60 | |
---|
| 61 | -u | --users Edit users print quotas, this is the default. |
---|
| 62 | |
---|
[719] | 63 | -P | --printer p Edit quotas on printer p only. Actually p can |
---|
| 64 | use wildcards characters to select only |
---|
| 65 | some printers. The default value is *, meaning |
---|
| 66 | all printers. |
---|
[715] | 67 | |
---|
| 68 | -g | --groups Edit groups print quotas instead of users. |
---|
| 69 | |
---|
| 70 | -p | --prototype u|g Uses user u or group g as a prototype to set |
---|
| 71 | print quotas |
---|
| 72 | |
---|
| 73 | -S | --softlimit sl Sets the quota soft limit to sl pages. |
---|
| 74 | |
---|
| 75 | -H | --hardlimit hl Sets the quota hard limit to hl pages. |
---|
| 76 | |
---|
| 77 | examples : |
---|
| 78 | |
---|
| 79 | $ edpykota -p jerome john paul george ringo |
---|
| 80 | |
---|
| 81 | This will set print quotas for the users john, paul, george and ringo |
---|
| 82 | to the same values than user jerome. User jerome must exist. |
---|
| 83 | |
---|
| 84 | $ edpykota --printer lp -S 50 -H 60 jerome |
---|
| 85 | |
---|
| 86 | This will set jerome's print quota on the lp printer to a soft limit |
---|
| 87 | of 50 pages, and a hard limit of 60 pages. If either user jerome or |
---|
| 88 | printer lp doesn't exist on the Quota Storage Server then nothing is done. |
---|
| 89 | |
---|
| 90 | $ edpykota --add --printer lp -S 50 -H 60 jerome |
---|
| 91 | |
---|
| 92 | Same as above, but if either user jerome or printer lp doesn't exist |
---|
| 93 | on the Quota Storage Server they are automatically added. |
---|
| 94 | WARNING : the CUPS PPD file for this printer must still be modified |
---|
| 95 | manually, as well as pykota's configuration file for a |
---|
| 96 | new printer to be managed successfully. |
---|
| 97 | |
---|
| 98 | $ edpykota -g -S 500 -H 550 financial support |
---|
| 99 | |
---|
| 100 | This will set print quota soft limit to 500 pages and hard limit |
---|
| 101 | to 550 pages for groups financial and support on all printers. |
---|
| 102 | |
---|
| 103 | This program is free software; you can redistribute it and/or modify |
---|
| 104 | it under the terms of the GNU General Public License as published by |
---|
| 105 | the Free Software Foundation; either version 2 of the License, or |
---|
| 106 | (at your option) any later version. |
---|
| 107 | |
---|
| 108 | This program is distributed in the hope that it will be useful, |
---|
| 109 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 110 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 111 | GNU General Public License for more details. |
---|
| 112 | |
---|
| 113 | You should have received a copy of the GNU General Public License |
---|
| 114 | along with this program; if not, write to the Free Software |
---|
| 115 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 116 | |
---|
| 117 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
| 118 | |
---|
[719] | 119 | class EdPyKota(PyKotaTool) : |
---|
| 120 | """A class for edpykota.""" |
---|
| 121 | def main(self, names, options) : |
---|
| 122 | """Edit user or group quotas.""" |
---|
| 123 | printernames = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 124 | if not printernames : |
---|
[748] | 125 | if options["add"] and options["printer"] and options["printer"].isalpha() : |
---|
| 126 | self.storage.addPrinter(options["printer"]) |
---|
| 127 | else : |
---|
| 128 | raise PyKotaToolError, "There's no printer matching %s" % options["printer"] |
---|
[719] | 129 | softlimit = hardlimit = None |
---|
| 130 | if options["softlimit"] : |
---|
| 131 | try : |
---|
| 132 | softlimit = int(options["softlimit"].strip()) |
---|
| 133 | except ValueError : |
---|
| 134 | raise PyKotaToolError, "Invalid softlimit value %s." % options["softlimit"] |
---|
| 135 | if options["hardlimit"] : |
---|
| 136 | try : |
---|
| 137 | hardlimit = int(options["hardlimit"].strip()) |
---|
| 138 | except ValueError : |
---|
| 139 | raise PyKotaToolError, "Invalid hardlimit value %s." % options["hardlimit"] |
---|
| 140 | if hardlimit < softlimit : |
---|
| 141 | # error, exchange them |
---|
| 142 | self.logger.log_message("Hard limit %i is less than soft limit %i, values will be exchanged." % (hardlimit, softlimit), "warn") |
---|
| 143 | (softlimit, hardlimit) = (hardlimit, softlimit) |
---|
| 144 | for printer in printernames : |
---|
| 145 | if options["prototype"] : |
---|
| 146 | if options["users"] : |
---|
| 147 | prototype = self.storage.getUserPQuota(options["prototype"], printer) |
---|
| 148 | else : |
---|
| 149 | prototype = self.storage.getGroupPQuota(options["prototype"], printer) |
---|
| 150 | if prototype is None : |
---|
| 151 | self.logger.message("Prototype %s not found in Quota Storage for printer %s." % (options["prototype"], printer)) |
---|
| 152 | continue # skip this printer |
---|
| 153 | else : |
---|
| 154 | (softlimit, hardlimit) = (prototype["softlimit"], prototype["hardlimit"]) |
---|
| 155 | if (hardlimit is None) or (softlimit is None) : |
---|
| 156 | raise PyKotaToolError, "Both hard and soft limits must be set ! Aborting." |
---|
| 157 | if hardlimit is None : |
---|
| 158 | hardlimit = softlimit |
---|
| 159 | self.logger.message("Undefined hard limit set to %i on printer %s." % (hardlimit, printer)) |
---|
| 160 | if softlimit is None : |
---|
| 161 | softlimit = hardlimit |
---|
| 162 | self.logger.message("Undefined soft limit set to %i on printer %s." % (softlimit, printer)) |
---|
| 163 | for name in names : |
---|
| 164 | if options["users"] : |
---|
| 165 | quota = self.storage.getUserPQuota(name, printer) |
---|
| 166 | else : |
---|
| 167 | quota = self.storage.getGroupPQuota(name, printer) |
---|
| 168 | if quota is None : |
---|
| 169 | # not found |
---|
| 170 | if options["add"] : |
---|
| 171 | if options["users"] : |
---|
| 172 | self.storage.addUserPQuota(name, printer) |
---|
| 173 | quota = self.storage.getUserPQuota(name, printer) |
---|
| 174 | else : |
---|
| 175 | self.storage.addGroupPQuota(name, printer) |
---|
| 176 | quota = self.storage.getGroupPQuota(name, printer) |
---|
| 177 | if quota is None : |
---|
| 178 | self.logger.log_message("Quota not found for object %s on printer %s." % (name, printer)) |
---|
| 179 | else : |
---|
| 180 | if options["users"] : |
---|
| 181 | self.storage.setUserPQuota(name, printer, softlimit, hardlimit) |
---|
| 182 | else : |
---|
| 183 | self.storage.setGroupPQuota(name, printer, softlimit, hardlimit) |
---|
| 184 | |
---|
[715] | 185 | if __name__ == "__main__" : |
---|
| 186 | try : |
---|
| 187 | defaults = { \ |
---|
| 188 | "users" : 1, \ |
---|
| 189 | "groups" : 0, \ |
---|
[719] | 190 | "printer" : "*", \ |
---|
[715] | 191 | } |
---|
| 192 | short_options = "vhaugp:P:S:H:" |
---|
| 193 | long_options = ["help", "version", "add", "users", "groups", "prototype=", "printer=", "softlimit=", "hardlimit="] |
---|
| 194 | |
---|
| 195 | # Initializes the command line tool |
---|
[720] | 196 | editor = EdPyKota(doc=__doc__) |
---|
[715] | 197 | |
---|
| 198 | # parse and checks the command line |
---|
[720] | 199 | (options, args) = editor.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
[715] | 200 | |
---|
| 201 | # sets long options |
---|
| 202 | options["help"] = options["h"] or options["help"] |
---|
| 203 | options["version"] = options["v"] or options["version"] |
---|
| 204 | options["add"] = options["a"] or options["add"] |
---|
| 205 | options["users"] = options["u"] or options["users"] or defaults["users"] |
---|
| 206 | options["groups"] = options["g"] or options["groups"] or defaults["groups"] |
---|
| 207 | options["prototype"] = options["p"] or options["prototype"] |
---|
[720] | 208 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
[715] | 209 | options["softlimit"] = options["S"] or options["softlimit"] |
---|
| 210 | options["hardlimit"] = options["H"] or options["hardlimit"] |
---|
| 211 | |
---|
| 212 | if options["help"] : |
---|
[720] | 213 | editor.display_usage_and_quit() |
---|
[715] | 214 | elif options["version"] : |
---|
[720] | 215 | editor.display_version_and_quit() |
---|
[715] | 216 | elif options["users"] and options["groups"] : |
---|
[720] | 217 | raise PyKotaToolError, "edpykota: options --users and --groups are incompatible." |
---|
| 218 | elif (options["softlimit"] or options["hardlimit"]) and options["prototype"] : |
---|
| 219 | raise PyKotaToolError, "edpykota: options --softlimit or --hardlimit and --prototype are incompatible." |
---|
[719] | 220 | elif options["groups"] : |
---|
[720] | 221 | raise PyKotaToolError, "edpykota: options --groups is currently not implemented." |
---|
[715] | 222 | else : |
---|
[720] | 223 | sys.exit(editor.main(args, options)) |
---|
[715] | 224 | except PyKotaToolError, msg : |
---|
| 225 | sys.stderr.write("%s\n" % msg) |
---|
| 226 | sys.stderr.flush() |
---|
| 227 | sys.exit(-1) |
---|