Created a client/common_lib/utils/import_site_class function to take
care of site specific importing of classes. Modified code to use it.
Signed-off-by: Mihai Rusu <dizzy@google.com>
git-svn-id: http://test.kernel.org/svn/autotest/trunk@2626 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/client/common_lib/utils.py b/client/common_lib/utils.py
index cde942f..235dc41 100644
--- a/client/common_lib/utils.py
+++ b/client/common_lib/utils.py
@@ -798,3 +798,41 @@
test_index = 0
(args, dargs) = self.test_list.pop(test_index)
fn(*args, **dargs)
+
+
+def import_site_class(path, module, classname, baseclass, modulefile=None):
+ """
+ Try to import site specific class from site specific file if it exists
+
+ Args:
+ path: full filename of the source file calling this (ie __file__)
+ module: full module name
+ classname: class name to be loaded from site file
+ baseclass: base class object to return when no site file present
+ modulefile: module filename
+
+ Returns: class object of the site class or baseclass
+
+ Raises: ImportError if the site file exists but imports fails
+ """
+
+ # get the short module name (without any dot prefix)
+ short_module = module[module.rfind(".") + 1:]
+
+ if not modulefile:
+ modulefile = short_module + ".py"
+
+ try:
+ site_exists = os.path.getsize(os.path.join(os.path.dirname(path),
+ modulefile))
+ except os.error:
+ site_exists = False
+
+ if site_exists:
+ # return the class object from the imported module
+ classobj = getattr(__import__(module, {}, {}, [short_module]),
+ classname)
+ else:
+ classobj = baseclass
+
+ return classobj