Changeset 3413 for pykota/trunk/bin/edpykota
- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/bin/edpykota
r3411 r3413 9 9 # the Free Software Foundation, either version 3 of the License, or 10 10 # (at your option) any later version. 11 # 11 # 12 12 # This program is distributed in the hope that it will be useful, 13 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 15 # GNU General Public License for more details. 16 # 16 # 17 17 # You should have received a copy of the GNU General Public License 18 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 38 38 39 39 edpykota [options] user1 user2 ... userN 40 40 41 41 edpykota [options] group1 group2 ... groupN 42 42 … … 45 45 -v | --version Prints edpykota's version number then exits. 46 46 -h | --help Prints this message then exits. 47 47 48 48 -a | --add Adds users or groups print quota entries if 49 49 they don't exist in database. 50 50 51 51 -d | --delete Deletes users or groups print quota entries. 52 52 Users or groups are never deleted, you have … … 54 54 The history will be purge from all matching 55 55 jobs, unless -g | --groups is used. 56 56 57 57 -P | --printer p Edit quotas on printer p only. Actually p can 58 58 use wildcards characters to select only 59 59 some printers. The default value is *, meaning 60 all printers. 61 You can specify several names or wildcards, 60 all printers. 61 You can specify several names or wildcards, 62 62 by separating them with commas. 63 64 -g | --groups Edit groups print quota entries instead of 63 64 -g | --groups Edit groups print quota entries instead of 65 65 users print quota entries. 66 66 67 67 -L | --list Lists users or groups print quota entries. 68 68 69 69 -n | --noquota Sets both soft and hard limits to None for users 70 70 or groups print quota entries. 71 71 72 72 -r | --reset Resets the actual page counter for the user 73 or group to zero on the specified printers. 73 or group to zero on the specified printers. 74 74 The life time page counter is kept unchanged. 75 75 76 76 -R | --hardreset Resets the actual and life time page counters 77 for the user or group to zero on the specified 77 for the user or group to zero on the specified 78 78 printers. This is a shortcut for '--used 0'. 79 79 80 80 -s | --skipexisting In combination with the --add option above, tells 81 81 edpykota to not modify existing print quota entries. 82 83 -S | --softlimit sl Sets the quota soft limit to sl pages. 84 82 83 -S | --softlimit sl Sets the quota soft limit to sl pages. 84 85 85 -H | --hardlimit hl Sets the quota hard limit to hl pages. 86 86 87 87 -I | --increase v Increase existing Soft and Hard limits by the value 88 88 of v. You can prefix v with + or -, if no sign is … … 104 104 user1 through userN and group1 through groupN can use wildcards 105 105 if the --add option is not set. 106 107 examples : 106 107 examples : 108 108 109 109 $ edpykota --add john paul george ringo 110 110 111 111 This will create print quota entries for users john, paul, george 112 112 and ringo on all printers. These print quota entries will have no 113 113 limit set. 114 114 115 115 $ edpykota --printer lp -S 50 -H 60 jerome 116 116 117 117 This will set jerome's print quota on the lp printer to a soft limit 118 118 of 50 pages, and a hard limit of 60 pages. Both user jerome and … … 120 120 commands, respectively. 121 121 122 $ edpykota -g -S 500 -H 550 financial support 123 122 $ edpykota -g -S 500 -H 550 financial support 123 124 124 This will set print quota soft limit to 500 pages and hard limit 125 125 to 550 pages for groups financial and support on all printers. 126 126 127 127 $ edpykota --reset jerome "jo*" 128 128 129 129 This will reset jerome's page counter to zero on all printers, as 130 130 well as every user whose name begins with 'jo'. … … 132 132 You can also reset the life time page counters by using the 133 133 --hardreset | -R command line option. 134 134 135 135 $ edpykota --printer hpcolor --noquota jerome 136 137 This will tell PyKota to not limit jerome when printing on the 138 hpcolor printer. All his jobs will be allowed on this printer, but 136 137 This will tell PyKota to not limit jerome when printing on the 138 hpcolor printer. All his jobs will be allowed on this printer, but 139 139 accounting of the pages he prints will still be kept. 140 140 Print Quotas for jerome on other printers are unchanged. 141 141 142 142 $ edpykota --delete --printer "HP*,XER*" jerome rachel 143 143 144 144 This will delete users jerome and rachel's print quota 145 145 entries on all printers which name begin with 'HP' or 146 146 'XER'. The jobs printed by these users on these printers 147 147 will be deleted from the history. 148 """) 149 150 class EdPyKota(PyKotaTool) : 148 """) 149 150 class EdPyKota(PyKotaTool) : 151 151 """A class for edpykota.""" 152 152 def modifyPQEntry(self, pqkey, pqentry, noquota, softlimit, hardlimit, increase, reset, hardreset, suffix, used) : … … 155 155 pqentry.setLimits(softlimit, hardlimit) 156 156 if increase : 157 newsoft = (pqentry.SoftLimit or 0) + increase 158 newhard = (pqentry.HardLimit or 0) + increase 157 newsoft = (pqentry.SoftLimit or 0) + increase 158 newhard = (pqentry.HardLimit or 0) + increase 159 159 if (newsoft >= 0) and (newhard >= 0) : 160 160 pqentry.setLimits(newsoft, newhard) 161 else : 161 else : 162 162 self.printInfo(_("You can't set negative limits for %s") % pqkey, "error") 163 163 if reset : 164 164 pqentry.reset() 165 if hardreset : 165 if hardreset : 166 166 pqentry.hardreset() 167 167 if suffix == "User" : 168 168 if used : 169 169 pqentry.setUsage(used) 170 170 171 171 def main(self, names, options) : 172 172 """Edit user or group quotas.""" 173 173 names = self.sanitizeNames(options, names) 174 suffix = (options["groups"] and "Group") or "User" 174 suffix = (options["groups"] and "Group") or "User" 175 175 printernames = options["printer"].split(",") 176 176 177 177 if not options["list"] : 178 178 percent = Percent(self) … … 182 182 if not options["list"] : 183 183 percent.setSize(len(printers) * len(entries)) 184 184 185 185 if options["list"] : 186 186 for printer in printers : … … 198 198 print " %s" % (_("Warning banners printed : %s") % pqentry.WarnCount) 199 199 print 200 elif options["delete"] : 200 elif options["delete"] : 201 201 percent.display("\n%s..." % _("Deletion")) 202 202 getattr(self.storage, "deleteMany%sPQuotas" % suffix)(printers, entries) … … 211 211 except ValueError : 212 212 raise PyKotaCommandLineError, _("Invalid used value %s.") % used 213 213 214 214 increase = options["increase"] 215 215 if increase : … … 218 218 except ValueError : 219 219 raise PyKotaCommandLineError, _("Invalid increase value %s.") % increase 220 220 221 221 noquota = options["noquota"] 222 reset = options["reset"] 222 reset = options["reset"] 223 223 hardreset = options["hardreset"] 224 224 softlimit = hardlimit = None … … 229 229 if softlimit < 0 : 230 230 raise ValueError 231 except ValueError : 231 except ValueError : 232 232 raise PyKotaCommandLineError, _("Invalid softlimit value %s.") % options["softlimit"] 233 233 if options["hardlimit"] : … … 236 236 if hardlimit < 0 : 237 237 raise ValueError 238 except ValueError : 238 except ValueError : 239 239 raise PyKotaCommandLineError, _("Invalid hardlimit value %s.") % options["hardlimit"] 240 if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) : 240 if (softlimit is not None) and (hardlimit is not None) and (hardlimit < softlimit) : 241 241 # error, exchange them 242 242 self.printInfo(_("Hard limit %i is less than soft limit %i, values will be exchanged.") % (hardlimit, softlimit)) 243 243 (softlimit, hardlimit) = (hardlimit, softlimit) 244 if hardlimit is None : 244 if hardlimit is None : 245 245 hardlimit = softlimit 246 246 if hardlimit is not None : 247 247 self.printInfo(_("Undefined hard limit set to soft limit (%s).") % str(hardlimit)) 248 if softlimit is None : 248 if softlimit is None : 249 249 softlimit = hardlimit 250 250 if softlimit is not None : 251 251 self.printInfo(_("Undefined soft limit set to hard limit (%s).") % str(softlimit)) 252 253 self.storage.beginTransaction() 252 253 self.storage.beginTransaction() 254 254 try : 255 255 if options["add"] : 256 256 percent.display("\n%s...\n" % _("Creation")) 257 if not entries : 257 if not entries : 258 258 self.printInfo(_("No entry matches %s. Please use pkusers to create them first.") % (" ".join(names)), "warn") 259 259 260 260 factory = globals()["Storage%sPQuota" % suffix] 261 261 for printer in printers : … … 270 270 hardreset, suffix, used) 271 271 oldpqentry = getattr(self.storage, "add%sPQuota" % suffix)(pqentry) 272 if oldpqentry is not None : 272 if oldpqentry is not None : 273 273 if skipexisting : 274 274 self.logdebug("%s print quota entry %s@%s already exists, skipping." % (suffix, ename, pname)) 275 else : 275 else : 276 276 self.logdebug("%s print quota entry %s@%s already exists, will be modified." % (suffix, ename, pname)) 277 277 self.modifyPQEntry(pqkey, oldpqentry, noquota, \ … … 279 279 increase, reset, \ 280 280 hardreset, suffix, used) 281 oldpqentry.save() 281 oldpqentry.save() 282 282 percent.oneMore() 283 else : 283 else : 284 284 percent.display("\n%s...\n" % _("Modification")) 285 285 for printer in printers : … … 287 287 pqkey = "%s@%s" % (entry.Name, printer.Name) 288 288 pqentry = getattr(self.storage, "get%sPQuota" % suffix)(entry, printer) 289 if pqentry.Exists : 289 if pqentry.Exists : 290 290 self.modifyPQEntry(pqkey, pqentry, noquota, \ 291 291 softlimit, hardlimit, \ 292 292 increase, reset, \ 293 293 hardreset, suffix, used) 294 pqentry.save() 294 pqentry.save() 295 295 percent.oneMore() 296 except : 296 except : 297 297 self.storage.rollbackTransaction() 298 298 raise 299 else : 299 else : 300 300 self.storage.commitTransaction() 301 301 302 302 if not options["list"] : 303 303 percent.done() 304 305 if __name__ == "__main__" : 304 305 if __name__ == "__main__" : 306 306 retcode = 0 307 307 try : … … 316 316 "printer=", "softlimit=", "hardlimit=", \ 317 317 "increase=", "used=", "skipexisting"] 318 318 319 319 # Initializes the command line tool 320 320 manager = EdPyKota(doc=__doc__) 321 321 manager.deferredInit() 322 322 323 323 # parse and checks the command line 324 324 (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) 325 325 326 326 # sets long options 327 327 options["help"] = options["h"] or options["help"] … … 331 331 options["printer"] = options["P"] or options["printer"] or defaults["printer"] 332 332 options["softlimit"] = options["S"] or options["softlimit"] 333 options["hardlimit"] = options["H"] or options["hardlimit"] 334 options["reset"] = options["r"] or options["reset"] 333 options["hardlimit"] = options["H"] or options["hardlimit"] 334 options["reset"] = options["r"] or options["reset"] 335 335 options["noquota"] = options["n"] or options["noquota"] 336 options["delete"] = options["d"] or options["delete"] 337 options["hardreset"] = options["R"] or options["hardreset"] 336 options["delete"] = options["d"] or options["delete"] 337 options["hardreset"] = options["R"] or options["hardreset"] 338 338 options["used"] = options["U"] or options["used"] 339 339 options["increase"] = options["I"] or options["increase"] 340 340 options["list"] = options["L"] or options["list"] 341 341 options["skipexisting"] = options["s"] or options["skipexisting"] 342 342 343 343 if options["help"] : 344 344 manager.display_usage_and_quit() … … 354 354 else : 355 355 retcode = manager.main(args, options) 356 except KeyboardInterrupt : 356 except KeyboardInterrupt : 357 357 logerr("\nInterrupted with Ctrl+C !\n") 358 358 retcode = -3 359 except PyKotaCommandLineError, msg : 359 except PyKotaCommandLineError, msg : 360 360 logerr("%s : %s\n" % (sys.argv[0], msg)) 361 361 retcode = -2 362 except SystemExit : 362 except SystemExit : 363 363 pass 364 364 except : 365 365 try : 366 366 manager.crashed("edpykota failed") 367 except : 367 except : 368 368 crashed("edpykota failed") 369 369 retcode = -1 … … 371 371 try : 372 372 manager.storage.close() 373 except (TypeError, NameError, AttributeError) : 373 except (TypeError, NameError, AttributeError) : 374 374 pass 375 376 sys.exit(retcode) 375 376 sys.exit(retcode)