forked from daos-stack/daos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_script.py
executable file
·326 lines (284 loc) · 11.5 KB
/
check_script.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python
# Copyright (c) 2016-2019 Intel Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Simple script wrapper for checking SCons files"""
from __future__ import print_function
import re
import os
import sys
import argparse
import subprocess
import tempfile
import errno
#pylint: disable=import-error
#pylint: disable=no-name-in-module
from distutils.spawn import find_executable
#pylint: enable=import-error
#pylint: enable=no-name-in-module
class WrapScript():
"""Create a wrapper for a scons file and maintain a line mapping"""
def __init__(self, fname):
old_lineno = 1
new_lineno = 1
self.line_map = {}
outfile = open("script", "w")
infile = open(fname, "r")
scons_header = False
for line in infile.readlines():
outfile.write(line)
self.line_map[new_lineno] = old_lineno
old_lineno += 1
new_lineno += 1
match = re.search(r'^(\s*)Import\(.(.*).\)', line)
if match:
if not scons_header:
scons_header = True
self.write_header(outfile)
variables = []
for var in match.group(2).split():
newvar = var.strip("\", '")
variables.append(newvar)
new_lineno += self.write_variables(outfile, match.group(1),
variables)
match = re.search(r'^(\s*)Export\(.(.*).\)', line)
if not match:
match = re.search(r'^(\s*).*exports=[.(.*).]', line)
if match:
if not scons_header:
scons_header = True
new_lineno += self.write_header(outfile)
variables = []
for var in match.group(2).split():
newvar = var.strip("\", '")
variables.append(newvar)
new_lineno += self.read_variables(outfile, match.group(1),
variables)
if not scons_header:
if re.search(r"^\"\"\"", line):
scons_header = True
elif not re.search(r'^#', line):
scons_header = True
if scons_header:
new_lineno += self.write_header(outfile)
@staticmethod
def read_variables(outfile, prefix, variables):
"""Add code to define fake variables for pylint"""
newlines = 2
outfile.write("# pylint: disable=invalid-name\n")
for variable in variables:
outfile.write("%sprint(\"%%s\" %% str(%s))\n" % (prefix, variable))
newlines += 1
outfile.write("# pylint: enable=invalid-name\n")
return newlines
@staticmethod
def write_variables(outfile, prefix, variables):
"""Add code to define fake variables for pylint"""
newlines = 2
outfile.write("# pylint: disable=invalid-name\n")
for variable in variables:
if variable.upper() == 'PREREQS':
newlines += 4
outfile.write("%sfrom prereq_tools import PreReqComponent\n"
% prefix)
outfile.write("%sscons_temp_env = DefaultEnvironment()\n"
% prefix)
outfile.write("%sscons_temp_opts = Variables()\n" % prefix)
outfile.write("%s%s = PreReqComponent(scons_temp_env, "
"scons_temp_opts)\n" % (prefix, variable))
variables.remove(variable)
for variable in variables:
if "ENV" in variable.upper():
newlines += 1
outfile.write("%s%s = DefaultEnvironment()\n" % (prefix,
variable))
elif "OPTS" in variable.upper():
newlines += 1
outfile.write("%s%s = Variables()\n" % (prefix, variable))
elif "PREFIX" in variable.upper():
newlines += 1
outfile.write("%s%s = ''\n" % (prefix, variable))
elif "TARGETS" in variable.upper() or "TGTS" in variable.upper():
newlines += 1
outfile.write("%s%s = ['fake']\n" % (prefix, variable))
else:
newlines += 1
outfile.write("%s%s = None\n" % (prefix, variable))
outfile.write("# pylint: enable=invalid-name\n")
return newlines
@staticmethod
def write_header(outfile):
"""write the header"""
outfile.write("""# pylint: disable=wildcard-import
from __future__ import print_function
from SCons.Script import *
from SCons.Variables import *
# pylint: enable=wildcard-import\n""")
return 5
def fix_log(self, log_file, fname):
"""Get the line number"""
os.unlink("script")
log_file.seek(0)
try:
output = tempfile.TemporaryFile(mode='w+', encoding='utf-8')
except TypeError:
output = tempfile.TemporaryFile()
for line in log_file.readlines():
match = re.search(r":(\d+):", line)
if match:
lineno = int(match.group(1))
if int(lineno) in self.line_map.keys():
line = line.replace(str(lineno),
str(self.line_map[lineno]),
1)
match = re.search("^(.*)Module script(.*)$", line)
if match:
line = "%sModule %s%s\n" % (match.group(1),
fname,
match.group(2))
output.write(line)
return output
def parse_report(log_file):
"""Create the report"""
error_count = 0
log_file.seek(0)
with open("pylint.log", "a") as pylint:
for line in log_file.readlines():
if re.search("rated", line):
sys.stdout.write(line)
elif re.search("^[WECR]:", line):
sys.stdout.write(line[3:])
pylint.write(line[3:])
error_count += 1
else:
sys.stdout.write(line)
return error_count
def find_pylint(version):
"""find pylint version"""
pylint = find_executable("pylint-%d" % version)
if pylint:
return pylint
python = find_executable("python%d" % version)
pylint = find_executable("pylint")
if python and pylint:
return "%s %s" % (python, pylint)
print("No python%d pylint found" % version)
return None
def create_rc(src_name):
"""Create a temporary rc file with python path set"""
root = os.path.dirname(os.path.realpath(__file__))
src_path = os.path.join(root, src_name)
name = os.path.join(root, "tmp_%s" % src_name)
with open(name, "w") as tmp:
tmp.write("[MASTER]\n")
tmp.write("init-hook='import sys; ")
tmp.write("sys.path.insert(0, \"%s\"); " % root)
tmp.write("sys.path.insert(0, \"%s/fake_scons\")'\n" % root)
with open(src_path, "r") as src:
for line in src.readlines():
tmp.write(line)
return name
#pylint: disable=too-many-branches
def check_script(fname, *args, **kw):
"""Check a python script for errors"""
tmp_fname = fname
wrapper = None
wrap = kw.get("wrap", False)
if wrap:
wrapper = WrapScript(fname)
tmp_fname = "script"
pylint_path = fname
else:
pylint_path = "{path}"
#Python 2 checking is no longer supported
pycmd = find_pylint(3)
rc_file = "tmp_pylint3.rc"
if pycmd is None:
print("Required pylint isn't installed on this machine")
return 0
rc_dir = os.path.dirname(os.path.realpath(__file__))
cmd = pycmd.split() + \
list(args) + \
["--rcfile=%s/%s" % (rc_dir, rc_file),
"--msg-template",
"{C}: %s:{line}: pylint-{symbol}: {msg}" % pylint_path,
tmp_fname]
if os.environ.get("DEBUG_CHECK_SCRIPT", 0):
print(" ".join(cmd))
try:
log_file = tempfile.TemporaryFile(mode='w+', encoding='utf-8')
except TypeError:
log_file = tempfile.TemporaryFile()
try:
subprocess.check_call(cmd, stdout=log_file)
except OSError as exception:
if exception.errno == errno.ENOENT:
print("pylint could not be found")
return 1
raise
except subprocess.CalledProcessError:
pass
if wrap:
log_file = wrapper.fix_log(log_file, fname)
error_count = parse_report(log_file)
print("")
return error_count
#pylint: enable=too-many-branches
def main():
"""Run the actual code in a function"""
parser = argparse.ArgumentParser("Check a Python script for errors")
parser.add_argument("fname", metavar='FILENAME', type=str, nargs='?',
default=None, help="Filename of script to check")
parser.add_argument("-w", dest='wrap', action='store_true',
help='Wrap the SCons script before checking')
parser.add_argument("-s", dest='self_check', action='store_true',
help='Perform a self check')
parser.add_argument("-p3", dest='P3', action='store_true',
help='Perform check using python3 (default)')
args = parser.parse_args()
error_count = 0
pylint_rc = create_rc("pylint.rc")
pylint3_rc = create_rc("pylint3.rc")
if args.self_check:
print("Checking SCons")
error_count += check_script("SCons",
"-d", "too-few-public-methods",
"-d", "too-many-public-methods",
"-d", "invalid-name",
"-d", "unused-argument",
"-d", "no-self-use")
print("Checking prereq_tools")
error_count += check_script("prereq_tools",
"-d", "too-many-lines",
"-d", "unused-argument")
print("Checking components")
error_count += check_script("components")
print("Checking build_info")
error_count += check_script("build_info")
print("Checking check_script.py")
error_count += check_script("check_script")
if args.fname:
error_count += check_script(args.fname, wrap=args.wrap,
P3=args.P3)
os.unlink(pylint_rc)
os.unlink(pylint3_rc)
if error_count:
sys.exit(1)
if __name__ == '__main__':
main()