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'] = []
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):
71 self.run_test(cr, uid, path)
72 module_dict['module'].append(file_struct)
73 f_list.append(file_struct)
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))
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
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']
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
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']
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
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
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
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)]})
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
148 self.result_details = self.get_result_details(self.result_dict)
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 %')])
155 return self.format_table(header, data_list=dict_struct)
158 def get_score(self, module_list, original_files, mod_folder=''):
160 module_length = len(module_list)
161 for i in module_list:
162 if i in original_files:
165 if mod_folder != 'wizard/':
166 self.result_dict[i] = [mod_folder + i, 'File name does not follow naming standards.']
169 score = module_length and float(score) / float(module_length)
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')])
176 res = str_html + self.format_html_table(header, data_list=dict_struct) + '</table></body></html>'
177 res = res.replace('''<td''', '''<td class="tdatastyle" ''')
181 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: