Skip to content

Commit

Permalink
Merge pull request OCA#204 from StefanRijnhart/5.0-backport_records
Browse files Browse the repository at this point in the history
[PRT] Backport improvements to the records module from 8.0
  • Loading branch information
pedrobaeza committed Mar 17, 2015
2 parents 6b4cc52 + 6900bb6 commit b840433
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 166 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
*~
1 change: 0 additions & 1 deletion bin/addons/openupgrade_records/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
import model
import lib
6 changes: 3 additions & 3 deletions bin/addons/openupgrade_records/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module Copyright (C) 2012 OpenUpgrade community
# This module Copyright (C) 2012-2014 OpenUpgrade community
# https://launchpad.net/~openupgrade-committers
#
# Contributors:
Expand All @@ -28,7 +28,7 @@
'name': 'OpenUpgrade Records',
'version': '0.2',
'category': 'Normal',
'description': """Allow OpenUpgrade records to be
'description': """Allow OpenUpgrade records to be
stored in the database and compare with other servers.
This module depends on OpenERP client lib:
Expand Down Expand Up @@ -57,7 +57,7 @@
'installable': True,
'auto_install': False,
'external_dependencies': {
'python' : ['openerplib'],
'python': ['openerplib'],
},
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
6 changes: 3 additions & 3 deletions bin/addons/openupgrade_records/__terp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module Copyright (C) 2012 OpenUpgrade community
# This module Copyright (C) 2012-2014 OpenUpgrade community
# https://launchpad.net/~openupgrade-committers
#
# Contributors:
Expand All @@ -28,7 +28,7 @@
'name': 'OpenUpgrade Records',
'version': '0.2',
'category': 'Normal',
'description': """Allow OpenUpgrade records to be
'description': """Allow OpenUpgrade records to be
stored in the database and compare with other servers.
This module depends on OpenERP client lib:
Expand Down Expand Up @@ -57,7 +57,7 @@
'installable': True,
'auto_install': False,
'external_dependencies': {
'python' : ['openerplib'],
'python': ['openerplib'],
},
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
2 changes: 0 additions & 2 deletions bin/addons/openupgrade_records/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
import apriori
import compare
172 changes: 95 additions & 77 deletions bin/addons/openupgrade_records/lib/compare.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,26 @@
# layouts from the OpenUpgrade server.
#####################################################################

import collections
import copy

try:
from openerp.addons.openupgrade_records.lib import apriori
except ImportError:
from openupgrade_records.lib import apriori

keys = [
'module',
'mode',
'model',
'field',
'type',
'isfunction',
'relation',
'required',
'selection_keys',
'req_default',
'inherits',
]

def module_map(module):
return apriori.renamed_modules.get(
module, module)
return apriori.renamed_modules.get(module, module)


def model_map(model):
return apriori.renamed_models.get(model, model)


IGNORE_FIELDS = [
]


def compare_records(dict_old, dict_new, fields):
"""
Expand All @@ -62,37 +58,41 @@ def compare_records(dict_old, dict_new, fields):
if (module_map(dict_old[field]) != dict_new[field]):
return False
elif field == 'model':
if (apriori.renamed_models.get(
dict_old[field], dict_old[field]) != dict_new[field]):
return False
else:
if dict_old[field] != dict_new[field]:
if (model_map(dict_old[field]) != dict_new[field]):
return False
elif dict_old[field] != dict_new[field]:
return False
return True


def search(item, item_list, fields):
"""
Find a match of a dictionary in a list of similar dictionaries
with respect to the keys in the 'fields' arguments.
Return the item if found or None.
"""
for i in item_list:
if not compare_records(item, i, fields):
for other in item_list:
if not compare_records(item, other, fields):
continue
return i
return other
# search for renamed fields
if 'field' in fields:
for other in item_list:
if not item['field'] or item['field'] != other.get('oldname'):
continue
if compare_records(dict(item, field=other['field']), other, fields):
return other
return None


def fieldprint(old, new, field, text, reprs):
fieldrepr = "%s (%s)" % (old['field'], old['type'])
repr = '%s / %s / %s' % (
old['module'].ljust(12), old['model'].ljust(24), fieldrepr.ljust(30))
if text:
reprs.setdefault(module_map(old['module']), []).append(
"%s: %s" % (repr, text))
else:
reprs.setdefault(module_map(old['module']), []).append(
"%s: %s is now \'%s\' ('%s')" % (
repr, field, new[field], old[field]))
fullrepr = '%-12s / %-24s / %-30s' % (
old['module'], old['model'], fieldrepr)
if not text:
text = "%s is now '%s' ('%s')" % (field, new[field], old[field])
reprs[module_map(old['module'])].append("%s: %s" % (fullrepr, text))


def report_generic(new, old, attrs, reprs):
for attr in attrs:
Expand All @@ -109,9 +109,13 @@ def report_generic(new, old, attrs, reprs):
else:
text = "not a function anymore"
fieldprint(old, new, None, text, reprs)
else:
if old[attr] != new[attr]:
fieldprint(old, new, attr, None, reprs)
elif attr == 'oldname':
if new.get('oldname') == old['field']:
text = 'was renamed to %s [nothing to to]' % new['field']
fieldprint(old, new, None, text, reprs)
elif old[attr] != new[attr]:
fieldprint(old, new, attr, None, reprs)


def compare_sets(old_records, new_records):
"""
Expand All @@ -121,20 +125,20 @@ def compare_sets(old_records, new_records):
module names as keys. Special case is the 'general' key
which contains overall remarks and matching statistics.
"""
reprs = {'general': []}
reprs = collections.defaultdict(list)

for record in old_records + new_records:
record['matched'] = False
origlen = len(old_records)
new_models = set([ column['model'] for column in new_records ])
old_models = set([ column['model'] for column in old_records ])
new_models = set([column['model'] for column in new_records])
old_models = set([column['model'] for column in old_records])

matched_direct = 0
matched_other_module = 0
matched_other_type = 0
matched_other_name = 0
in_obsolete_models = 0

obsolete_models = []
for model in old_models:
if model not in new_models:
Expand All @@ -149,15 +153,15 @@ def compare_sets(old_records, new_records):
for model in new_models:
if model not in old_models:
reprs['general'].append('new model %s' % model)

def match(match_fields, report_fields, warn=False):
count = 0
for column in copy.copy(old_records):
found = search(column, new_records, match_fields)
if found:
if warn:
pass
#print "Tentatively"
# print "Tentatively"
report_generic(found, column, report_fields, reprs)
old_records.remove(column)
new_records.remove(found)
Expand All @@ -166,20 +170,23 @@ def match(match_fields, report_fields, warn=False):

matched_direct = match(
['module', 'mode', 'model', 'field'],
['relation', 'type', 'selection_keys', 'inherits', 'isfunction', 'required'])
['relation', 'type', 'selection_keys', 'inherits',
'isfunction', 'required', 'oldname'])

# other module, same type and operation
matched_other_module = match(
['mode', 'model', 'field', 'type'],
['module', 'relation', 'selection_keys', 'inherits', 'isfunction', 'required'])
['module', 'relation', 'selection_keys', 'inherits',
'isfunction', 'required', 'oldname'])

# other module, same operation, other type
matched_other_type = match(
['mode', 'model', 'field'],
['relation', 'type', 'selection_keys', 'inherits', 'isfunction', 'required'])
['relation', 'type', 'selection_keys', 'inherits',
'isfunction', 'required', 'oldname'])

# fields with other names
#matched_other_name = match(
# matched_other_name = match(
# ['module', 'type', 'relation'],
# ['field', 'relation', 'type', 'selection_keys',
# 'inherits', 'isfunction', 'required'], warn=True)
Expand All @@ -190,51 +197,62 @@ def match(match_fields, report_fields, warn=False):
]
for column in old_records:
# we do not care about removed function fields
if not column['isfunction']:
if column['mode'] == 'create':
column['mode'] = ''
fieldprint(
column, None, None, "DEL " + ", ".join(
[k + ': ' + str(column[k]) for k in printkeys if column[k]]
), reprs)
if column['isfunction'] or column['field'] in IGNORE_FIELDS:
continue
if column['mode'] == 'create':
column['mode'] = ''
fieldprint(
column, None, None, "DEL " + ", ".join(
[k + ': ' + str(column[k]) for k in printkeys if column[k]]
), reprs)

for column in new_records:
# we do not care about newly added function fields
if not column['isfunction']:
if column['mode'] == 'create':
column['mode'] = ''
fieldprint(
column, None, None, "NEW " + ", ".join(
[k + ': ' + str(column[k]) for k in printkeys if column[k]]
), reprs)
if column['isfunction'] or column['field'] in IGNORE_FIELDS:
continue
if column['mode'] == 'create':
column['mode'] = ''
fieldprint(
column, None, None, "NEW " + ", ".join(
[k + ': ' + str(column[k]) for k in printkeys if column[k]]
), reprs)

for line in [
"# %d fields matched," % (origlen - len(old_records)),
"# Direct match: %d" % matched_direct,
"# Found in other module: %d" % matched_other_module,
"# Found with different type: %d" % matched_other_type,
"# Found with different name: %d" % matched_other_name,
"# In obsolete models: %d" % in_obsolete_models,
"# Not matched: %d" % len(old_records),
"# New columns: %d" % len(new_records),
]:
"# %d fields matched," % (origlen - len(old_records)),
"# Direct match: %d" % matched_direct,
"# Found in other module: %d" % matched_other_module,
"# Found with different type: %d" % matched_other_type,
"# Found with different name: %d" % matched_other_name,
"# In obsolete models: %d" % in_obsolete_models,
"# Not matched: %d" % len(old_records),
"# New columns: %d" % len(new_records)
]:
reprs['general'].append(line)
return reprs


def compare_xml_sets(old_records, new_records):
reprs = {}
reprs = collections.defaultdict(list)
match_fields = ['module', 'model', 'name']
for column in copy.copy(old_records):
found = search(column, new_records, match_fields)
if found:
old_records.remove(column)
new_records.remove(found)
for entry in sorted(
old_records, key=lambda k: '%s%s' % (k['model'].ljust(128), k['name'])):
reprs.setdefault(module_map(entry['module']), []).append(
'deleted xml-id of model %s: %s' % (entry['model'], entry['name']))
for entry in sorted(
new_records, key=lambda k: '%s%s' % (k['model'].ljust(128), k['name'])):
reprs.setdefault(module_map(entry['module']), []).append(
'new xml-id of model %s: %s' % (entry['model'], entry['name']))

for record in old_records:
record['old'] = True
for record in new_records:
record['new'] = True

sorted_records = sorted(
old_records + new_records,
key=lambda k: (k['model'], 'old' in k, k['name'])
)
for entry in sorted_records:
if 'old' in entry:
content = 'DEL %(model)s: %(name)s' % entry
elif 'new' in entry:
content = 'NEW %(model)s: %(name)s' % entry
reprs[module_map(entry['module'])].append(content)
return reprs
1 change: 0 additions & 1 deletion bin/addons/openupgrade_records/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
import analysis_wizard
import generate_records_wizard
import install_all_wizard

Loading

0 comments on commit b840433

Please sign in to comment.