分析执行函数 :param params: 分析所需要的资源 1.项目地址 2.工作地址 3.分析参数 :return:
(self, params)
| 40 | pass |
| 41 | |
| 42 | def analyze(self, params): |
| 43 | ''' |
| 44 | 分析执行函数 |
| 45 | :param params: 分析所需要的资源 1.项目地址 2.工作地址 3.分析参数 |
| 46 | :return: |
| 47 | ''' |
| 48 | source_dir = params['source_dir'] |
| 49 | work_dir = os.getcwd() |
| 50 | rules = params['rules'] |
| 51 | |
| 52 | toscans = PythonTool.get_scan_files(params) |
| 53 | |
| 54 | if not toscans: |
| 55 | logger.debug("To-be-scanned diff files is empty ") |
| 56 | return [] |
| 57 | |
| 58 | cmd_args_list = self._get_cmd_args(source_dir, toscans, rules) |
| 59 | |
| 60 | # 结果解析异常JSONDecodeError, linux某些终端下会在josn开头添加 [?1034h 或 ^[[?1034h[ |
| 61 | if sys.platform in ("linux", "linux2"): |
| 62 | os.environ['TERM'] = '' |
| 63 | |
| 64 | relpos = len(source_dir) + 1 |
| 65 | items = [] |
| 66 | for i in range(len(cmd_args_list)): |
| 67 | error_output = os.path.join(work_dir, 'result_'+str(i)+'.json') |
| 68 | # self.print_log("cmd: %s" % ' '.join(cmd_args_list[i])) |
| 69 | logger.info("cmd_len: %d" % len(' '.join(cmd_args_list[i]))) |
| 70 | sp = SubProcController(command=cmd_args_list[i], |
| 71 | cwd=work_dir, |
| 72 | return_code_callback=self.on_status, |
| 73 | stdout_filepath=error_output, |
| 74 | stdout_line_callback=self.print_log, |
| 75 | stderr_line_callback=self.stderr_callback) |
| 76 | sp.wait() |
| 77 | |
| 78 | if not os.path.exists(error_output) or os.stat(error_output).st_size == 0: |
| 79 | logger.info("result_"+str(i)+".json - result is empty") |
| 80 | continue |
| 81 | try: |
| 82 | with open(error_output, 'r', encoding='utf-8') as f: |
| 83 | lines = f.readlines() |
| 84 | except: |
| 85 | with open(error_output, 'r', encoding='gbk') as f: |
| 86 | lines = f.readlines() |
| 87 | for line in lines: |
| 88 | if line.find(CODEDOG_FLAG) >= 0: |
| 89 | issue = line.split(CODEDOG_FLAG) |
| 90 | try: |
| 91 | assert len(issue)==5 |
| 92 | except AssertionError: |
| 93 | logger.info("格式化错误信息失败,错误为: %s" % line) |
| 94 | continue |
| 95 | # flake输出格式为: |
| 96 | # {path}[CODEDOG]{line}[CODEDOG]{col}[CODEDOG]{rule}[CODEDOG]{msg} |
| 97 | items.append({ |
| 98 | 'path': issue[0][relpos:], |
| 99 | 'rule': issue[3], |
nothing calls this directly
no test coverage detected