blob: dcad6a828caefdf2d60897f0114ffe9402755e20 [file] [log] [blame]
mblighd90942c2008-04-04 15:08:12 +00001__author__ = "jadmanski@google.com (John Admanski)"
2
jadmanski17066f82008-10-27 20:23:14 +00003import os, sys, new, glob
mblighd90942c2008-04-04 15:08:12 +00004
5
6def _create_module(name):
jadmanski0afbb632008-06-06 21:10:57 +00007 """Create a single top-level module"""
8 module = new.module(name)
9 sys.modules[name] = module
10 return module
mblighd90942c2008-04-04 15:08:12 +000011
12
13def _create_module_and_parents(name):
jadmanski0afbb632008-06-06 21:10:57 +000014 """Create a module, and all the necessary parents"""
15 parts = name.split(".")
16 # first create the top-level module
17 parent = _create_module(parts[0])
18 created_parts = [parts[0]]
19 parts.pop(0)
20 # now, create any remaining child modules
21 while parts:
22 child_name = parts.pop(0)
23 module = new.module(child_name)
24 setattr(parent, child_name, module)
25 created_parts.append(child_name)
26 sys.modules[".".join(created_parts)] = module
27 parent = module
mblighd90942c2008-04-04 15:08:12 +000028
29
30def _import_children_into_module(parent_module_name, path):
jadmanski0afbb632008-06-06 21:10:57 +000031 """Import all the packages on a path into a parent module"""
32 # find all the packages at 'path'
33 names = []
34 for filename in os.listdir(path):
35 full_name = os.path.join(path, filename)
36 if not os.path.isdir(full_name):
37 continue # skip files
jadmanski8a1ce952008-07-08 19:12:35 +000038 if "." in filename:
39 continue # if "." is in the name it's not a valid package name
40 if not os.access(full_name, os.R_OK | os.X_OK):
41 continue # need read + exec access to make a dir importable
jadmanski0afbb632008-06-06 21:10:57 +000042 if "__init__.py" in os.listdir(full_name):
43 names.append(filename)
44 # import all the packages and insert them into 'parent_module'
45 sys.path.insert(0, path)
46 for name in names:
47 module = __import__(name)
48 # add the package to the parent
49 parent_module = sys.modules[parent_module_name]
50 setattr(parent_module, name, module)
51 full_name = parent_module_name + "." + name
52 sys.modules[full_name] = module
53 # restore the system path
54 sys.path.pop(0)
mblighd90942c2008-04-04 15:08:12 +000055
56
mbligh4205d892008-07-14 16:23:20 +000057def import_module(module, from_where):
58 """Equivalent to 'from from_where import module'
59 Returns the corresponding module"""
60 from_module = __import__(from_where, globals(), locals(), [module])
61 return getattr(from_module, module)
62
63
mblighd90942c2008-04-04 15:08:12 +000064def setup(base_path, root_module_name=""):
jadmanski0afbb632008-06-06 21:10:57 +000065 """
66 Perform all the necessary setup so that all the packages at
67 'base_path' can be imported via "import root_module_name.package".
68 If root_module_name is empty, then all the packages at base_path
69 are inserted as top-level packages.
mblighd90942c2008-04-04 15:08:12 +000070
jadmanski0afbb632008-06-06 21:10:57 +000071 Also, setup all the common.* aliases for modules in the common
72 library.
mblighc61d26d2008-07-17 00:22:52 +000073
74 The setup must be different if you are running on an Autotest server
75 or on a test manchine that just has the client directories installed.
jadmanski0afbb632008-06-06 21:10:57 +000076 """
jadmanski17066f82008-10-27 20:23:14 +000077 # Hack out logging.py*
78 logging_py = os.path.join(os.path.dirname(__file__), "common_lib",
79 "logging.py*")
80 if glob.glob(logging_py):
81 os.system("rm -f %s" % logging_py)
82
mblighc61d26d2008-07-17 00:22:52 +000083 # Hack... Any better ideas?
84 if (root_module_name == 'autotest_lib.client' and
85 os.path.exists(os.path.join(os.path.dirname(__file__),
86 '..', 'server'))):
87 root_module_name = 'autotest_lib'
88 base_path = os.path.abspath(os.path.join(base_path, '..'))
89
jadmanski0afbb632008-06-06 21:10:57 +000090 _create_module_and_parents(root_module_name)
91 _import_children_into_module(root_module_name, base_path)
mblighd90942c2008-04-04 15:08:12 +000092
93
mbligh23e10112008-05-14 20:52:51 +000094# This must run on Python versions less than 2.4.
95dirname = os.path.dirname(sys.modules[__name__].__file__)
96common_dir = os.path.abspath(os.path.join(dirname, "common_lib"))
97sys.path.insert(0, common_dir)
98import check_version
99sys.path.pop(0)
100check_version.check_python_version()