Commit d6f5892b authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Move LoadModule function to utils



It can be used by unittests for daemons/* or scripts/*.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarLuca Bigliardi <shammash@google.com>
parent 2a887df9
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
# 02110-1301, USA. # 02110-1301, USA.
import imp
import optparse import optparse
import os import os
import sys import sys
...@@ -504,19 +503,6 @@ def GetFunctionName(name): ...@@ -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): def GetCommands(filename, module):
"""Returns the commands defined in a module. """Returns the commands defined in a module.
...@@ -551,10 +537,11 @@ def main(): ...@@ -551,10 +537,11 @@ def main():
WriteCompletion(sw, scriptname, WriteCompletion(sw, scriptname,
GetFunctionName(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", WriteCompletion(sw, "%s/burnin" % constants.TOOLSDIR, "_ganeti_burnin",
opts=burnin.OPTIONS, args=burnin.ARGUMENTS) opts=burnin.OPTIONS, args=burnin.ARGUMENTS)
......
...@@ -42,6 +42,7 @@ import fcntl ...@@ -42,6 +42,7 @@ import fcntl
import resource import resource
import logging import logging
import signal import signal
import imp
from cStringIO import StringIO from cStringIO import StringIO
...@@ -1992,6 +1993,25 @@ def ReadWatcherPauseFile(filename, now=None, remove_after=3600): ...@@ -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): class FileLock(object):
"""Utility class for file locks. """Utility class for file locks.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment