#! /usr/bin/env python # PyKota Print Quota Editor # # PyKota - Print Quotas for CUPS # # (c) 2003 Jerome Alet # You're welcome to redistribute this software under the # terms of the GNU General Public Licence version 2.0 # or, at your option, any higher version. # # You can read the complete GNU GPL in the file COPYING # which should come along with this software, or visit # the Free Software Foundation's WEB site http://www.fsf.org # # $Id$ # # $Log$ # Revision 1.4 2003/02/06 14:28:59 jalet # edpykota should be ok, minus some typos # # Revision 1.3 2003/02/06 10:47:21 jalet # Documentation string and command line options didn't match. # # Revision 1.2 2003/02/06 10:39:23 jalet # Preliminary edpykota work. # # Revision 1.1 2003/02/05 21:41:09 jalet # Skeletons added for all command line tools # # # import sys from pykota import version from pykota.tool import PyKotaTool, PyKotaToolError __doc__ = """edpykota v%s (C) 2003 C@LL - Conseil Internet & Logiciels Libres A Print Quota editor for PyKota. command line usage : edpykota [options] user1 user2 ... userN edpykota [options] group1 group2 ... groupN options : -v | --version Prints edpykota's version number then exits. -h | --help Prints this message then exits. -a | --add Adds users and/or printers if they don't exist on the Quota Storage Server. -u | --users Edit users print quotas, this is the default. -P | --printer p Edit quotas on printer p only. Actually p can use wildcards characters to select only some printers. The default value is *, meaning all printers. -g | --groups Edit groups print quotas instead of users. -p | --prototype u|g Uses user u or group g as a prototype to set print quotas -S | --softlimit sl Sets the quota soft limit to sl pages. -H | --hardlimit hl Sets the quota hard limit to hl pages. examples : $ edpykota -p jerome john paul george ringo This will set print quotas for the users john, paul, george and ringo to the same values than user jerome. User jerome must exist. $ edpykota --printer lp -S 50 -H 60 jerome This will set jerome's print quota on the lp printer to a soft limit of 50 pages, and a hard limit of 60 pages. If either user jerome or printer lp doesn't exist on the Quota Storage Server then nothing is done. $ edpykota --add --printer lp -S 50 -H 60 jerome Same as above, but if either user jerome or printer lp doesn't exist on the Quota Storage Server they are automatically added. WARNING : the CUPS PPD file for this printer must still be modified manually, as well as pykota's configuration file for a new printer to be managed successfully. $ edpykota -g -S 500 -H 550 financial support This will set print quota soft limit to 500 pages and hard limit to 550 pages for groups financial and support on all printers. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. Please e-mail bugs to: %s""" % (version.__version__, version.__author__) class EdPyKota(PyKotaTool) : """A class for edpykota.""" def main(self, names, options) : """Edit user or group quotas.""" printernames = self.storage.getMatchingPrinters(options["printer"]) if not printernames : raise PyKotaToolError, "There's no printer matching %s" % options["printer"] softlimit = hardlimit = None if options["softlimit"] : try : softlimit = int(options["softlimit"].strip()) except ValueError : raise PyKotaToolError, "Invalid softlimit value %s." % options["softlimit"] if options["hardlimit"] : try : hardlimit = int(options["hardlimit"].strip()) except ValueError : raise PyKotaToolError, "Invalid hardlimit value %s." % options["hardlimit"] if hardlimit < softlimit : # error, exchange them self.logger.log_message("Hard limit %i is less than soft limit %i, values will be exchanged." % (hardlimit, softlimit), "warn") (softlimit, hardlimit) = (hardlimit, softlimit) for printer in printernames : if options["prototype"] : if options["users"] : prototype = self.storage.getUserPQuota(options["prototype"], printer) else : prototype = self.storage.getGroupPQuota(options["prototype"], printer) if prototype is None : self.logger.message("Prototype %s not found in Quota Storage for printer %s." % (options["prototype"], printer)) continue # skip this printer else : (softlimit, hardlimit) = (prototype["softlimit"], prototype["hardlimit"]) if (hardlimit is None) or (softlimit is None) : raise PyKotaToolError, "Both hard and soft limits must be set ! Aborting." if hardlimit is None : hardlimit = softlimit self.logger.message("Undefined hard limit set to %i on printer %s." % (hardlimit, printer)) if softlimit is None : softlimit = hardlimit self.logger.message("Undefined soft limit set to %i on printer %s." % (softlimit, printer)) for name in names : if options["users"] : quota = self.storage.getUserPQuota(name, printer) else : quota = self.storage.getGroupPQuota(name, printer) if quota is None : # not found if options["add"] : if options["users"] : self.storage.addUserPQuota(name, printer) quota = self.storage.getUserPQuota(name, printer) else : self.storage.addGroupPQuota(name, printer) quota = self.storage.getGroupPQuota(name, printer) if quota is None : self.logger.log_message("Quota not found for object %s on printer %s." % (name, printer)) else : if options["users"] : self.storage.setUserPQuota(name, printer, softlimit, hardlimit) else : self.storage.setGroupPQuota(name, printer, softlimit, hardlimit) if __name__ == "__main__" : try : defaults = { \ "users" : 1, \ "groups" : 0, \ "printer" : "*", \ } short_options = "vhaugp:P:S:H:" long_options = ["help", "version", "add", "users", "groups", "prototype=", "printer=", "softlimit=", "hardlimit="] # Initializes the command line tool tool = EdPyKota(doc=__doc__) # parse and checks the command line (options, args) = tool.parseCommandline(sys.argv[1:], short_options, long_options) # sets long options options["help"] = options["h"] or options["help"] options["version"] = options["v"] or options["version"] options["add"] = options["a"] or options["add"] options["users"] = options["u"] or options["users"] or defaults["users"] options["groups"] = options["g"] or options["groups"] or defaults["groups"] options["prototype"] = options["p"] or options["prototype"] options["printer"] = options["P"] or options["printer"] options["softlimit"] = options["S"] or options["softlimit"] options["hardlimit"] = options["H"] or options["hardlimit"] if options["help"] : tool.display_usage_and_quit() elif options["version"] : tool.display_version_and_quit() elif options["users"] and options["groups"] : raise PyKotaToolError, "edpykota: options --users and --groups are incompatible.\n" elif (options["softlimit"] or options["hardlimit"]) and optiions["prototype"] : raise PyKotaToolError, "edpykota: options --softlimit or --hardlimit and --prototype are incompatible.\n" elif options["groups"] : raise PyKotaToolError, "edpykota: options --groups is currently not implemented.\n" else : sys.exit(apply(tool.main, args, options)) except PyKotaToolError, msg : sys.stderr.write("%s\n" % msg) sys.stderr.flush() sys.exit(-1)