report/render: fixes from xrg branch
[odoo/odoo.git] / addons / base_module_quality / structure_test / structure_test.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import os
23
24 from tools.translate import _
25 from base_module_quality import base_module_quality
26 import pooler
27
28 class quality_test(base_module_quality.abstract_quality_check):
29
30     def __init__(self):
31         super(quality_test, self).__init__()
32         self.name = _("Structure Test")
33         self.note = _("""
34 This test checks if the module satisfy tiny structure
35 """)
36         self.bool_installed_only = False
37         self.result_dict = {}
38         self.module_score = 0.0
39         self.count = 0
40         self.recur = True
41         self.min_score = 30
42         return None
43
44     def run_test_struct(self, cr, uid, module_path):
45         len_module = len(module_path.split('/'))
46         module_name = module_path.split('/')
47         module_name = module_name[len_module-1]
48         list_files = os.listdir(module_path)
49         self.result_dict = {}
50         f_list = []
51         module_dict = {}
52         module_dict['module'] = []
53         final_score = 0.0
54
55         if not module_name.islower():
56             self.result_dict[module_name] = [module_name, 'Module name should have in lowercase']
57         for file_struct in list_files:
58             if file_struct.split('.')[-1] != 'pyc':
59                 path = os.path.join(module_path, file_struct)
60                 if file_struct == 'wizard' and os.path.isdir(path):
61                     module_dict[file_struct] = []
62                 elif file_struct == 'report' and os.path.isdir(path):
63                     module_dict[file_struct] = []
64                 elif file_struct == 'security' and os.path.isdir(path):
65                     module_dict[file_struct] = []
66                 elif file_struct == 'process' and os.path.isdir(path):
67                     module_dict[file_struct] = []
68                 elif file_struct != 'i18n' and os.path.isdir(path):
69                     self.run_test(cr, uid, path)
70                 module_dict['module'].append(file_struct)
71                 f_list.append(file_struct)
72         for i in f_list:
73             path = os.path.join(module_path, i)
74             if os.path.isdir(path) and not i == 'i18n':
75                 for j in os.listdir(path):
76                     if i in ['report', 'wizard', 'security', 'module', 'process'] and j.split('.')[-1] != 'pyc':
77                         module_dict[i].append(j)
78                         f_list.append(os.path.join(i, j))
79
80         # module files calculation (module.py,module_view.xml,etc..)
81         com_list = ['_unit_test.xml', '.py', '_view.xml', '_workflow.xml' , '_wizard.xml', '_report.xml', '_data.xml', '_demo.xml', '_security.xml', '_sequence.xml', '_graph.xml']
82         com_list = map(lambda x: module_name + x, com_list)
83         main_file = ['__init__.py', '__openerp__.py']
84         com_list.extend(main_file)
85         module_dict['module'] = filter(lambda x: len(x.split(".")) > 1, module_dict['module'])
86         score = self.get_score(module_dict['module'], com_list)
87         self.count = self.count + 1
88         final_score += score
89
90         # report folder checking...
91         if module_dict.has_key('report'):
92             report_pys = filter(lambda x: (len(x.split('.'))>1 and x.split('.')[1] == 'py') and x != '__init__.py', module_dict['report'])
93             report_pys = map(lambda x:x.split('.')[0], report_pys)
94             reports = ['.sxw', '.rml', '.xsl', '.py', '.xml']
95             org_list_rep = []
96             for pys in report_pys:
97                 for report in reports:
98                     org_list_rep.append(pys + report)
99             org_list_rep.append('__init__.py')
100             score_report = self.get_score(module_dict['report'], org_list_rep, 'report/')
101             self.count = self.count + 1
102             final_score += score_report
103
104         # wizard folder checking...
105         if module_dict.has_key('wizard'):
106             wizard_pys = filter(lambda x: (len(x.split('.'))>1 and x.split('.')[1] == 'py') and x != '__init__.py', module_dict['wizard'])
107             wizard_pys = map(lambda x:x.split('.')[0], wizard_pys)
108             wizards = ['_view.xml', '_workflow.xml', '.py']
109             org_list_wiz = []
110             for pys in wizard_pys:
111                 for report in wizards:
112                     org_list_wiz.append(pys + report)
113             org_list_wiz.append('__init__.py')
114             score_wizard = self.get_score(module_dict['wizard'], org_list_wiz, 'wizard/')
115             self.count = self.count + 1
116             final_score += score_wizard
117
118         # security folder checking...
119         if module_dict.has_key('security'):
120             security = [module_name + '_security.xml']
121             security.extend(['ir.model.access.csv'])
122             score_security = self.get_score(module_dict['security'], security, 'security/')
123             self.count = self.count + 1
124             final_score += score_security
125
126         # process folder checking...
127         if module_dict.has_key('process'):
128             process = [module_name + '_process.xml']
129             score_process = self.get_score(module_dict['process'], process, 'process/')
130             self.count = self.count + 1
131             final_score += score_process
132
133         # final score
134         self.module_score +=  final_score
135         self.score = self.module_score / (self.count)
136         self.result = self.get_result({ module_name: [module_name, int(self.score*100)]})
137         return None
138
139     def run_test(self, cr, uid, module_path):
140         self.run_test_struct(cr, uid, module_path)
141         if self.score*100 < self.min_score:
142             self.message = 'Score is below than minimal score(%s%%)' % self.min_score
143         else:
144             self.message = ''
145         if self.score != 1:
146             self.result_details = self.get_result_details(self.result_dict)
147         return None
148
149
150     def get_result(self, dict_struct):
151         header = ('{| border="1" cellspacing="0" cellpadding="5" align="left" \n! %-40s \n! %-10s \n', [_('Module Name'), _('Result in %')])
152         if not self.error:
153             return self.format_table(header, data_list=dict_struct)
154         return ""
155
156     def get_score(self, module_list, original_files, mod_folder=''):
157         score = 0
158         module_length = len(module_list)
159         for i in module_list:
160             if i in original_files:
161                 score += 1
162             else:
163                 if mod_folder != 'wizard/':
164                     self.result_dict[i] = [mod_folder + i, 'File name does not follow naming standards.']
165                     score -= 1
166                     module_length -= 1
167         score = module_length and float(score) / float(module_length)
168         return score
169
170     def get_result_details(self, dict_struct):
171         str_html = '''<html><head>%s</head><body><table class="tablestyle">'''%(self.get_style())
172         header = ('<tr><th class="tdatastyle">%s</th><th class="tdatastyle">%s</th></tr>', [_('File Name'), _('Feedback about structure of module')])
173         if not self.error:
174             res = str_html + self.format_html_table(header, data_list=dict_struct) + '</table></body></html>'
175             res = res.replace('''<td''', '''<td class="tdatastyle" ''')
176             return res
177         return ""
178
179 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: