Skip to content

Instantly share code, notes, and snippets.

@sebalix
Last active June 27, 2017 16:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebalix/8cb0acc2cff419f4529e to your computer and use it in GitHub Desktop.
Save sebalix/8cb0acc2cff419f4529e to your computer and use it in GitHub Desktop.
Script to fetch the schema of all data models from an Odoo server. Then, it can compare two schema to show the differences between them (fields added, modified or removed).
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Script to fetch the schema of all data models from an Odoo server.
Then, it can compare two schema to show the differences between them (fields
added, modified or removed).
"""
import sys
import json
import oerplib
# OpenERP connection
HOST = '10.0.3.31'
PORT = 8069
DB = 'oerplib_test'
USER = 'admin'
PWD = 'admin'
class DictDiffer(object):
"""Calculate the difference between two dictionaries as:
(1) items added
(2) items removed
(3) keys same in both but changed values
(4) keys same in both and unchanged values
"""
def __init__(self, current_dict, past_dict):
self.current_dict, self.past_dict = current_dict, past_dict
self.set_current = set(current_dict.keys())
self.set_past = set(past_dict.keys())
self.intersect = self.set_current.intersection(self.set_past)
def added(self):
return self.set_current - self.intersect
def removed(self):
return self.set_past - self.intersect
def changed(self):
return set(o for o in self.intersect
if self.past_dict[o] != self.current_dict[o])
def unchanged(self):
return set(o for o in self.intersect
if self.past_dict[o] == self.current_dict[o])
def dump_schema():
# OpenERP login
oerp = oerplib.OERP(HOST, DB, port=PORT)
oerp.login(USER, PWD)
model_obj = oerp.get('ir.model')
records = model_obj.search_read([], ['model'])
schemas = {}
for record in records:
try:
schema = oerp.execute(record['model'], 'fields_get')
except oerplib.error.Error as exc:
pass
else:
schemas[record['model']] = schema
print(json.dumps(schemas, indent=2))
def compare_schemas():
with open(sys.argv[1], mode='r') as file_s1:
s1 = json.load(file_s1)
with open(sys.argv[2], mode='r') as file_s2:
s2 = json.load(file_s2)
diff_models = DictDiffer(s2, s1)
print("---- ADDED ----")
for model in diff_models.added():
print(" - %s" % model)
print("\n---- REMOVED ----")
for model in diff_models.removed():
print(" - %s" % model)
print("\n---- MODIFIED ----")
for model in diff_models.changed():
print(" - %s" % model)
subdiff = DictDiffer(s2[model], s1[model])
for field in subdiff.added():
print("%sA %s" % (' ' * 8, field))
for field in subdiff.removed():
print("%sR %s" % (' ' * 8, field))
for field in subdiff.changed():
print("%sM %s" % (' ' * 8, field))
attrs_diff = DictDiffer(s2[model][field], s1[model][field])
for attr in attrs_diff.added():
print("%sA %s" % (' ' * 12, attr))
for attr in attrs_diff.removed():
print("%sR %s" % (' ' * 12, attr))
for attr in attrs_diff.changed():
print("%sM %s (%s => %s)" % (
' ' * 12, attr,
repr(s1[model][field][attr]),
repr(s2[model][field][attr])))
if __name__ == '__main__':
if len(sys.argv) == 3:
compare_schemas()
else:
dump_schema()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment