[IMP] website theme: update themes to fix CSS glitches, add Yeti theme, fix Default...
[odoo/odoo.git] / openerp / modules / loading.py
index 066b78f..52ca356 100644 (file)
@@ -3,7 +3,7 @@
 #
 #    OpenERP, Open Source Management Solution
 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
-#    Copyright (C) 2010-2011 OpenERP s.a. (<http://openerp.com>).
+#    Copyright (C) 2010-2013 OpenERP s.a. (<http://openerp.com>).
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU Affero General Public License as
 
 """
 
-import base64
-import imp
 import itertools
 import logging
 import os
-import re
 import sys
-import zipfile
-import zipimport
-
-from cStringIO import StringIO
-from os.path import join as opj
-from zipfile import PyZipFile, ZIP_DEFLATED
-
+import threading
 
 import openerp
 import openerp.modules.db
 import openerp.modules.graph
 import openerp.modules.migration
-import openerp.netsvc as netsvc
+import openerp.modules.registry
 import openerp.osv as osv
-import openerp.pooler as pooler
-import openerp.release as release
 import openerp.tools as tools
-import openerp.tools.osutil as osutil
+from openerp import SUPERUSER_ID
 
-from openerp.tools.safe_eval import safe_eval as eval
 from openerp.tools.translate import _
-from openerp.modules.module import \
-    get_modules, get_modules_with_version, \
-    load_information_from_description_file, \
-    get_module_resource, zip_directory, \
-    get_module_path, initialize_sys_path, \
-    register_module_classes, init_module_models
-
-logger = netsvc.Logger()
+from openerp.modules.module import initialize_sys_path, \
+    load_openerp_module, init_module_models, adapt_version
 
-
-def open_openerp_namespace():
-    # See comment for open_openerp_namespace.
-    if openerp.conf.deprecation.open_openerp_namespace:
-        for k, v in list(sys.modules.items()):
-            if k.startswith('openerp.') and sys.modules.get(k[8:]) is None:
-                sys.modules[k[8:]] = v
+_logger = logging.getLogger(__name__)
+_test_logger = logging.getLogger('openerp.tests')
 
 
 def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=None, report=None):
@@ -75,11 +52,10 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=
        :param graph: graph of module nodes to load
        :param status: status dictionary for keeping track of progress
        :param perform_checks: whether module descriptors should be checked for validity (prints warnings
-                              for same cases, and even raise osv_except if certificate is invalid)
+                              for same cases)
        :param skip_modules: optional list of module names (packages) which have previously been loaded and can be skipped
        :return: list of modules that were installed or updated
     """
-    logger = logging.getLogger('init.load')
     def process_sql_file(cr, fp):
         queries = fp.read().split(';')
         for query in queries:
@@ -95,17 +71,20 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=
 
     def load_test(module_name, idref, mode):
         cr.commit()
-        if not tools.config.options['test_disable']:
-            try:
-                _load_data(cr, module_name, idref, mode, 'test')
-            except Exception, e:
-                logging.getLogger('init.test').exception(
-                    'Tests failed to execute in module %s', module_name)
-            finally:
-                if tools.config.options['test_commit']:
-                    cr.commit()
-                else:
-                    cr.rollback()
+        try:
+            threading.currentThread().testing = True
+            _load_data(cr, module_name, idref, mode, 'test')
+            return True
+        except Exception:
+            _test_logger.exception(
+                'module %s: an exception occurred in a test', module_name)
+            return False
+        finally:
+            threading.currentThread().testing = False
+            if tools.config.options['test_commit']:
+                cr.commit()
+            else:
+                cr.rollback()
 
     def _load_data(cr, module_name, idref, mode, kind):
         """
@@ -117,7 +96,7 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=
 
         """
         for filename in package.data[kind]:
-            logger.info("module %s: loading %s", module_name, filename)
+            _logger.info("module %s: loading %s", module_name, filename)
             _, ext = os.path.splitext(filename)
             pathname = os.path.join(module_name, filename)
             fp = tools.file_open(pathname)
@@ -125,6 +104,7 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=
             if kind in ('demo', 'demo_xml'):
                 noupdate = True
             try:
+                ext = ext.lower()
                 if ext == '.csv':
                     if kind in ('init', 'init_xml'):
                         noupdate = True
@@ -132,9 +112,13 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=
                 elif ext == '.sql':
                     process_sql_file(cr, fp)
                 elif ext == '.yml':
-                    tools.convert_yaml_import(cr, module_name, fp, idref, mode, noupdate)
-                else:
+                    tools.convert_yaml_import(cr, module_name, fp, kind, idref, mode, noupdate, report)
+                elif ext == '.xml':
                     tools.convert_xml_import(cr, module_name, fp, idref, mode, noupdate, report)
+                elif ext == '.js':
+                    pass # .js files are valid but ignored here.
+                else:
+                    _logger.warning("Can't load unknown file type %s.", filename)
             finally:
                 fp.close()
 
@@ -143,13 +127,17 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=
 
     processed_modules = []
     loaded_modules = []
-    pool = pooler.get_pool(cr.dbname)
+    registry = openerp.registry(cr.dbname)
     migrations = openerp.modules.migration.MigrationManager(cr, graph)
-    logger.debug('loading %d packages...', len(graph))
+    _logger.info('loading %d modules...', len(graph))
 
-    # get db timestamp
-    cr.execute("select now()::timestamp")
-    dt_before_load = cr.fetchone()[0]
+    # Query manual fields for all models at once and save them on the registry
+    # so the initialization code for each model does not have to do it
+    # one model at a time.
+    registry.fields_by_model = {}
+    cr.execute('SELECT * FROM ir_model_fields WHERE state=%s', ('manual',))
+    for field in cr.dictfetchall():
+        registry.fields_by_model.setdefault(field['model'], []).append(field)
 
     # register, instantiate and initialize models for each modules
     for index, package in enumerate(graph):
@@ -159,38 +147,39 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=
         if skip_modules and module_name in skip_modules:
             continue
 
-        logger.info('module %s: loading objects', package.name)
+        _logger.debug('module %s: loading objects', package.name)
         migrations.migrate_module(package, 'pre')
-        register_module_classes(package.name)
-        models = pool.load(cr, package)
+        load_openerp_module(package.name)
+
+        models = registry.load(cr, package)
+
         loaded_modules.append(package.name)
-        if package.state in ('to install', 'to upgrade'):
+        if hasattr(package, 'init') or hasattr(package, 'update') or package.state in ('to install', 'to upgrade'):
             init_module_models(cr, package.name, models)
-
+        registry._init_modules.add(package.name)
         status['progress'] = float(index) / len(graph)
 
         # Can't put this line out of the loop: ir.module.module will be
         # registered by init_module_models() above.
-        modobj = pool.get('ir.module.module')
+        modobj = registry['ir.module.module']
 
         if perform_checks:
-            modobj.check(cr, 1, [module_id])
+            modobj.check(cr, SUPERUSER_ID, [module_id])
 
         idref = {}
 
-        if package.state == 'to install':
+        mode = 'update'
+        if hasattr(package, 'init') or package.state == 'to install':
             mode = 'init'
-        else:
-            mode = 'update'
 
-        if package.state in ('to install', 'to upgrade'):
+        if hasattr(package, 'init') or hasattr(package, 'update') or package.state in ('to install', 'to upgrade'):
             if package.state=='to upgrade':
                 # upgrading the module information
-                modobj.write(cr, 1, [module_id], modobj.get_values_from_terp(package.data))
+                modobj.write(cr, SUPERUSER_ID, [module_id], modobj.get_values_from_terp(package.data))
             load_init_xml(module_name, idref, mode)
             load_update_xml(module_name, idref, mode)
             load_data(module_name, idref, mode)
-            if package.dbdemo and package.state != 'installed':
+            if hasattr(package, 'demo') or (package.dbdemo and package.state != 'installed'):
                 status['progress'] = (index + 0.75) / len(graph)
                 load_demo_xml(module_name, idref, mode)
                 load_demo(module_name, idref, mode)
@@ -200,25 +189,36 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=
                 # on demo data. Other tests can be added into the regular
                 # 'data' section, but should probably not alter the data,
                 # as there is no rollback.
-                load_test(module_name, idref, mode)
+                if tools.config.options['test_enable']:
+                    report.record_result(load_test(module_name, idref, mode))
+
+                    # Run the `fast_suite` and `checks` tests given by the module.
+                    if module_name == 'base':
+                        # Also run the core tests after the database is created.
+                        report.record_result(openerp.modules.module.run_unit_tests('openerp'))
+                    report.record_result(openerp.modules.module.run_unit_tests(module_name))
 
             processed_modules.append(package.name)
 
             migrations.migrate_module(package, 'post')
 
-            ver = release.major_version + '.' + package.data['version']
+            ver = adapt_version(package.data['version'])
             # Set new modules and dependencies
-            modobj.write(cr, 1, [module_id], {'state': 'installed', 'latest_version': ver})
+            modobj.write(cr, SUPERUSER_ID, [module_id], {'state': 'installed', 'latest_version': ver})
             # Update translations for all installed languages
-            modobj.update_translations(cr, 1, [module_id], None)
+            modobj.update_translations(cr, SUPERUSER_ID, [module_id], None)
 
             package.state = 'installed'
+            for kind in ('init', 'demo', 'update'):
+                if hasattr(package, kind):
+                    delattr(package, kind)
 
         cr.commit()
 
-    # mark new res_log records as read
-    cr.execute("update res_log set read=True where create_date >= %s", (dt_before_load,))
-
+    # The query won't be valid for models created later (i.e. custom model
+    # created after the registry has been loaded), so empty its result.
+    registry.fields_by_model = None
+    
     cr.commit()
 
     return loaded_modules, processed_modules
@@ -235,18 +235,18 @@ def _check_module_names(cr, module_names):
             # find out what module name(s) are incorrect:
             cr.execute("SELECT name FROM ir_module_module")
             incorrect_names = mod_names.difference([x['name'] for x in cr.dictfetchall()])
-            logging.getLogger('init').warning('invalid module names, ignored: %s', ", ".join(incorrect_names))
+            _logger.warning('invalid module names, ignored: %s', ", ".join(incorrect_names))
 
-def load_marked_modules(cr, graph, states, force, progressdict, report, loaded_modules):
+def load_marked_modules(cr, graph, states, force, progressdict, report, loaded_modules, perform_checks):
     """Loads modules marked with ``states``, adding them to ``graph`` and
        ``loaded_modules`` and returns a list of installed/upgraded modules."""
     processed_modules = []
     while True:
         cr.execute("SELECT name from ir_module_module WHERE state IN %s" ,(tuple(states),))
         module_list = [name for (name,) in cr.fetchall() if name not in graph]
-        new_modules_in_graph = graph.add_modules(cr, module_list, force)
-        logger.notifyChannel('init', netsvc.LOG_DEBUG, 'Updating graph with %d more modules' % (len(module_list)))
-        loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules)
+        graph.add_modules(cr, module_list, force)
+        _logger.debug('Updating graph with %d more modules', len(module_list))
+        loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)
         processed_modules.extend(processed)
         loaded_modules.extend(loaded)
         if not processed: break
@@ -256,11 +256,9 @@ def load_marked_modules(cr, graph, states, force, progressdict, report, loaded_m
 def load_modules(db, force_demo=False, status=None, update_module=False):
     # TODO status['progress'] reporting is broken: used twice (and reset each
     # time to zero) in load_module_graph, not fine-grained enough.
-    # It should be a method exposed by the pool.
+    # It should be a method exposed by the registry.
     initialize_sys_path()
 
-    open_openerp_namespace()
-
     force = []
     if force_demo:
         force.append('demo')
@@ -268,133 +266,117 @@ def load_modules(db, force_demo=False, status=None, update_module=False):
     cr = db.cursor()
     try:
         if not openerp.modules.db.is_initialized(cr):
-            logger.notifyChannel("init", netsvc.LOG_INFO, "init db")
+            _logger.info("init db")
             openerp.modules.db.initialize(cr)
-            update_module = True
+            tools.config["init"]["all"] = 1
+            tools.config['update']['all'] = 1
+            if not tools.config['without_demo']:
+                tools.config["demo"]['all'] = 1
 
-        # This is a brand new pool, just created in pooler.get_db_and_pool()
-        pool = pooler.get_pool(cr.dbname)
+        # This is a brand new registry, just created in
+        # openerp.modules.registry.RegistryManager.new().
+        registry = openerp.registry(cr.dbname)
 
-        report = tools.assertion_report()
         if 'base' in tools.config['update'] or 'all' in tools.config['update']:
             cr.execute("update ir_module_module set state=%s where name=%s and state=%s", ('to upgrade', 'base', 'installed'))
 
         # STEP 1: LOAD BASE (must be done before module dependencies can be computed for later steps) 
         graph = openerp.modules.graph.Graph()
-        graph.add_module(cr, 'base', force_demo)
+        graph.add_module(cr, 'base', force)
         if not graph:
-            logger.notifyChannel('init', netsvc.LOG_CRITICAL, 'module base cannot be loaded! (hint: verify addons-path)')
+            _logger.critical('module base cannot be loaded! (hint: verify addons-path)')
             raise osv.osv.except_osv(_('Could not load base module'), _('module base cannot be loaded! (hint: verify addons-path)'))
 
         # processed_modules: for cleanup step after install
         # loaded_modules: to avoid double loading
-        # After load_module_graph(), 'base' has been installed or updated and its state is 'installed'.
-        loaded_modules, processed_modules = load_module_graph(cr, graph, status, report=report)
+        report = registry._assertion_report
+        loaded_modules, processed_modules = load_module_graph(cr, graph, status, perform_checks=update_module, report=report)
 
         if tools.config['load_language']:
             for lang in tools.config['load_language'].split(','):
                 tools.load_language(cr, lang)
 
         # STEP 2: Mark other modules to be loaded/updated
-        # This is a one-shot use of tools.config[init|update] from the command line
-        # arguments. It is directly cleared to not interfer with later create/update
-        # issued via RPC.
         if update_module:
-            modobj = pool.get('ir.module.module')
-            if ('base' in tools.config['init']) or ('base' in tools.config['update']) \
-                or ('all' in tools.config['init']) or ('all' in tools.config['update']):
-                logger.notifyChannel('init', netsvc.LOG_INFO, 'updating modules list')
-                modobj.update_list(cr, 1)
-
-            if 'all' in tools.config['init']:
-                ids = modobj.search(cr, 1, [])
-                tools.config['init'] = dict.fromkeys([m['name'] for m in modobj.read(cr, 1, ids, ['name'])], 1)
+            modobj = registry['ir.module.module']
+            if ('base' in tools.config['init']) or ('base' in tools.config['update']):
+                _logger.info('updating modules list')
+                modobj.update_list(cr, SUPERUSER_ID)
 
             _check_module_names(cr, itertools.chain(tools.config['init'].keys(), tools.config['update'].keys()))
 
-            mods = [k for k in tools.config['init'] if tools.config['init'][k] and k not in ('base', 'all')]
-            ids = modobj.search(cr, 1, ['&', ('state', '=', 'uninstalled'), ('name', 'in', mods)])
-            if ids:
-                modobj.button_install(cr, 1, ids) # goes from 'uninstalled' to 'to install'
+            mods = [k for k in tools.config['init'] if tools.config['init'][k]]
+            if mods:
+                ids = modobj.search(cr, SUPERUSER_ID, ['&', ('state', '=', 'uninstalled'), ('name', 'in', mods)])
+                if ids:
+                    modobj.button_install(cr, SUPERUSER_ID, ids)
 
-            mods = [k for k in tools.config['update'] if tools.config['update'][k] and k not in ('base', 'all')]
-            ids = modobj.search(cr, 1, ['&', ('state', '=', 'installed'), ('name', 'in', mods)])
-            if ids:
-                modobj.button_upgrade(cr, 1, ids) # goes from 'installed' to 'to upgrade'
+            mods = [k for k in tools.config['update'] if tools.config['update'][k]]
+            if mods:
+                ids = modobj.search(cr, SUPERUSER_ID, ['&', ('state', '=', 'installed'), ('name', 'in', mods)])
+                if ids:
+                    modobj.button_upgrade(cr, SUPERUSER_ID, ids)
+
+            cr.execute("update ir_module_module set state=%s where name=%s", ('installed', 'base'))
 
-        for kind in ('init', 'demo', 'update'):
-            tools.config[kind] = {}
 
         # STEP 3: Load marked modules (skipping base which was done in STEP 1)
         # IMPORTANT: this is done in two parts, first loading all installed or
         #            partially installed modules (i.e. installed/to upgrade), to
         #            offer a consistent system to the second part: installing
         #            newly selected modules.
-        states_to_load = ['installed', 'to upgrade']
-        processed = load_marked_modules(cr, graph, states_to_load, force, status, report, loaded_modules)
+        #            We include the modules 'to remove' in the first step, because
+        #            they are part of the "currently installed" modules. They will
+        #            be dropped in STEP 6 later, before restarting the loading
+        #            process.
+        states_to_load = ['installed', 'to upgrade', 'to remove']
+        processed = load_marked_modules(cr, graph, states_to_load, force, status, report, loaded_modules, update_module)
         processed_modules.extend(processed)
         if update_module:
             states_to_load = ['to install']
-            processed = load_marked_modules(cr, graph, states_to_load, force, status, report, loaded_modules)
+            processed = load_marked_modules(cr, graph, states_to_load, force, status, report, loaded_modules, update_module)
             processed_modules.extend(processed)
 
         # load custom models
         cr.execute('select model from ir_model where state=%s', ('manual',))
         for model in cr.dictfetchall():
-            pool.get('ir.model').instanciate(cr, 1, model['model'], {})
+            registry['ir.model'].instanciate(cr, SUPERUSER_ID, model['model'], {})
 
-        # STEP 4: Finish and cleanup
+        # STEP 4: Finish and cleanup installations
         if processed_modules:
             cr.execute("""select model,name from ir_model where id NOT IN (select distinct model_id from ir_model_access)""")
             for (model, name) in cr.fetchall():
-                model_obj = pool.get(model)
-                if model_obj and not model_obj.is_transient():
-                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'Model %s (%s) has no access rules!' % (model, name))
+                if model in registry and not registry[model].is_transient():
+                    _logger.warning('The model %s has no access rules, consider adding one. E.g. access_%s,access_%s,model_%s,,1,1,1,1',
+                        model, model.replace('.', '_'), model.replace('.', '_'), model.replace('.', '_'))
 
             # Temporary warning while we remove access rights on osv_memory objects, as they have
             # been replaced by owner-only access rights
             cr.execute("""select distinct mod.model, mod.name from ir_model_access acc, ir_model mod where acc.model_id = mod.id""")
             for (model, name) in cr.fetchall():
-                model_obj = pool.get(model)
-                if model_obj and model_obj.is_transient():
-                    logger.notifyChannel('init', netsvc.LOG_WARNING, 'The transient model %s (%s) should not have explicit access rules!' % (model, name))
+                if model in registry and registry[model].is_transient():
+                    _logger.warning('The transient model %s (%s) should not have explicit access rules!', model, name)
 
             cr.execute("SELECT model from ir_model")
             for (model,) in cr.fetchall():
-                obj = pool.get(model)
-                if obj:
-                    obj._check_removed_columns(cr, log=True)
+                if model in registry:
+                    registry[model]._check_removed_columns(cr, log=True)
                 else:
-                    logger.notifyChannel('init', netsvc.LOG_WARNING, "Model %s is declared but cannot be loaded! (Perhaps a module was partially removed or renamed)" % model)
+                    _logger.warning("Model %s is declared but cannot be loaded! (Perhaps a module was partially removed or renamed)", model)
 
             # Cleanup orphan records
-            pool.get('ir.model.data')._process_end(cr, 1, processed_modules)
+            registry['ir.model.data']._process_end(cr, SUPERUSER_ID, processed_modules)
 
-        if report.get_report():
-            logger.notifyChannel('init', netsvc.LOG_INFO, report)
+        for kind in ('init', 'demo', 'update'):
+            tools.config[kind] = {}
 
         cr.commit()
-        if update_module:
-            # Remove records referenced from ir_model_data for modules to be
-            # removed (and removed the references from ir_model_data).
-            cr.execute("select id,name from ir_module_module where state=%s", ('to remove',))
-            for mod_id, mod_name in cr.fetchall():
-                cr.execute('select model,res_id from ir_model_data where noupdate=%s and module=%s order by id desc', (False, mod_name,))
-                for rmod, rid in cr.fetchall():
-                    uid = 1
-                    rmod_module= pool.get(rmod)
-                    if rmod_module:
-                        # TODO group by module so that we can delete multiple ids in a call
-                        rmod_module.unlink(cr, uid, [rid])
-                    else:
-                        logger.notifyChannel('init', netsvc.LOG_ERROR, 'Could not locate %s to remove res=%d' % (rmod,rid))
-                cr.execute('delete from ir_model_data where noupdate=%s and module=%s', (False, mod_name,))
-                cr.commit()
 
-            # Remove menu items that are not referenced by any of other
-            # (child) menu item, ir_values, or ir_model_data.
-            # This code could be a method of ir_ui_menu.
-            # TODO: remove menu without actions of children
+        # STEP 5: Cleanup menus 
+        # Remove menu items that are not referenced by any of other
+        # (child) menu item, ir_values, or ir_model_data.
+        # TODO: This code could be a method of ir_ui_menu. Remove menu without actions of children
+        if update_module:
             while True:
                 cr.execute('''delete from
                         ir_ui_menu
@@ -408,11 +390,31 @@ def load_modules(db, force_demo=False, status=None, update_module=False):
                 if not cr.rowcount:
                     break
                 else:
-                    logger.notifyChannel('init', netsvc.LOG_INFO, 'removed %d unused menus' % (cr.rowcount,))
+                    _logger.info('removed %d unused menus', cr.rowcount)
+
+        # STEP 6: Uninstall modules to remove
+        if update_module:
+            # Remove records referenced from ir_model_data for modules to be
+            # removed (and removed the references from ir_model_data).
+            cr.execute("SELECT id FROM ir_module_module WHERE state=%s", ('to remove',))
+            mod_ids_to_remove = [x[0] for x in cr.fetchall()]
+            if mod_ids_to_remove:
+                registry['ir.module.module'].module_uninstall(cr, SUPERUSER_ID, mod_ids_to_remove)
+                # Recursive reload, should only happen once, because there should be no
+                # modules to remove next time
+                cr.commit()
+                _logger.info('Reloading registry once more after uninstalling modules')
+                return openerp.modules.registry.RegistryManager.new(cr.dbname, force_demo, status, update_module)
+
+        if report.failures:
+            _logger.error('At least one test failed when loading the modules.')
+        else:
+            _logger.info('Modules loaded.')
+
+        # STEP 7: call _register_hook on every model
+        for model in registry.models.values():
+            model._register_hook(cr)
 
-            # Pretend that modules to be removed are actually uninstalled.
-            cr.execute("update ir_module_module set state=%s where state=%s", ('uninstalled', 'to remove',))
-            cr.commit()
     finally:
         cr.close()