From 9df738cad338ccbb2fcbe119ec443544143f936d Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Fri, 20 Sep 2024 08:45:36 +0200 Subject: [PATCH 1/3] Name the linters, and add them only if available Complements: 0cf3bbadc6931036e8e69a50e0786756be4f7375 --- vcs-diff-lint | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/vcs-diff-lint b/vcs-diff-lint index f54270b..1544898 100755 --- a/vcs-diff-lint +++ b/vcs-diff-lint @@ -65,6 +65,7 @@ def file_type(filename): class _Linter: + name = "unknown" filetype = None path_filters = None linter_tags: List[str] = [] @@ -79,6 +80,11 @@ class _Linter: """ if the paths in linter output need adjustments """ return old, new + @classmethod + def is_ready(cls): + """ Return True if the linter is available on host and working """ + return True + def _sed_filter(self): if not self.renames: return None @@ -141,6 +147,7 @@ class PylintLinter(_Linter): """ Generate pyilnt error output that is compatible with 'csdiff'. """ + name = "Pylint" linter_tags = [ "pylint", "python", @@ -149,6 +156,10 @@ class PylintLinter(_Linter): def is_compatible(self, file): return file.type == 'python' + @classmethod + def is_ready(cls): + return os.path.exists("/usr/bin/pylint") + def command(self, projectdir, filenames): abs_pylintrc = os.path.join(self.gitroot, projectdir, 'pylintrc') pylintrc = os.path.realpath(abs_pylintrc) @@ -163,6 +174,7 @@ class MypyLinter(_Linter): """ Generate Mypy error output compatible with 'csdiff'. """ + name = "Mypy" linter_tags = [ "mypy", "python", @@ -171,6 +183,10 @@ class MypyLinter(_Linter): def is_compatible(self, file): return file.type == 'python' + @classmethod + def is_ready(cls): + return os.path.exists("/usr/bin/mypy") + def command(self, projectdir, filenames): cmd = [CSDIFF_MYPY] + filenames return cmd, {} @@ -180,12 +196,17 @@ class RuffLinter(_Linter): """ Generate Ruff error output compatible with 'csdiff'. """ + name = "Ruff" linter_tags = [ "ruff", "python", ] full_project_scan = True + @classmethod + def is_ready(cls): + return os.path.exists("/usr/bin/ruff") + def is_compatible(self, file): return file.type == 'python' @@ -357,10 +378,13 @@ class _Worker: # pylint: disable=too-few-public-methods for pattern in self.options.linter_tags: for tag in linter.linter_tags: if fnmatch.fnmatch(tag, pattern): - selected_linters.add(linter) found = True break if found: + if linter.is_ready(): + selected_linters.add(linter) + else: + log.info("Not adding %s, not ready", linter.name) break for linterClass in selected_linters: From 5d59630a6f83be4ef356873b028b4e58178c14cb Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Fri, 20 Sep 2024 08:47:05 +0200 Subject: [PATCH 2/3] Nicer info/debug output INFO: Ruff is linting in /tmp/vcs-diff-lint-lrt3kl9e/new_dir/. - commit 9df738c INFO: Ruff is linting in /tmp/vcs-diff-lint-lrt3kl9e/old_dir/. - commit 0cf3bba INFO: Pylint is linting in /tmp/vcs-diff-lint-lrt3kl9e/new_dir/. - commit 9df738c INFO: Pylint is linting in /tmp/vcs-diff-lint-lrt3kl9e/old_dir/. - commit 0cf3bba --- vcs-diff-lint | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/vcs-diff-lint b/vcs-diff-lint index 1544898..fb740f8 100755 --- a/vcs-diff-lint +++ b/vcs-diff-lint @@ -122,12 +122,13 @@ class _Linter: os.chdir(abs_projectdir) sha1_cmd = ['git', 'rev-parse', '--short', 'HEAD'] sha1 = check_output(sha1_cmd).decode("utf-8").strip() - log.info("linting in %s - commit %s", abs_projectdir, sha1) + log.info("%s is linting in %s - commit %s", self.name, + abs_projectdir, sha1) files = [f.filename for f in files if self.is_compatible(f)] if not files and not self.full_project_scan: return linter_cmd, linter_env = self.command(projectdir, files) - log.debug("Running linter command: %s", linter_cmd) + log.debug("%s command: %s", self.name, linter_cmd) env = os.environ.copy() env.update(linter_env) sed_cmd = self._sed_filter() From ae14ba9533c8372adfded11ad41d00870fc9b68e Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Fri, 20 Sep 2024 08:58:54 +0200 Subject: [PATCH 3/3] lint: break large method --- vcs-diff-lint | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/vcs-diff-lint b/vcs-diff-lint index fb740f8..c304651 100755 --- a/vcs-diff-lint +++ b/vcs-diff-lint @@ -314,12 +314,7 @@ class _Worker: # pylint: disable=too-few-public-methods return path = os.path.normpath(os.path.join(path, '..')) - def _run_linters(self, old_report_fd, new_report_fd): - # pylint: disable=too-many-locals - lookup = get_rename_map(self.options, self.projectsubdir) - if not lookup: - return - + def _prepare_linted_dirs(self): # prepare the old checkout old_gitroot = os.path.join(self.workdir, 'old_dir') new_gitroot = os.path.join(self.workdir, 'new_dir') @@ -333,18 +328,28 @@ class _Worker: # pylint: disable=too-few-public-methods finally: os.chdir(ret_cwd) + # prepare the new checkout try: os.chdir(new_gitroot) with Popen(["git", "-C", origin_from, "diff", "--cached"], stdout=PIPE) as diff: check_call(["git", "apply", "--allow-empty", "--index", "-"], - stdin=diff.stdout) + stdin=diff.stdout) with Popen(["git", "-C", origin_from, "diff"], stdout=PIPE) as diff: check_call(["git", "apply", "--allow-empty", "-"], - stdin=diff.stdout) + stdin=diff.stdout) finally: os.chdir(ret_cwd) + return old_gitroot, new_gitroot + + def _run_linters(self, old_report_fd, new_report_fd): + # pylint: disable=too-many-locals + lookup = get_rename_map(self.options, self.projectsubdir) + if not lookup: + return + + old_gitroot, new_gitroot = self._prepare_linted_dirs() def add_file(gitroot, files, filename): if not filename: