Changeset 3413 for pykota/trunk/bin/pkusers
- Timestamp:
- 09/27/08 22:02:37 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/bin/pkusers
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/>. … … 40 40 41 41 pkusers [options] user1 user2 user3 ... userN 42 43 or : 42 43 or : 44 44 45 45 pkusers --groups [options] group1 group2 group3 ... groupN … … 49 49 -v | --version Prints pkusers's version number then exits. 50 50 -h | --help Prints this message then exits. 51 51 52 52 -a | --add Adds users if they don't exist on the database. 53 53 If they exist, they are modified unless 54 54 -s|--skipexisting is also used. 55 55 56 56 -d | --delete Deletes users from the quota storage. 57 57 … … 62 62 63 63 -D | --description d Adds a textual description to users or groups. 64 64 65 65 -g | --groups Edit users groups instead of users. 66 67 -o | --overcharge f Sets the overcharging factor applied to the user 68 when computing the cost of a print job. Positive or 66 67 -o | --overcharge f Sets the overcharging factor applied to the user 68 when computing the cost of a print job. Positive or 69 69 negative floating point values are allowed, 70 70 this allows you to do some really creative … … 74 74 cost of the job for a particular user. 75 75 Only users have such a coefficient. 76 76 77 77 -i | --ingroups g1[,g2...] Puts the users into each of the groups 78 78 listed, separated by commas. The groups 79 79 must already exist in the Quota Storage. 80 80 81 81 -L | --list Lists users or groups. 82 83 -l | --limitby l Choose if the user/group is limited in printing 82 83 -l | --limitby l Choose if the user/group is limited in printing 84 84 by its account balance or by its page quota. 85 85 The default value is 'quota'. Allowed values 86 are 'quota' 'balance' 'noquota' 'noprint' 86 are 'quota' 'balance' 'noquota' 'noprint' 87 87 and 'nochange' : 88 88 89 89 - quota : limit by number of pages per printer. 90 90 - balance : limit by number of credits in account. 91 91 - noquota : no limit, accounting still done. 92 - nochange : no limit, accounting not done. 93 - noprint : printing is denied. 92 - nochange : no limit, accounting not done. 93 - noprint : printing is denied. 94 94 NB : nochange and noprint are not supported for groups. 95 96 -b | --balance b Sets the user's account balance to b. 95 96 -b | --balance b Sets the user's account balance to b. 97 97 Account balance may be increase or decreased 98 98 if b is prefixed with + or -. … … 102 102 Groups don't have a real balance, but the 103 103 sum of their users' account balance. 104 104 105 105 -C | --comment txt Defines some informational text to be associated 106 106 with a change to an user's account balance. 107 107 Only meaningful if -b | --balance is also used. 108 109 110 -r | --remove In combination with the --ingroups option above, 108 109 110 -r | --remove In combination with the --ingroups option above, 111 111 remove users from the specified users groups. 112 112 113 113 -s | --skipexisting In combination with the --add option above, tells 114 114 pkusers to not modify existing users. 115 115 116 116 user1 through userN and group1 through groupN can use wildcards 117 117 if the --add option is not set. 118 119 examples : 118 119 examples : 120 120 121 121 $ pkusers --add john paul george ringo/ringo@example.com 122 122 123 123 This will add users john, paul, george and ringo to the quota 124 database. User ringo's email address will also be set to 124 database. User ringo's email address will also be set to 125 125 'ringo@example.com' 126 126 127 127 $ pkusers --ingroups coders,it jerome 128 128 129 129 User jerome is put into the groups "coders" and "it" which must 130 130 already exist in the quota database. 131 131 132 132 $ pkusers --limitby balance jerome 133 133 134 134 This will tell PyKota to limit jerome by his account's balance 135 135 when printing. 136 136 137 137 $ pkusers --balance +10.0 --comment "He paid with his blood !" jerome 138 138 139 139 This will increase jerome's account balance by 10.0 (in your 140 140 own currency). You can decrease the account balance with a 141 141 dash prefix, and set it to a fixed amount with no prefix. 142 142 A comment will be stored for this balance change. 143 143 144 144 $ pkusers --delete jerome rachel 145 145 146 146 This will completely delete jerome and rachel from the quota 147 147 database. All their quotas and jobs will be deleted too. 148 148 149 149 $ pkusers --overcharge 2.5 poorstudent 150 150 151 151 This will overcharge the poorstudent user by a factor of 2.5. 152 152 153 153 $ pkusers --overcharge -1 jerome 154 154 155 155 User jerome will actually earn money whenever he prints. 156 156 157 157 $ pkusers --overcharge 0 boss 158 158 159 159 User boss can print at will, it won't cost him anything because the 160 160 cost of each print job will be multiplied by zero before charging … … 165 165 This will set the email address for each user to username@example.com 166 166 """) 167 168 class PKUsers(PyKotaTool) : 167 168 class PKUsers(PyKotaTool) : 169 169 """A class for a users and users groups manager.""" 170 170 def modifyEntry(self, entry, groups, limitby, description, overcharge=None, balance=None, balancevalue=None, comment=None, email=None) : … … 172 172 if description is not None : # NB : "" is allowed ! 173 173 entry.setDescription(description) 174 if limitby : 174 if limitby : 175 175 entry.setLimitBy(limitby) 176 176 if not groups : … … 181 181 raise PyKotaCommandLineError, _("Invalid email address %s") % email 182 182 entry.setEmail(email) 183 if overcharge is not None : # NB : 0 is allowed ! 183 if overcharge is not None : # NB : 0 is allowed ! 184 184 entry.setOverChargeFactor(overcharge) 185 185 if balance : … … 192 192 newlifetimepaid = float(entry.LifeTimePaid or 0.0) + diff 193 193 entry.setAccountBalance(balancevalue, newlifetimepaid, comment) 194 195 def manageUsersGroups(self, ugroups, user, remove) : 194 195 def manageUsersGroups(self, ugroups, user, remove) : 196 196 """Manage user group membership.""" 197 197 for ugroup in ugroups : … … 200 200 else : 201 201 ugroup.addUserToGroup(user) 202 202 203 203 def main(self, names, options) : 204 204 """Manage users or groups.""" 205 205 names = self.sanitizeNames(options, names) 206 suffix = (options["groups"] and "Group") or "User" 207 206 suffix = (options["groups"] and "Group") or "User" 207 208 208 if not options["list"] : 209 209 percent = Percent(self) 210 210 211 211 if not options["add"] : 212 212 if not options["list"] : … … 219 219 percent.display("\n") 220 220 raise PyKotaCommandLineError, _("There's no %s matching %s") % (_(suffix.lower()), " ".join(names)) 221 if not options["list"] : 221 if not options["list"] : 222 222 percent.setSize(len(entries)) 223 223 224 224 if options["list"] : 225 225 if suffix == "User" : … … 229 229 email = entry.Email 230 230 if not email : 231 if maildomain : 231 if maildomain : 232 232 email = "%s@%s" % (entry.Name, maildomain) 233 elif smtpserver : 233 elif smtpserver : 234 234 email = "%s@%s" % (entry.Name, smtpserver) 235 else : 235 else : 236 236 email = "%s@%s" % (entry.Name, "localhost") 237 237 msg = "%s - <%s>" % (entry.Name, email) 238 238 if entry.Description : 239 239 msg += " - %s" % entry.Description 240 print msg 240 print msg 241 241 print " %s" % (_("Limited by : %s") % entry.LimitBy) 242 242 print " %s" % (_("Account balance : %.2f") % (entry.AccountBalance or 0.0)) … … 244 244 print " %s" % (_("Overcharging factor : %.2f") % entry.OverCharge) 245 245 print 246 else : 246 else : 247 247 for entry in entries : 248 248 msg = "%s" % entry.Name 249 249 if entry.Description : 250 250 msg += " - %s" % entry.Description 251 print msg 251 print msg 252 252 print " %s" % (_("Limited by : %s") % entry.LimitBy) 253 253 print " %s" % (_("Group balance : %.2f") % (entry.AccountBalance or 0.0)) 254 254 print " %s" % (_("Total paid so far : %.2f") % (entry.LifeTimePaid or 0.0)) 255 255 print 256 elif options["delete"] : 256 elif options["delete"] : 257 257 percent.display("\n%s..." % _("Deletion")) 258 258 getattr(self.storage, "deleteMany%ss" % suffix)(entries) … … 266 266 'noprint', 'nochange') : 267 267 raise PyKotaCommandLineError, _("Invalid limitby value %s") % options["limitby"] 268 if (limitby in ('nochange', 'noprint')) and options["groups"] : 268 if (limitby in ('nochange', 'noprint')) and options["groups"] : 269 269 raise PyKotaCommandLineError, _("Invalid limitby value %s") % options["limitby"] 270 270 271 271 overcharge = options["overcharge"] 272 272 if overcharge : 273 273 try : 274 274 overcharge = float(overcharge.strip()) 275 except (ValueError, AttributeError) : 275 except (ValueError, AttributeError) : 276 276 raise PyKotaCommandLineError, _("Invalid overcharge value %s") % options["overcharge"] 277 277 278 278 balance = options["balance"] 279 279 if balance : … … 281 281 try : 282 282 balancevalue = float(balance) 283 except ValueError : 283 except ValueError : 284 284 raise PyKotaCommandLineError, _("Invalid balance value %s") % options["balance"] 285 else : 285 else : 286 286 balancevalue = None 287 287 288 288 if options["ingroups"] : 289 289 usersgroups = self.storage.getMatchingGroups(options["ingroups"]) 290 290 if not usersgroups : 291 291 raise PyKotaCommandLineError, _("There's no users group matching %s") % " ".join(options["ingroups"].split(',')) 292 else : 292 else : 293 293 usersgroups = [] 294 294 295 295 description = options["description"] 296 296 if description : 297 297 description = description.strip() 298 298 299 299 comment = options["comment"] 300 300 if comment : 301 301 comment = comment.strip() 302 email = options["email"] 302 email = options["email"] 303 303 if email : 304 304 email = email.strip() 305 skipexisting = options["skipexisting"] 305 skipexisting = options["skipexisting"] 306 306 groups = options["groups"] 307 307 remove = options["remove"] 308 308 self.storage.beginTransaction() 309 try : 310 if options["add"] : 311 rejectunknown = self.config.getRejectUnknown() 309 try : 310 if options["add"] : 311 rejectunknown = self.config.getRejectUnknown() 312 312 percent.display("%s...\n" % _("Creation")) 313 313 percent.setSize(len(names)) … … 325 325 try : 326 326 grp.getgrnam(ename) 327 except KeyError : 327 except KeyError : 328 328 self.printInfo(_("Unknown group %s") % ename, "error") 329 329 reject = 1 330 else : 330 else : 331 331 try : 332 332 pwd.getpwnam(ename) 333 except KeyError : 333 except KeyError : 334 334 self.printInfo(_("Unknown user %s") % ename, "error") 335 335 reject = 1 336 if not reject : 336 if not reject : 337 337 entry = globals()["Storage%s" % suffix](self.storage, ename) 338 338 if groups : 339 339 self.modifyEntry(entry, groups, limitby, \ 340 340 description) 341 else : 341 else : 342 342 self.modifyEntry(entry, groups, limitby, \ 343 343 description, overcharge,\ … … 348 348 if skipexisting : 349 349 self.logdebug(_("%s %s already exists, skipping.") % (_(suffix), ename)) 350 else : 350 else : 351 351 self.logdebug(_("%s %s already exists, will be modified.") % (_(suffix), ename)) 352 352 if groups : … … 373 373 if groups : 374 374 self.modifyEntry(entry, groups, limitby, description) 375 else : 375 else : 376 376 self.modifyEntry(entry, groups, limitby, description, \ 377 377 overcharge, balance, balancevalue, \ 378 378 comment, email) 379 self.manageUsersGroups(usersgroups, entry, remove) 380 entry.save() 379 self.manageUsersGroups(usersgroups, entry, remove) 380 entry.save() 381 381 percent.oneMore() 382 except : 382 except : 383 383 self.storage.rollbackTransaction() 384 384 raise 385 else : 385 else : 386 386 self.storage.commitTransaction() 387 387 388 388 if not options["list"] : 389 389 percent.done() 390 391 if __name__ == "__main__" : 390 391 if __name__ == "__main__" : 392 392 retcode = 0 393 393 try : … … 401 401 "ingroups=", "limitby=", "balance=", "comment=", \ 402 402 ] 403 404 403 404 405 405 # Initializes the command line tool 406 406 manager = PKUsers(doc=__doc__) 407 407 manager.deferredInit() 408 408 409 409 # parse and checks the command line 410 410 (options, args) = manager.parseCommandline(sys.argv[1:], short_options, long_options) 411 411 412 412 # sets long options 413 413 options["help"] = options["h"] or options["help"] … … 415 415 options["add"] = options["a"] or options["add"] 416 416 options["description"] = options["D"] or options["description"] 417 options["delete"] = options["d"] or options["delete"] 417 options["delete"] = options["d"] or options["delete"] 418 418 options["groups"] = options["g"] or options["groups"] 419 419 options["list"] = options["L"] or options["list"] … … 421 421 options["skipexisting"] = options["s"] or options["skipexisting"] 422 422 options["limitby"] = options["l"] or options["limitby"] 423 options["balance"] = options["b"] or options["balance"] 423 options["balance"] = options["b"] or options["balance"] 424 424 options["ingroups"] = options["i"] or options["ingroups"] 425 425 options["overcharge"] = options["o"] or options["overcharge"] 426 426 options["comment"] = options["C"] or options["comment"] or defaults["comment"] 427 427 options["email"] = options["e"] or options["email"] 428 428 429 429 if options["help"] : 430 430 manager.display_usage_and_quit() … … 436 436 or (options["groups"] and (options["balance"] or options["ingroups"] or options["overcharge"])) : 437 437 raise PyKotaCommandLineError, _("incompatible options, see help.") 438 elif options["remove"] and not options["ingroups"] : 438 elif options["remove"] and not options["ingroups"] : 439 439 raise PyKotaCommandLineError, _("You have to pass user groups names on the command line") 440 440 elif (not args) and (options["add"] or options["delete"]) : … … 442 442 else : 443 443 retcode = manager.main(args, options) 444 except KeyboardInterrupt : 444 except KeyboardInterrupt : 445 445 logerr("\nInterrupted with Ctrl+C !\n") 446 446 retcode = -3 447 except PyKotaCommandLineError, msg : 447 except PyKotaCommandLineError, msg : 448 448 logerr("%s : %s\n" % (sys.argv[0], msg)) 449 449 retcode = -2 450 except SystemExit : 450 except SystemExit : 451 451 pass 452 452 except : 453 453 try : 454 454 manager.crashed("pkusers failed") 455 except : 455 except : 456 456 crashed("pkusers failed") 457 457 retcode = -1 … … 459 459 try : 460 460 manager.storage.close() 461 except (TypeError, NameError, AttributeError) : 461 except (TypeError, NameError, AttributeError) : 462 462 pass 463 464 sys.exit(retcode) 463 464 sys.exit(retcode)