This repository has been archived by the owner on Dec 10, 2023. It is now read-only.
/
PyChecker.py
233 lines (201 loc) · 7.21 KB
/
PyChecker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------
# Copyright (c) 2010-2019 Denis Machard
# This file is part of the extensive automation project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
# -------------------------------------------------------------------
"""
Module to make some code analysis
"""
from pylint import lint
from pylint.reporters.text import TextReporter
import os
import sys
import copy
try:
xrange
except NameError: # support python3
xrange = range
# unicode = str with python3
if sys.version_info > (3,):
unicode = str
# retrieve the path
arg = sys.argv[0]
pathname = os.path.dirname(arg)
PATH = os.path.abspath(pathname)
GenerateRepport=False
if len(sys.argv) == 2:
if sys.argv[1] == "--report":
GenerateRepport = True
class WritableObject(object):
"""
dummy output stream for pylint
"""
def __init__(self):
"""
Constructor
"""
self.content = []
def write(self, st):
"""
dummy write
"""
self.content.append(st)
def read(self):
"""
dummy read
"""
return self.content
# All codes: http://docs.pylint.org/features.html
# http://pylint-messages.wikidot.com/all-codes
# Argument for pylint
# * [R]efactor for a "good practice" metric violation
# * [C]onvention for coding standard violation
# * [W]arning for stylistic problems, or minor programming issues
# * [E]rror for important programming issues (i.e. most probably bug)
# * [F]atal for errors which prevented further processing
# disable all check and enable error and fatal messages
# Astroid was not able to interpret all possible types of this variable, so we disable the following code
# E1101 = no-member
# E1103 = maybe-no-member
# E0611 = no-name-in-module
# Enable following because code can contains error:
# W0631: Using possibly undefined loop variable
# W0633: Attempting to unpack a non-sequence%s
ARGS = [ "--disable=all", "--enable=E",
"--enable=F", "--disable=E1101" ,
"--disable=E0611", "--disable=E1103",
"--enable=W0631", "--enable=C0111",
"--enable=C0112", "--disable=W0633"
]
# report generation or not ?
if GenerateRepport:
ARGS.extend( [ "-r", "y" ] )
else:
ARGS.extend( [ "-r", "n" ] )
pathName = "code_analysis"
prefixName = "Code"
# exceptions, because there are generated automatically
EXCEPTIONS = [
( "Resources.py", "" ),
( "Translations.py", "" ),
]
print("Checking code according to PEP8 - Style Guide for Python Code")
print("Analysing ERROR or FATAL messages...")
# walk on the project folder and returns just python
# files on a list
result = [os.path.join(dp, f) for dp, dn, filenames in os.walk(PATH) for f in filenames if os.path.splitext(f)[1] == '.py']
print("%s files to inspect" % len(result))
# nb errors counters
nbAllError = 0
nbAllFatal = 0
nbAllWarning = 0
nbAllRefactor = 0
nbAllConvention = 0
nbTot = 0
# note on 10
notes = []
# creating the output folder
if not GenerateRepport:
os.mkdir( "%s\\Scripts\\%s\\" % (PATH,pathName) )
nbExceptions = 0
for filename in result:
print("Inspecting the file %s" % filename)
# checking files exception and update pylint arguments if needed
ARGS_RUN = copy.copy(ARGS)
exceptDetected = False
for val in EXCEPTIONS:
fName, fMsg = val
if fName == os.path.basename(filename):
if not len(fMsg):
exceptDetected = True
break
ARGS_RUN.append(fMsg)
if exceptDetected:
nbExceptions += 1
continue
print(ARGS_RUN)
# execute pylint to analyze the code
pylint_output = WritableObject()
lint.Run([filename]+ARGS_RUN, reporter=TextReporter(pylint_output), exit=False)
if GenerateRepport:
# extract the note
note = 0
for line in pylint_output.read():
if line.startswith("Your code has been rated at"):
note = line.split("Your code has been rated at ")[1].split('/10')[0]
notes.append( float(note) )
break
else:
nbTotError = 0
nbTotFatal = 0
nbTotWarning = 0
nbTotRefactor = 0
nbTotConvention = 0
nbTot = 0
if not len( pylint_output.read() ):
print('\tNo error or fatal message detected')
else:
f = open( "%s\\Scripts\\%s\\%s_%s.log" % (PATH,
pathName,
prefixName,
os.path.basename(filename) ), 'w')
f.write( ''.join(pylint_output.read()) )
f.close()
for line in pylint_output.read():
if line.startswith("E:"): nbTotError += 1
if line.startswith("F:"): nbTotFatal += 1
if line.startswith("W:"): nbTotWarning += 1
if line.startswith("R:"): nbTotRefactor += 1
if line.startswith("C:"): nbTotConvention += 1
if nbTotError:
print('\t%s error(s) detected' % (nbTotError))
nbAllError += nbTotError
if nbTotFatal:
print('\t%s fatal(s) detected' % (nbTotFatal))
nbAllFatal += nbTotFatal
if nbTotWarning:
print('\t%s warning(s) detected' % (nbTotWarning))
nbAllWarning += nbTotWarning
if nbTotRefactor:
print('\t%s refactor(s) detected' % (nbTotRefactor))
nbAllRefactor += nbTotRefactor
if nbTotConvention:
print('\t%s convention(s) detected' % (nbTotConvention))
nbAllConvention += nbTotConvention
# final report
if not GenerateRepport:
print("")
print("To validate the code:")
print("\t- No error message")
print("\t- No fatal message")
print("If it's the case, your code is no valid, please to fix it")
print("")
print("Total: %s error(s) detected" % nbAllError)
print("Total: %s fatal(s) detected" % nbAllFatal)
print("Total: %s warning(s) detected" % nbAllWarning)
print("Total: %s refactor(s) detected" % nbAllRefactor)
print("Total: %s convention(s) detected" % nbAllConvention)
else:
print("")
print("Criteria:")
print("\t- No error message")
print("\t- No fatal message")
print("If the note is not equal to 10, please to review your code")
print("")
print("Global evaluation: %s/10" % (sum(notes) / (len(result) - nbExceptions) ))