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