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