r101446 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101445‎ | r101446 | r101447 >
Date:00:17, 1 November 2011
Author:laner
Status:deferred
Tags:
Comment:
Adding support for logging levels, and log files
Modified paths:
  • /trunk/tools/subversion/user-management/homedirectorymanager.py (modified) (history)
  • /trunk/tools/subversion/user-management/manage-exports (modified) (history)

Diff [purge]

Index: trunk/tools/subversion/user-management/manage-exports
@@ -17,6 +17,8 @@
1818 ldapSupportLib = ldapsupportlib.LDAPSupportLib()
1919 ldapSupportLib.addParserOptions(parser)
2020
 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)")
2123 (options, args) = parser.parse_args()
2224 ldapSupportLib.setBindInfoByOptions(options, parser)
2325
@@ -35,6 +37,10 @@
3638 project_name = project[1]["cn"][0]
3739 if not os.path.exists(basedir + project_name):
3840 os.mkdir(basedir + project_name, 0755)
 41+ if options.logfile:
 42+ hdm.logfile = options.logfile
 43+ if options.loglevel:
 44+ hdm.setDebugLevel(options.loglevel)
3945 hdm.basedir = basedir + project_name + "/"
4046 hdm.group = project_name
4147 hdm.run()
Index: trunk/tools/subversion/user-management/homedirectorymanager.py
@@ -9,6 +9,10 @@
1010 except ImportError:
1111 sys.stderr.write("Unable to import LDAP library.\n")
1212
 13+NONE = 0
 14+INFO = 10
 15+DEBUG = 20
 16+
1317 class HomeDirectoryManager:
1418
1519 def __init__(self):
@@ -37,8 +41,13 @@
3842 self.skelFiles = {}
3943 self.skelFiles['/etc/skel/'] = ['.bashrc', '.profile', '.bash_logout']
4044
 45+ # Log file to use, rather than stdout
 46+ self.logfile = None
 47+
 48+ self.loglevel = INFO
 49+ self.logfile = None
 50+
4151 self.dryRun = False
42 - self.debugStatus = False
4352
4453 if (os.path.exists('/usr/sbin/nscd')):
4554 os.system('nscd -i passwd')
@@ -52,18 +61,24 @@
5362 ldapSupportLib.addParserOptions(parser)
5463
5564 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)")
5765 parser.add_option("--basedir", dest="basedir", help="Base directory to manage home directories (default: /home)")
5866 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)")
5969 (self.options, args) = parser.parse_args()
6070
6171 self.dryRun = self.options.dryRun
62 - self.debugStatus = self.options.debugStatus
6372 if self.options.basedir:
6473 self.basedir = self.options.basedir
6574 if self.options.group:
6675 self.group = self.options.group
6776
 77+ if options.logfile:
 78+ self.logfile = options.logfile
 79+
 80+ if options.loglevel:
 81+ self.setLogLevel(options.loglevel)
 82+
6883 # use proxy agent by default
6984 ldapSupportLib.setBindInfoByOptions(self.options, parser)
7085
@@ -296,28 +311,51 @@
297312 os.utime(self.basedir + userdir + "/.ssh/authorized_keys", (atime, time.mktime(d_ldap_mtime.timetuple())))
298313
299314 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
301323
302324 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)
305333
 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+
306344 def chown(self, path, user, group):
307345 if not self.dryRun:
308346 os.chown(path, user, group)
309 - if self.dryRun or self.debugStatus:
 347+ if self.dryRun or self.loglevel >= DEBUG:
310348 self.log('chown %s %d %d' % (path, user, group))
311349
312350 def mkdir(self, path, mode):
313351 if not self.dryRun:
314352 os.mkdir(path, mode)
315 - if self.dryRun or self.debugStatus:
 353+ if self.dryRun or self.loglevel >= DEBUG:
316354 self.log('mkdir %s %o' % (path, mode))
317355
318356 def chmod(self, path, mode):
319357 if not self.dryRun:
320358 os.chmod(path, mode)
321 - if self.dryRun or self.debugStatus:
 359+ if self.dryRun or self.loglevel >= DEBUG:
322360 self.log('chmod %s %o' % (path, mode))
323361
324362 def writeFile(self, path, contents):
@@ -325,19 +363,19 @@
326364 f = open(path, 'w')
327365 f.write(contents)
328366 f.close()
329 - if self.dryRun or self.debugStatus:
 367+ if self.dryRun or self.loglevel >= DEBUG:
330368 self.log("write file %s:\n%s" % (path, contents))
331369
332370 def rename(self, oldPath, newPath):
333371 if not self.dryRun:
334372 os.rename(oldPath, newPath)
335 - if self.dryRun or self.debugStatus:
 373+ if self.dryRun or self.loglevel >= DEBUG:
336374 self.log('rename %s %s' % (oldPath, newPath))
337375
338376 def copy(self, srcPath, dstPath):
339377 if not self.dryRun:
340378 shutil.copy(srcPath, dstPath)
341 - if self.dryRun or self.debugStatus:
 379+ if self.dryRun or self.loglevel >= DEBUG:
342380 self.log('copy %s %s' % (srcPath, dstPath))
343381
344382 def main():

Follow-up revisions

RevisionCommit summaryAuthorDate
r101447Follow up to r101446.laner00:26, 1 November 2011

Status & tagging log