[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 | # |
---|
| 8 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
| 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 |
---|
| 21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
| 25 | # $Log$ |
---|
[1853] | 26 | # Revision 1.18 2004/10/20 12:32:51 jalet |
---|
| 27 | # Fixed typo in help |
---|
| 28 | # |
---|
[1821] | 29 | # Revision 1.17 2004/10/13 21:40:10 jalet |
---|
| 30 | # Now mandates that the user be a PyKota administrator to allow dangerous |
---|
| 31 | # command line options. --list is still allowed to mere mortals though. |
---|
| 32 | # |
---|
[1803] | 33 | # Revision 1.16 2004/10/11 22:53:05 jalet |
---|
| 34 | # Postponed string interpolation to help message's output method |
---|
| 35 | # |
---|
[1796] | 36 | # Revision 1.15 2004/10/11 12:49:06 jalet |
---|
| 37 | # Renders help translatable |
---|
| 38 | # |
---|
[1605] | 39 | # Revision 1.14 2004/07/19 22:37:13 jalet |
---|
| 40 | # pykosd is now a very good tool |
---|
| 41 | # |
---|
[1584] | 42 | # Revision 1.13 2004/07/01 19:56:41 jalet |
---|
| 43 | # Better dispatching of error messages |
---|
| 44 | # |
---|
[1582] | 45 | # Revision 1.12 2004/07/01 17:45:47 jalet |
---|
| 46 | # Added code to handle the description field for printers |
---|
| 47 | # |
---|
[1546] | 48 | # Revision 1.11 2004/06/18 13:34:48 jalet |
---|
| 49 | # Now all tracebacks include PyKota's version number |
---|
| 50 | # |
---|
[1526] | 51 | # Revision 1.10 2004/06/07 18:43:40 jalet |
---|
| 52 | # Fixed over-verbose exits when displaying help or version number |
---|
| 53 | # |
---|
[1517] | 54 | # Revision 1.9 2004/06/03 21:50:34 jalet |
---|
| 55 | # Improved error logging. |
---|
| 56 | # crashrecipient directive added. |
---|
| 57 | # Now exports the job's size in bytes too. |
---|
| 58 | # |
---|
[1453] | 59 | # Revision 1.8 2004/05/06 20:30:24 jalet |
---|
| 60 | # Added --skipexisting command line option to pkprinters |
---|
| 61 | # |
---|
[1452] | 62 | # Revision 1.7 2004/05/06 12:51:58 jalet |
---|
| 63 | # Documentation |
---|
| 64 | # |
---|
[1451] | 65 | # Revision 1.6 2004/05/06 12:37:29 jalet |
---|
| 66 | # pkpgcounter : comments |
---|
| 67 | # pkprinters : when --add is used, existing printers are now skipped. |
---|
| 68 | # |
---|
[1438] | 69 | # Revision 1.5 2004/04/16 16:52:09 jalet |
---|
| 70 | # Better formatting |
---|
| 71 | # |
---|
[1437] | 72 | # Revision 1.4 2004/04/16 16:47:57 jalet |
---|
| 73 | # pkprinters now accept the --list command line option |
---|
| 74 | # |
---|
[1332] | 75 | # Revision 1.3 2004/02/04 13:24:41 jalet |
---|
| 76 | # pkprinters can now remove printers from printers groups. |
---|
| 77 | # |
---|
[1331] | 78 | # Revision 1.2 2004/02/04 12:52:37 jalet |
---|
| 79 | # pkprinters' help |
---|
| 80 | # |
---|
[1330] | 81 | # Revision 1.1 2004/02/04 11:16:59 jalet |
---|
| 82 | # pkprinters command line tool added. |
---|
| 83 | # |
---|
| 84 | # |
---|
| 85 | # |
---|
| 86 | |
---|
[1821] | 87 | import os |
---|
[1330] | 88 | import sys |
---|
[1821] | 89 | import pwd |
---|
[1330] | 90 | |
---|
[1796] | 91 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_ |
---|
[1330] | 92 | |
---|
[1796] | 93 | __doc__ = N_("""pkprinters v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres |
---|
[1330] | 94 | A Printers Manager for PyKota. |
---|
| 95 | |
---|
| 96 | command line usage : |
---|
| 97 | |
---|
| 98 | pkprinters [options] printer1 printer2 printer3 ... printerN |
---|
| 99 | |
---|
| 100 | options : |
---|
| 101 | |
---|
| 102 | -v | --version Prints pkprinters's version number then exits. |
---|
| 103 | -h | --help Prints this message then exits. |
---|
| 104 | |
---|
[1331] | 105 | -a | --add Adds printers if they don't exist on the Quota |
---|
[1453] | 106 | Storage Server. If they exist, they are modified |
---|
| 107 | unless -s|--skipexisting is also used. |
---|
[1330] | 108 | |
---|
[1331] | 109 | -d | --delete Deletes printers from the quota storage. |
---|
[1582] | 110 | |
---|
[1853] | 111 | -D | --description d Adds a textual description to printers. |
---|
[1330] | 112 | |
---|
| 113 | -c | --charge p[,j] Sets the price per page and per job to charge. |
---|
| 114 | Job price is optional. |
---|
| 115 | If both are to be set, separate them with a comma. |
---|
| 116 | Floating point and negative values are allowed. |
---|
| 117 | |
---|
[1332] | 118 | -g | --groups pg1[,pg2...] Adds or Remove the printer(s) to the printer |
---|
| 119 | groups pg1, pg2, etc... which must already exist. |
---|
[1330] | 120 | A printer group is just like a normal printer, |
---|
| 121 | only that it is usually unknown from the printing |
---|
| 122 | system. Create printer groups exactly the same |
---|
| 123 | way that you create printers, then add other |
---|
| 124 | printers to them with this option. |
---|
| 125 | Accounting is done on a printer and on all |
---|
| 126 | the printer groups it belongs to, quota checking |
---|
| 127 | is done on a printer and on all the printer groups |
---|
| 128 | it belongs to. |
---|
[1332] | 129 | If the --remove option below is not used, the |
---|
| 130 | default action is to add printers to the specified |
---|
| 131 | printer groups. |
---|
| 132 | |
---|
[1437] | 133 | -l | --list List informations about the printer(s) and the |
---|
| 134 | printers groups it is a member of. |
---|
| 135 | |
---|
[1332] | 136 | -r | --remove In combination with the --groups option above, |
---|
| 137 | remove printers from the specified printers groups. |
---|
[1453] | 138 | |
---|
| 139 | -s | --skipexisting In combination with the --add option above, tells |
---|
| 140 | pkprinters to not modify existing printers. |
---|
[1330] | 141 | |
---|
| 142 | printer1 through printerN can contain wildcards if the --add option |
---|
| 143 | is not set. |
---|
| 144 | |
---|
| 145 | examples : |
---|
| 146 | |
---|
[1582] | 147 | $ pkprinters --add -D "HP Printer" --charge 0.05,0.1 hp2100 hp2200 hp8000 |
---|
[1330] | 148 | |
---|
[1452] | 149 | Will create three printers named hp2100, hp2200 and hp8000. |
---|
[1330] | 150 | Their price per page will be set at 0.05 unit, and their price |
---|
| 151 | per job will be set at 0.1 unit. Units are in your own currency, |
---|
| 152 | or whatever you want them to mean. |
---|
[1582] | 153 | All of their descriptions will be set to the string "HP Printer". |
---|
| 154 | If any of these printers already exists, it will also be modified |
---|
| 155 | unless the -s|--skipexisting command line option is also used. |
---|
[1330] | 156 | |
---|
| 157 | $ pkprinters --delete "*" |
---|
| 158 | |
---|
| 159 | This will completely delete all printers and associated quota information, |
---|
| 160 | as well as their job history. USE WITH CARE ! |
---|
| 161 | |
---|
| 162 | $ pkprinters --groups Laser,HP "hp*" |
---|
| 163 | |
---|
| 164 | This will put all printers which name matches "hp*" into printers groups |
---|
| 165 | Laser and HP, which MUST already exist. |
---|
| 166 | |
---|
[1332] | 167 | $ pkprinters --groups LexMark --remove hp2200 |
---|
| 168 | |
---|
| 169 | This will remove the hp2200 printer from the LexMark printer group. |
---|
| 170 | |
---|
[1330] | 171 | This program is free software; you can redistribute it and/or modify |
---|
| 172 | it under the terms of the GNU General Public License as published by |
---|
| 173 | the Free Software Foundation; either version 2 of the License, or |
---|
| 174 | (at your option) any later version. |
---|
| 175 | |
---|
| 176 | This program is distributed in the hope that it will be useful, |
---|
| 177 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 178 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 179 | GNU General Public License for more details. |
---|
| 180 | |
---|
| 181 | You should have received a copy of the GNU General Public License |
---|
| 182 | along with this program; if not, write to the Free Software |
---|
| 183 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 184 | |
---|
[1803] | 185 | Please e-mail bugs to: %s""") |
---|
[1330] | 186 | |
---|
| 187 | class PKPrinters(PyKotaTool) : |
---|
| 188 | """A class for edpykota.""" |
---|
| 189 | def main(self, names, options) : |
---|
| 190 | """Manage printers.""" |
---|
[1821] | 191 | if (not self.config.isAdmin) and (not options["list"]) : |
---|
| 192 | raise PyKotaToolError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) |
---|
| 193 | |
---|
[1437] | 194 | if options["list"] and not names : |
---|
| 195 | names = ["*"] |
---|
| 196 | |
---|
[1330] | 197 | if options["groups"] : |
---|
| 198 | printersgroups = self.storage.getMatchingPrinters(options["groups"]) |
---|
| 199 | if not printersgroups : |
---|
| 200 | raise PyKotaToolError, _("There's no printer matching %s") % " ".join(options["groups"].split(',')) |
---|
| 201 | |
---|
| 202 | if options["charge"] : |
---|
| 203 | try : |
---|
| 204 | charges = [float(part) for part in options["charge"].split(',', 1)] |
---|
| 205 | except ValueError : |
---|
| 206 | raise PyKotaToolError, _("Invalid charge amount value %s") % options["charge"] |
---|
| 207 | else : |
---|
| 208 | if len(charges) > 2 : |
---|
| 209 | charges = charges[:2] |
---|
| 210 | if len(charges) != 2 : |
---|
| 211 | charges = [charges[0], None] |
---|
| 212 | (perpage, perjob) = charges |
---|
| 213 | |
---|
| 214 | if options["add"] : |
---|
| 215 | printers = [] |
---|
| 216 | for pname in names : |
---|
| 217 | printer = self.storage.getPrinter(pname) |
---|
[1451] | 218 | if printer.Exists : |
---|
[1453] | 219 | if options["skipexisting"] : |
---|
[1584] | 220 | self.printInfo(_("Printer %s already exists, skipping.") % printer.Name) |
---|
[1453] | 221 | else : |
---|
[1584] | 222 | self.printInfo(_("Printer %s already exists, will be modified.") % printer.Name) |
---|
[1453] | 223 | printers.append(printer) |
---|
[1451] | 224 | else : |
---|
[1330] | 225 | if self.isValidName(pname) : |
---|
| 226 | printer = self.storage.addPrinter(pname) |
---|
| 227 | if not printer.Exists : |
---|
| 228 | raise PyKotaToolError, _("Impossible to add printer %s") % pname |
---|
[1451] | 229 | else : |
---|
| 230 | printers.append(printer) |
---|
[1330] | 231 | else : |
---|
| 232 | raise PyKotaToolError, _("Invalid printer name %s") % pname |
---|
| 233 | else : |
---|
| 234 | printers = self.storage.getMatchingPrinters(",".join(names)) |
---|
| 235 | if not printers : |
---|
| 236 | raise PyKotaToolError, _("There's no printer matching %s") % " ".join(names) |
---|
| 237 | |
---|
| 238 | for printer in printers : |
---|
| 239 | if options["delete"] : |
---|
| 240 | printer.delete() |
---|
[1437] | 241 | elif options["list"] : |
---|
| 242 | parents = ", ".join([p.Name for p in self.storage.getParentPrinters(printer)]) |
---|
| 243 | if parents : |
---|
| 244 | parents = "%s %s" % (_("in"), parents) |
---|
[1582] | 245 | print "%s [%s] (%s + #*%s) %s" % \ |
---|
| 246 | (printer.Name, printer.Description, printer.PricePerJob, \ |
---|
[1437] | 247 | printer.PricePerPage, parents) |
---|
[1330] | 248 | else : |
---|
| 249 | if options["charge"] : |
---|
| 250 | printer.setPrices(perpage, perjob) |
---|
[1582] | 251 | if options["description"] is not None : |
---|
| 252 | printer.setDescription(options["description"].strip()) |
---|
[1330] | 253 | if options["groups"] : |
---|
| 254 | for pgroup in printersgroups : |
---|
[1332] | 255 | if options["remove"] : |
---|
| 256 | pgroup.delPrinterFromGroup(printer) |
---|
| 257 | else : |
---|
| 258 | pgroup.addPrinterToGroup(printer) |
---|
[1330] | 259 | |
---|
| 260 | if __name__ == "__main__" : |
---|
| 261 | retcode = 0 |
---|
| 262 | try : |
---|
[1582] | 263 | short_options = "hvac:D:dg:lrs" |
---|
| 264 | long_options = ["help", "version", "add", "charge=", "description=", "delete", "groups=", "list", "remove", "skipexisting"] |
---|
[1330] | 265 | |
---|
| 266 | # Initializes the command line tool |
---|
| 267 | manager = PKPrinters(doc=__doc__) |
---|
| 268 | |
---|
| 269 | # parse and checks the command line |
---|
| 270 | (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
| 271 | |
---|
| 272 | # sets long options |
---|
| 273 | options["help"] = options["h"] or options["help"] |
---|
| 274 | options["version"] = options["v"] or options["version"] |
---|
| 275 | options["add"] = options["a"] or options["add"] |
---|
| 276 | options["charge"] = options["c"] or options["charge"] |
---|
[1582] | 277 | options["description"] = options["D"] or options["description"] |
---|
[1330] | 278 | options["delete"] = options["d"] or options["delete"] |
---|
| 279 | options["groups"] = options["g"] or options["groups"] |
---|
[1437] | 280 | options["list"] = options["l"] or options["list"] |
---|
[1332] | 281 | options["remove"] = options["r"] or options["remove"] |
---|
[1453] | 282 | options["skipexisting"] = options["s"] or options["skipexisting"] |
---|
[1330] | 283 | |
---|
| 284 | if options["help"] : |
---|
| 285 | manager.display_usage_and_quit() |
---|
| 286 | elif options["version"] : |
---|
| 287 | manager.display_version_and_quit() |
---|
[1582] | 288 | elif (options["delete"] and (options["add"] or options["groups"] or options["charge"] or options["remove"] or options["description"])) \ |
---|
[1453] | 289 | or (options["skipexisting"] and not options["add"]) \ |
---|
[1582] | 290 | or (options["list"] and (options["add"] or options["delete"] or options["groups"] or options["charge"] or options["remove"] or options["description"])) : |
---|
[1330] | 291 | raise PyKotaToolError, _("incompatible options, see help.") |
---|
[1332] | 292 | elif options["remove"] and not options["groups"] : |
---|
| 293 | raise PyKotaToolError, _("You have to pass printer groups names on the command line") |
---|
[1437] | 294 | elif (not args) and (not options["list"]) : |
---|
[1330] | 295 | raise PyKotaToolError, _("You have to pass printer names on the command line") |
---|
| 296 | else : |
---|
| 297 | retcode = manager.main(args, options) |
---|
[1526] | 298 | except SystemExit : |
---|
| 299 | pass |
---|
[1517] | 300 | except : |
---|
| 301 | try : |
---|
| 302 | manager.crashed("pkprinters failed") |
---|
| 303 | except : |
---|
[1546] | 304 | crashed("pkprinters failed") |
---|
[1330] | 305 | retcode = -1 |
---|
| 306 | |
---|
| 307 | try : |
---|
| 308 | manager.storage.close() |
---|
| 309 | except (TypeError, NameError, AttributeError) : |
---|
| 310 | pass |
---|
| 311 | |
---|
| 312 | sys.exit(retcode) |
---|