quality module
[odoo/odoo.git] / addons / base_module_quality / pylint_test / pylint_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
24 import os
25 from tools import config
26 from tools.translate import _
27
28 from base_module_quality import base_module_quality
29
30
31 class quality_test(base_module_quality.abstract_quality_check):
32
33     def __init__(self):
34         super(quality_test, self).__init__()
35         self.name = _("Pylint Test")
36         self.note = _("""This test uses Pylint and checks if the module satisfies the coding standard of Python. See http://www.logilab.org/project/name/pylint for further info.\n """)
37         self.bool_installed_only = False
38         self.ponderation = 1.0
39         self.result = ""
40         self.result_details = ""
41         return None
42
43     def run_test(self, cr, uid, module_path):
44         config_file_path = config['addons_path']+'/base_module_quality/pylint_test/pylint_test_config.txt'
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         dict = {}
55         self.result_details += '''<html>
56         <head>
57             <link rel="stylesheet" type="text/css" href="/tg_widgets/openerp/css/wiki.css" media="all">
58         </head>
59         <body>'''
60         for file in list_files:
61             if file.split('.')[-1] == 'py' and not file.endswith('__init__.py') and not file.endswith('__terp__.py'):
62                 file_path = os.path.join(module_path, file)
63                 try:
64                     res = os.popen('pylint --rcfile=' + config_file_path + ' ' + file_path).read()
65                 except:
66                     self.result += _("Error. Is pylint correctly installed?")+"\n"
67                     break
68                 n += 1
69                 leftchar = -1
70                 while res[leftchar:leftchar+1] != ' ' and leftchar-1 <= 0:
71                     leftchar -= 1
72                 rightchar = -10
73                 while res[rightchar:rightchar+1] != '/' and rightchar+1 <= 0:
74                     rightchar += 1
75                 try:
76                     score += float(res[leftchar+1:rightchar])
77                     #self.result += file + ": " + res[leftchar+1:rightchar] + "/10\n"
78                     dict[file] = [file, res[leftchar+1:rightchar]]
79                 except:
80                     score += 0
81                     #self.result += file + ": "+_("Unable to parse the result. Check the details.")+"\n"
82                     dict[file] = [file, _("Unable to parse the result. Check the details.")]
83                 self.result_details += res.replace('''<div''', '''<div class="wikiwidget readonlyfield"''')
84         self.result_details += '</body></html>'
85         average_score = n and score / n or score
86         self.score = (max(average_score,0)) / 10
87         self.result = self.get_result(dict)
88         return None
89
90     def get_result(self, dict):
91         header = ('{| border="1" cellspacing="0" cellpadding="5" align="left" \n! %-40s \n! %-10s \n', [_('File Name'), _('Result (/10)')])
92         if not self.error:
93             return self.format_table(header, data_list=dict)
94         return ""
95
96 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
97