From 64ec5f36dfaaf100a7fe802065fce3f288746846 Mon Sep 17 00:00:00 2001 From: Vo Minh Thu Date: Mon, 9 Jan 2012 09:12:06 +0100 Subject: [PATCH] [IMP] modules: began to replace sys.path appending with an import hook. bzr revid: vmt@openerp.com-20120109081206-p10yack8lhip11gt --- openerp-server | 53 +++++++++++++++++++++++++++++++++++++++ openerp/modules/module.py | 23 +---------------- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/openerp-server b/openerp-server index 71a52d5f06b..9a4a04f2970 100755 --- a/openerp-server +++ b/openerp-server @@ -42,6 +42,9 @@ import openerp __author__ = openerp.release.author __version__ = openerp.release.version +import sys +import imp + def check_root_user(): """ Exit if the process's user is 'root' (on POSIX system).""" if os.name == 'posix': @@ -213,6 +216,56 @@ if __name__ == "__main__": check_root_user() openerp.tools.config.parse_config(sys.argv[1:]) + + + class ImportHook(object): + + def find_module(self, module_name, package_path): + module_parts = module_name.split('.') + if len(module_parts) == 3 and module_name.startswith('openerp.addons.'): + return self # We act as a loader too. + + # TODO list of loadable modules can be cached instead of always + # calling get_module_path(). + if len(module_parts) == 1 and \ + openerp.modules.module.get_module_path(module_parts[0]): + return self # We act as a loader too. + + def load_module(self, module_name): + + module_parts = module_name.split('.') + if len(module_parts) == 3 and module_name.startswith('openerp.addons.'): + module_part = module_parts[2] + if module_name in sys.modules: + return sys.modules[module_name] + + if len(module_parts) == 1: + module_part = module_parts[0] + if module_part in sys.modules: + return sys.modules[module_part] + + try: + # Check if the bare module name clashes with another module. + f, path, descr = imp.find_module(module_part) + print "Warning: ambiguous import:", module_name, f, path, descr + except ImportError, e: + # Using `import openerp.addons.` instead of + # `import ` is ugly but not harmful. + pass + + f, path, descr = imp.find_module(module_part, openerp.modules.module.ad_paths) + mod = imp.load_module(module_name, f, path, descr) + sys.modules[module_part] = mod + sys.modules['openerp.addons.' + module_part] = mod + return mod + + openerp.modules.module.initialize_sys_path() + sys.meta_path.append(ImportHook()) + + import openerp.addons.hr + print sys.modules.get('openerp.addons.hr') + print sys.modules.get('hr') + check_postgres_user() openerp.netsvc.init_logger() report_configuration() diff --git a/openerp/modules/module.py b/openerp/modules/module.py index 7f934275894..2282067c3b6 100644 --- a/openerp/modules/module.py +++ b/openerp/modules/module.py @@ -63,21 +63,11 @@ def initialize_sys_path(): not in the PYTHONPATH. """ global ad_paths - if ad_paths: return ad_paths = map(lambda m: os.path.abspath(tools.ustr(m.strip())), tools.config['addons_path'].split(',')) - - sys.path.insert(1, _ad) - - ad_cnt=1 - for adp in ad_paths: - if adp != _ad: - sys.path.insert(ad_cnt, adp) - ad_cnt+=1 - - ad_paths.append(_ad) # for get_module_path + ad_paths.append(_ad) # for get_module_path def get_module_path(module, downloaded=False): @@ -298,17 +288,6 @@ def init_module_models(cr, module_name, obj_list): t[1](cr, *t[2]) cr.commit() -# Import hook to write a addon m in both sys.modules['m'] and -# sys.modules['openerp.addons.m']. Otherwise it could be loaded twice -# if imported twice using different names. -#class MyImportHook(object): -# def find_module(self, module_name, package_path): -# print ">>>", module_name, package_path -# def load_module(self, module_name): -# raise ImportError("Restricted") - -#sys.meta_path.append(MyImportHook()) - def register_module_classes(m): """ Register module named m, if not already registered.