Skip to content

Commit 1455938

Browse files
Add external autoincludes.json
Add external autoincludes.json commit_hash:1b3de1732fee590582ae5620570facc2c0987390
1 parent 8d4c37b commit 1455938

File tree

8 files changed

+218
-0
lines changed

8 files changed

+218
-0
lines changed

build/conf/autoincludes.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
"library/cpp/geo",
3+
"util"
4+
]
5+

build/conf/settings.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,5 @@ _FOLDABLE_VARS=\
125125

126126
ARCADIA_TEST_ROOT=../arcadia_tests_data/
127127
DEFAULT_REQUIREMENTS=network:restricted cpu:1 ram:32
128+
129+
AUTOINCLUDE_PATHS=build/conf/autoincludes.json
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import argparse
2+
import json
3+
from dataclasses import dataclass
4+
from typing import Optional
5+
6+
7+
@dataclass
8+
class LinterArgs:
9+
source_root: str
10+
project_path: str
11+
output_path: str
12+
lint_name: str
13+
depends: dict[str, str]
14+
global_resources: dict[str, str]
15+
configs: list[str]
16+
extra_params: dict[str, str]
17+
report_file: str
18+
files: list[str]
19+
20+
21+
def get_params(raw_args: Optional[list[str]] = None) -> LinterArgs:
22+
parser = argparse.ArgumentParser()
23+
parser.add_argument("--params")
24+
parser.add_argument("--source-root")
25+
parser.add_argument("--project-path")
26+
parser.add_argument("--output-path")
27+
parser.add_argument("--lint-name", default="")
28+
parser.add_argument("--depends", action="append")
29+
parser.add_argument("--global-resource", action="append", dest="global_resources")
30+
parser.add_argument("--config", action="append", dest="configs")
31+
parser.add_argument("--extra-param", action="append", dest="extra_params")
32+
parser.add_argument("--report-file", default="-")
33+
parser.add_argument("files", nargs="*")
34+
args = parser.parse_args(raw_args)
35+
36+
if args.params:
37+
with open(args.params) as f:
38+
params = json.load(f)
39+
source_root = params["source_root"]
40+
project_path = params["project_path"]
41+
output_path = params["output_path"]
42+
lint_name = params.get("lint_name", "")
43+
depends = params.get("depends", {})
44+
global_resources = params.get("global_resources", {})
45+
configs = params.get("configs", [])
46+
extra_params = params.get("extra_params", {})
47+
report_file = params["report_file"]
48+
files = params["files"]
49+
else:
50+
source_root = args.source_root
51+
project_path = args.project_path
52+
output_path = args.output_path
53+
lint_name = args.lint_name
54+
depends = _parse_kv_arg(args.depends, ":")
55+
global_resources = _parse_kv_arg(args.global_resources, ":")
56+
configs = args.configs if args.configs else []
57+
extra_params = _parse_kv_arg(args.extra_params, "=")
58+
report_file = args.report_file
59+
files = args.files
60+
61+
return LinterArgs(
62+
source_root=source_root,
63+
project_path=project_path,
64+
output_path=output_path,
65+
lint_name=lint_name,
66+
depends=depends,
67+
global_resources=global_resources,
68+
configs=configs,
69+
extra_params=extra_params,
70+
report_file=report_file,
71+
files=files,
72+
)
73+
74+
75+
def _parse_kv_arg(arg, sep):
76+
result = {}
77+
if arg:
78+
for item in arg:
79+
var, val = item.split(sep, 1)
80+
result[var] = val
81+
return result
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import json
2+
import sys
3+
from enum import Enum
4+
from typing import Optional
5+
6+
7+
class LintStatus(Enum):
8+
GOOD = "GOOD"
9+
FAIL = "FAIL"
10+
SKIPPED = "SKIPPED"
11+
12+
13+
class LintReport():
14+
def __init__(self):
15+
self._report = {}
16+
17+
def add(self, file_name: str, status: LintStatus, message: str = "", elapsed: float = 0.0):
18+
self._report[file_name] = {
19+
"status": status.value,
20+
"message": message,
21+
"elapsed": elapsed,
22+
}
23+
24+
def dump(self, report_file, pretty: Optional[bool] = None):
25+
data = {
26+
"report": self._report,
27+
}
28+
if report_file == "-":
29+
if pretty is None:
30+
pretty = True
31+
self._do_dump(sys.stdout, data, pretty)
32+
else:
33+
with open(report_file, "w") as f:
34+
self._do_dump(f, data, pretty)
35+
36+
@staticmethod
37+
def _do_dump(dest, data, pretty):
38+
indent = 4 if pretty else None
39+
json.dump(data, dest, indent=indent)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
PY3_LIBRARY()
2+
3+
PY_SRCS(
4+
linter_params.py
5+
reporter.py
6+
)
7+
8+
END()
9+
10+
RECURSE_FOR_TESTS(
11+
tests
12+
)

tools/cpp_style_checker/__main__.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import difflib
2+
import json
3+
import subprocess
4+
import time
5+
import yaml
6+
7+
from library.python.testing.custom_linter_util import linter_params, reporter
8+
from library.python.testing.style import rules
9+
10+
11+
def main():
12+
params = linter_params.get_params()
13+
14+
clang_format_binary = params.depends["contrib/libs/clang16/tools/clang-format/clang-format"]
15+
style_config_path = params.configs[0]
16+
17+
with open(style_config_path) as f:
18+
style_config = yaml.safe_load(f)
19+
style_config_json = json.dumps(style_config)
20+
21+
report = reporter.LintReport()
22+
for file_name in params.files:
23+
start_time = time.time()
24+
status, message = check_file(clang_format_binary, style_config_json, file_name)
25+
elapsed = time.time() - start_time
26+
report.add(file_name, status, message, elapsed=elapsed)
27+
28+
report.dump(params.report_file)
29+
30+
31+
def check_file(clang_format_binary, style_config_json, filename):
32+
with open(filename, "rb") as f:
33+
actual_source = f.read()
34+
35+
skip_reason = rules.get_skip_reason(filename, actual_source, skip_links=False)
36+
if skip_reason:
37+
return reporter.LintStatus.SKIPPED, "Style check is omitted: {}".format(skip_reason)
38+
39+
command = [clang_format_binary, '-assume-filename=' + filename, '-style=' + style_config_json]
40+
styled_source = subprocess.check_output(command, input=actual_source)
41+
42+
if styled_source == actual_source:
43+
return reporter.LintStatus.GOOD, ""
44+
else:
45+
diff = make_diff(actual_source, styled_source)
46+
return reporter.LintStatus.FAIL, diff
47+
48+
49+
def make_diff(left, right):
50+
result = ""
51+
for line in difflib.unified_diff(left.decode().splitlines(), right.decode().splitlines(), fromfile='L', tofile='R'):
52+
line = line.rstrip("\n")
53+
if line:
54+
if line[0] == "-":
55+
line = "[[bad]]" + line + "[[rst]]"
56+
elif line[0] == "+":
57+
line = "[[good]]" + line + "[[rst]]"
58+
result += line + "\n"
59+
return result
60+
61+
62+
if __name__ == "__main__":
63+
main()

tools/cpp_style_checker/ya.make

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
PY3_PROGRAM()
2+
3+
PEERDIR(
4+
contrib/python/PyYAML
5+
library/python/testing/custom_linter_util
6+
library/python/testing/style
7+
)
8+
9+
PY_SRCS(
10+
__main__.py
11+
)
12+
13+
END()

util/ya.common

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
IF (NOT USE_SYSTEM_PYTHON)
2+
STYLE_CPP()
3+
ENDIF()

0 commit comments

Comments
 (0)