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