[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$ |
---|
[825] | 19 | # Revision 1.24 2003/02/27 23:48:41 jalet |
---|
| 20 | # Correctly maps PyKota's log levels to syslog log levels |
---|
| 21 | # |
---|
[824] | 22 | # Revision 1.23 2003/02/27 22:55:20 jalet |
---|
| 23 | # WARN log priority doesn't exist. |
---|
| 24 | # |
---|
[820] | 25 | # Revision 1.22 2003/02/27 09:37:02 jalet |
---|
| 26 | # Wildcards seem to work now |
---|
| 27 | # |
---|
[815] | 28 | # Revision 1.21 2003/02/27 09:04:46 jalet |
---|
| 29 | # user and group names can be passed as wildcards if the --add option |
---|
| 30 | # is not set. The default is to act on all users or groups. |
---|
| 31 | # |
---|
[791] | 32 | # Revision 1.20 2003/02/10 12:07:30 jalet |
---|
| 33 | # Now repykota should output the recorded total page number for each printer too. |
---|
| 34 | # |
---|
[775] | 35 | # Revision 1.19 2003/02/09 13:40:29 jalet |
---|
| 36 | # typo |
---|
| 37 | # |
---|
[772] | 38 | # Revision 1.18 2003/02/09 12:56:53 jalet |
---|
| 39 | # Internationalization begins... |
---|
| 40 | # |
---|
[769] | 41 | # Revision 1.17 2003/02/08 22:47:23 jalet |
---|
| 42 | # Option --reset can now be used without having to use soft and hard limits |
---|
| 43 | # on the command line. |
---|
| 44 | # |
---|
[768] | 45 | # Revision 1.16 2003/02/08 22:39:46 jalet |
---|
| 46 | # --reset command line option added |
---|
| 47 | # |
---|
[767] | 48 | # Revision 1.15 2003/02/08 22:20:01 jalet |
---|
| 49 | # Clarification on why we don't check with /etc/passwd to see if the user |
---|
| 50 | # name is valid or not. |
---|
| 51 | # |
---|
[766] | 52 | # Revision 1.14 2003/02/08 22:18:15 jalet |
---|
| 53 | # Now checks user and group names for validity before adding them |
---|
| 54 | # |
---|
[763] | 55 | # Revision 1.13 2003/02/08 22:09:02 jalet |
---|
| 56 | # Only printer was added the first time. |
---|
| 57 | # |
---|
[762] | 58 | # Revision 1.12 2003/02/08 21:44:49 jalet |
---|
| 59 | # Python 2.1 string module doesn't define ascii_letters |
---|
| 60 | # |
---|
[758] | 61 | # Revision 1.11 2003/02/08 09:42:44 jalet |
---|
| 62 | # Better handle wrong or bad command line arguments |
---|
| 63 | # |
---|
[757] | 64 | # Revision 1.10 2003/02/08 09:39:20 jalet |
---|
| 65 | # typos |
---|
| 66 | # |
---|
[756] | 67 | # Revision 1.9 2003/02/08 09:38:06 jalet |
---|
| 68 | # Badly placed test |
---|
| 69 | # |
---|
[750] | 70 | # Revision 1.8 2003/02/07 22:53:57 jalet |
---|
| 71 | # Checks if printer name is valid before adding it |
---|
| 72 | # |
---|
[749] | 73 | # Revision 1.7 2003/02/07 22:17:58 jalet |
---|
| 74 | # Incomplete test |
---|
| 75 | # |
---|
[748] | 76 | # Revision 1.6 2003/02/07 22:13:13 jalet |
---|
| 77 | # Perhaps edpykota is now able to add printers !!! Oh, stupid me ! |
---|
| 78 | # |
---|
[720] | 79 | # Revision 1.5 2003/02/06 14:49:04 jalet |
---|
| 80 | # edpykota should be ok now |
---|
| 81 | # |
---|
[719] | 82 | # Revision 1.4 2003/02/06 14:28:59 jalet |
---|
| 83 | # edpykota should be ok, minus some typos |
---|
| 84 | # |
---|
[717] | 85 | # Revision 1.3 2003/02/06 10:47:21 jalet |
---|
| 86 | # Documentation string and command line options didn't match. |
---|
| 87 | # |
---|
[715] | 88 | # Revision 1.2 2003/02/06 10:39:23 jalet |
---|
| 89 | # Preliminary edpykota work. |
---|
| 90 | # |
---|
[696] | 91 | # Revision 1.1 2003/02/05 21:41:09 jalet |
---|
| 92 | # Skeletons added for all command line tools |
---|
| 93 | # |
---|
| 94 | # |
---|
| 95 | # |
---|
| 96 | |
---|
[715] | 97 | import sys |
---|
| 98 | |
---|
| 99 | from pykota import version |
---|
| 100 | from pykota.tool import PyKotaTool, PyKotaToolError |
---|
| 101 | |
---|
| 102 | __doc__ = """edpykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres |
---|
| 103 | A Print Quota editor for PyKota. |
---|
| 104 | |
---|
| 105 | command line usage : |
---|
| 106 | |
---|
[717] | 107 | edpykota [options] user1 user2 ... userN |
---|
| 108 | edpykota [options] group1 group2 ... groupN |
---|
[715] | 109 | |
---|
| 110 | options : |
---|
| 111 | |
---|
| 112 | -v | --version Prints edpykota's version number then exits. |
---|
| 113 | -h | --help Prints this message then exits. |
---|
| 114 | |
---|
| 115 | -a | --add Adds users and/or printers if they don't |
---|
| 116 | exist on the Quota Storage Server. |
---|
| 117 | |
---|
| 118 | -u | --users Edit users print quotas, this is the default. |
---|
| 119 | |
---|
[719] | 120 | -P | --printer p Edit quotas on printer p only. Actually p can |
---|
| 121 | use wildcards characters to select only |
---|
| 122 | some printers. The default value is *, meaning |
---|
| 123 | all printers. |
---|
[715] | 124 | |
---|
| 125 | -g | --groups Edit groups print quotas instead of users. |
---|
| 126 | |
---|
| 127 | -p | --prototype u|g Uses user u or group g as a prototype to set |
---|
| 128 | print quotas |
---|
| 129 | |
---|
[768] | 130 | -r | --reset Resets the printed page counter for the user |
---|
| 131 | or group to zero. The life time page counter |
---|
| 132 | is kept unchanged. |
---|
| 133 | |
---|
[715] | 134 | -S | --softlimit sl Sets the quota soft limit to sl pages. |
---|
| 135 | |
---|
| 136 | -H | --hardlimit hl Sets the quota hard limit to hl pages. |
---|
| 137 | |
---|
[815] | 138 | user1 through userN and group1 through groupN can use wildcards |
---|
| 139 | if the --add option is not set. |
---|
| 140 | |
---|
[715] | 141 | examples : |
---|
| 142 | |
---|
| 143 | $ edpykota -p jerome john paul george ringo |
---|
| 144 | |
---|
| 145 | This will set print quotas for the users john, paul, george and ringo |
---|
| 146 | to the same values than user jerome. User jerome must exist. |
---|
| 147 | |
---|
| 148 | $ edpykota --printer lp -S 50 -H 60 jerome |
---|
| 149 | |
---|
| 150 | This will set jerome's print quota on the lp printer to a soft limit |
---|
| 151 | of 50 pages, and a hard limit of 60 pages. If either user jerome or |
---|
| 152 | printer lp doesn't exist on the Quota Storage Server then nothing is done. |
---|
| 153 | |
---|
| 154 | $ edpykota --add --printer lp -S 50 -H 60 jerome |
---|
| 155 | |
---|
| 156 | Same as above, but if either user jerome or printer lp doesn't exist |
---|
| 157 | on the Quota Storage Server they are automatically added. |
---|
| 158 | WARNING : the CUPS PPD file for this printer must still be modified |
---|
| 159 | manually, as well as pykota's configuration file for a |
---|
| 160 | new printer to be managed successfully. |
---|
| 161 | |
---|
| 162 | $ edpykota -g -S 500 -H 550 financial support |
---|
| 163 | |
---|
| 164 | This will set print quota soft limit to 500 pages and hard limit |
---|
| 165 | to 550 pages for groups financial and support on all printers. |
---|
[769] | 166 | |
---|
[815] | 167 | $ edpykota --reset jerome "jo*" |
---|
[769] | 168 | |
---|
[815] | 169 | This will reset jerome's page counter to zero on all printers, as |
---|
| 170 | well as every user whose name begins with 'jo'. |
---|
| 171 | Their life time page counter on each printer will be kept unchanged. |
---|
[715] | 172 | |
---|
| 173 | This program is free software; you can redistribute it and/or modify |
---|
| 174 | it under the terms of the GNU General Public License as published by |
---|
| 175 | the Free Software Foundation; either version 2 of the License, or |
---|
| 176 | (at your option) any later version. |
---|
| 177 | |
---|
| 178 | This program is distributed in the hope that it will be useful, |
---|
| 179 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 180 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 181 | GNU General Public License for more details. |
---|
| 182 | |
---|
| 183 | You should have received a copy of the GNU General Public License |
---|
| 184 | along with this program; if not, write to the Free Software |
---|
| 185 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 186 | |
---|
| 187 | Please e-mail bugs to: %s""" % (version.__version__, version.__author__) |
---|
| 188 | |
---|
[719] | 189 | class EdPyKota(PyKotaTool) : |
---|
| 190 | """A class for edpykota.""" |
---|
| 191 | def main(self, names, options) : |
---|
| 192 | """Edit user or group quotas.""" |
---|
| 193 | printernames = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 194 | if not printernames : |
---|
[750] | 195 | pname = options["printer"] |
---|
| 196 | if options["add"] and pname : |
---|
[763] | 197 | if self.isValidName(pname) : |
---|
[750] | 198 | self.storage.addPrinter(pname) |
---|
[791] | 199 | printernames = [ (pname, 0) ] |
---|
[750] | 200 | else : |
---|
[772] | 201 | raise PyKotaToolError, _("Invalid printer name %s") % pname |
---|
[748] | 202 | else : |
---|
[772] | 203 | raise PyKotaToolError, _("There's no printer matching %s") % pname |
---|
[719] | 204 | softlimit = hardlimit = None |
---|
| 205 | if options["softlimit"] : |
---|
| 206 | try : |
---|
| 207 | softlimit = int(options["softlimit"].strip()) |
---|
| 208 | except ValueError : |
---|
[772] | 209 | raise PyKotaToolError, _("Invalid softlimit value %s.") % options["softlimit"] |
---|
[719] | 210 | if options["hardlimit"] : |
---|
| 211 | try : |
---|
| 212 | hardlimit = int(options["hardlimit"].strip()) |
---|
| 213 | except ValueError : |
---|
[772] | 214 | raise PyKotaToolError, _("Invalid hardlimit value %s.") % options["hardlimit"] |
---|
[749] | 215 | if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) : |
---|
[719] | 216 | # error, exchange them |
---|
[825] | 217 | self.logger.log_message(_("Hard limit %i is less than soft limit %i, values will be exchanged.") % (hardlimit, softlimit)) |
---|
[719] | 218 | (softlimit, hardlimit) = (hardlimit, softlimit) |
---|
[815] | 219 | if not names : |
---|
| 220 | if options["add"] : |
---|
| 221 | raise PyKotaToolError, _("You have to pass user names on the command line") |
---|
| 222 | else : |
---|
| 223 | names = [ "*" ] # all users |
---|
[791] | 224 | for (printer, printerpagecounter) in printernames : |
---|
[719] | 225 | if options["prototype"] : |
---|
| 226 | if options["users"] : |
---|
| 227 | prototype = self.storage.getUserPQuota(options["prototype"], printer) |
---|
| 228 | else : |
---|
| 229 | prototype = self.storage.getGroupPQuota(options["prototype"], printer) |
---|
| 230 | if prototype is None : |
---|
[772] | 231 | self.logger.log_message(_("Prototype %s not found in Quota Storage for printer %s.") % (options["prototype"], printer)) |
---|
[719] | 232 | continue # skip this printer |
---|
| 233 | else : |
---|
| 234 | (softlimit, hardlimit) = (prototype["softlimit"], prototype["hardlimit"]) |
---|
| 235 | if hardlimit is None : |
---|
| 236 | hardlimit = softlimit |
---|
[772] | 237 | self.logger.log_message(_("Undefined hard limit set to soft limit (%s) on printer %s.") % (str(hardlimit), printer)) |
---|
[719] | 238 | if softlimit is None : |
---|
| 239 | softlimit = hardlimit |
---|
[772] | 240 | self.logger.log_message(_("Undefined soft limit set to hard limit (%s) on printer %s.") % (str(softlimit), printer)) |
---|
[769] | 241 | if (not options["reset"]) and ((hardlimit is None) or (softlimit is None)) : |
---|
[772] | 242 | raise PyKotaToolError, _("Both hard and soft limits must be set ! Aborting.") |
---|
[820] | 243 | if options["add"] : |
---|
| 244 | allnames = names |
---|
| 245 | else : |
---|
| 246 | if options["users"] : |
---|
| 247 | allnames = self.storage.getPrinterUsers(printer) |
---|
| 248 | else : |
---|
| 249 | allnames = self.storage.getPrinterGroups(printer) |
---|
[815] | 250 | for name in [n for n in allnames if self.matchString(n, names)]: |
---|
[719] | 251 | if options["users"] : |
---|
| 252 | quota = self.storage.getUserPQuota(name, printer) |
---|
| 253 | else : |
---|
| 254 | quota = self.storage.getGroupPQuota(name, printer) |
---|
| 255 | if quota is None : |
---|
| 256 | # not found |
---|
| 257 | if options["add"] : |
---|
[767] | 258 | # In case we want to add something, it is crucial |
---|
| 259 | # that we DON'T check with the system accounts files |
---|
| 260 | # like /etc/passwd because users may be defined |
---|
| 261 | # only remotely |
---|
[719] | 262 | if options["users"] : |
---|
[766] | 263 | if self.isValidName(name) : |
---|
| 264 | self.storage.addUserPQuota(name, printer) |
---|
| 265 | quota = self.storage.getUserPQuota(name, printer) |
---|
| 266 | else : |
---|
[772] | 267 | self.logger.log_message(_("Invalid user name %s") % name) |
---|
[719] | 268 | else : |
---|
[766] | 269 | if self.isValidName(name) : |
---|
| 270 | self.storage.addGroupPQuota(name, printer) |
---|
| 271 | quota = self.storage.getGroupPQuota(name, printer) |
---|
| 272 | else : |
---|
[772] | 273 | self.logger.log_message(_("Invalid group name %s") % name) |
---|
[719] | 274 | if quota is None : |
---|
[772] | 275 | self.logger.log_message(_("Quota not found for object %s on printer %s.") % (name, printer)) |
---|
[719] | 276 | else : |
---|
| 277 | if options["users"] : |
---|
[769] | 278 | if (softlimit is not None) and (hardlimit is not None) : |
---|
| 279 | self.storage.setUserPQuota(name, printer, softlimit, hardlimit) |
---|
[768] | 280 | if options["reset"] : |
---|
| 281 | self.storage.resetUserPQuota(name, printer) |
---|
[719] | 282 | else : |
---|
[769] | 283 | if (softlimit is not None) and (hardlimit is not None) : |
---|
| 284 | self.storage.setGroupPQuota(name, printer, softlimit, hardlimit) |
---|
[768] | 285 | if options["reset"] : |
---|
| 286 | self.storage.resetGroupPQuota(name, printer) |
---|
[719] | 287 | |
---|
[715] | 288 | if __name__ == "__main__" : |
---|
| 289 | try : |
---|
| 290 | defaults = { \ |
---|
| 291 | "users" : 1, \ |
---|
| 292 | "groups" : 0, \ |
---|
[719] | 293 | "printer" : "*", \ |
---|
[715] | 294 | } |
---|
[768] | 295 | short_options = "vhaugrp:P:S:H:" |
---|
| 296 | long_options = ["help", "version", "add", "users", "groups", "reset", "prototype=", "printer=", "softlimit=", "hardlimit="] |
---|
[715] | 297 | |
---|
| 298 | # Initializes the command line tool |
---|
[720] | 299 | editor = EdPyKota(doc=__doc__) |
---|
[715] | 300 | |
---|
| 301 | # parse and checks the command line |
---|
[720] | 302 | (options, args) = editor.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
[715] | 303 | |
---|
| 304 | # sets long options |
---|
| 305 | options["help"] = options["h"] or options["help"] |
---|
| 306 | options["version"] = options["v"] or options["version"] |
---|
| 307 | options["add"] = options["a"] or options["add"] |
---|
| 308 | options["users"] = options["u"] or options["users"] or defaults["users"] |
---|
| 309 | options["groups"] = options["g"] or options["groups"] or defaults["groups"] |
---|
| 310 | options["prototype"] = options["p"] or options["prototype"] |
---|
[720] | 311 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
[715] | 312 | options["softlimit"] = options["S"] or options["softlimit"] |
---|
| 313 | options["hardlimit"] = options["H"] or options["hardlimit"] |
---|
[768] | 314 | options["reset"] = options["r"] or options["reset"] |
---|
[715] | 315 | |
---|
| 316 | if options["help"] : |
---|
[720] | 317 | editor.display_usage_and_quit() |
---|
[715] | 318 | elif options["version"] : |
---|
[720] | 319 | editor.display_version_and_quit() |
---|
[715] | 320 | elif options["users"] and options["groups"] : |
---|
[772] | 321 | raise PyKotaToolError, _("edpykota: options --users and --groups are incompatible.") |
---|
[720] | 322 | elif (options["softlimit"] or options["hardlimit"]) and options["prototype"] : |
---|
[772] | 323 | raise PyKotaToolError, _("edpykota: options --softlimit or --hardlimit and --prototype are incompatible.") |
---|
[719] | 324 | elif options["groups"] : |
---|
[775] | 325 | raise PyKotaToolError, _("edpykota: option --groups is currently not implemented.") |
---|
[715] | 326 | else : |
---|
[720] | 327 | sys.exit(editor.main(args, options)) |
---|
[715] | 328 | except PyKotaToolError, msg : |
---|
| 329 | sys.stderr.write("%s\n" % msg) |
---|
| 330 | sys.stderr.flush() |
---|
| 331 | sys.exit(-1) |
---|