mbligh | d90942c | 2008-04-04 15:08:12 +0000 | [diff] [blame] | 1 | __author__ = "jadmanski@google.com (John Admanski)" |
| 2 | |
jadmanski | 17066f8 | 2008-10-27 20:23:14 +0000 | [diff] [blame] | 3 | import os, sys, new, glob |
mbligh | d90942c | 2008-04-04 15:08:12 +0000 | [diff] [blame] | 4 | |
| 5 | |
| 6 | def _create_module(name): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 7 | """Create a single top-level module""" |
| 8 | module = new.module(name) |
| 9 | sys.modules[name] = module |
| 10 | return module |
mbligh | d90942c | 2008-04-04 15:08:12 +0000 | [diff] [blame] | 11 | |
| 12 | |
| 13 | def _create_module_and_parents(name): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 14 | """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 |
mbligh | d90942c | 2008-04-04 15:08:12 +0000 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def _import_children_into_module(parent_module_name, path): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 31 | """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 |
jadmanski | 8a1ce95 | 2008-07-08 19:12:35 +0000 | [diff] [blame] | 38 | 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 |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 42 | 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) |
mbligh | d90942c | 2008-04-04 15:08:12 +0000 | [diff] [blame] | 55 | |
| 56 | |
mbligh | 4205d89 | 2008-07-14 16:23:20 +0000 | [diff] [blame] | 57 | def 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 | |
mbligh | d90942c | 2008-04-04 15:08:12 +0000 | [diff] [blame] | 64 | def setup(base_path, root_module_name=""): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 65 | """ |
| 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. |
mbligh | d90942c | 2008-04-04 15:08:12 +0000 | [diff] [blame] | 70 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 71 | Also, setup all the common.* aliases for modules in the common |
| 72 | library. |
mbligh | c61d26d | 2008-07-17 00:22:52 +0000 | [diff] [blame] | 73 | |
| 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. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 76 | """ |
jadmanski | 17066f8 | 2008-10-27 20:23:14 +0000 | [diff] [blame] | 77 | # 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 | |
mbligh | c61d26d | 2008-07-17 00:22:52 +0000 | [diff] [blame] | 83 | # 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 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 90 | _create_module_and_parents(root_module_name) |
| 91 | _import_children_into_module(root_module_name, base_path) |
mbligh | d90942c | 2008-04-04 15:08:12 +0000 | [diff] [blame] | 92 | |
| 93 | |
mbligh | 23e1011 | 2008-05-14 20:52:51 +0000 | [diff] [blame] | 94 | # This must run on Python versions less than 2.4. |
| 95 | dirname = os.path.dirname(sys.modules[__name__].__file__) |
| 96 | common_dir = os.path.abspath(os.path.join(dirname, "common_lib")) |
| 97 | sys.path.insert(0, common_dir) |
| 98 | import check_version |
| 99 | sys.path.pop(0) |
| 100 | check_version.check_python_version() |