Skip to content

Commit d50e1a2

Browse files
committed
🐛解决cppcheck可能出现中文编码错误问题
1 parent 2976166 commit d50e1a2

1 file changed

Lines changed: 33 additions & 23 deletions

File tree

client/tool/cppcheck.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# ==============================================================================
77

88
"""
9-
cppcheck 扫描任务
9+
cppcheck 分析任务
1010
"""
1111

1212
import os
@@ -35,11 +35,11 @@ def __init__(self, params):
3535
self.sensitive_word_maps = {"cppcheck": "Tool", "Cppcheck": "Tool"}
3636

3737
def analyze(self, params):
38-
"""执行cppcheck扫描任务
38+
"""执行cppcheck分析任务
3939
4040
:param params: 需包含下面键值:
41-
'rules': lint扫描的规则列表
42-
'incr_scan' : 是否增量扫描
41+
'rules': lint分析的规则列表
42+
'incr_scan' : 是否增量分析
4343
4444
:return: return a :py:class:`IssueResponse`
4545
"""
@@ -112,20 +112,21 @@ def _get_id_severity_map(self):
112112
return id_severity_map
113113

114114
def _get_needed_visitors(self, id_severity_map, rule_list):
115-
"""cppcheck不能指定规则扫描,只能指定规则级别,这里通过rules获取所属的规则级别"""
115+
"""cppcheck不能指定规则分析,只能指定规则级别,这里通过rules获取所属的规则级别"""
116116
assert rule_list is not None
117117
# cppcheck默认就是开启error规则(且无法指定enable=error),所以这里取补集
118118
return {id_severity_map[rule_name] for rule_name in rule_list} - {"error"}
119119

120120
def _run_cppcheck(self, files_path, rules, id_severity_map):
121121
"""
122-
执行cppcheck扫描工具
122+
执行cppcheck分析工具
123123
:param files_path:
124124
:param rules:
125125
:param id_severity_map:
126126
:return:
127127
"""
128128
CPPCHECK_HOME = os.environ["CPPCHECK_HOME"]
129+
LogPrinter.info("使用 cppcheck 为 %s" % CPPCHECK_HOME)
129130
path_mgr = PathMgr()
130131
cmd_args = [
131132
"cppcheck",
@@ -155,32 +156,37 @@ def _run_cppcheck(self, files_path, rules, id_severity_map):
155156
custom_cfgs = ["--library=" + cfg for cfg in custom_cfgs]
156157
cmd_args.extend(custom_cfgs)
157158

158-
# 指定扫描文件
159+
# 指定分析文件
159160
cmd_args.append("--file-list=%s" % files_path)
160161
scan_result_path = "cppcheck_result.xml"
161162
self.print_log(f"cmd: {' '.join(cmd_args)}")
163+
cmd_args = path_mgr.format_cmd_arg_list(cmd_args)
162164
SubProcController(
163165
cmd_args,
164166
cwd=CPPCHECK_HOME,
165167
stderr_filepath=scan_result_path,
166168
stderr_line_callback=self._error_callback,
167169
stdout_line_callback=self.print_log,
168170
).wait()
169-
if not os.path.exists(scan_result_path):
170-
return scan_result_path
171-
if sys.platform == "win32":
172-
result_content = open(scan_result_path, "r", encoding="gbk").read()
173-
open(scan_result_path, "w", encoding="utf-8").write(result_content)
174-
with open(scan_result_path, "r", encoding="utf-8") as rf:
175-
scan_result = rf.read()
176-
177-
if not scan_result:
178-
return scan_result_path
171+
# if not os.path.exists(scan_result_path):
172+
# return scan_result_path
173+
# try:
174+
# if sys.platform == "win32":
175+
# result_content = open(scan_result_path, 'r', encoding='gbk').read()
176+
# open(scan_result_path, 'w', encoding='utf-8').write(result_content)
177+
# with open(scan_result_path, 'r', encoding='utf-8') as rf:
178+
# scan_result = rf.read()
179+
# except UnicodeDecodeError as e:
180+
# LogPrinter.warning(e)
181+
# with open(scan_result_path, 'rb')
182+
# if not scan_result:
183+
# return scan_result_path
179184

180185
# 2019-8-28 偶现因为系统编码输出到结果文件,导致解析失败的情况
181-
error_msg = "/bin/sh: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)\n"
182-
if scan_result.startswith(error_msg):
183-
scan_result = scan_result[len(error_msg) :]
186+
# 2022-10-14 解析结果不再使用xml格式而是逐行解析,故不需要在解析前查看该文件
187+
# error_msg = "/bin/sh: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)\n"
188+
# if scan_result.startswith(error_msg):
189+
# scan_result = scan_result[len(error_msg):]
184190

185191
# self.print_log("%s's result: \n%s" % (scan_result_path, scan_result))
186192

@@ -200,9 +206,13 @@ def _format_result(self, source_dir, scan_result_path, rules, supported_rules):
200206
"""格式化工具执行结果"""
201207
issues = []
202208
relpos = len(source_dir) + 1
203-
with open(scan_result_path, "r") as rf:
209+
with open(scan_result_path, "rb") as rf:
204210
lines = rf.readlines()
205211
for line in lines:
212+
try:
213+
line = line.decode("utf-8")
214+
except:
215+
line = line.decode("gbk")
206216
error = line.split("[CODEDOG]")
207217
if len(error) != 5:
208218
LogPrinter.info("该error信息不全或格式有误: %s" % line)
@@ -232,8 +242,8 @@ def _format_result(self, source_dir, scan_result_path, rules, supported_rules):
232242
def check_tool_usable(self, tool_params):
233243
"""
234244
这里判断机器是否支持运行cppcheck
235-
1. 支持的话,便在客户机器上扫描
236-
2. 不支持的话,就发布任务到公线机器扫描
245+
1. 支持的话,便在客户机器上分析
246+
2. 不支持的话,就发布任务到公线机器分析
237247
:return:
238248
"""
239249
if SubProcController(["cppcheck", "--version"], stderr_line_callback=self.print_log).wait() != 0:

0 commit comments

Comments
 (0)