Index: trunk/tools/subversion/user-management/manage-exports |
— | — | @@ -17,6 +17,8 @@ |
18 | 18 | ldapSupportLib = ldapsupportlib.LDAPSupportLib() |
19 | 19 | ldapSupportLib.addParserOptions(parser) |
20 | 20 | |
| 21 | + parser.add_option("--logfile", dest="logfile", help="Write output to the specified log file. (default: stdin)") |
| 22 | + parser.add_option("--loglevel", dest="loglevel", help="Change level of logging; NONE, INFO, DEBUG (default: INFO)") |
21 | 23 | (options, args) = parser.parse_args() |
22 | 24 | ldapSupportLib.setBindInfoByOptions(options, parser) |
23 | 25 | |
— | — | @@ -35,6 +37,10 @@ |
36 | 38 | project_name = project[1]["cn"][0] |
37 | 39 | if not os.path.exists(basedir + project_name): |
38 | 40 | os.mkdir(basedir + project_name, 0755) |
| 41 | + if options.logfile: |
| 42 | + hdm.logfile = options.logfile |
| 43 | + if options.loglevel: |
| 44 | + hdm.setDebugLevel(options.loglevel) |
39 | 45 | hdm.basedir = basedir + project_name + "/" |
40 | 46 | hdm.group = project_name |
41 | 47 | hdm.run() |
Index: trunk/tools/subversion/user-management/homedirectorymanager.py |
— | — | @@ -9,6 +9,10 @@ |
10 | 10 | except ImportError: |
11 | 11 | sys.stderr.write("Unable to import LDAP library.\n") |
12 | 12 | |
| 13 | +NONE = 0 |
| 14 | +INFO = 10 |
| 15 | +DEBUG = 20 |
| 16 | + |
13 | 17 | class HomeDirectoryManager: |
14 | 18 | |
15 | 19 | def __init__(self): |
— | — | @@ -37,8 +41,13 @@ |
38 | 42 | self.skelFiles = {} |
39 | 43 | self.skelFiles['/etc/skel/'] = ['.bashrc', '.profile', '.bash_logout'] |
40 | 44 | |
| 45 | + # Log file to use, rather than stdout |
| 46 | + self.logfile = None |
| 47 | + |
| 48 | + self.loglevel = INFO |
| 49 | + self.logfile = None |
| 50 | + |
41 | 51 | self.dryRun = False |
42 | | - self.debugStatus = False |
43 | 52 | |
44 | 53 | if (os.path.exists('/usr/sbin/nscd')): |
45 | 54 | os.system('nscd -i passwd') |
— | — | @@ -52,18 +61,24 @@ |
53 | 62 | ldapSupportLib.addParserOptions(parser) |
54 | 63 | |
55 | 64 | parser.add_option("--dry-run", action="store_true", dest="dryRun", help="Show what would be done, but don't actually do anything") |
56 | | - parser.add_option("--debug", action="store_true", dest="debugStatus", help="Run in debug mode (you likely want to use --dry-run with this)") |
57 | 65 | parser.add_option("--basedir", dest="basedir", help="Base directory to manage home directories (default: /home)") |
58 | 66 | parser.add_option("--group", dest="group", help="Only manage home directories for users in the provided group (default: manage all users)") |
| 67 | + parser.add_option("--loglevel", dest="loglevel", help="Change level of logging; NONE, INFO, DEBUG (default: INFO)") |
| 68 | + parser.add_option("--logfile", dest="logfile", help="Log file to write to (default: stdout)") |
59 | 69 | (self.options, args) = parser.parse_args() |
60 | 70 | |
61 | 71 | self.dryRun = self.options.dryRun |
62 | | - self.debugStatus = self.options.debugStatus |
63 | 72 | if self.options.basedir: |
64 | 73 | self.basedir = self.options.basedir |
65 | 74 | if self.options.group: |
66 | 75 | self.group = self.options.group |
67 | 76 | |
| 77 | + if options.logfile: |
| 78 | + self.logfile = options.logfile |
| 79 | + |
| 80 | + if options.loglevel: |
| 81 | + self.setLogLevel(options.loglevel) |
| 82 | + |
68 | 83 | # use proxy agent by default |
69 | 84 | ldapSupportLib.setBindInfoByOptions(self.options, parser) |
70 | 85 | |
— | — | @@ -296,28 +311,51 @@ |
297 | 312 | os.utime(self.basedir + userdir + "/.ssh/authorized_keys", (atime, time.mktime(d_ldap_mtime.timetuple()))) |
298 | 313 | |
299 | 314 | def log(self, logstring): |
300 | | - print datetime.datetime.now().strftime("%m/%d/%Y - %H:%M:%S - ") + logstring |
| 315 | + if self.loglevel >= INFO: |
| 316 | + log = datetime.datetime.now().strftime("%m/%d/%Y - %H:%M:%S - ") + logstring + "\n" |
| 317 | + if self.logfile: |
| 318 | + lf = open(self.logfile, 'a') |
| 319 | + lf.write(log) |
| 320 | + lf.close() |
| 321 | + else: |
| 322 | + print log |
301 | 323 | |
302 | 324 | def logDebug(self, logstring): |
303 | | - if self.debugStatus == True: |
304 | | - sys.stderr.write("Debug: " + logstring + "\n") |
| 325 | + if self.loglevel >= DEBUG: |
| 326 | + log = datetime.datetime.now().strftime("%m/%d/%Y - %H:%M:%S - ") + "(Debug) " + logstring + "\n" |
| 327 | + if self.logfile: |
| 328 | + lf = open(self.logfile, 'a') |
| 329 | + lf.write(log) |
| 330 | + lf.close() |
| 331 | + else: |
| 332 | + sys.stderr.write(log) |
305 | 333 | |
| 334 | + def setDebugLevel(self, loglevel): |
| 335 | + if loglevel.lower() == "debug": |
| 336 | + self.loglevel = DEBUG |
| 337 | + else if loglevel.lower() == "info": |
| 338 | + self.loglevel = INFO |
| 339 | + else if loglevel.lower() == "none": |
| 340 | + self.loglevel = NONE |
| 341 | + else: |
| 342 | + self.loglevel = INFO |
| 343 | + |
306 | 344 | def chown(self, path, user, group): |
307 | 345 | if not self.dryRun: |
308 | 346 | os.chown(path, user, group) |
309 | | - if self.dryRun or self.debugStatus: |
| 347 | + if self.dryRun or self.loglevel >= DEBUG: |
310 | 348 | self.log('chown %s %d %d' % (path, user, group)) |
311 | 349 | |
312 | 350 | def mkdir(self, path, mode): |
313 | 351 | if not self.dryRun: |
314 | 352 | os.mkdir(path, mode) |
315 | | - if self.dryRun or self.debugStatus: |
| 353 | + if self.dryRun or self.loglevel >= DEBUG: |
316 | 354 | self.log('mkdir %s %o' % (path, mode)) |
317 | 355 | |
318 | 356 | def chmod(self, path, mode): |
319 | 357 | if not self.dryRun: |
320 | 358 | os.chmod(path, mode) |
321 | | - if self.dryRun or self.debugStatus: |
| 359 | + if self.dryRun or self.loglevel >= DEBUG: |
322 | 360 | self.log('chmod %s %o' % (path, mode)) |
323 | 361 | |
324 | 362 | def writeFile(self, path, contents): |
— | — | @@ -325,19 +363,19 @@ |
326 | 364 | f = open(path, 'w') |
327 | 365 | f.write(contents) |
328 | 366 | f.close() |
329 | | - if self.dryRun or self.debugStatus: |
| 367 | + if self.dryRun or self.loglevel >= DEBUG: |
330 | 368 | self.log("write file %s:\n%s" % (path, contents)) |
331 | 369 | |
332 | 370 | def rename(self, oldPath, newPath): |
333 | 371 | if not self.dryRun: |
334 | 372 | os.rename(oldPath, newPath) |
335 | | - if self.dryRun or self.debugStatus: |
| 373 | + if self.dryRun or self.loglevel >= DEBUG: |
336 | 374 | self.log('rename %s %s' % (oldPath, newPath)) |
337 | 375 | |
338 | 376 | def copy(self, srcPath, dstPath): |
339 | 377 | if not self.dryRun: |
340 | 378 | shutil.copy(srcPath, dstPath) |
341 | | - if self.dryRun or self.debugStatus: |
| 379 | + if self.dryRun or self.loglevel >= DEBUG: |
342 | 380 | self.log('copy %s %s' % (srcPath, dstPath)) |
343 | 381 | |
344 | 382 | def main(): |