-
Notifications
You must be signed in to change notification settings - Fork 2
/
odoo.diff-records
executable file
·90 lines (79 loc) · 3.38 KB
/
odoo.diff-records
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#! /usr/bin/env python2
"""Print the differences between a pair of records.
"""
import sys
import argparse
from oekit.oe_client_env import OEClientEnv
from csvkit.unicsv import UnicodeCSVWriter
class OdooDiffDB(object):
def __init__(self, options):
self.options = options
def get_left_model(self):
return self._get_model(self.options.left_db)
def get_right_model(self):
return self._get_model(self.options.right_db)
def _get_model(self, dbarg):
env = OEClientEnv()
kwargs = {}
if dbarg:
kwargs['dbname'] = dbarg
return env.get_proxy(**kwargs).get_model(self.options.model)
class OdooRecordDiff(object):
def __init__(self, options):
self.options = options
self.diff_db = OdooDiffDB(options)
def main(self):
"""Main program."""
left_model = self.diff_db.get_left_model()
right_model = self.diff_db.get_right_model()
left_fields = left_model.execute('fields_get')
right_fields = right_model.execute('fields_get')
common_field_names = set(left_fields) & set(right_fields)
left_only_field_names = set(left_fields) - set(right_fields)
if left_only_field_names:
sys.stderr.write("Fields only on left: {}\n".format(', '.join(left_only_field_names)))
right_only_field_names = set(right_fields) - set(left_fields)
if right_only_field_names:
sys.stderr.write( "Fields only on right: {}\n".format(', '.join(right_only_field_names)) )
field_names = sorted(common_field_names)
left = left_model.read(self.options.left_id, field_names)
if not left:
sys.stderr.write("Left record {} does not exist\n".format(self.options.left_id))
right = right_model.read(self.options.right_id, field_names)
if not right:
sys.stderr.write("Right record {} does not exist\n".format(self.options.right_id))
if not (left or right):
return
writer = UnicodeCSVWriter(sys.stdout)
if self.options.header:
if not right:
writer.writerow(['field', 'left'])
elif not left:
writer.writerow(['field', 'right'])
else:
writer.writerow(['field', 'left', 'right'])
for field in field_names:
if not right:
writer.writerow([field, left[field]])
elif not left:
writer.writerow([field, right[field]])
elif left[field] != right[field]:
writer.writerow([field, left[field], right[field]])
return
def get_options():
"""Get options for the script."""
parser = argparse.ArgumentParser(
description="print the differences between a pair of records",
)
# parser.add_argument() calls here
parser.add_argument('model', help='e.g. res.partner')
parser.add_argument('left_id', type=int, help='e.g. 10')
parser.add_argument('right_id', type=int, help='e.g. 20')
parser.add_argument('--left-db', help='Specify name of left database')
parser.add_argument('--right-db', help='Specify name of right database')
parser.add_argument('--no-header', '-H', dest='header', action='store_false', help='leave out the header')
options = parser.parse_args()
# extra processing of options here
return options
if __name__ == "__main__":
OdooRecordDiff(get_options()).main()