Move LoadModule function to utils
authorMichael Hanselmann <hansmi@google.com>
Thu, 3 Sep 2009 10:45:04 +0000 (12:45 +0200)
committerMichael Hanselmann <hansmi@google.com>
Thu, 3 Sep 2009 15:44:49 +0000 (17:44 +0200)
It can be used by unittests for daemons/* or scripts/*.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Luca Bigliardi <shammash@google.com>

autotools/build-bash-completion
lib/utils.py

index 6f39ef5..b0522b1 100755 (executable)
@@ -19,7 +19,6 @@
 # 02110-1301, USA.
 
 
 # 02110-1301, USA.
 
 
-import imp
 import optparse
 import os
 import sys
 import optparse
 import os
 import sys
@@ -504,19 +503,6 @@ def GetFunctionName(name):
   return "_" + re.sub(r"[^a-z0-9]+", "_", name.lower())
 
 
   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.
 
 def GetCommands(filename, module):
   """Returns the commands defined in a module.
 
@@ -551,10 +537,11 @@ def main():
 
     WriteCompletion(sw, scriptname,
                     GetFunctionName(scriptname),
 
     WriteCompletion(sw, scriptname,
                     GetFunctionName(scriptname),
-                    commands=GetCommands(filename, LoadModule(filename)))
+                    commands=GetCommands(filename,
+                                         utils.LoadModule(filename)))
 
   # Burnin script
 
   # 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)
 
   WriteCompletion(sw, "%s/burnin" % constants.TOOLSDIR, "_ganeti_burnin",
                   opts=burnin.OPTIONS, args=burnin.ARGUMENTS)
 
index 5a23d70..1e78b7f 100644 (file)
@@ -42,6 +42,7 @@ import fcntl
 import resource
 import logging
 import signal
 import resource
 import logging
 import signal
+import imp
 
 from cStringIO import StringIO
 
 
 from cStringIO import StringIO
 
@@ -1992,6 +1993,25 @@ def ReadWatcherPauseFile(filename, now=None, remove_after=3600):
   return value
 
 
   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.
 
 class FileLock(object):
   """Utility class for file locks.