2011-05-11 17:48:41 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
2013-01-17 13:51:08 +00:00
|
|
|
# Copyright (C) 2010-2013 OpenERP s.a. (<http://openerp.com>).
|
2011-05-11 17:48:41 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
""" Modules (also called addons) management.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import itertools
|
2011-08-01 08:57:28 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import sys
|
2011-12-01 15:31:56 +00:00
|
|
|
import threading
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2011-08-01 08:57:28 +00:00
|
|
|
import openerp
|
2011-05-11 17:48:41 +00:00
|
|
|
import openerp.modules.db
|
|
|
|
import openerp.modules.graph
|
|
|
|
import openerp.modules.migration
|
2013-03-01 14:32:03 +00:00
|
|
|
import openerp.modules.registry
|
2011-08-01 08:57:28 +00:00
|
|
|
import openerp.osv as osv
|
|
|
|
import openerp.tools as tools
|
2012-08-31 13:53:09 +00:00
|
|
|
from openerp import SUPERUSER_ID
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2011-08-01 08:57:28 +00:00
|
|
|
from openerp.tools.translate import _
|
2012-03-22 16:08:06 +00:00
|
|
|
from openerp.modules.module import initialize_sys_path, \
|
2013-01-17 13:51:08 +00:00
|
|
|
load_openerp_module, init_module_models, adapt_version
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2012-01-24 12:42:52 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
2013-03-27 15:38:43 +00:00
|
|
|
_test_logger = logging.getLogger('openerp.tests')
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=None, report=None):
|
|
|
|
"""Migrates+Updates or Installs all module nodes from ``graph``
|
|
|
|
: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
|
2012-09-26 12:15:36 +00:00
|
|
|
for same cases)
|
2011-05-11 17:48:41 +00:00
|
|
|
: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
|
|
|
|
"""
|
|
|
|
def process_sql_file(cr, fp):
|
|
|
|
queries = fp.read().split(';')
|
|
|
|
for query in queries:
|
|
|
|
new_query = ' '.join(query.split())
|
|
|
|
if new_query:
|
|
|
|
cr.execute(new_query)
|
|
|
|
|
2011-08-01 08:57:28 +00:00
|
|
|
load_init_xml = lambda *args: _load_data(cr, *args, kind='init_xml')
|
|
|
|
load_update_xml = lambda *args: _load_data(cr, *args, kind='update_xml')
|
|
|
|
load_demo_xml = lambda *args: _load_data(cr, *args, kind='demo_xml')
|
|
|
|
load_data = lambda *args: _load_data(cr, *args, kind='data')
|
|
|
|
load_demo = lambda *args: _load_data(cr, *args, kind='demo')
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2011-08-01 08:57:28 +00:00
|
|
|
def load_test(module_name, idref, mode):
|
2011-05-11 17:48:41 +00:00
|
|
|
cr.commit()
|
2012-03-01 13:46:08 +00:00
|
|
|
try:
|
|
|
|
threading.currentThread().testing = True
|
|
|
|
_load_data(cr, module_name, idref, mode, 'test')
|
2012-03-02 11:02:27 +00:00
|
|
|
return True
|
2012-03-22 16:08:06 +00:00
|
|
|
except Exception:
|
2013-03-27 15:38:43 +00:00
|
|
|
_test_logger.exception(
|
2012-03-02 11:28:34 +00:00
|
|
|
'module %s: an exception occurred in a test', module_name)
|
2012-03-02 11:02:27 +00:00
|
|
|
return False
|
2012-03-01 13:46:08 +00:00
|
|
|
finally:
|
|
|
|
threading.currentThread().testing = False
|
|
|
|
if tools.config.options['test_commit']:
|
|
|
|
cr.commit()
|
|
|
|
else:
|
|
|
|
cr.rollback()
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
def _load_data(cr, module_name, idref, mode, kind):
|
|
|
|
"""
|
|
|
|
|
|
|
|
kind: data, demo, test, init_xml, update_xml, demo_xml.
|
|
|
|
|
|
|
|
noupdate is False, unless it is demo data or it is csv data in
|
|
|
|
init mode.
|
|
|
|
|
|
|
|
"""
|
|
|
|
for filename in package.data[kind]:
|
2013-05-07 09:24:22 +00:00
|
|
|
_logger.info("module %s: loading %s", module_name, filename)
|
2011-05-11 17:48:41 +00:00
|
|
|
_, ext = os.path.splitext(filename)
|
|
|
|
pathname = os.path.join(module_name, filename)
|
|
|
|
fp = tools.file_open(pathname)
|
|
|
|
noupdate = False
|
|
|
|
if kind in ('demo', 'demo_xml'):
|
|
|
|
noupdate = True
|
|
|
|
try:
|
2012-10-24 10:54:54 +00:00
|
|
|
ext = ext.lower()
|
2011-05-11 17:48:41 +00:00
|
|
|
if ext == '.csv':
|
|
|
|
if kind in ('init', 'init_xml'):
|
|
|
|
noupdate = True
|
|
|
|
tools.convert_csv_import(cr, module_name, pathname, fp.read(), idref, mode, noupdate)
|
|
|
|
elif ext == '.sql':
|
|
|
|
process_sql_file(cr, fp)
|
|
|
|
elif ext == '.yml':
|
2012-08-22 10:11:56 +00:00
|
|
|
tools.convert_yaml_import(cr, module_name, fp, kind, idref, mode, noupdate, report)
|
2012-10-24 10:54:54 +00:00
|
|
|
elif ext == '.xml':
|
2011-05-11 17:48:41 +00:00
|
|
|
tools.convert_xml_import(cr, module_name, fp, idref, mode, noupdate, report)
|
2012-11-14 10:31:40 +00:00
|
|
|
elif ext == '.js':
|
|
|
|
pass # .js files are valid but ignored here.
|
2012-10-24 10:54:54 +00:00
|
|
|
else:
|
2012-11-14 10:31:40 +00:00
|
|
|
_logger.warning("Can't load unknown file type %s.", filename)
|
2011-05-11 17:48:41 +00:00
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
if status is None:
|
|
|
|
status = {}
|
|
|
|
|
|
|
|
processed_modules = []
|
2011-07-12 13:33:43 +00:00
|
|
|
loaded_modules = []
|
2013-03-27 09:31:25 +00:00
|
|
|
registry = openerp.registry(cr.dbname)
|
2011-05-11 17:48:41 +00:00
|
|
|
migrations = openerp.modules.migration.MigrationManager(cr, graph)
|
2013-02-18 12:14:41 +00:00
|
|
|
_logger.info('loading %d modules...', len(graph))
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2012-12-14 10:58:20 +00:00
|
|
|
# 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.
|
2013-03-27 09:31:25 +00:00
|
|
|
registry.fields_by_model = {}
|
2012-12-11 10:59:54 +00:00
|
|
|
cr.execute('SELECT * FROM ir_model_fields WHERE state=%s', ('manual',))
|
|
|
|
for field in cr.dictfetchall():
|
2013-03-27 09:31:25 +00:00
|
|
|
registry.fields_by_model.setdefault(field['model'], []).append(field)
|
2011-10-17 14:46:15 +00:00
|
|
|
|
2011-07-11 12:49:58 +00:00
|
|
|
# register, instantiate and initialize models for each modules
|
2011-08-01 08:54:57 +00:00
|
|
|
for index, package in enumerate(graph):
|
2011-07-11 12:49:58 +00:00
|
|
|
module_name = package.name
|
|
|
|
module_id = package.id
|
|
|
|
|
|
|
|
if skip_modules and module_name in skip_modules:
|
2011-05-11 17:48:41 +00:00
|
|
|
continue
|
2011-07-11 12:49:58 +00:00
|
|
|
|
2013-02-18 12:14:41 +00:00
|
|
|
_logger.debug('module %s: loading objects', package.name)
|
2011-05-11 17:48:41 +00:00
|
|
|
migrations.migrate_module(package, 'pre')
|
2012-02-09 15:27:32 +00:00
|
|
|
load_openerp_module(package.name)
|
2012-02-09 14:14:44 +00:00
|
|
|
|
2013-03-27 09:31:25 +00:00
|
|
|
models = registry.load(cr, package)
|
2012-12-11 10:59:54 +00:00
|
|
|
|
2011-07-12 13:33:43 +00:00
|
|
|
loaded_modules.append(package.name)
|
2012-11-29 14:07:45 +00:00
|
|
|
if hasattr(package, 'init') or hasattr(package, 'update') or package.state in ('to install', 'to upgrade'):
|
2011-05-11 17:48:41 +00:00
|
|
|
init_module_models(cr, package.name, models)
|
2013-03-27 09:31:25 +00:00
|
|
|
registry._init_modules.add(package.name)
|
2011-08-01 08:54:57 +00:00
|
|
|
status['progress'] = float(index) / len(graph)
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2011-07-11 14:10:45 +00:00
|
|
|
# Can't put this line out of the loop: ir.module.module will be
|
|
|
|
# registered by init_module_models() above.
|
2013-03-27 09:31:25 +00:00
|
|
|
modobj = registry['ir.module.module']
|
2011-07-11 14:10:45 +00:00
|
|
|
|
2011-05-11 17:48:41 +00:00
|
|
|
if perform_checks:
|
2012-03-30 16:34:22 +00:00
|
|
|
modobj.check(cr, SUPERUSER_ID, [module_id])
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
idref = {}
|
|
|
|
|
2012-11-29 14:07:45 +00:00
|
|
|
mode = 'update'
|
|
|
|
if hasattr(package, 'init') or package.state == 'to install':
|
2011-05-11 17:48:41 +00:00
|
|
|
mode = 'init'
|
|
|
|
|
2012-11-29 14:07:45 +00:00
|
|
|
if hasattr(package, 'init') or hasattr(package, 'update') or package.state in ('to install', 'to upgrade'):
|
2011-05-11 17:48:41 +00:00
|
|
|
if package.state=='to upgrade':
|
|
|
|
# upgrading the module information
|
2012-03-30 16:34:22 +00:00
|
|
|
modobj.write(cr, SUPERUSER_ID, [module_id], modobj.get_values_from_terp(package.data))
|
2011-08-01 08:57:28 +00:00
|
|
|
load_init_xml(module_name, idref, mode)
|
|
|
|
load_update_xml(module_name, idref, mode)
|
|
|
|
load_data(module_name, idref, mode)
|
2012-11-29 14:07:45 +00:00
|
|
|
if hasattr(package, 'demo') or (package.dbdemo and package.state != 'installed'):
|
2011-08-01 08:54:57 +00:00
|
|
|
status['progress'] = (index + 0.75) / len(graph)
|
2011-08-01 08:57:28 +00:00
|
|
|
load_demo_xml(module_name, idref, mode)
|
|
|
|
load_demo(module_name, idref, mode)
|
2011-07-11 12:49:58 +00:00
|
|
|
cr.execute('update ir_module_module set demo=%s where id=%s', (True, module_id))
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
# launch tests only in demo mode, as most tests will depend
|
|
|
|
# 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.
|
2012-03-01 13:46:08 +00:00
|
|
|
if tools.config.options['test_enable']:
|
2012-03-02 11:02:27 +00:00
|
|
|
report.record_result(load_test(module_name, idref, mode))
|
2012-03-01 13:46:08 +00:00
|
|
|
|
|
|
|
# Run the `fast_suite` and `checks` tests given by the module.
|
|
|
|
if module_name == 'base':
|
2012-03-02 11:02:27 +00:00
|
|
|
# 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))
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
processed_modules.append(package.name)
|
|
|
|
|
|
|
|
migrations.migrate_module(package, 'post')
|
|
|
|
|
2013-01-17 13:51:08 +00:00
|
|
|
ver = adapt_version(package.data['version'])
|
2011-05-11 17:48:41 +00:00
|
|
|
# Set new modules and dependencies
|
2012-03-30 16:34:22 +00:00
|
|
|
modobj.write(cr, SUPERUSER_ID, [module_id], {'state': 'installed', 'latest_version': ver})
|
2011-05-11 17:48:41 +00:00
|
|
|
# Update translations for all installed languages
|
2012-03-30 16:34:22 +00:00
|
|
|
modobj.update_translations(cr, SUPERUSER_ID, [module_id], None)
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
package.state = 'installed'
|
2012-11-29 14:07:45 +00:00
|
|
|
for kind in ('init', 'demo', 'update'):
|
|
|
|
if hasattr(package, kind):
|
|
|
|
delattr(package, kind)
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2011-07-11 12:49:58 +00:00
|
|
|
cr.commit()
|
2012-12-11 10:59:54 +00:00
|
|
|
|
2012-12-14 10:58:20 +00:00
|
|
|
# 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.
|
2013-03-27 09:31:25 +00:00
|
|
|
registry.fields_by_model = None
|
2012-04-02 11:47:46 +00:00
|
|
|
|
2011-05-11 17:48:41 +00:00
|
|
|
cr.commit()
|
|
|
|
|
2011-07-12 13:33:43 +00:00
|
|
|
return loaded_modules, processed_modules
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
def _check_module_names(cr, module_names):
|
|
|
|
mod_names = set(module_names)
|
|
|
|
if 'base' in mod_names:
|
|
|
|
# ignore dummy 'all' module
|
|
|
|
if 'all' in mod_names:
|
|
|
|
mod_names.remove('all')
|
|
|
|
if mod_names:
|
|
|
|
cr.execute("SELECT count(id) AS count FROM ir_module_module WHERE name in %s", (tuple(mod_names),))
|
|
|
|
if cr.dictfetchone()['count'] != len(mod_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()])
|
2012-01-24 12:42:52 +00:00
|
|
|
_logger.warning('invalid module names, ignored: %s', ", ".join(incorrect_names))
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2012-12-11 10:59:54 +00:00
|
|
|
def load_marked_modules(cr, graph, states, force, progressdict, report, loaded_modules, perform_checks):
|
2011-07-12 13:33:43 +00:00
|
|
|
"""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]
|
2012-03-22 16:08:06 +00:00
|
|
|
graph.add_modules(cr, module_list, force)
|
2012-01-24 12:42:52 +00:00
|
|
|
_logger.debug('Updating graph with %d more modules', len(module_list))
|
2012-12-11 10:59:54 +00:00
|
|
|
loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)
|
2011-07-12 13:33:43 +00:00
|
|
|
processed_modules.extend(processed)
|
|
|
|
loaded_modules.extend(loaded)
|
|
|
|
if not processed: break
|
|
|
|
return processed_modules
|
|
|
|
|
|
|
|
|
2011-05-11 17:48:41 +00:00
|
|
|
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.
|
2013-03-27 09:31:25 +00:00
|
|
|
# It should be a method exposed by the registry.
|
2011-05-11 17:48:41 +00:00
|
|
|
initialize_sys_path()
|
|
|
|
|
2011-05-17 09:18:22 +00:00
|
|
|
force = []
|
|
|
|
if force_demo:
|
|
|
|
force.append('demo')
|
|
|
|
|
2011-05-11 17:48:41 +00:00
|
|
|
cr = db.cursor()
|
2011-05-17 09:18:22 +00:00
|
|
|
try:
|
|
|
|
if not openerp.modules.db.is_initialized(cr):
|
2012-01-24 12:42:52 +00:00
|
|
|
_logger.info("init db")
|
2011-05-11 17:48:41 +00:00
|
|
|
openerp.modules.db.initialize(cr)
|
2012-11-29 14:07:45 +00:00
|
|
|
tools.config["init"]["all"] = 1
|
|
|
|
tools.config['update']['all'] = 1
|
|
|
|
if not tools.config['without_demo']:
|
|
|
|
tools.config["demo"]['all'] = 1
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2013-03-27 09:31:25 +00:00
|
|
|
# This is a brand new registry, just created in
|
2013-04-02 09:29:14 +00:00
|
|
|
# openerp.modules.registry.RegistryManager.new().
|
2013-03-27 09:31:25 +00:00
|
|
|
registry = openerp.registry(cr.dbname)
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
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()
|
2012-11-29 14:07:45 +00:00
|
|
|
graph.add_module(cr, 'base', force)
|
2011-05-11 17:48:41 +00:00
|
|
|
if not graph:
|
2012-01-24 12:42:52 +00:00
|
|
|
_logger.critical('module base cannot be loaded! (hint: verify addons-path)')
|
2011-05-11 17:48:41 +00:00
|
|
|
raise osv.osv.except_osv(_('Could not load base module'), _('module base cannot be loaded! (hint: verify addons-path)'))
|
2011-09-02 13:27:12 +00:00
|
|
|
|
|
|
|
# processed_modules: for cleanup step after install
|
|
|
|
# loaded_modules: to avoid double loading
|
2013-03-27 09:31:25 +00:00
|
|
|
report = registry._assertion_report
|
2012-12-11 10:59:54 +00:00
|
|
|
loaded_modules, processed_modules = load_module_graph(cr, graph, status, perform_checks=update_module, report=report)
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
if update_module:
|
2013-03-27 09:31:25 +00:00
|
|
|
modobj = registry['ir.module.module']
|
2012-11-29 14:07:45 +00:00
|
|
|
if ('base' in tools.config['init']) or ('base' in tools.config['update']):
|
2012-01-24 12:42:52 +00:00
|
|
|
_logger.info('updating modules list')
|
2012-08-31 13:53:09 +00:00
|
|
|
modobj.update_list(cr, SUPERUSER_ID)
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2011-11-17 14:46:28 +00:00
|
|
|
_check_module_names(cr, itertools.chain(tools.config['init'].keys(), tools.config['update'].keys()))
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2012-11-29 14:07:45 +00:00
|
|
|
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)
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2012-11-29 14:07:45 +00:00
|
|
|
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'))
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# STEP 3: Load marked modules (skipping base which was done in STEP 1)
|
2011-07-12 13:33:43 +00:00
|
|
|
# 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.
|
2012-03-30 16:34:22 +00:00
|
|
|
# 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.
|
2013-09-09 09:50:04 +00:00
|
|
|
# IMPORTANT 2: We have to loop here until all relevant modules have been
|
|
|
|
# processed, because in some rare cases the dependencies have
|
|
|
|
# changed, and modules that depend on an uninstalled module
|
|
|
|
# will not be processed on the first pass.
|
|
|
|
# It's especially useful for migrations.
|
|
|
|
previously_processed = -1
|
|
|
|
while previously_processed < len(processed_modules):
|
|
|
|
previously_processed = len(processed_modules)
|
|
|
|
processed_modules += load_marked_modules(cr, graph,
|
|
|
|
['installed', 'to upgrade', 'to remove'],
|
|
|
|
force, status, report, loaded_modules, update_module)
|
|
|
|
if update_module:
|
|
|
|
processed_modules += load_marked_modules(cr, graph,
|
|
|
|
['to install'], force, status, report,
|
|
|
|
loaded_modules, update_module)
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
# load custom models
|
|
|
|
cr.execute('select model from ir_model where state=%s', ('manual',))
|
|
|
|
for model in cr.dictfetchall():
|
2013-03-27 09:31:25 +00:00
|
|
|
registry['ir.model'].instanciate(cr, SUPERUSER_ID, model['model'], {})
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2012-03-30 16:34:22 +00:00
|
|
|
# STEP 4: Finish and cleanup installations
|
2011-05-11 17:48:41 +00:00
|
|
|
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():
|
2013-03-29 14:07:23 +00:00
|
|
|
if model in registry and not registry[model].is_transient():
|
2012-11-15 16:06:11 +00:00
|
|
|
_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('.', '_'))
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
# 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():
|
2013-03-29 14:07:23 +00:00
|
|
|
if model in registry and registry[model].is_transient():
|
2012-01-24 12:42:52 +00:00
|
|
|
_logger.warning('The transient model %s (%s) should not have explicit access rules!', model, name)
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
cr.execute("SELECT model from ir_model")
|
|
|
|
for (model,) in cr.fetchall():
|
2013-03-29 14:07:23 +00:00
|
|
|
if model in registry:
|
|
|
|
registry[model]._check_removed_columns(cr, log=True)
|
2011-05-11 17:48:41 +00:00
|
|
|
else:
|
2012-01-24 12:42:52 +00:00
|
|
|
_logger.warning("Model %s is declared but cannot be loaded! (Perhaps a module was partially removed or renamed)", model)
|
2011-05-11 17:48:41 +00:00
|
|
|
|
|
|
|
# Cleanup orphan records
|
2013-03-27 09:31:25 +00:00
|
|
|
registry['ir.model.data']._process_end(cr, SUPERUSER_ID, processed_modules)
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2012-11-29 14:07:45 +00:00
|
|
|
for kind in ('init', 'demo', 'update'):
|
|
|
|
tools.config[kind] = {}
|
|
|
|
|
2011-05-11 17:48:41 +00:00
|
|
|
cr.commit()
|
|
|
|
|
2012-03-30 16:34:22 +00:00
|
|
|
# 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:
|
2011-05-11 17:48:41 +00:00
|
|
|
while True:
|
|
|
|
cr.execute('''delete from
|
|
|
|
ir_ui_menu
|
|
|
|
where
|
|
|
|
(id not IN (select parent_id from ir_ui_menu where parent_id is not null))
|
|
|
|
and
|
|
|
|
(id not IN (select res_id from ir_values where model='ir.ui.menu'))
|
|
|
|
and
|
|
|
|
(id not IN (select res_id from ir_model_data where model='ir.ui.menu'))''')
|
|
|
|
cr.commit()
|
|
|
|
if not cr.rowcount:
|
|
|
|
break
|
|
|
|
else:
|
2012-03-30 16:34:22 +00:00
|
|
|
_logger.info('removed %d unused menus', cr.rowcount)
|
2011-05-11 17:48:41 +00:00
|
|
|
|
2012-03-30 16:34:22 +00:00
|
|
|
# STEP 6: Uninstall modules to remove
|
|
|
|
if update_module:
|
2011-05-11 17:48:41 +00:00
|
|
|
# Remove records referenced from ir_model_data for modules to be
|
|
|
|
# removed (and removed the references from ir_model_data).
|
2012-03-30 16:34:22 +00:00
|
|
|
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:
|
2013-03-27 09:31:25 +00:00
|
|
|
registry['ir.module.module'].module_uninstall(cr, SUPERUSER_ID, mod_ids_to_remove)
|
2012-03-30 16:34:22 +00:00
|
|
|
# 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')
|
2013-04-02 09:29:14 +00:00
|
|
|
return openerp.modules.registry.RegistryManager.new(cr.dbname, force_demo, status, update_module)
|
2012-01-24 12:42:52 +00:00
|
|
|
|
2013-09-12 18:29:26 +00:00
|
|
|
# STEP 7: verify custom views on every model
|
|
|
|
if update_module:
|
|
|
|
Views = registry['ir.ui.view']
|
|
|
|
custom_view_test = True
|
|
|
|
for model in registry.models.keys():
|
|
|
|
if not Views._validate_custom_views(cr, SUPERUSER_ID, model):
|
|
|
|
custom_view_test = False
|
|
|
|
_logger.error('invalid custom view(s) for model %s', model)
|
|
|
|
report.record_result(custom_view_test)
|
|
|
|
|
2012-03-02 11:02:27 +00:00
|
|
|
if report.failures:
|
|
|
|
_logger.error('At least one test failed when loading the modules.')
|
|
|
|
else:
|
|
|
|
_logger.info('Modules loaded.')
|
2012-11-26 11:22:05 +00:00
|
|
|
|
2013-09-12 18:29:26 +00:00
|
|
|
# STEP 8: call _register_hook on every model
|
2013-03-27 09:31:25 +00:00
|
|
|
for model in registry.models.values():
|
2012-12-19 13:21:38 +00:00
|
|
|
model._register_hook(cr)
|
2012-11-26 11:22:05 +00:00
|
|
|
|
2011-05-11 17:48:41 +00:00
|
|
|
finally:
|
|
|
|
cr.close()
|
|
|
|
|
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|