使用gradle模式,处理gralde项目
(self, params)
| 133 | return self.__get_issues_from_file(error_output, source_dir) |
| 134 | |
| 135 | def gradle_mode_analyze(self, params): |
| 136 | """ |
| 137 | 使用gradle模式,处理gralde项目 |
| 138 | """ |
| 139 | source_dir = params["source_dir"] |
| 140 | build_cwd = os.environ.get("BUILD_CWD", None) |
| 141 | # 构建当前目录 |
| 142 | build_cwd = os.path.join(source_dir, build_cwd) if build_cwd else source_dir |
| 143 | enabled_rules = params["rules"] |
| 144 | |
| 145 | if os.environ.get(LINT_TASK_NAME_ENV, False): |
| 146 | lint_cmd = ["gradle", os.environ[LINT_TASK_NAME_ENV]] |
| 147 | elif os.environ.get(LINT_CMD, None): |
| 148 | lint_cmd = os.environ[LINT_CMD] |
| 149 | else: |
| 150 | lint_cmd = ["gradle", "lint"] |
| 151 | |
| 152 | lint_cmd = PathMgr().format_cmd_arg_list(lint_cmd) |
| 153 | self.print_log("subprocc : %s" % lint_cmd) |
| 154 | sub_proc = SubProcController( |
| 155 | lint_cmd, cwd=build_cwd, stdout_line_callback=self.print_log, stderr_line_callback=self.print_log |
| 156 | ) |
| 157 | sub_proc.wait() |
| 158 | err_log = self.__get_stderr_log(sub_proc) |
| 159 | if err_log.find("FAILURE: Build failed with an exception") != -1: |
| 160 | raise AnalyzeTaskError("lint执行失败,请查看日志排查原因") |
| 161 | self.print_log("%s done." % lint_cmd) |
| 162 | # 执行完扫描后,无法在项目中找到lint文件,但文件却存在,因此sleep3秒 |
| 163 | time.sleep(3) |
| 164 | error_output = [] |
| 165 | issues = [] |
| 166 | LogPrinter.info("lint report find : %s" % source_dir) |
| 167 | for dirpath, _, filenames in os.walk(source_dir): |
| 168 | for f in filenames: |
| 169 | if not isinstance(f, str): |
| 170 | LogPrinter.info("source have not str name file: %s" % str(f)) |
| 171 | f = str(f) |
| 172 | if f.find("lint") >= 0 and f.endswith(".xml"): |
| 173 | error_output.append(os.path.join(dirpath, f)) |
| 174 | LogPrinter.info(u"total get %s result file ." % str(len(error_output))) |
| 175 | for unit in error_output: |
| 176 | LogPrinter.info(u"read issue from %s" % unit) |
| 177 | issues.extend(self.__get_issues_from_file(unit, source_dir)) |
| 178 | LogPrinter.info(u"total get %s issue ." % str(len(issues))) |
| 179 | result = [] |
| 180 | for issue in issues: |
| 181 | if issue["rule"] in enabled_rules: |
| 182 | result.append(issue) |
| 183 | self.print_log(u"Lint found issue: %s" % str(result)) |
| 184 | return result |
| 185 | |
| 186 | def __get_stderr_log(self, sub_proc): |
| 187 | """ |
no test coverage detected