-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck
executable file
·179 lines (150 loc) · 5.63 KB
/
check
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
import sys
dbname = sys.argv[1]
config_file = sys.argv[2]
dbname2 = sys.argv[3]
from trytond.config import config as CONFIG
CONFIG.update_etc(config_file)
from trytond.transaction import Transaction
from trytond.pool import Pool
import logging
from lxml import etree
import yaml
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
from blessings import Terminal
t = Terminal()
Pool.start()
pool = Pool(dbname)
pool.init()
context = {'company': 1}
try:
# Using LibYaml-based parser if available because it is much faster.
from yaml import CSafeDumper as SafeDumper
except ImportError:
from yaml import SafeDumper
def to_yaml(obj, output_file, padding=None, separator=None):
res = yaml.dump(obj, output_file, Dumper=SafeDumper, default_flow_style=False)
if padding:
res = '\n'.join([padding + x for x in
res.splitlines()])
if separator:
res = separator.join(res.splitlines())
return res
def download_fields_view_get():
'''
Create a file with result of fields_view_get to can diff easily
'''
logger.info(t.yellow('Checking fields_view_get...'))
version = '4.8'
filename = "%s_%s"%(dbname, version)
log_file = open(filename, 'w')
with Transaction().start(dbname, 1):
View = pool.get('ir.ui.view')
views = View.search([
('model', '!=', ''),
], order=[('id', 'asc')])
for view in views:
if view.inherit and view.inherit.model == view.model:
view_id = view.inherit.id
else:
view_id = view.id
model = view.model
Model = pool.get(model)
res = None
if model in ('project.test.build.group'):
continue
try:
res = Model.fields_view_get(view_id)
except ValueError:
logging.error(t.red('ERROR PROCESSING VIEW'))
logging.error(t.red('View ID: %d; Model: %s' % (view.id,
view.model)))
logging.error(t.red('Arch:\n%s' % view.arch))
raise
r = res.copy()
del r['arch']
r['fields'] = r['fields'].keys()
to_yaml(r, log_file)
def check_data():
'''
Test that all records can be read
'''
logger.info(t.yellow('Checking data...'))
with Transaction().start(dbname, 0):
Company = pool.get('company.company')
companies = [x.id for x in Company.search([])]
for company in companies:
logger.info('Checking data on %s...' % company)
with Transaction().start(dbname, 0, context={
'company': company,
}):
for model in pool._pool[dbname]['model'].keys():
logger.info('Company: %d, Model: %s...' % (model, company))
Model = pool.get(model)
if not hasattr(Model, 'search'):
continue
for x in Model.search([]):
# We're not loading lazy fields
pass
def check_view():
'Test validity of all views of the module'
logger.info(t.yellow('Checking views...'))
with Transaction().start(dbname, 1):
View = pool.get('ir.ui.view')
views = View.search([
('model', '!=', ''),
])
for view in views:
if view.inherit and view.inherit.model == view.model:
view_id = view.inherit.id
else:
view_id = view.id
model = view.model
Model = pool.get(model)
try:
res = Model.fields_view_get(view_id)
except ValueError:
logging.error(t.red('ERROR PROCESSING VIEW'))
logging.error(t.red('View ID: %d; Model: %s' % (view.id,
view.model)))
logging.error(t.red('Arch:\n%s' % view.arch))
raise
assert res['model'] == model
tree = etree.fromstring(res['arch'])
validator = etree.RelaxNG(etree=View.get_rng(res['type']))
# Don't use assert_ because 2to3 convert to assertTrue
validator.assertValid(tree)
tree_root = tree.getroottree().getroot()
for element in tree_root.iter():
if element.tag in ('field', 'label', 'separator', 'group'):
for attr in ('name', 'icon'):
field = element.get(attr)
if field:
assert field in res['fields'], (
'Missing field: %s' % field)
def check_model_access():
'Test missing default model access'
logger.info(t.yellow('Checking model access...'))
with Transaction().start(dbname, 1):
Access = pool.get('ir.model.access')
no_groups = {a.model.name for a in Access.search([
('group', '=', None),
])}
with_groups = {a.model.name for a in Access.search([
('group', '!=', None),
])}
assert no_groups >= with_groups, (
'Model "%(models)s" are missing a default access' % {
'models': list(with_groups - no_groups), })
for check in (download_fields_view_get, check_model_access, check_view, check_data):
try:
check()
except:
logging.exception(t.red('Check failed.'))
pass