diff --git a/autotools/build-bash-completion b/autotools/build-bash-completion index 6f39ef5d4ccbc9dcdeff17673babe1fd0a2a9df1..b0522b19e029b0d9e3a0eea3b30e0e899bb48dc6 100755 --- a/autotools/build-bash-completion +++ b/autotools/build-bash-completion @@ -19,7 +19,6 @@ # 02110-1301, USA. -import imp import optparse import os import sys @@ -504,19 +503,6 @@ def GetFunctionName(name): return "_" + re.sub(r"[^a-z0-9]+", "_", name.lower()) -def LoadModule(filename): - """Loads an external module by filename. - - """ - (name, ext) = os.path.splitext(filename) - - fh = open(filename, "U") - try: - return imp.load_module(name, fh, filename, (ext, "U", imp.PY_SOURCE)) - finally: - fh.close() - - def GetCommands(filename, module): """Returns the commands defined in a module. @@ -551,10 +537,11 @@ def main(): WriteCompletion(sw, scriptname, GetFunctionName(scriptname), - commands=GetCommands(filename, LoadModule(filename))) + commands=GetCommands(filename, + utils.LoadModule(filename))) # Burnin script - burnin = LoadModule("tools/burnin") + burnin = utils.LoadModule("tools/burnin") WriteCompletion(sw, "%s/burnin" % constants.TOOLSDIR, "_ganeti_burnin", opts=burnin.OPTIONS, args=burnin.ARGUMENTS) diff --git a/lib/utils.py b/lib/utils.py index 5a23d70b42a5323ec5934ec8cea7d804f0338720..1e78b7ff473bf2892f0b83643407ecd1a9eee8ca 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -42,6 +42,7 @@ import fcntl import resource import logging import signal +import imp from cStringIO import StringIO @@ -1992,6 +1993,25 @@ def ReadWatcherPauseFile(filename, now=None, remove_after=3600): return value +def LoadModule(filename): + """Loads an external module by filename. + + Use this function with caution. Python will always write the compiled source + to a file named "${filename}c". + + @type filename: string + @param filename: Path to module + + """ + (name, ext) = os.path.splitext(filename) + + fh = open(filename, "U") + try: + return imp.load_module(name, fh, filename, (ext, "U", imp.PY_SOURCE)) + finally: + fh.close() + + class FileLock(object): """Utility class for file locks.