1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
24 from tools.translate import _
25 from base_module_quality import base_module_quality
28 class quality_test(base_module_quality.abstract_quality_check):
31 super(quality_test, self).__init__()
32 self.name = _("Structure Test")
34 This test checks if the module satisfy tiny structure
36 self.bool_installed_only = False
38 self.module_score = 0.0
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)
52 module_dict['module'] = []
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)
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))
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
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']
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
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']
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
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
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
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)]})
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
146 self.result_details = self.get_result_details(self.result_dict)
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 %')])
153 return self.format_table(header, data_list=dict_struct)
156 def get_score(self, module_list, original_files, mod_folder=''):
158 module_length = len(module_list)
159 for i in module_list:
160 if i in original_files:
163 if mod_folder != 'wizard/':
164 self.result_dict[i] = [mod_folder + i, 'File name does not follow naming standards.']
167 score = module_length and float(score) / float(module_length)
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')])
174 res = str_html + self.format_html_table(header, data_list=dict_struct) + '</table></body></html>'
175 res = res.replace('''<td''', '''<td class="tdatastyle" ''')
179 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: