-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
288 lines (251 loc) · 10.6 KB
/
main.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Copyright (c) 2022 THL A29 Limited
#
# This source code file is made available under MIT License
# See LICENSE for details
# ==============================================================================
import re
import os
import sys
import json
import fnmatch
import argparse
import subprocess
import settings
class SQLCheck(object):
@staticmethod
def init_env():
tool_dir = settings.TOOL_DIR
os.environ["SQLCHECK_HOME"] = os.path.join(tool_dir, settings.PLATFORMS[sys.platform], "sqlcheck-x86_64")
os.environ["PATH"] = os.pathsep.join(
[
os.path.join(os.environ["SQLCHECK_HOME"], "bin"),
os.environ["PATH"],
]
)
def __parse_args(self):
"""
解析命令
:return:
"""
argparser = argparse.ArgumentParser()
subparsers = argparser.add_subparsers(dest="command", help="Commands", required=True)
# 检查在当前机器环境是否可用
subparsers.add_parser("check", help="检查在当前机器环境是否可用")
# 执行代码扫描
subparsers.add_parser("scan", help="执行代码扫描")
return argparser.parse_args()
def __get_task_params(self):
"""
获取需要任务参数
:return:
"""
task_request_file = os.environ.get("TASK_REQUEST")
with open(task_request_file, "r") as rf:
task_request = json.load(rf)
task_params = task_request["task_params"]
return task_params
def __get_dir_files(self, root_dir, want_suffix=""):
"""
在指定的目录下,递归获取符合后缀名要求的所有文件
:param root_dir:
:param want_suffix:
str|tuple,文件后缀名.单个直接传,比如 ".py";多个以元组形式,比如 (".h", ".c", ".cpp")
默认为空字符串,会匹配所有文件
:return: list, 文件路径列表
"""
files = set()
for dirpath, _, filenames in os.walk(root_dir):
for f in filenames:
if f.lower().endswith(want_suffix):
fullpath = os.path.join(dirpath, f)
files.add(fullpath)
files = list(files)
return files
def __format_str(self, text):
"""
格式化字符串
:param text:
:return:
"""
text = text.strip()
if isinstance(text, bytes):
text = text.decode("utf-8")
return text.strip("'\"")
def __run_cmd(self, cmd_args):
"""
执行命令行
"""
print("[run cmd] %s" % " ".join(cmd_args))
p = subprocess.Popen(cmd_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdoutput, erroutput) = p.communicate()
stdoutput = self.__format_str(stdoutput)
erroutput = self.__format_str(erroutput)
if stdoutput:
print(">> stdout: %s" % stdoutput)
if erroutput:
print(">> stderr: %s" % erroutput)
return stdoutput, erroutput
def __convert_to_regex(self, wildcard_paths):
"""
通配符转换为正则表达式
:param wildcard_paths:
:return:
"""
return [fnmatch.translate(pattern) for pattern in wildcard_paths]
def __get_path_filters(self, task_params):
"""
获取过滤路径(工具按需使用),支持用户配置通配符和正则表达式2种格式的过滤路径表达式,该方法会将通配符转换为正则表达式,合并使用
:param task_params:
:return: 合并后的正则表达式过滤路径格式
"""
# 用户输入的原始参数
wildcard_include_paths = task_params["path_filters"].get("inclusion", [])
wildcard_exclude_paths = task_params["path_filters"].get("exclusion", [])
regex_include_paths = task_params["path_filters"].get("re_inclusion", [])
regex_exlucde_paths = task_params["path_filters"].get("re_exclusion", [])
print(">> 过滤路径原始配置:")
print(">> 说明:")
print(">> include - 只扫描指定文件, exclude - 过滤掉指定文件, 优先级: exclude > include (即:如果A文件同时匹配,会优先exclude,被过滤)")
print("include(通配符格式): %s" % wildcard_include_paths)
print("exclude(通配符格式): %s" % wildcard_exclude_paths)
print("include(正则表达式格式): %s" % regex_include_paths)
print("exclude(正则表达式格式): %s" % regex_exlucde_paths)
# 通配符转换为正则表达式
if wildcard_include_paths:
converted_include_paths = self.__convert_to_regex(wildcard_include_paths)
regex_include_paths.extend(converted_include_paths)
if wildcard_exclude_paths:
converted_exclude_paths = self.__convert_to_regex(wildcard_exclude_paths)
regex_exlucde_paths.extend(converted_exclude_paths)
print(">> 合并后过滤路径;")
print("include(正则表达式格式): %s" % regex_include_paths)
print("exclude(正则表达式格式): %s" % regex_exlucde_paths)
return {"re_inclusion": regex_include_paths, "re_exclusion": regex_exlucde_paths}
def __scan(self):
"""
分析代码
"""
# 代码目录直接从环境变量获取
source_dir = os.environ.get("SOURCE_DIR", None)
print("[debug] source_dir: %s" % source_dir)
# 其他参数从task_request.json文件获取
task_params = self.__get_task_params()
rules = task_params.get("rules", list())
# ------------------------------------------------------------------ #
# 获取需要扫描的文件列表
# 此处获取到的文件列表,已经根据项目配置的过滤路径过滤
# 增量扫描时,从SCAN_FILES获取到的文件列表与从DIFF_FILES获取到的相同
# ------------------------------------------------------------------ #
scan_files_env = os.getenv("SCAN_FILES")
if scan_files_env and os.path.exists(scan_files_env):
with open(scan_files_env, "r") as rf:
scan_files = json.load(rf)
# print("[debug] files to scan: %s" % len(scan_files))
scan_files = [path for path in scan_files if path.endswith(".sql")]
print("[debug] env: %s" % os.environ)
issues = list()
for path in scan_files:
scan_cmds = ["sqlcheck", "-v", "-f", path]
try:
stdout, stderr = self.__run_cmd(scan_cmds)
except Exception as err:
print("scan %s failed: %s" % (path, str(err)))
continue
if stderr:
raise Exception("Tool exec error: %s" % stderr)
issues.extend(self.handle_data(stdout, path, rules))
print("[debug] issues: %s" % issues)
# 输出结果到指定的json文件
with open("result.json", "w") as fp:
json.dump(issues, fp, indent=2)
# 2022/10/11 适配 Matching Expression 多行的情况
def handle_data(self, stdout, path, rules):
issues = list()
start = False
msg = list()
rule = None
expression = ""
start_expression = False
finish_issue = False
for line in stdout.splitlines():
line = line.strip()
if line.startswith(f"[{path}]:"):
rule = self.__convert(line.split(")")[-1].strip())
start = True
msg.append(line.split(":")[-1].strip())
elif line.startswith("[Matching Expression:"):
start = False
expression += line
if line[-1] != "]":
start_expression = True
else:
finish_issue = True
elif start:
msg.append(line)
elif start_expression:
expression += line
# print(expression)
if line == "":
# if line[-1] == "]":
start_expression = False
finish_issue = True
if finish_issue:
line_list = None
if expression.find("lines") != -1:
line_list = [int(num.strip()) for num in expression.split("lines")[-1].strip()[:-1].split(",")]
else:
line_list = [int(expression.split("line")[-1].strip()[:-1])]
# 2023/2/14 增加规则过滤
if rule in rules:
for line_no in line_list:
issues.append({"path": path, "line": line_no, "column": 0, "msg": "\n".join(msg), "rule": rule})
msg = list()
rule = None
expression = ""
finish_issue = False
return issues
def __convert(self, one_string, space_character=" |-"):
"""
one_string:输入的字符串
space_character:字符串的间隔符,以其做为分隔标志
"""
# 将字符串转化为list
string_list = re.split(space_character, str(one_string))
# str.capitalize():将字符串的首字母转化为大写
others_capital = [word.capitalize() for word in string_list]
# 将list组合成为字符串,中间无连接符。
hump_string = "".join(others_capital)
return hump_string
def __check_usable(self):
"""
检查工具在当前机器环境下是否可用
"""
# 这里只是一个demo,检查python3命令是否可用,请按需修改为实际检查逻辑
check_cmd_args = ["sqlcheck", "--version"]
try:
stdout, stderr = self.__run_cmd(check_cmd_args)
except Exception as err:
print("tool is not usable: %s" % str(err))
return False
return True
def run(self):
self.init_env()
args = self.__parse_args()
if args.command == "check":
print(">> check tool usable ...")
is_usable = self.__check_usable()
result_path = "check_result.json"
if os.path.exists(result_path):
os.remove(result_path)
with open(result_path, "w") as fp:
data = {"usable": is_usable}
json.dump(data, fp)
elif args.command == "scan":
print(">> start to scan code ...")
self.__scan()
else:
print("[Error] need command(check, scan) ...")
if __name__ == "__main__":
SQLCheck().run()