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