[2970] | 1 | #! /usr/bin/env python |
---|
[1144] | 2 | # -*- coding: ISO-8859-15 -*- |
---|
[696] | 3 | |
---|
| 4 | # PyKota Print Quota Editor |
---|
| 5 | # |
---|
[952] | 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
[696] | 7 | # |
---|
[2622] | 8 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
[873] | 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. |
---|
[696] | 13 | # |
---|
[873] | 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. |
---|
[696] | 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
[2073] | 25 | # |
---|
[696] | 26 | |
---|
[715] | 27 | import sys |
---|
[2762] | 28 | |
---|
[2829] | 29 | from pykota.tool import Percent, PyKotaTool, PyKotaCommandLineError, crashed, N_ |
---|
| 30 | from pykota.storage import StorageUserPQuota, StorageGroupPQuota |
---|
[715] | 31 | |
---|
[2344] | 32 | __doc__ = N_("""edpykota v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
[2267] | 33 | |
---|
[715] | 34 | A Print Quota editor for PyKota. |
---|
| 35 | |
---|
| 36 | command line usage : |
---|
| 37 | |
---|
[717] | 38 | edpykota [options] user1 user2 ... userN |
---|
[937] | 39 | |
---|
[717] | 40 | edpykota [options] group1 group2 ... groupN |
---|
[715] | 41 | |
---|
| 42 | options : |
---|
| 43 | |
---|
| 44 | -v | --version Prints edpykota's version number then exits. |
---|
| 45 | -h | --help Prints this message then exits. |
---|
| 46 | |
---|
[2712] | 47 | -a | --add Adds users or groups print quota entries if |
---|
| 48 | they don't exist in database. |
---|
[715] | 49 | |
---|
[2712] | 50 | -d | --delete Deletes users or groups print quota entries. |
---|
| 51 | Users or groups are never deleted, you have |
---|
| 52 | to use the pkusers command to delete them. |
---|
[2728] | 53 | The history will be purge from all matching |
---|
| 54 | jobs, unless -g | --groups is used. |
---|
[921] | 55 | |
---|
[719] | 56 | -P | --printer p Edit quotas on printer p only. Actually p can |
---|
| 57 | use wildcards characters to select only |
---|
| 58 | some printers. The default value is *, meaning |
---|
[1156] | 59 | all printers. |
---|
| 60 | You can specify several names or wildcards, |
---|
| 61 | by separating them with commas. |
---|
[715] | 62 | |
---|
[2712] | 63 | -g | --groups Edit groups print quota entries instead of |
---|
| 64 | users print quota entries. |
---|
[715] | 65 | |
---|
[2728] | 66 | -L | --list Lists users or groups print quota entries. |
---|
| 67 | |
---|
[2712] | 68 | -n | --noquota Sets both soft and hard limits to None for users |
---|
| 69 | or groups print quota entries. |
---|
[843] | 70 | |
---|
[1755] | 71 | -r | --reset Resets the actual page counter for the user |
---|
| 72 | or group to zero on the specified printers. |
---|
| 73 | The life time page counter is kept unchanged. |
---|
[768] | 74 | |
---|
[1755] | 75 | -R | --hardreset Resets the actual and life time page counters |
---|
| 76 | for the user or group to zero on the specified |
---|
[1967] | 77 | printers. This is a shortcut for '--used 0'. |
---|
[1755] | 78 | |
---|
[2728] | 79 | -s | --skipexisting In combination with the --add option above, tells |
---|
| 80 | edpykota to not modify existing print quota entries. |
---|
| 81 | |
---|
[715] | 82 | -S | --softlimit sl Sets the quota soft limit to sl pages. |
---|
| 83 | |
---|
| 84 | -H | --hardlimit hl Sets the quota hard limit to hl pages. |
---|
[2454] | 85 | |
---|
[2712] | 86 | -I | --increase v Increase existing Soft and Hard limits by the value |
---|
[2454] | 87 | of v. You can prefix v with + or -, if no sign is |
---|
| 88 | used, + is assumed. |
---|
[1967] | 89 | |
---|
[2712] | 90 | -U | --used u Sets the page counters for the user u pages on |
---|
| 91 | the selected printers. Doesn't work for groups, since |
---|
| 92 | their page counters are the sum of all their members' |
---|
| 93 | page counters. |
---|
| 94 | Useful for migrating users from a different system |
---|
[1967] | 95 | where they have already used some pages. Actual |
---|
| 96 | and Life Time page counters may be increased or decreased |
---|
[2712] | 97 | if u is prefixed with + or -. |
---|
[1967] | 98 | WARNING : BOTH page counters are modified in all cases, |
---|
| 99 | so be careful. |
---|
[2712] | 100 | NB : if u equals '0', then the action taken is |
---|
[1967] | 101 | the same as if --hardreset was used. |
---|
| 102 | |
---|
[815] | 103 | user1 through userN and group1 through groupN can use wildcards |
---|
| 104 | if the --add option is not set. |
---|
| 105 | |
---|
[715] | 106 | examples : |
---|
| 107 | |
---|
[2712] | 108 | $ edpykota --add john paul george ringo |
---|
[715] | 109 | |
---|
[2712] | 110 | This will create print quota entries for users john, paul, george |
---|
| 111 | and ringo on all printers. These print quota entries will have no |
---|
| 112 | limit set. |
---|
[715] | 113 | |
---|
| 114 | $ edpykota --printer lp -S 50 -H 60 jerome |
---|
| 115 | |
---|
| 116 | This will set jerome's print quota on the lp printer to a soft limit |
---|
[2712] | 117 | of 50 pages, and a hard limit of 60 pages. Both user jerome and |
---|
| 118 | printer lp have been previously created with the pkusers and pkprinters |
---|
| 119 | commands, respectively. |
---|
[715] | 120 | |
---|
| 121 | $ edpykota -g -S 500 -H 550 financial support |
---|
| 122 | |
---|
| 123 | This will set print quota soft limit to 500 pages and hard limit |
---|
| 124 | to 550 pages for groups financial and support on all printers. |
---|
[769] | 125 | |
---|
[815] | 126 | $ edpykota --reset jerome "jo*" |
---|
[769] | 127 | |
---|
[815] | 128 | This will reset jerome's page counter to zero on all printers, as |
---|
| 129 | well as every user whose name begins with 'jo'. |
---|
| 130 | Their life time page counter on each printer will be kept unchanged. |
---|
[1755] | 131 | You can also reset the life time page counters by using the |
---|
| 132 | --hardreset | -R command line option. |
---|
[843] | 133 | |
---|
[847] | 134 | $ edpykota --printer hpcolor --noquota jerome |
---|
[843] | 135 | |
---|
[847] | 136 | This will tell PyKota to not limit jerome when printing on the |
---|
| 137 | hpcolor printer. All his jobs will be allowed on this printer, but |
---|
| 138 | accounting of the pages he prints will still be kept. |
---|
[848] | 139 | Print Quotas for jerome on other printers are unchanged. |
---|
[917] | 140 | |
---|
[2712] | 141 | $ edpykota --delete --printer "HP*,XER*" jerome rachel |
---|
[917] | 142 | |
---|
[2712] | 143 | This will delete users jerome and rachel's print quota |
---|
| 144 | entries on all printers which name begin with 'HP' or |
---|
| 145 | 'XER'. The jobs printed by these users on these printers |
---|
| 146 | will be deleted from the history. |
---|
[2344] | 147 | """) |
---|
[715] | 148 | |
---|
[719] | 149 | class EdPyKota(PyKotaTool) : |
---|
| 150 | """A class for edpykota.""" |
---|
[2755] | 151 | def modifyPQEntry(self, pqkey, pqentry, noquota, softlimit, hardlimit, increase, reset, hardreset, suffix, used) : |
---|
| 152 | """Modifies a print quota entry.""" |
---|
| 153 | if noquota or ((softlimit is not None) and (hardlimit is not None)) : |
---|
| 154 | pqentry.setLimits(softlimit, hardlimit) |
---|
| 155 | if increase : |
---|
| 156 | newsoft = (pqentry.SoftLimit or 0) + increase |
---|
| 157 | newhard = (pqentry.HardLimit or 0) + increase |
---|
| 158 | if (newsoft >= 0) and (newhard >= 0) : |
---|
| 159 | pqentry.setLimits(newsoft, newhard) |
---|
| 160 | else : |
---|
| 161 | self.printInfo(_("You can't set negative limits for %s") % pqkey, "error") |
---|
| 162 | if reset : |
---|
| 163 | pqentry.reset() |
---|
| 164 | if hardreset : |
---|
| 165 | pqentry.hardreset() |
---|
| 166 | if suffix == "User" : |
---|
| 167 | if used : |
---|
| 168 | pqentry.setUsage(used) |
---|
| 169 | |
---|
[719] | 170 | def main(self, names, options) : |
---|
| 171 | """Edit user or group quotas.""" |
---|
[2762] | 172 | names = self.sanitizeNames(options, names) |
---|
[1041] | 173 | suffix = (options["groups"] and "Group") or "User" |
---|
[2731] | 174 | printernames = options["printer"].split(",") |
---|
[2743] | 175 | |
---|
[2749] | 176 | if not options["list"] : |
---|
[2783] | 177 | percent = Percent(self) |
---|
[2789] | 178 | percent.display("%s..." % _("Extracting datas")) |
---|
[2749] | 179 | printers = self.storage.getMatchingPrinters(options["printer"]) |
---|
| 180 | entries = getattr(self.storage, "getMatching%ss" % suffix)(",".join(names)) |
---|
[2783] | 181 | if not options["list"] : |
---|
| 182 | percent.setSize(len(printers) * len(entries)) |
---|
[1041] | 183 | |
---|
[2772] | 184 | if options["list"] : |
---|
[2755] | 185 | for printer in printers : |
---|
| 186 | for entry in entries : |
---|
| 187 | pqentry = getattr(self.storage, "get%sPQuota" % suffix)(entry, printer) |
---|
| 188 | if pqentry.Exists : |
---|
| 189 | print "%s@%s" % (entry.Name, printer.Name) |
---|
| 190 | print " %s" % (_("Page counter : %s") % pqentry.PageCounter) |
---|
| 191 | print " %s" % (_("Lifetime page counter : %s") % pqentry.LifePageCounter) |
---|
| 192 | print " %s" % (_("Soft limit : %s") % pqentry.SoftLimit) |
---|
| 193 | print " %s" % (_("Hard limit : %s") % pqentry.HardLimit) |
---|
| 194 | print " %s" % (_("Date limit : %s") % pqentry.DateLimit) |
---|
| 195 | print " %s (Not supported yet)" % (_("Maximum job size : %s") % ((pqentry.MaxJobSize and (_("%s pages") % pqentry.MaxJobSize)) or _("Unlimited"))) |
---|
| 196 | if hasattr(pqentry, "WarnCount") : |
---|
| 197 | print " %s" % (_("Warning banners printed : %s") % pqentry.WarnCount) |
---|
| 198 | print |
---|
[2772] | 199 | elif options["delete"] : |
---|
[2783] | 200 | percent.display("\n%s..." % _("Deletion")) |
---|
[2772] | 201 | getattr(self.storage, "deleteMany%sPQuotas" % suffix)(printers, entries) |
---|
[2782] | 202 | percent.display("\n") |
---|
[2657] | 203 | else : |
---|
[2731] | 204 | skipexisting = options["skipexisting"] |
---|
[2749] | 205 | used = options["used"] |
---|
| 206 | if used : |
---|
| 207 | used = used.strip() |
---|
| 208 | try : |
---|
| 209 | int(used) |
---|
| 210 | except ValueError : |
---|
| 211 | raise PyKotaCommandLineError, _("Invalid used value %s.") % used |
---|
| 212 | |
---|
| 213 | increase = options["increase"] |
---|
| 214 | if increase : |
---|
| 215 | try : |
---|
| 216 | increase = int(increase.strip()) |
---|
| 217 | except ValueError : |
---|
| 218 | raise PyKotaCommandLineError, _("Invalid increase value %s.") % increase |
---|
| 219 | |
---|
| 220 | noquota = options["noquota"] |
---|
[2755] | 221 | reset = options["reset"] |
---|
| 222 | hardreset = options["hardreset"] |
---|
| 223 | softlimit = hardlimit = None |
---|
[2749] | 224 | if not noquota : |
---|
| 225 | if options["softlimit"] : |
---|
| 226 | try : |
---|
| 227 | softlimit = int(options["softlimit"].strip()) |
---|
| 228 | if softlimit < 0 : |
---|
| 229 | raise ValueError |
---|
| 230 | except ValueError : |
---|
| 231 | raise PyKotaCommandLineError, _("Invalid softlimit value %s.") % options["softlimit"] |
---|
| 232 | if options["hardlimit"] : |
---|
| 233 | try : |
---|
| 234 | hardlimit = int(options["hardlimit"].strip()) |
---|
| 235 | if hardlimit < 0 : |
---|
| 236 | raise ValueError |
---|
| 237 | except ValueError : |
---|
| 238 | raise PyKotaCommandLineError, _("Invalid hardlimit value %s.") % options["hardlimit"] |
---|
| 239 | if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) : |
---|
| 240 | # error, exchange them |
---|
| 241 | self.printInfo(_("Hard limit %i is less than soft limit %i, values will be exchanged.") % (hardlimit, softlimit)) |
---|
| 242 | (softlimit, hardlimit) = (hardlimit, softlimit) |
---|
| 243 | if hardlimit is None : |
---|
| 244 | hardlimit = softlimit |
---|
| 245 | if hardlimit is not None : |
---|
| 246 | self.printInfo(_("Undefined hard limit set to soft limit (%s).") % str(hardlimit)) |
---|
| 247 | if softlimit is None : |
---|
| 248 | softlimit = hardlimit |
---|
| 249 | if softlimit is not None : |
---|
| 250 | self.printInfo(_("Undefined soft limit set to hard limit (%s).") % str(softlimit)) |
---|
| 251 | |
---|
[2772] | 252 | self.storage.beginTransaction() |
---|
| 253 | try : |
---|
| 254 | if options["add"] : |
---|
[2783] | 255 | percent.display("\n%s...\n" % _("Creation")) |
---|
[3053] | 256 | if not entries : |
---|
| 257 | self.printInfo(_("No entry matches %s. Please use pkusers to create them first.") % (" ".join(names)), "warn") |
---|
[2772] | 258 | |
---|
| 259 | factory = globals()["Storage%sPQuota" % suffix] |
---|
[2755] | 260 | for printer in printers : |
---|
[2772] | 261 | pname = printer.Name |
---|
[2755] | 262 | for entry in entries : |
---|
[2772] | 263 | ename = entry.Name |
---|
| 264 | pqkey = "%s@%s" % (ename, pname) |
---|
| 265 | pqentry = factory(self.storage, entry, printer) |
---|
| 266 | self.modifyPQEntry(pqkey, pqentry, noquota, \ |
---|
| 267 | softlimit, hardlimit, \ |
---|
| 268 | increase, reset, \ |
---|
| 269 | hardreset, suffix, used) |
---|
| 270 | oldpqentry = getattr(self.storage, "add%sPQuota" % suffix)(pqentry) |
---|
| 271 | if oldpqentry is not None : |
---|
| 272 | if skipexisting : |
---|
| 273 | self.logdebug("%s print quota entry %s@%s already exists, skipping." % (suffix, ename, pname)) |
---|
| 274 | else : |
---|
| 275 | self.logdebug("%s print quota entry %s@%s already exists, will be modified." % (suffix, ename, pname)) |
---|
| 276 | self.modifyPQEntry(pqkey, oldpqentry, noquota, \ |
---|
| 277 | softlimit, hardlimit, \ |
---|
| 278 | increase, reset, \ |
---|
| 279 | hardreset, suffix, used) |
---|
| 280 | oldpqentry.save() |
---|
[2782] | 281 | percent.oneMore() |
---|
[2772] | 282 | else : |
---|
[2783] | 283 | percent.display("\n%s...\n" % _("Modification")) |
---|
[2782] | 284 | for printer in printers : |
---|
| 285 | for entry in entries : |
---|
| 286 | pqkey = "%s@%s" % (entry.Name, printer.Name) |
---|
| 287 | pqentry = getattr(self.storage, "get%sPQuota" % suffix)(entry, printer) |
---|
| 288 | if pqentry.Exists : |
---|
| 289 | self.modifyPQEntry(pqkey, pqentry, noquota, \ |
---|
| 290 | softlimit, hardlimit, \ |
---|
| 291 | increase, reset, \ |
---|
| 292 | hardreset, suffix, used) |
---|
| 293 | pqentry.save() |
---|
| 294 | percent.oneMore() |
---|
[2772] | 295 | except : |
---|
| 296 | self.storage.rollbackTransaction() |
---|
| 297 | raise |
---|
| 298 | else : |
---|
| 299 | self.storage.commitTransaction() |
---|
| 300 | |
---|
[2783] | 301 | if not options["list"] : |
---|
[2782] | 302 | percent.done() |
---|
[2772] | 303 | |
---|
[715] | 304 | if __name__ == "__main__" : |
---|
[1113] | 305 | retcode = 0 |
---|
[715] | 306 | try : |
---|
| 307 | defaults = { \ |
---|
[719] | 308 | "printer" : "*", \ |
---|
[715] | 309 | } |
---|
[2728] | 310 | short_options = "vhdnagrLP:S:H:G:RU:I:s" |
---|
[2712] | 311 | long_options = ["help", "version", \ |
---|
[2728] | 312 | "delete", "list", \ |
---|
[2712] | 313 | "noquota", "add", \ |
---|
| 314 | "groups", "reset", "hardreset", \ |
---|
| 315 | "printer=", "softlimit=", "hardlimit=", \ |
---|
[2728] | 316 | "increase=", "used=", "skipexisting"] |
---|
[715] | 317 | |
---|
| 318 | # Initializes the command line tool |
---|
[2712] | 319 | manager = EdPyKota(doc=__doc__) |
---|
| 320 | manager.deferredInit() |
---|
[715] | 321 | |
---|
| 322 | # parse and checks the command line |
---|
[2712] | 323 | (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) |
---|
[715] | 324 | |
---|
| 325 | # sets long options |
---|
| 326 | options["help"] = options["h"] or options["help"] |
---|
| 327 | options["version"] = options["v"] or options["version"] |
---|
| 328 | options["add"] = options["a"] or options["add"] |
---|
[895] | 329 | options["groups"] = options["g"] or options["groups"] |
---|
[720] | 330 | options["printer"] = options["P"] or options["printer"] or defaults["printer"] |
---|
[715] | 331 | options["softlimit"] = options["S"] or options["softlimit"] |
---|
| 332 | options["hardlimit"] = options["H"] or options["hardlimit"] |
---|
[768] | 333 | options["reset"] = options["r"] or options["reset"] |
---|
[843] | 334 | options["noquota"] = options["n"] or options["noquota"] |
---|
[921] | 335 | options["delete"] = options["d"] or options["delete"] |
---|
[1755] | 336 | options["hardreset"] = options["R"] or options["hardreset"] |
---|
[1967] | 337 | options["used"] = options["U"] or options["used"] |
---|
[2454] | 338 | options["increase"] = options["I"] or options["increase"] |
---|
[2728] | 339 | options["list"] = options["L"] or options["list"] |
---|
| 340 | options["skipexisting"] = options["s"] or options["skipexisting"] |
---|
[715] | 341 | |
---|
| 342 | if options["help"] : |
---|
[2712] | 343 | manager.display_usage_and_quit() |
---|
[715] | 344 | elif options["version"] : |
---|
[2712] | 345 | manager.display_version_and_quit() |
---|
[2728] | 346 | elif (options["add"] and options["delete"]) \ |
---|
| 347 | or (options["noquota"] and (options["hardlimit"] or options["softlimit"])) \ |
---|
| 348 | or (options["groups"] and options["used"]) \ |
---|
| 349 | or (options["skipexisting"] and not options["add"]) : |
---|
[2512] | 350 | raise PyKotaCommandLineError, _("incompatible options, see help.") |
---|
[2658] | 351 | elif options["delete"] and not args : |
---|
| 352 | raise PyKotaCommandLineError, _("You have to pass user or group names on the command line") |
---|
[715] | 353 | else : |
---|
[2712] | 354 | retcode = manager.main(args, options) |
---|
[2216] | 355 | except KeyboardInterrupt : |
---|
| 356 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
[2609] | 357 | retcode = -3 |
---|
[2512] | 358 | except PyKotaCommandLineError, msg : |
---|
| 359 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
[2609] | 360 | retcode = -2 |
---|
[1526] | 361 | except SystemExit : |
---|
| 362 | pass |
---|
[1517] | 363 | except : |
---|
| 364 | try : |
---|
[2712] | 365 | manager.crashed("edpykota failed") |
---|
[1517] | 366 | except : |
---|
[1546] | 367 | crashed("edpykota failed") |
---|
[1113] | 368 | retcode = -1 |
---|
[715] | 369 | |
---|
[1113] | 370 | try : |
---|
[2712] | 371 | manager.storage.close() |
---|
[1113] | 372 | except (TypeError, NameError, AttributeError) : |
---|
| 373 | pass |
---|
| 374 | |
---|
| 375 | sys.exit(retcode) |
---|