diff options
author | alevitskii <[email protected]> | 2025-09-04 10:00:21 +0300 |
---|---|---|
committer | alevitskii <[email protected]> | 2025-09-04 10:19:32 +0300 |
commit | 8da3e97c84353501738d1ff485708bf8b7185eb8 (patch) | |
tree | dfe1363035f5daa04064c8cdacc3835b3a5cdf90 /tools/cpp_style_checker/wrapper.py | |
parent | 4c3f3af78aac956c2d034edb4a0d7c778b9fe9d3 (diff) |
DEPENDS on linter wrappers to get them exported to opensource
DEPENDS on linter wrappers to get them exported to oss
commit_hash:286fa6981744f667a509749a33afcc3421903842
Diffstat (limited to 'tools/cpp_style_checker/wrapper.py')
-rw-r--r-- | tools/cpp_style_checker/wrapper.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/tools/cpp_style_checker/wrapper.py b/tools/cpp_style_checker/wrapper.py new file mode 100644 index 00000000000..fe09bc6cb3c --- /dev/null +++ b/tools/cpp_style_checker/wrapper.py @@ -0,0 +1,80 @@ +import difflib +import os +import subprocess +import time +from pathlib import PurePath + +from build.plugins.lib.test_const import CLANG_FORMAT_RESOURCE +from library.python.testing.custom_linter_util import linter_params, reporter +from library.python.testing.style import rules + + +def main(): + params = linter_params.get_params() + + if 'custom_clang_format' in params.extra_params: + dep_result = next( + ( + PurePath(params.depends[dep]) + for dep in params.depends + if str(PurePath(dep).parent) == params.extra_params['custom_clang_format'] + ), + None, + ) + if dep_result is None: + raise Exception('Could not find clang-format binary') + + if 'custom_clang_format_bin' in params.extra_params: + # dep_result is not a clang-format binary (package etc) + clang_format_binary = str(dep_result.parent / params.extra_params['custom_clang_format_bin']) + else: + # dep_result is a clang-format binary + clang_format_binary = str(dep_result) + else: + clang_format_binary = os.path.join(params.global_resources[CLANG_FORMAT_RESOURCE], 'clang-format') + + style_config_path = params.configs[0] + + report = reporter.LintReport() + for file_name in params.files: + start_time = time.perf_counter() + status, message = check_file(clang_format_binary, style_config_path, file_name) + elapsed = time.perf_counter() - start_time + report.add(file_name, status, message, elapsed=elapsed) + + report.dump(params.report_file) + + +def check_file(clang_format_binary, style_config_path, filename): + with open(filename, "rb") as f: + actual_source = f.read() + + skip_reason = rules.get_skip_reason(filename, actual_source, skip_links=False) + if skip_reason: + return reporter.LintStatus.SKIPPED, "Style check is omitted: {}".format(skip_reason) + + command = [clang_format_binary, '-assume-filename=' + filename, '-style=file:' + style_config_path] + styled_source = subprocess.check_output(command, input=actual_source) + + if styled_source == actual_source: + return reporter.LintStatus.GOOD, "" + else: + diff = make_diff(actual_source, styled_source) + return reporter.LintStatus.FAIL, diff + + +def make_diff(left, right): + result = "" + for line in difflib.unified_diff(left.decode().splitlines(), right.decode().splitlines(), fromfile='L', tofile='R'): + line = line.rstrip("\n") + if line: + if line[0] == "-": + line = "[[bad]]" + line + "[[rst]]" + elif line[0] == "+": + line = "[[good]]" + line + "[[rst]]" + result += line + "\n" + return result + + +if __name__ == "__main__": + main() |