#! /usr/bin/env python # -*- coding: ISO-8859-15 -*- # PyKota Printers Manager # # PyKota - Print Quotas for CUPS and LPRng # # (c) 2003, 2004, 2005 Jerome Alet # 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. # # $Id$ # # $Log$ # Revision 1.19 2005/01/17 08:44:24 jalet # Modified copyright years # # Revision 1.18 2004/10/20 12:32:51 jalet # Fixed typo in help # # Revision 1.17 2004/10/13 21:40:10 jalet # Now mandates that the user be a PyKota administrator to allow dangerous # command line options. --list is still allowed to mere mortals though. # # Revision 1.16 2004/10/11 22:53:05 jalet # Postponed string interpolation to help message's output method # # Revision 1.15 2004/10/11 12:49:06 jalet # Renders help translatable # # Revision 1.14 2004/07/19 22:37:13 jalet # pykosd is now a very good tool # # Revision 1.13 2004/07/01 19:56:41 jalet # Better dispatching of error messages # # Revision 1.12 2004/07/01 17:45:47 jalet # Added code to handle the description field for printers # # Revision 1.11 2004/06/18 13:34:48 jalet # Now all tracebacks include PyKota's version number # # Revision 1.10 2004/06/07 18:43:40 jalet # Fixed over-verbose exits when displaying help or version number # # Revision 1.9 2004/06/03 21:50:34 jalet # Improved error logging. # crashrecipient directive added. # Now exports the job's size in bytes too. # # Revision 1.8 2004/05/06 20:30:24 jalet # Added --skipexisting command line option to pkprinters # # Revision 1.7 2004/05/06 12:51:58 jalet # Documentation # # Revision 1.6 2004/05/06 12:37:29 jalet # pkpgcounter : comments # pkprinters : when --add is used, existing printers are now skipped. # # Revision 1.5 2004/04/16 16:52:09 jalet # Better formatting # # Revision 1.4 2004/04/16 16:47:57 jalet # pkprinters now accept the --list command line option # # Revision 1.3 2004/02/04 13:24:41 jalet # pkprinters can now remove printers from printers groups. # # Revision 1.2 2004/02/04 12:52:37 jalet # pkprinters' help # # Revision 1.1 2004/02/04 11:16:59 jalet # pkprinters command line tool added. # # # import os import sys import pwd from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ __doc__ = N_("""pkprinters v%s (c) 2003, 2004, 2005 C@LL - Conseil Internet & Logiciels Libres A Printers Manager for PyKota. command line usage : pkprinters [options] printer1 printer2 printer3 ... printerN options : -v | --version Prints pkprinters's version number then exits. -h | --help Prints this message then exits. -a | --add Adds printers if they don't exist on the Quota Storage Server. If they exist, they are modified unless -s|--skipexisting is also used. -d | --delete Deletes printers from the quota storage. -D | --description d Adds a textual description to printers. -c | --charge p[,j] Sets the price per page and per job to charge. Job price is optional. If both are to be set, separate them with a comma. Floating point and negative values are allowed. -g | --groups pg1[,pg2...] Adds or Remove the printer(s) to the printer groups pg1, pg2, etc... which must already exist. A printer group is just like a normal printer, only that it is usually unknown from the printing system. Create printer groups exactly the same way that you create printers, then add other printers to them with this option. Accounting is done on a printer and on all the printer groups it belongs to, quota checking is done on a printer and on all the printer groups it belongs to. If the --remove option below is not used, the default action is to add printers to the specified printer groups. -l | --list List informations about the printer(s) and the printers groups it is a member of. -r | --remove In combination with the --groups option above, remove printers from the specified printers groups. -s | --skipexisting In combination with the --add option above, tells pkprinters to not modify existing printers. printer1 through printerN can contain wildcards if the --add option is not set. examples : $ pkprinters --add -D "HP Printer" --charge 0.05,0.1 hp2100 hp2200 hp8000 Will create three printers named hp2100, hp2200 and hp8000. Their price per page will be set at 0.05 unit, and their price per job will be set at 0.1 unit. Units are in your own currency, or whatever you want them to mean. All of their descriptions will be set to the string "HP Printer". If any of these printers already exists, it will also be modified unless the -s|--skipexisting command line option is also used. $ pkprinters --delete "*" This will completely delete all printers and associated quota information, as well as their job history. USE WITH CARE ! $ pkprinters --groups Laser,HP "hp*" This will put all printers which name matches "hp*" into printers groups Laser and HP, which MUST already exist. $ pkprinters --groups LexMark --remove hp2200 This will remove the hp2200 printer from the LexMark printer group. 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""") class PKPrinters(PyKotaTool) : """A class for edpykota.""" def main(self, names, options) : """Manage printers.""" if (not self.config.isAdmin) and (not options["list"]) : raise PyKotaToolError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) if options["list"] and not names : names = ["*"] if options["groups"] : printersgroups = self.storage.getMatchingPrinters(options["groups"]) if not printersgroups : raise PyKotaToolError, _("There's no printer matching %s") % " ".join(options["groups"].split(',')) if options["charge"] : try : charges = [float(part) for part in options["charge"].split(',', 1)] except ValueError : raise PyKotaToolError, _("Invalid charge amount value %s") % options["charge"] else : if len(charges) > 2 : charges = charges[:2] if len(charges) != 2 : charges = [charges[0], None] (perpage, perjob) = charges if options["add"] : printers = [] for pname in names : printer = self.storage.getPrinter(pname) if printer.Exists : if options["skipexisting"] : self.printInfo(_("Printer %s already exists, skipping.") % printer.Name) else : self.printInfo(_("Printer %s already exists, will be modified.") % printer.Name) printers.append(printer) else : if self.isValidName(pname) : printer = self.storage.addPrinter(pname) if not printer.Exists : raise PyKotaToolError, _("Impossible to add printer %s") % pname else : printers.append(printer) else : raise PyKotaToolError, _("Invalid printer name %s") % pname else : printers = self.storage.getMatchingPrinters(",".join(names)) if not printers : raise PyKotaToolError, _("There's no printer matching %s") % " ".join(names) for printer in printers : if options["delete"] : printer.delete() elif options["list"] : parents = ", ".join([p.Name for p in self.storage.getParentPrinters(printer)]) if parents : parents = "%s %s" % (_("in"), parents) print "%s [%s] (%s + #*%s) %s" % \ (printer.Name, printer.Description, printer.PricePerJob, \ printer.PricePerPage, parents) else : if options["charge"] : printer.setPrices(perpage, perjob) if options["description"] is not None : printer.setDescription(options["description"].strip()) if options["groups"] : for pgroup in printersgroups : if options["remove"] : pgroup.delPrinterFromGroup(printer) else : pgroup.addPrinterToGroup(printer) if __name__ == "__main__" : retcode = 0 try : short_options = "hvac:D:dg:lrs" long_options = ["help", "version", "add", "charge=", "description=", "delete", "groups=", "list", "remove", "skipexisting"] # Initializes the command line tool manager = PKPrinters(doc=__doc__) # parse and checks the command line (options, args) = manager.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["charge"] = options["c"] or options["charge"] options["description"] = options["D"] or options["description"] options["delete"] = options["d"] or options["delete"] options["groups"] = options["g"] or options["groups"] options["list"] = options["l"] or options["list"] options["remove"] = options["r"] or options["remove"] options["skipexisting"] = options["s"] or options["skipexisting"] if options["help"] : manager.display_usage_and_quit() elif options["version"] : manager.display_version_and_quit() elif (options["delete"] and (options["add"] or options["groups"] or options["charge"] or options["remove"] or options["description"])) \ or (options["skipexisting"] and not options["add"]) \ or (options["list"] and (options["add"] or options["delete"] or options["groups"] or options["charge"] or options["remove"] or options["description"])) : raise PyKotaToolError, _("incompatible options, see help.") elif options["remove"] and not options["groups"] : raise PyKotaToolError, _("You have to pass printer groups names on the command line") elif (not args) and (not options["list"]) : raise PyKotaToolError, _("You have to pass printer names on the command line") else : retcode = manager.main(args, options) except SystemExit : pass except : try : manager.crashed("pkprinters failed") except : crashed("pkprinters failed") retcode = -1 try : manager.storage.close() except (TypeError, NameError, AttributeError) : pass sys.exit(retcode)