[IMP]: base_module_quality: Apply minor changes
[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 #        count = 0
54         final_score = 0.0
55
56         if not module_name.islower():
57             self.result_dict[module_name] = [module_name, 'Module name should have in lowercase']
58         for file_struct in list_files:
59             if file_struct.split('.')[-1] != 'pyc':
60                 path = os.path.join(module_path, file_struct)
61                 if file_struct == 'wizard' and os.path.isdir(path):
62                     module_dict[file_struct] = []
63                 elif file_struct == 'report' and os.path.isdir(path):
64                     module_dict[file_struct] = []
65                 elif file_struct == 'security' and os.path.isdir(path):
66                     module_dict[file_struct] = []
67                 elif file_struct == 'process' and os.path.isdir(path):
68                     module_dict[file_struct] = []
69                 elif file_struct != 'i18n' and os.path.isdir(path):
70 #                    self.counter += 1
71                     self.run_test(cr, uid, path)
72                 module_dict['module'].append(file_struct)
73                 f_list.append(file_struct)
74         for i in f_list:
75             path = os.path.join(module_path, i)
76             if os.path.isdir(path) and not i == 'i18n':
77                 for j in os.listdir(path):
78                     if i in ['report', 'wizard', 'security', 'module', 'process'] and j.split('.')[-1] != 'pyc':
79                         module_dict[i].append(j)
80                         f_list.append(os.path.join(i, j))
81
82         # module files calculation (module.py,module_view.xml,etc..)
83         com_list = ['_unit_test.xml', '.py', '_view.xml', '_workflow.xml' , '_wizard.xml', '_report.xml', '_data.xml', '_demo.xml', '_security.xml', '_sequence.xml', '_graph.xml']
84         com_list = map(lambda x: module_name + x, com_list)
85         main_file = ['__init__.py', '__openerp__.py']
86         com_list.extend(main_file)
87         module_dict['module'] = filter(lambda x: len(x.split(".")) > 1, module_dict['module'])
88         score = self.get_score(module_dict['module'], com_list)
89         self.count = self.count + 1
90         final_score += score
91
92         # report folder checking...
93         if module_dict.has_key('report'):
94             report_pys = filter(lambda x: (len(x.split('.'))>1 and x.split('.')[1] == 'py') and x != '__init__.py', module_dict['report'])
95             report_pys = map(lambda x:x.split('.')[0], report_pys)
96             reports = ['.sxw', '.rml', '.xsl', '.py', '.xml']
97             org_list_rep = []
98             for pys in report_pys:
99                 for report in reports:
100                     org_list_rep.append(pys + report)
101             org_list_rep.append('__init__.py')
102             score_report = self.get_score(module_dict['report'], org_list_rep, 'report/')
103             self.count = self.count + 1
104             final_score += score_report
105
106         # wizard folder checking...
107         if module_dict.has_key('wizard'):
108             wizard_pys = filter(lambda x: (len(x.split('.'))>1 and x.split('.')[1] == 'py') and x != '__init__.py', module_dict['wizard'])
109             wizard_pys = map(lambda x:x.split('.')[0], wizard_pys)
110             wizards = ['_view.xml', '_workflow.xml', '.py']
111             org_list_wiz = []
112             for pys in wizard_pys:
113                 for report in wizards:
114                     org_list_wiz.append(pys + report)
115             org_list_wiz.append('__init__.py')
116             score_wizard = self.get_score(module_dict['wizard'], org_list_wiz, 'wizard/')
117             self.count = self.count + 1
118             final_score += score_wizard
119
120         # security folder checking...
121         if module_dict.has_key('security'):
122             security = [module_name + '_security.xml']
123             security.extend(['ir.model.access.csv'])
124             score_security = self.get_score(module_dict['security'], security, 'security/')
125             self.count = self.count + 1
126             final_score += score_security
127
128         # process folder checking...
129         if module_dict.has_key('process'):
130             process = [module_name + '_process.xml']
131             score_process = self.get_score(module_dict['process'], process, 'process/')
132             self.count = self.count + 1
133             final_score += score_process
134
135         # final score
136         self.module_score +=  final_score
137         self.score = self.module_score / (self.count)
138         self.result = self.get_result({ module_name: [module_name, int(self.score*100)]})
139         return None
140
141     def run_test(self, cr, uid, module_path):
142         self.run_test_struct(cr, uid, module_path)
143         if self.score*100 < self.min_score:
144             self.message = 'Score is below than minimal score(%s%%)' % self.min_score
145         else:
146             self.message = ''
147         if self.score != 1:
148             self.result_details = self.get_result_details(self.result_dict)
149         return None
150
151
152     def get_result(self, dict_struct):
153         header = ('{| border="1" cellspacing="0" cellpadding="5" align="left" \n! %-40s \n! %-10s \n', [_('Module Name'), _('Result in %')])
154         if not self.error:
155             return self.format_table(header, data_list=dict_struct)
156         return ""
157
158     def get_score(self, module_list, original_files, mod_folder=''):
159         score = 0
160         module_length = len(module_list)
161         for i in module_list:
162             if i in original_files:
163                 score += 1
164             else:
165                 if mod_folder != 'wizard/':
166                     self.result_dict[i] = [mod_folder + i, 'File name does not follow naming standards.']
167                     score -= 1
168                     module_length -= 1
169         score = module_length and float(score) / float(module_length)
170         return score
171
172     def get_result_details(self, dict_struct):
173         str_html = '''<html><head>%s</head><body><table class="tablestyle">'''%(self.get_style())
174         header = ('<tr><th class="tdatastyle">%s</th><th class="tdatastyle">%s</th></tr>', [_('File Name'), _('Feedback about structure of module')])
175         if not self.error:
176             res = str_html + self.format_html_table(header, data_list=dict_struct) + '</table></body></html>'
177             res = res.replace('''<td''', '''<td class="tdatastyle" ''')
178             return res
179         return ""
180
181 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: