From 01449bfb379bb13bef3bb3f033b67f8719d94ce1 Mon Sep 17 00:00:00 2001
From: Vo Minh Thu <vmt@openerp.com>
Date: Thu, 9 Feb 2012 16:27:32 +0100
Subject: [PATCH] [IMP] post_load: factored common code. This also fix bug
 lp:929466

lp bug: https://launchpad.net/bugs/929466 fixed

bzr revid: vmt@openerp.com-20120209152732-24ud1f70krliv2k5
---
 openerp-server              |  5 +---
 openerp/modules/__init__.py |  2 +-
 openerp/modules/loading.py  |  5 ++--
 openerp/modules/module.py   | 52 +++++++++++++++----------------------
 openerp/wsgi.py             |  5 +---
 5 files changed, 26 insertions(+), 43 deletions(-)

diff --git a/openerp-server b/openerp-server
index 97be3a858816..4ff97ddc0094 100755
--- a/openerp-server
+++ b/openerp-server
@@ -241,10 +241,7 @@ if __name__ == "__main__":
 
     for m in openerp.conf.server_wide_modules:
         try:
-            if m not in openerp.modules.module.loaded:
-                __import__('openerp.addons.' + m)
-                openerp.modules.module.loaded.append(m)
-                openerp.modules.module.call_post_load_hook(m)
+            openerp.modules.module.load_openerp_module(m)
         except Exception:
             msg = ''
             if m == 'web':
diff --git a/openerp/modules/__init__.py b/openerp/modules/__init__.py
index 171d84182690..f28c397bb230 100644
--- a/openerp/modules/__init__.py
+++ b/openerp/modules/__init__.py
@@ -36,7 +36,7 @@ from openerp.modules.module import \
     load_information_from_description_file, \
     get_module_resource, zip_directory, \
     get_module_path, initialize_sys_path, \
-    register_module_classes, init_module_models
+    load_openerp_module, init_module_models
 
 from openerp.modules.loading import load_modules
 
diff --git a/openerp/modules/loading.py b/openerp/modules/loading.py
index b41b7595832d..b3f831f97098 100644
--- a/openerp/modules/loading.py
+++ b/openerp/modules/loading.py
@@ -56,10 +56,9 @@ from openerp.tools.translate import _
 from openerp.modules.module import \
     get_modules, get_modules_with_version, \
     load_information_from_description_file, \
-    call_post_load_hook, \
     get_module_resource, zip_directory, \
     get_module_path, initialize_sys_path, \
-    register_module_classes, init_module_models
+    load_openerp_module, init_module_models
 
 _logger = logging.getLogger(__name__)
 
@@ -163,7 +162,7 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=
 
         _logger.info('module %s: loading objects', package.name)
         migrations.migrate_module(package, 'pre')
-        register_module_classes(package.name)
+        load_openerp_module(package.name)
 
         models = pool.load(cr, package)
         loaded_modules.append(package.name)
diff --git a/openerp/modules/module.py b/openerp/modules/module.py
index 8d93ea1d6584..6238aadaed02 100644
--- a/openerp/modules/module.py
+++ b/openerp/modules/module.py
@@ -389,53 +389,43 @@ def init_module_models(cr, module_name, obj_list):
         t[1](cr, *t[2])
     cr.commit()
 
-def register_module_classes(m):
-    """ Register module named m, if not already registered.
+def load_openerp_module(module_name):
+    """ Load an OpenERP module, if not already loaded.
 
     This loads the module and register all of its models, thanks to either
     the MetaModel metaclass, or the explicit instantiation of the model.
-
+    This is also used to load server-wide module (i.e. it is also used
+    when there is no model to register).
     """
-
-    def log(e):
-        mt = isinstance(e, zipimport.ZipImportError) and 'zip ' or ''
-        msg = "Couldn't load %smodule %s" % (mt, m)
-        _logger.critical(msg)
-        _logger.critical(e)
-
     global loaded
-    if m in loaded:
+    if module_name in loaded:
         return
 
     initialize_sys_path()
     try:
-        mod_path = get_module_path(m)
+        mod_path = get_module_path(module_name)
         zip_mod_path = mod_path + '.zip'
         if not os.path.isfile(zip_mod_path):
-            __import__('openerp.addons.' + m)
+            __import__('openerp.addons.' + module_name)
         else:
             zimp = zipimport.zipimporter(zip_mod_path)
-            zimp.load_module(m)
+            zimp.load_module(module_name)
+
+        # Call the module's post-load hook. This can done before any model or
+        # data has been initialized. This is ok as the post-load hook is for
+        # server-wide (instead of registry-specific) functionalities.
+        info = load_information_from_description_file(module_name)
+        if info['post_load']:
+            getattr(sys.modules['openerp.addons.' + module_name], info['post_load'])()
+
     except Exception, e:
-        log(e)
+        mt = isinstance(e, zipimport.ZipImportError) and 'zip ' or ''
+        msg = "Couldn't load %smodule %s" % (mt, module_name)
+        _logger.critical(msg)
+        _logger.critical(e)
         raise
     else:
-        loaded.append(m)
-
-    call_post_load_hook(m)
-
-# TODO replace the __import__() by register_module_classes() above
-# and combine call_post_load_hook with it for 6.2.
-def call_post_load_hook(module_name):
-    """
-    Call the module's post-load hook. This can done before any model or
-    data has been initialized. This is ok as the post-load hook is for
-    server-wide (instead of registry-specific) functionalities.
-    The module must have been loaded with as openerp.addons.<module_name>.
-    """
-    info = load_information_from_description_file(module_name)
-    if info['post_load']:
-        getattr(sys.modules['openerp.addons.' + module_name], info['post_load'])()
+        loaded.append(module_name)
 
 def get_modules():
     """Returns the list of module names
diff --git a/openerp/wsgi.py b/openerp/wsgi.py
index 17009ddedd43..32fb59f8d91f 100644
--- a/openerp/wsgi.py
+++ b/openerp/wsgi.py
@@ -464,10 +464,7 @@ def on_starting(server):
     openerp.modules.loading.open_openerp_namespace()
     for m in openerp.conf.server_wide_modules:
         try:
-            if m not in openerp.modules.module.loaded:
-                __import__('openerp.addons.' + m)
-                openerp.modules.module.call_post_load_hook(m)
-                openerp.modules.module.loaded.append(m)
+            openerp.modules.module.load_openerp_module(m)
         except Exception:
             msg = ''
             if m == 'web':
-- 
GitLab