[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
This commit is contained in:
Vo Minh Thu 2012-02-09 16:27:32 +01:00
parent d14a3e9df0
commit 01449bfb37
5 changed files with 26 additions and 43 deletions

View File

@ -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':

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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':