[MERGE]
[odoo/odoo.git] / addons / base_module_quality / structure_test / structure_test.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22 import os
23
24 from tools.translate import _
25 from base_module_quality import base_module_quality
26
27 class quality_test(base_module_quality.abstract_quality_check):
28
29     def __init__(self):
30         super(quality_test, self).__init__()
31         self.name = _("Structure Test")
32         self.note = _("""
33 This test checks if the module satisfy tiny structure
34 """)
35         self.bool_installed_only = False
36         self.result_dict = {}
37         self.module_score = 0.0
38         self.count = 0
39         self.recur = True
40         return None
41
42     def run_test_struct(self, cr, uid, module_path):
43         len_module = len(module_path.split('/'))
44         module_name = module_path.split('/')
45         module_name = module_name[len_module-1]
46         list_files = os.listdir(module_path)
47         self.result_dict = {}
48         f_list = []
49         module_dict = {}
50         module_dict['module'] = []
51 #        count = 0
52         final_score = 0.0
53
54         if not module_name.islower():
55             self.result_dict[module_name] = [module_name, 'Module name should have in lowercase']
56         for file_struct in list_files:
57             if file_struct.split('.')[-1] != 'pyc':
58                 path = os.path.join(module_path, file_struct)
59                 if file_struct == 'wizard' and os.path.isdir(path):
60                     module_dict[file_struct] = []
61                 elif file_struct == 'report' and os.path.isdir(path):
62                     module_dict[file_struct] = []
63                 elif file_struct == 'security' and os.path.isdir(path):
64                     module_dict[file_struct] = []
65                 elif file_struct == 'process' and os.path.isdir(path):
66                     module_dict[file_struct] = []
67                 elif file_struct != 'i18n' and os.path.isdir(path):
68 #                    self.counter += 1
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', '__terp__.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 #        self.result_details += self.get_result_details(self.result_dict)
138         return None
139
140     def run_test(self, cr, uid, module_path):
141         self.run_test_struct(cr, uid, module_path)
142         if self.score != 1:
143             self.result_details = self.get_result_details(self.result_dict)
144         return None
145
146
147     def get_result(self, dict_struct):
148         header = ('{| border="1" cellspacing="0" cellpadding="5" align="left" \n! %-40s \n! %-10s \n', [_('Module Name'), _('Result in %')])
149         if not self.error:
150             return self.format_table(header, data_list=dict_struct)
151         return ""
152
153     def get_score(self, module_list, original_files, mod_folder=''):
154         score = 0
155         module_length = len(module_list)
156         for i in module_list:
157             if i in original_files:
158                 score += 1
159             else:
160                 if mod_folder != 'wizard/':
161                     self.result_dict[i] = [mod_folder + i, 'File name does not follow naming standards.']
162                     score -= 1
163                     module_length -= 1
164         score = module_length and float(score) / float(module_length)
165         return score
166
167     def get_result_details(self, dict_struct):
168         str_html = '''<html><head></head><body><table>'''
169         header = ('<tr><th>%s</th><th>%s</th></tr>', [_('File Name'), _('Feedback about structure of module')])
170         if not self.error:
171             res = str_html + self.format_html_table(header, data_list=dict_struct) + '</table></body></html>'
172             return res
173         return ""
174
175 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
176