Skip to content

Commit a153250

Browse files
authored
Merge pull request #745 from yalechen-cyw/main
🎨 update tcaql py
2 parents 7f5cdc1 + 2742914 commit a153250

1 file changed

Lines changed: 110 additions & 78 deletions

File tree

client/tool/util/tca_ql.py

Lines changed: 110 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
from util.api.fileserver import RetryFileServer
3030
from util.configlib import ConfigReader
3131
from util.envset import EnvSet
32-
from util.errcode import E_NODE_TASK_PARAM
33-
from util.exceptions import AnalyzeTaskError, TaskError
32+
from util.textutil import CodecClient
3433
from util.pathfilter import FilterPathUtil
3534
from util.pathlib import PathMgr
3635
from task.codelintmodel import CodeLintModel
3736
from task.scmmgr import SCMMgr
37+
from task.basic.common import subprocc_log
3838
from util.subprocc import SubProcController
3939
from util.zipmgr import Zip
4040
from util.logutil import LogPrinter
@@ -53,8 +53,6 @@
5353

5454

5555
class TcaQl(CodeLintModel):
56-
""" """
57-
5856
def __init__(self, params):
5957
CodeLintModel.__init__(self, params)
6058

@@ -71,10 +69,13 @@ def __download_database(self, params, path):
7169
file_server = RetryFileServer(retry_times=2).get_server()
7270
if file_server.download_big_file(file_name, zip_file):
7371
logger.info("下载成功")
74-
Zip.decompress_by_7z(zip_file, db_path)
72+
Zip().decompress_by_7z(zip_file, db_path)
73+
os.remove(zip_file)
7574
return True
75+
else:
76+
return False
7677
except Exception as e:
77-
logger.warning(f"下载失败 {e}")
78+
logger.warning(f"下载失败")
7879
return False
7980

8081
def __upload_database(self, params, path):
@@ -128,6 +129,33 @@ def __generate_config_file(self, rule_list, work_dir, source_dir, toscans):
128129
tree.write(setting_file, encoding="utf-8")
129130
return setting_file
130131

132+
def __get_zeus_version(self, params):
133+
# 2023/1/10 调整获取Zeus版本的方式
134+
envs = os.environ
135+
ZEUS_HOME = envs.get("ZEUS_HOME", None)
136+
work_dir = params.work_dir
137+
scan_cmd = self.get_cmd(
138+
"Zeus",
139+
[
140+
"--version",
141+
],
142+
)
143+
lang_ext_output = os.path.join(work_dir, "Zeus_version.txt")
144+
spc = SubProcController(
145+
scan_cmd,
146+
cwd=ZEUS_HOME,
147+
stdout_line_callback=subprocc_log,
148+
stderr_line_callback=subprocc_log,
149+
stdout_filepath=lang_ext_output,
150+
)
151+
spc.wait()
152+
153+
fi = open(lang_ext_output, "rb")
154+
# 读取文件内容并解码为字符串
155+
content = CodecClient().decode(fi.read())
156+
fi.close()
157+
return content.split(" ")[-1].strip()
158+
131159
def compile(self, params, lang):
132160
"""
133161
编译函数,指代码生成数据流
@@ -140,10 +168,10 @@ def compile(self, params, lang):
140168
repo_id = params.repo_id
141169
envs = os.environ
142170
ZEUS_HOME = envs.get("ZEUS_HOME", None)
143-
tool_path = os.path.join(ZEUS_HOME, "Zeus")
144171
scm_revision = params["scm_revision"]
145-
db_name = f"{repo_id}_{scm_revision}_{lang}"
146-
db_path = os.path.join(db_dir, db_name + ".db")
172+
version = self.__get_zeus_version(params)
173+
db_name = f"{repo_id}_{scm_revision}_{lang}_{version}"
174+
db_path = os.path.join(db_dir, f"{db_name}.db")
147175
inc = params["incr_scan"]
148176
want_suffix = lang_map[lang]
149177
logger.info("是否为增量编译: %s" % inc)
@@ -156,13 +184,15 @@ def compile(self, params, lang):
156184
return
157185
if inc:
158186
last_scm_revision = params["scm_last_revision"]
159-
last_db_name = f"{repo_id}_{last_scm_revision}_{lang}"
187+
last_db_name = f"{repo_id}_{last_scm_revision}_{lang}_{version}"
160188
last_db_path = os.path.join(db_dir, last_db_name + ".db")
161189
logger.info(f"下载上个成功分析版本数据库{last_db_name}")
162190
if not self.__download_database(params, last_db_name):
163191
logger.info("下载全量数据库失败将重新生成,本次分析将只分析增量部分")
164-
else:
192+
elif os.path.exists(last_db_path):
165193
shutil.copyfile(last_db_path, db_path)
194+
else:
195+
logger.error("下载失败,本次将只分析增量文件,建议全量分析下项目。")
166196
diffs = SCMMgr(params).get_scm_diff()
167197
# 增量需要所有增量文件都重新生成,故这里不能过滤文件
168198
toscans = [diff.path.replace(os.sep, "/") for diff in diffs if diff.path.endswith(tuple(want_suffix))]
@@ -173,65 +203,69 @@ def compile(self, params, lang):
173203
for toscan in toscans:
174204
wf.writelines(toscan)
175205
wf.write("\n")
176-
inc_build_cmd = [
177-
"./Zeus",
178-
"inc_compile",
179-
"-l",
180-
lang,
181-
"-p",
182-
db_name,
183-
"-db",
184-
db_dir,
185-
"-s",
186-
source_dir,
187-
# "-d",
188-
"-f",
189-
file_list,
190-
]
191-
logger.info(inc_build_cmd)
192-
inc_build_cmd = self.get_cmd(tool_path, inc_build_cmd)
206+
inc_build_cmd = self.get_cmd(
207+
"Zeus",
208+
[
209+
"inc_compile",
210+
"-l",
211+
lang,
212+
"-p",
213+
db_name,
214+
"-db",
215+
db_dir,
216+
"-s",
217+
source_dir,
218+
# "-d",
219+
"-f",
220+
file_list,
221+
],
222+
)
223+
logger.info(" ".join(inc_build_cmd))
193224
# cmd_args += toscans
194225
sp = SubProcController(
195226
command=inc_build_cmd,
196227
cwd=ZEUS_HOME,
197-
stdout_line_callback=self.subprocc_log,
198-
stderr_line_callback=self.subprocc_log,
228+
stdout_line_callback=subprocc_log,
229+
stderr_line_callback=subprocc_log,
199230
)
200231
sp.wait()
201232
logger.info(sp.returncode)
202233
self.__upload_database(params, db_name)
203234
return
204235
# 全量编译命令
205-
toscans = [path.replace(os.sep, "/") for path in PathMgr().get_dir_files(source_dir, tuple(want_suffix))]
236+
toscans = [
237+
path.replace(os.sep, "/") for path in PathMgr().get_dir_files(source_dir, want_suffix=tuple(want_suffix))
238+
]
206239
toscans = FilterPathUtil(params).get_include_files(toscans, relpos)
207240
if not toscans:
208241
return []
209242
with open(file_list, "w") as wf:
210243
for toscan in toscans:
211244
wf.writelines(toscan)
212245
wf.write("\n")
213-
full_build_cmd = [
214-
# "./Zeus",
215-
"compile",
216-
"-p",
217-
db_name,
218-
"-cc",
219-
db_dir,
220-
"-l",
221-
lang,
222-
"-s",
223-
source_dir,
224-
"-f",
225-
file_list
226-
# "-d", # 调试使用
227-
]
228-
logger.info(full_build_cmd)
229-
full_build_cmd = self.get_cmd(tool_path, full_build_cmd)
246+
full_build_cmd = self.get_cmd(
247+
"Zeus",
248+
[
249+
"compile",
250+
"-p",
251+
db_name,
252+
"-cc",
253+
db_dir,
254+
"-l",
255+
lang,
256+
"-s",
257+
source_dir,
258+
"-f",
259+
file_list
260+
# "-d", # 调试使用
261+
],
262+
)
263+
logger.info(" ".join(full_build_cmd))
230264
sp = SubProcController(
231265
command=full_build_cmd,
232266
cwd=ZEUS_HOME,
233-
stdout_line_callback=self.subprocc_log,
234-
stderr_line_callback=self.subprocc_log,
267+
stdout_line_callback=subprocc_log,
268+
stderr_line_callback=subprocc_log,
235269
)
236270
sp.wait()
237271
logger.info(sp.returncode)
@@ -251,10 +285,10 @@ def analyze(self, params, lang):
251285
repo_id = params.repo_id
252286
envs = os.environ
253287
HADES_HOME = envs.get("HADES_HOME", None)
254-
tool_path = os.path.join(HADES_HOME, "Hades")
255288
scm_revision = params["scm_revision"]
256-
db_name = f"{repo_id}_{scm_revision}_{lang}"
257-
db_path = os.path.join(db_dir, db_name + ".db")
289+
version = self.__get_zeus_version(params)
290+
db_name = f"{repo_id}_{scm_revision}_{lang}_{version}"
291+
db_path = os.path.join(db_dir, f"{db_name}.db")
258292
if not os.path.exists(db_path):
259293
if not os.path.exists(db_dir):
260294
os.makedirs(db_dir)
@@ -274,7 +308,8 @@ def analyze(self, params, lang):
274308
]
275309
else:
276310
toscans = [
277-
path.replace(os.sep, "/")[relpos:] for path in PathMgr().get_dir_files(source_dir, tuple(want_suffix))
311+
path.replace(os.sep, "/")[relpos:]
312+
for path in PathMgr().get_dir_files(source_dir, want_suffix=tuple(want_suffix))
278313
]
279314
# 过滤文件以及过滤文件取相对路径
280315
toscans = FilterPathUtil(params).get_include_files([os.path.join(source_dir, path) for path in toscans], relpos)
@@ -287,32 +322,33 @@ def analyze(self, params, lang):
287322
return []
288323
output_json = os.path.join(work_dir, "result.json")
289324
setting_file = self.__generate_config_file(rules, work_dir, source_dir, toscans)
290-
analyze_cmd = [
291-
# "./Hades",
292-
"analyze",
293-
"-l",
294-
lang,
295-
"-cc",
296-
db_dir,
297-
"-db",
298-
db_name,
299-
"-o",
300-
output_json,
301-
"-c",
302-
setting_file,
303-
# "-d",
304-
]
325+
analyze_cmd = self.get_cmd(
326+
"Hades",
327+
[
328+
"analyze",
329+
"-l",
330+
lang,
331+
"-cc",
332+
db_dir,
333+
"-db",
334+
db_name,
335+
"-o",
336+
output_json,
337+
"-c",
338+
setting_file,
339+
# "-d",
340+
],
341+
)
305342
logger.info(analyze_cmd)
306343
task_dir = os.path.dirname(os.getcwd())
307344
request_file = os.path.abspath(os.path.join(task_dir, "task_request.json"))
308345
os.environ["TASK_REQUEST"] = request_file
309346
issues = []
310-
analyze_cmd = self.get_cmd(tool_path, analyze_cmd)
311347
sp = SubProcController(
312348
command=analyze_cmd,
313349
cwd=HADES_HOME,
314-
stdout_line_callback=self.subprocc_log,
315-
stderr_line_callback=self.subprocc_log,
350+
stdout_line_callback=subprocc_log,
351+
stderr_line_callback=subprocc_log,
316352
env=EnvSet().get_origin_env(),
317353
)
318354
sp.wait()
@@ -326,12 +362,8 @@ def analyze(self, params, lang):
326362
# self.__upload_database(params, db_name)
327363
return issues
328364

329-
def subprocc_log(self, line):
330-
""""""
331-
logger.info(line)
332-
333-
def get_cmd(self, tool_path, options):
334-
return __lu__().format_cmd(tool_path, options)
365+
def get_cmd(self, tool_path, args):
366+
return __lu__().format_cmd(tool_path, args)
335367

336368

337369
tool = TcaQl

0 commit comments

Comments
 (0)