Skip to content

Commit

Permalink
Merge pull request OCA#135 from StefanRijnhart/5.0-port
Browse files Browse the repository at this point in the history
5.0 port
  • Loading branch information
pedrobaeza committed Nov 25, 2014
2 parents 6d5118a + aa41f4b commit 6b4cc52
Show file tree
Hide file tree
Showing 29 changed files with 1,720 additions and 6 deletions.
1 change: 0 additions & 1 deletion .bzrignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ bin/addons/*
bin/filestore*
.Python
include
lib
bin/activate
bin/activate_this.py
bin/easy_install
Expand Down
137 changes: 133 additions & 4 deletions bin/addons/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import os, sys, imp
from os.path import join as opj
import types
import itertools
import zipimport
import osv
Expand All @@ -40,6 +41,15 @@

logger = netsvc.Logger()

### OpenUpgrade
def table_exists(cr, table):
""" Check whether a certain table or view exists """
cr.execute(
'SELECT count(relname) FROM pg_class WHERE relname = %s',
(table,))
return cr.fetchone()[0] == 1
### End of OpenUpgrade

_ad = os.path.abspath(opj(tools.config['root_path'], 'addons')) # default addons path (base)
ad = os.path.abspath(tools.config['addons_path']) # alternate addons path

Expand Down Expand Up @@ -554,8 +564,7 @@ def mergedict(a, b):
if mod:
del mod


def load_module_graph(cr, graph, status=None, perform_checks=True, **kwargs):
def load_module_graph(cr, graph, status=None, registry=None, perform_checks=True, **kwargs):
# **kwargs is passed directly to convert_xml_import
if not status:
status = {}
Expand All @@ -570,11 +579,126 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, **kwargs):
has_updates = False
modobj = None

import string

local_registry = {}
def log_model(model):
"""
OpenUpgrade: Store the characteristics of the BaseModel and its fields
in the local registry, so that we can compare changes with the
main registry
"""

# persistent models only
if isinstance(model, osv.osv.osv_memory):
return

model_registry = local_registry.setdefault(
model._name, {})
if model._inherits:
model_registry['_inherits'] = {'_inherits': unicode(model._inherits)}
for k, v in model._columns.items():
properties = {
'type': v._type,
'isfunction': (
isinstance(v, osv.fields.function) and 'function' or ''),
'relation': (
v._type in ('many2many', 'many2one','one2many')
and v._obj or ''
),
'required': v.required and 'required' or '',
'selection_keys': '',
'req_default': '',
'inherits': '',
}
if v._type == 'selection':
if hasattr(v.selection, "__iter__"):
properties['selection_keys'] = unicode(
sorted([x[0] for x in v.selection]))
else:
properties['selection_keys'] = 'function'
if v.required and k in model._defaults:
if isinstance(model._defaults[k], types.FunctionType):
# todo: in OpenERP 5 (and in 6 as well),
# literals are wrapped in a lambda function.
properties['req_default'] = 'function'
else:
properties['req_default'] = unicode(model._defaults[k])
for key, value in properties.items():
if value:
model_registry.setdefault(k, {})[key] = value

def get_record_id(cr, module, model, field, mode):
"""
OpenUpgrade: get or create the id from the record table matching
the key parameter values
"""
cr.execute(
"SELECT id FROM openupgrade_record "
"WHERE module = %s AND model = %s AND "
"field = %s AND mode = %s AND type = %s",
(module, model, field, mode, 'field')
)
record = cr.fetchone()
if record:
return record[0]
cr.execute(
"INSERT INTO openupgrade_record "
"(module, model, field, mode, type) "
"VALUES (%s, %s, %s, %s, %s)",
(module, model, field, mode, 'field')
)
cr.execute(
"SELECT id FROM openupgrade_record "
"WHERE module = %s AND model = %s AND "
"field = %s AND mode = %s AND type = %s",
(module, model, field, mode, 'field')
)
return cr.fetchone()[0]

def compare_registries(cr, module):
"""
OpenUpgrade: Compare the local registry with the global registry,
log any differences and merge the local registry with
the global one.
"""
if not table_exists(cr, 'openupgrade_record'):
return
for model, fields in local_registry.items():
registry.setdefault(model, {})
for field, attributes in fields.items():
old_field = registry[model].setdefault(field, {})
mode = old_field and 'modify' or 'create'
record_id = False
for key, value in attributes.items():
if key not in old_field or old_field[key] != value:
if not record_id:
record_id = get_record_id(
cr, module, model, field, mode)
cr.execute(
"SELECT id FROM openupgrade_attribute "
"WHERE name = %s AND value = %s AND "
"record_id = %s",
(key, value, record_id)
)
if not cr.fetchone():
cr.execute(
"INSERT INTO openupgrade_attribute "
"(name, value, record_id) VALUES (%s, %s, %s)",
(key, value, record_id)
)
old_field[key] = value

for package in graph:
logger.notifyChannel('init', netsvc.LOG_INFO, 'module %s: loading objects' % package.name)
migrations.migrate_module(package, 'pre')
register_class(package.name)
modules = pool.instanciate(package.name, cr)

local_registry = {}
for model in modules:
log_model(model)
compare_registries(cr, package.name)
if hasattr(package, 'init') or hasattr(package, 'update') or package.state in ('to install', 'to upgrade'):
init_module_objects(cr, package.name, modules)
cr.commit()
Expand Down Expand Up @@ -685,13 +809,18 @@ def load_modules(db, force_demo=False, status=None, update_module=False):
if force_demo:
force.append('demo')
pool = pooler.get_pool(cr.dbname)
registry = {}
try:
report = tools.assertion_report()
# NOTE: Try to also load the modules that have been marked as uninstallable previously...
STATES_TO_LOAD = ['installed', 'to upgrade', 'uninstallable']
graph = create_graph(cr, ['base'], force)

has_updates = load_module_graph(cr, graph, status, perform_checks=(not update_module), report=report)
try:
has_updates = load_module_graph(cr, graph, status, perform_checks=(not update_module), registry=registry, report=report)
except Exception, e:
print e
raise

if update_module:
modobj = pool.get('ir.module.module')
Expand Down Expand Up @@ -733,7 +862,7 @@ def load_modules(db, force_demo=False, status=None, update_module=False):
# nothing to load
break
logger.notifyChannel('init', netsvc.LOG_DEBUG, 'Updating graph with %d more modules' % (len(module_list)))
r = load_module_graph(cr, graph, status, report=report)
r = load_module_graph(cr, graph, status, registry=registry, report=report)
has_updates = has_updates or r

if has_updates:
Expand Down
6 changes: 6 additions & 0 deletions bin/addons/base/ir/ir_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
from tools.translate import _
import pooler

from openupgrade import openupgrade_log

def _get_fields_type(self, cr, uid, context=None):
cr.execute('select distinct ttype,ttype from ir_model_fields')
return cr.fetchall()
Expand Down Expand Up @@ -459,6 +461,10 @@ def _update_dummy(self,cr, uid, model, module, xml_id=False, store=True):
return id

def _update(self,cr, uid, model, module, values, xml_id=False, store=True, noupdate=False, mode='init', res_id=False, context=None):
#OpenUpgrade: log entry (used in csv import)
if xml_id:
openupgrade_log.log_xml_id(cr, module, xml_id)

warning = True
model_obj = self.pool.get(model)
if not context:
Expand Down
2 changes: 2 additions & 0 deletions bin/addons/openupgrade_records/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import model
import lib
63 changes: 63 additions & 0 deletions bin/addons/openupgrade_records/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module Copyright (C) 2012 OpenUpgrade community
# https://launchpad.net/~openupgrade-committers
#
# Contributors:
# Therp BV <http://therp.nl>
#
# 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/>.
#
##############################################################################


{
'name': 'OpenUpgrade Records',
'version': '0.2',
'category': 'Normal',
'description': """Allow OpenUpgrade records to be
stored in the database and compare with other servers.
This module depends on OpenERP client lib:
easy_install openerp-client-lib
""",
'author': 'OpenUpgrade Community',
'maintainer': 'OpenUpgrade Community',
'contributors': ['Therp BV'],
'website': 'https://launchpad.net/~openupgrade-committers',
'depends': [],
'init_xml': [],
'update_xml': [
'view/openupgrade_record.xml',
'view/comparison_config.xml',
'view/analysis_wizard.xml',
'view/generate_records_wizard.xml',
'view/install_all_wizard.xml',
'security/ir.model.access.csv',
],
'demo_xml': [
],
'test': [
],
'installable': True,
'auto_install': False,
'external_dependencies': {
'python' : ['openerplib'],
},
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
63 changes: 63 additions & 0 deletions bin/addons/openupgrade_records/__terp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module Copyright (C) 2012 OpenUpgrade community
# https://launchpad.net/~openupgrade-committers
#
# Contributors:
# Therp BV <http://therp.nl>
#
# 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/>.
#
##############################################################################


{
'name': 'OpenUpgrade Records',
'version': '0.2',
'category': 'Normal',
'description': """Allow OpenUpgrade records to be
stored in the database and compare with other servers.
This module depends on OpenERP client lib:
easy_install openerp-client-lib
""",
'author': 'OpenUpgrade Community',
'maintainer': 'OpenUpgrade Community',
'contributors': ['Therp BV'],
'website': 'https://launchpad.net/~openupgrade-committers',
'depends': [],
'init_xml': [],
'update_xml': [
'view/openupgrade_record.xml',
'view/comparison_config.xml',
'view/analysis_wizard.xml',
'view/generate_records_wizard.xml',
'view/install_all_wizard.xml',
'security/ir.model.access.csv',
],
'demo_xml': [
],
'test': [
],
'installable': True,
'auto_install': False,
'external_dependencies': {
'python' : ['openerplib'],
},
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
2 changes: 2 additions & 0 deletions bin/addons/openupgrade_records/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import apriori
import compare
9 changes: 9 additions & 0 deletions bin/addons/openupgrade_records/lib/apriori.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
""" Encode any known changes to the database here
to help the matching process
"""

renamed_modules = {
}

renamed_models = {
}
Loading

0 comments on commit 6b4cc52

Please sign in to comment.