MCPcopy Index your code
hub / github.com/Tencent/CodeAnalysis / analyze

Method analyze

client/tool/regexscanner.py:144–219  ·  view source on GitHub ↗

执行regexscanner分析任务 :param params: 需包含下面键值: 'rules': lint分析的规则列表 'incr_scan' : 是否增量分析 :return: return a :py:class:`IssueResponse`

(self, params)

Source from the content-addressed store, hash-verified

142 return config_rules_path
143
144 def analyze(self, params):
145 '''执行regexscanner分析任务
146 :param params: 需包含下面键值:
147 'rules': lint分析的规则列表
148 'incr_scan' : 是否增量分析
149 :return: return a :py:class:`IssueResponse`
150 '''
151 source_dir = params['source_dir']
152 work_dir = params['work_dir']
153 incr_scan = params['incr_scan']
154 rules = params["rules"]
155
156 files_path = os.path.join(work_dir, "regexscanner_paths.txt")
157 output_path = os.path.join(work_dir, "regexscanner_result.json")
158
159 toscans = []
160 if incr_scan:
161 diffs = SCMMgr(params).get_scm_diff()
162 toscans = [os.path.join(source_dir, diff.path) for diff in diffs if diff.state != 'del']
163 else:
164 toscans = PathMgr().get_dir_files(source_dir)
165
166 # filter include and exclude path
167 relpos = len(source_dir) + 1
168 toscans = FilterPathUtil(params).get_include_files(toscans, relpos)
169
170 toscans = self.get_valid_encode_files(toscans)
171
172 if not toscans:
173 logger.debug("To-be-scanned files is empty ")
174 return []
175 logger.debug("files to scan: %d" % len(toscans))
176 with open(files_path, "w", encoding="UTF-8") as f:
177 f.write("\n".join(toscans))
178
179 # 写入规则
180 config_rules_path = self.__format_rules(work_dir, params['rule_list'])
181
182 # 执行分析工具
183 options = [
184 "--filelist=%s" % files_path,
185 "--project-root=%s" % source_dir,
186 "--ruleset=%s" % config_rules_path,
187 "--output-format=json",
188 "--output=%s" % output_path,
189 ]
190 scan_cmd = self.get_cmd(options)
191 logger.info(f"scan_cmd: {' '.join(scan_cmd)}")
192
193 subproc = SubProcController(
194 scan_cmd, stdout_line_callback=logger.info, stderr_line_callback=logger.info)
195 subproc.wait()
196
197 if not os.path.exists(output_path):
198 logger.info("No results file generated.")
199 raise AnalyzeTaskError("Tool running error")
200
201 issues = []

Callers

nothing calls this directly

Calls 10

__format_rulesMethod · 0.95
get_cmdMethod · 0.95
AnalyzeTaskErrorClass · 0.90
get_dir_filesMethod · 0.80
debugMethod · 0.80
writeMethod · 0.45
infoMethod · 0.45
loadMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected