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