aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authoralevitskii <alevitskii@yandex-team.com>2024-11-14 09:02:30 +0300
committeralevitskii <alevitskii@yandex-team.com>2024-11-14 09:14:07 +0300
commit14559389ace814f895db4ee116d884ae70bc7469 (patch)
tree5a8647173c6438875441a01e63593cd4ef708c88 /tools
parent8d4c37b7de13075dfe40beb41c1f807d4754de00 (diff)
downloadydb-14559389ace814f895db4ee116d884ae70bc7469.tar.gz
Add external autoincludes.json
Add external autoincludes.json commit_hash:1b3de1732fee590582ae5620570facc2c0987390
Diffstat (limited to 'tools')
-rw-r--r--tools/cpp_style_checker/__main__.py63
-rw-r--r--tools/cpp_style_checker/ya.make13
2 files changed, 76 insertions, 0 deletions
diff --git a/tools/cpp_style_checker/__main__.py b/tools/cpp_style_checker/__main__.py
new file mode 100644
index 0000000000..f318bb4417
--- /dev/null
+++ b/tools/cpp_style_checker/__main__.py
@@ -0,0 +1,63 @@
+import difflib
+import json
+import subprocess
+import time
+import yaml
+
+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()
+
+ clang_format_binary = params.depends["contrib/libs/clang16/tools/clang-format/clang-format"]
+ style_config_path = params.configs[0]
+
+ with open(style_config_path) as f:
+ style_config = yaml.safe_load(f)
+ style_config_json = json.dumps(style_config)
+
+ report = reporter.LintReport()
+ for file_name in params.files:
+ start_time = time.time()
+ status, message = check_file(clang_format_binary, style_config_json, file_name)
+ elapsed = time.time() - start_time
+ report.add(file_name, status, message, elapsed=elapsed)
+
+ report.dump(params.report_file)
+
+
+def check_file(clang_format_binary, style_config_json, 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=' + style_config_json]
+ 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()
diff --git a/tools/cpp_style_checker/ya.make b/tools/cpp_style_checker/ya.make
new file mode 100644
index 0000000000..b25a270045
--- /dev/null
+++ b/tools/cpp_style_checker/ya.make
@@ -0,0 +1,13 @@
+PY3_PROGRAM()
+
+PEERDIR(
+ contrib/python/PyYAML
+ library/python/testing/custom_linter_util
+ library/python/testing/style
+)
+
+PY_SRCS(
+ __main__.py
+)
+
+END()