small changes
[odoo/odoo.git] / addons / base_module_quality / terp_test / terp_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
23 import os
24 import tools
25
26 from base_module_quality import base_module_quality
27 import pooler
28 import re
29
30 class quality_test(base_module_quality.abstract_quality_check):
31
32     def __init__(self):
33 #        '''
34 #        This test checks the quality of __terp__.py file in the selected module.
35 #        '''
36         super(quality_test, self).__init__()
37         self.name = _("Terp Test")
38         self.bool_installed_only = False
39         self.no_terp = False
40         self.ponderation = 2
41
42         return None
43
44     def run_test(self, cr, uid, module_path):
45         list_files = os.listdir(module_path)
46         for i in list_files:
47             path = os.path.join(module_path, i)
48             if os.path.isdir(path):
49                 for j in os.listdir(path):
50                     list_files.append(os.path.join(i, j))
51
52         n = 0
53         score = 0.0
54         feel_good_factor = 0
55         feel_bad_factor = 0
56         if '__terp__.py' not in list_files:
57             self.no_terp = True
58         if self.no_terp:
59             self.result += "The module does not contain the __terp__.py file"
60
61         terp_file = os.path.join(module_path,'__terp__.py')
62         res = eval(tools.file_open(terp_file).read())
63
64         terp_keys = ['category', 'name', 'description', 'author', 'website', 'update_xml', 'init_xml', 'depends', 'version', 'active', 'installable', 'demo_xml']
65
66         for key in terp_keys:
67             if key in res:
68                 feel_good_factor += 1
69                 if isinstance(res[key],(str,unicode)):
70                     if not res[key]:
71                         feel_bad_factor += 1
72                     else:
73                         if key == 'description' and res[key] and len(str(res[key]))>=25:
74                             feel_good_factor += 1
75                             if res['description'].count('\n') >= 4:# description contains minimum 5 lines
76                                 feel_good_factor += 1
77                         if key == 'website':
78                             ptrn = re.compile('https?://[\w\.]*') # reg ex matching on temporary basis.
79                             result = ptrn.search(str(res[key]))
80                             if result:
81                                 feel_good_factor += 1
82             else:
83                 feel_bad_factor += 1
84
85         self.score = round((feel_good_factor) / float(feel_good_factor + feel_bad_factor),2)
86         self.result += "\nThis test checks if the module satisfies the current coding standard for __terp__.py file used by OpenERP."
87 #        self.result += "__terp__.py : "+ str(self.score) + "/10\n"
88         return None
89
90     def get_result(self, cr, uid, module_path, module_state):
91 #        self.run_test(cr, uid, module_path)
92 #        summary = "\n===TERP Test===:\n"
93         if self.no_terp:
94            summary += """
95 The module does not contain the __terp__.py file.\n\n """
96 #        else:
97 #            summary += """
98 #    This test checks if the module satisfies the current coding standard for __terp__.py file used by OpenERP.
99 #    """ + "Score: " + str(self.score) + "/10\n"
100         return summary
101
102     def get_result_details(self):
103         detail = "\n===TERP Test===\n" + self.result
104         return detail
105
106
107
108 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: