(self, params)
| 37 | self.sensitive_word_maps = {"checkstyle": "Tool", "Checkstyle": "Tool"} |
| 38 | |
| 39 | def analyze(self, params): |
| 40 | source_dir = params["source_dir"] |
| 41 | envs = os.environ |
| 42 | rule_list = params["rule_list"] |
| 43 | incr_scan = params["incr_scan"] |
| 44 | path_exclude = params.path_filters.get("exclusion", []) |
| 45 | path_include = params.path_filters.get("inclusion", []) |
| 46 | checkstyle_home = envs.get("CHECKSTYLE_HOME") |
| 47 | |
| 48 | # analyze step |
| 49 | config_file = self.generate_config(rule_list) |
| 50 | |
| 51 | # 指定内置的配置文件 |
| 52 | config_type = envs.get("CHECKSTYLE_TYPE", None) |
| 53 | if config_type: |
| 54 | config_file = os.path.join(checkstyle_home, "config", "%s_checks.xml" % config_type) |
| 55 | # 指定代码库中的checkstyle配置文件 |
| 56 | if "CHECKSTYLE_CONFIG" in envs: |
| 57 | config_file = os.path.join(source_dir, envs.get("CHECKSTYLE_CONFIG")) |
| 58 | LogPrinter.info("config file: %s" % config_file) |
| 59 | |
| 60 | scan_cmd = [ |
| 61 | os.path.join(os.environ["JDK_8_HOME"], "bin", "java"), |
| 62 | "-jar", |
| 63 | os.path.join(os.environ["CHECKSTYLE_HOME"], "checkstyle.jar"), |
| 64 | "-c", |
| 65 | config_file, |
| 66 | "--debug", |
| 67 | ] |
| 68 | |
| 69 | if path_exclude: |
| 70 | for path in path_exclude: |
| 71 | # 这里是为了适配win下路径分隔符\的情况,才需要path.replace("/", os.sep) |
| 72 | scan_cmd.append('--exclude-regexp="%s"' % path.replace("/", os.sep)) |
| 73 | |
| 74 | # 支持增全量 |
| 75 | toscans = [] |
| 76 | want_suffix = ".java" |
| 77 | if incr_scan: |
| 78 | diffs = SCMMgr(params).get_scm_diff() |
| 79 | toscans = [ |
| 80 | os.path.join(source_dir, diff.path).replace(os.sep, "/") |
| 81 | for diff in diffs |
| 82 | if diff.path.endswith(tuple(want_suffix)) and diff.state != "del" |
| 83 | ] |
| 84 | relpos = len(source_dir) + 1 |
| 85 | toscans = FilterPathUtil(params).get_include_files(toscans, relpos) |
| 86 | else: |
| 87 | toscans = [source_dir] |
| 88 | # sh参数个数有限制 |
| 89 | if sys.platform == "win32" and len(max(toscans)) > 32500: |
| 90 | toscans = [source_dir] |
| 91 | |
| 92 | if not toscans: |
| 93 | LogPrinter.debug("To-be-scanned files is empty ") |
| 94 | return [] |
| 95 | |
| 96 | issues = ThreadRunner(scan_cmd, source_dir, params).run(toscans) |
nothing calls this directly
no test coverage detected