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