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