[1330] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
| 4 | # PyKota Printers Manager |
---|
| 5 | # |
---|
| 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 7 | # |
---|
[2622] | 8 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
[1330] | 9 | # This program is free software; you can redistribute it and/or modify |
---|
| 10 | # it under the terms of the GNU General Public License as published by |
---|
| 11 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 12 | # (at your option) any later version. |
---|
| 13 | # |
---|
| 14 | # This program is distributed in the hope that it will be useful, |
---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | # GNU General Public License for more details. |
---|
| 18 | # |
---|
| 19 | # You should have received a copy of the GNU General Public License |
---|
| 20 | # along with this program; if not, write to the Free Software |
---|
[2303] | 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
[1330] | 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
[2028] | 25 | # |
---|
[1330] | 26 | |
---|
[1821] | 27 | import os |
---|
[1330] | 28 | import sys |
---|
[1821] | 29 | import pwd |
---|
[1330] | 30 | |
---|
[2512] | 31 | from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ |
---|
[1330] | 32 | |
---|
[2344] | 33 | __doc__ = N_("""pkprinters v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[2267] | 34 | |
---|
[1330] | 35 | A Printers Manager for PyKota. |
---|
| 36 | |
---|
| 37 | command line usage : |
---|
| 38 | |
---|
| 39 | pkprinters [options] printer1 printer2 printer3 ... printerN |
---|
| 40 | |
---|
| 41 | options : |
---|
| 42 | |
---|
| 43 | -v | --version Prints pkprinters's version number then exits. |
---|
| 44 | -h | --help Prints this message then exits. |
---|
| 45 | |
---|
[1331] | 46 | -a | --add Adds printers if they don't exist on the Quota |
---|
[1453] | 47 | Storage Server. If they exist, they are modified |
---|
| 48 | unless -s|--skipexisting is also used. |
---|
[1330] | 49 | |
---|
[1331] | 50 | -d | --delete Deletes printers from the quota storage. |
---|
[1582] | 51 | |
---|
[1853] | 52 | -D | --description d Adds a textual description to printers. |
---|
[1330] | 53 | |
---|
| 54 | -c | --charge p[,j] Sets the price per page and per job to charge. |
---|
| 55 | Job price is optional. |
---|
| 56 | If both are to be set, separate them with a comma. |
---|
| 57 | Floating point and negative values are allowed. |
---|
| 58 | |
---|
[1332] | 59 | -g | --groups pg1[,pg2...] Adds or Remove the printer(s) to the printer |
---|
| 60 | groups pg1, pg2, etc... which must already exist. |
---|
[1330] | 61 | A printer group is just like a normal printer, |
---|
| 62 | only that it is usually unknown from the printing |
---|
| 63 | system. Create printer groups exactly the same |
---|
| 64 | way that you create printers, then add other |
---|
| 65 | printers to them with this option. |
---|
| 66 | Accounting is done on a printer and on all |
---|
| 67 | the printer groups it belongs to, quota checking |
---|
| 68 | is done on a printer and on all the printer groups |
---|
| 69 | it belongs to. |
---|
[1332] | 70 | If the --remove option below is not used, the |
---|
| 71 | default action is to add printers to the specified |
---|
| 72 | printer groups. |
---|
| 73 | |
---|
[1437] | 74 | -l | --list List informations about the printer(s) and the |
---|
| 75 | printers groups it is a member of. |
---|
| 76 | |
---|
[1332] | 77 | -r | --remove In combination with the --groups option above, |
---|
| 78 | remove printers from the specified printers groups. |
---|
[1453] | 79 | |
---|
| 80 | -s | --skipexisting In combination with the --add option above, tells |
---|
| 81 | pkprinters to not modify existing printers. |
---|
[2465] | 82 | |
---|
| 83 | -m | --maxjobsize s Sets the maximum job size allowed on the printer |
---|
| 84 | to s pages. |
---|
| 85 | |
---|
| 86 | -p | --passthrough Activate passthrough mode for the printer. In this |
---|
| 87 | mode, users are allowed to print without any impact |
---|
| 88 | on their quota or account balance. |
---|
| 89 | |
---|
| 90 | -n | --nopassthrough Deactivate passthrough mode for the printer. |
---|
| 91 | Without -p or -n, printers are created in |
---|
| 92 | normal mode, i.e. no passthrough. |
---|
[1330] | 93 | |
---|
| 94 | printer1 through printerN can contain wildcards if the --add option |
---|
| 95 | is not set. |
---|
| 96 | |
---|
| 97 | examples : |
---|
| 98 | |
---|
[1582] | 99 | $ pkprinters --add -D "HP Printer" --charge 0.05,0.1 hp2100 hp2200 hp8000 |
---|
[1330] | 100 | |
---|
[1452] | 101 | Will create three printers named hp2100, hp2200 and hp8000. |
---|
[1330] | 102 | Their price per page will be set at 0.05 unit, and their price |
---|
| 103 | per job will be set at 0.1 unit. Units are in your own currency, |
---|
| 104 | or whatever you want them to mean. |
---|
[1582] | 105 | All of their descriptions will be set to the string "HP Printer". |
---|
| 106 | If any of these printers already exists, it will also be modified |
---|
| 107 | unless the -s|--skipexisting command line option is also used. |
---|
[1330] | 108 | |
---|
| 109 | $ pkprinters --delete "*" |
---|
| 110 | |
---|
| 111 | This will completely delete all printers and associated quota information, |
---|
| 112 | as well as their job history. USE WITH CARE ! |
---|
| 113 | |
---|
| 114 | $ pkprinters --groups Laser,HP "hp*" |
---|
| 115 | |
---|
| 116 | This will put all printers which name matches "hp*" into printers groups |
---|
| 117 | Laser and HP, which MUST already exist. |
---|
| 118 | |
---|
[1332] | 119 | $ pkprinters --groups LexMark --remove hp2200 |
---|
| 120 | |
---|
| 121 | This will remove the hp2200 printer from the LexMark printer group. |
---|
[2344] | 122 | """) |
---|
[1330] | 123 | |
---|
| 124 | class PKPrinters(PyKotaTool) : |
---|
[2336] | 125 | """A class for a printers manager.""" |
---|
[1330] | 126 | def main(self, names, options) : |
---|
| 127 | """Manage printers.""" |
---|
[1821] | 128 | if (not self.config.isAdmin) and (not options["list"]) : |
---|
[2512] | 129 | raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) |
---|
[1821] | 130 | |
---|
[2657] | 131 | if options["delete"] : |
---|
[2674] | 132 | self.display("%s...\n" % _("Deletion")) |
---|
[2657] | 133 | todelete = self.storage.getMatchingPrinters(",".join(names)) |
---|
| 134 | nbtotal = len(todelete) |
---|
| 135 | for i in range(nbtotal) : |
---|
| 136 | todelete[i].delete() |
---|
| 137 | percent = 100.0 * float(i) / float(nbtotal) |
---|
| 138 | self.display("\r%.02f%%" % percent) |
---|
| 139 | else : |
---|
[2674] | 140 | if options["add"] : |
---|
| 141 | self.display("%s...\n" % _("Creation")) |
---|
| 142 | printers = [] |
---|
| 143 | nbtotal = len(names) |
---|
| 144 | for i in range(nbtotal) : |
---|
| 145 | pname = names[i] |
---|
| 146 | printer = self.storage.getPrinter(pname) |
---|
| 147 | if printer.Exists : |
---|
| 148 | if options["skipexisting"] : |
---|
| 149 | self.printInfo(_("Printer %s already exists, skipping.") % printer.Name) |
---|
| 150 | else : |
---|
| 151 | self.printInfo(_("Printer %s already exists, will be modified.") % printer.Name) |
---|
| 152 | printers.append(printer) |
---|
| 153 | else : |
---|
| 154 | if self.isValidName(pname) : |
---|
| 155 | printer = self.storage.addPrinter(pname) |
---|
| 156 | if not printer.Exists : |
---|
| 157 | raise PyKotaToolError, _("Impossible to add printer %s") % pname |
---|
| 158 | else : |
---|
| 159 | printers.append(printer) |
---|
| 160 | else : |
---|
| 161 | raise PyKotaCommandLineError, _("Invalid printer name %s") % pname |
---|
| 162 | percent = 100.0 * float(i) / float(nbtotal) |
---|
| 163 | self.display("\r%.02f%%" % percent) |
---|
| 164 | self.display("\r100.00%%\r \r%s\n" % _("Done.")) |
---|
| 165 | else : |
---|
| 166 | if not names : |
---|
| 167 | names = ["*"] |
---|
| 168 | printers = self.storage.getMatchingPrinters(",".join(names)) |
---|
| 169 | if not printers : |
---|
| 170 | raise PyKotaCommandLineError, _("There's no printer matching %s") % " ".join(names) |
---|
| 171 | |
---|
| 172 | if not options["list"] : |
---|
| 173 | self.display("%s...\n" % _("Modification")) |
---|
[1330] | 174 | |
---|
[2657] | 175 | if options["groups"] : |
---|
| 176 | printersgroups = self.storage.getMatchingPrinters(options["groups"]) |
---|
| 177 | if not printersgroups : |
---|
| 178 | raise PyKotaCommandLineError, _("There's no printer matching %s") % " ".join(options["groups"].split(',')) |
---|
[2674] | 179 | |
---|
[2657] | 180 | if options["charge"] : |
---|
| 181 | try : |
---|
| 182 | charges = [float(part) for part in options["charge"].split(',', 1)] |
---|
| 183 | except ValueError : |
---|
| 184 | raise PyKotaCommandLineError, _("Invalid charge amount value %s") % options["charge"] |
---|
| 185 | else : |
---|
| 186 | if len(charges) > 2 : |
---|
| 187 | charges = charges[:2] |
---|
| 188 | if len(charges) != 2 : |
---|
| 189 | charges = [charges[0], None] |
---|
| 190 | (perpage, perjob) = charges |
---|
| 191 | |
---|
| 192 | if options["maxjobsize"] : |
---|
| 193 | try : |
---|
| 194 | maxjobsize = int(options["maxjobsize"]) |
---|
| 195 | if maxjobsize < 0 : |
---|
| 196 | raise ValueError |
---|
| 197 | except ValueError : |
---|
| 198 | raise PyKotaCommandLineError, _("Invalid maximum job size value %s") % options["maxjobsize"] |
---|
| 199 | else : |
---|
| 200 | maxjobsize = None |
---|
| 201 | |
---|
[2674] | 202 | nbtotal = len(printers) |
---|
| 203 | for i in range(nbtotal) : |
---|
| 204 | printer = printers[i] |
---|
[2657] | 205 | if options["list"] : |
---|
| 206 | parents = ", ".join([p.Name for p in self.storage.getParentPrinters(printer)]) |
---|
| 207 | if parents : |
---|
| 208 | parents = "%s %s" % (_("in"), parents) |
---|
| 209 | print "%s [%s] (%s + #*%s)" % \ |
---|
| 210 | (printer.Name, printer.Description, printer.PricePerJob, \ |
---|
| 211 | printer.PricePerPage) |
---|
| 212 | print " %s" % (_("Passthrough mode : %s") % ((printer.PassThrough and _("ON")) or _("OFF"))) |
---|
| 213 | print " %s" % (_("Maximum job size : %s") % ((printer.MaxJobSize and (_("%s pages") % printer.MaxJobSize)) or _("Unlimited"))) |
---|
| 214 | if parents : |
---|
| 215 | print " %s" % parents |
---|
| 216 | else : |
---|
| 217 | if options["charge"] : |
---|
| 218 | printer.setPrices(perpage, perjob) |
---|
| 219 | if options["description"] is not None : |
---|
| 220 | printer.setDescription(options["description"].strip()) |
---|
| 221 | if options["nopassthrough"] and printer.PassThrough : |
---|
| 222 | self.storage.setPrinterPassThroughMode(printer, 0) |
---|
| 223 | if options["passthrough"] and not printer.PassThrough : |
---|
| 224 | self.storage.setPrinterPassThroughMode(printer, 1) |
---|
| 225 | if (maxjobsize is not None) and (printer.MaxJobSize != maxjobsize) : |
---|
| 226 | self.storage.setPrinterMaxJobSize(printer, maxjobsize) |
---|
| 227 | if options["groups"] : |
---|
| 228 | for pgroup in printersgroups : |
---|
| 229 | if options["remove"] : |
---|
| 230 | pgroup.delPrinterFromGroup(printer) |
---|
| 231 | else : |
---|
| 232 | pgroup.addPrinterToGroup(printer) |
---|
[2674] | 233 | percent = 100.0 * float(i) / float(nbtotal) |
---|
| 234 | self.display("\r%.02f%%" % percent) |
---|
| 235 | |
---|
| 236 | if not options["list"] : |
---|
| 237 | self.display("\r100.00%%\r \r%s\n" % _("Done.")) |
---|
[1330] | 238 | |
---|
| 239 | if __name__ == "__main__" : |
---|
| 240 | retcode = 0 |
---|
| 241 | try : |
---|
[2465] | 242 | short_options = "hvac:D:dg:lrsnpm:" |
---|
| 243 | long_options = ["help", "version", "add", "charge=", "description=", \ |
---|
| 244 | "delete", "groups=", "list", "remove", \ |
---|
| 245 | "skipexisting", "passthrough", "nopassthrough", \ |
---|
| 246 | "maxjobsize="] |
---|
[1330] | 247 | |
---|
| 248 | # Initializes the command line tool |
---|
| 249 | manager = PKPrinters(doc=__doc__) |
---|
[2210] | 250 | manager.deferredInit() |
---|
[1330] | 251 | |
---|
| 252 | # parse and checks the command line |
---|
| 253 | (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
| 254 | |
---|
| 255 | # sets long options |
---|
| 256 | options["help"] = options["h"] or options["help"] |
---|
| 257 | options["version"] = options["v"] or options["version"] |
---|
| 258 | options["add"] = options["a"] or options["add"] |
---|
| 259 | options["charge"] = options["c"] or options["charge"] |
---|
[1582] | 260 | options["description"] = options["D"] or options["description"] |
---|
[1330] | 261 | options["delete"] = options["d"] or options["delete"] |
---|
| 262 | options["groups"] = options["g"] or options["groups"] |
---|
[1437] | 263 | options["list"] = options["l"] or options["list"] |
---|
[1332] | 264 | options["remove"] = options["r"] or options["remove"] |
---|
[1453] | 265 | options["skipexisting"] = options["s"] or options["skipexisting"] |
---|
[2465] | 266 | options["maxjobsize"] = options["m"] or options["maxjobsize"] |
---|
| 267 | options["passthrough"] = options["p"] or options["passthrough"] |
---|
| 268 | options["nopassthrough"] = options["n"] or options["nopassthrough"] |
---|
[1330] | 269 | |
---|
| 270 | if options["help"] : |
---|
| 271 | manager.display_usage_and_quit() |
---|
| 272 | elif options["version"] : |
---|
| 273 | manager.display_version_and_quit() |
---|
[1582] | 274 | elif (options["delete"] and (options["add"] or options["groups"] or options["charge"] or options["remove"] or options["description"])) \ |
---|
[1453] | 275 | or (options["skipexisting"] and not options["add"]) \ |
---|
[2465] | 276 | or (options["list"] and (options["add"] or options["delete"] or options["groups"] or options["charge"] or options["remove"] or options["description"])) \ |
---|
| 277 | or (options["passthrough"] and options["nopassthrough"]) : |
---|
[2512] | 278 | raise PyKotaCommandLineError, _("incompatible options, see help.") |
---|
[1332] | 279 | elif options["remove"] and not options["groups"] : |
---|
[2512] | 280 | raise PyKotaCommandLineError, _("You have to pass printer groups names on the command line") |
---|
[1437] | 281 | elif (not args) and (not options["list"]) : |
---|
[2512] | 282 | raise PyKotaCommandLineError, _("You have to pass printer names on the command line") |
---|
[1330] | 283 | else : |
---|
| 284 | retcode = manager.main(args, options) |
---|
[2216] | 285 | except KeyboardInterrupt : |
---|
| 286 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 287 | retcode = -3 |
---|
[2512] | 288 | except PyKotaCommandLineError, msg : |
---|
| 289 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 290 | retcode = -2 |
---|
[1526] | 291 | except SystemExit : |
---|
| 292 | pass |
---|
[1517] | 293 | except : |
---|
| 294 | try : |
---|
| 295 | manager.crashed("pkprinters failed") |
---|
| 296 | except : |
---|
[1546] | 297 | crashed("pkprinters failed") |
---|
[1330] | 298 | retcode = -1 |
---|
| 299 | |
---|
| 300 | try : |
---|
| 301 | manager.storage.close() |
---|
| 302 | except (TypeError, NameError, AttributeError) : |
---|
| 303 | pass |
---|
| 304 | |
---|
| 305 | sys.exit(retcode) |
---|