-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathcheck_envoy_includes.py
executable file
·59 lines (47 loc) · 2.11 KB
/
check_envoy_includes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
"""Ensure that includes are referenced via external/envoy."""
import os
import re
import sys
import subprocess
from pathlib import Path
def _get_inspection_targets_from_dir(dir):
result = []
result.extend(Path(dir).rglob("*.cc"))
result.extend(Path(dir).rglob("*.h"))
return result
def _inspect_line(bazel_output_base, file_path, line):
match = re.findall(r'#include "([^"]*)"', line)
if len(match) == 1:
path = match[0]
if path.startswith("external/") or path.startswith("envoy/"):
return True
found_in_nighthawk_sources = os.path.isfile(path) or os.path.isfile(
"source/" + path) or os.path.isfile("include/" + path)
if not found_in_nighthawk_sources:
alternative_found = False
potential_envoy_path = os.path.join(bazel_output_base, "external/envoy/", path)
alternative_found = os.path.isfile(potential_envoy_path)
if not alternative_found:
potential_envoy_path = os.path.join(bazel_output_base, "external/envoy/source", path)
alternative_found = os.path.isfile(potential_envoy_path)
if alternative_found:
sys.stderr.writelines("Bad include in file " + str(file_path) + ": " + path)
sys.stderr.writelines(" (Possible fixed path: %s" %
potential_envoy_path[len(bazel_output_base) + 1:] + ")\n")
return False
return True
def _inspect_file(bazel_output_base, file_path):
offending_lines = []
with open(str(file_path), encoding='utf-8') as f:
lines = f.readlines()
offending_lines.extend(
line for line in lines if not _inspect_line(bazel_output_base, file_path, line.strip()))
return offending_lines
if __name__ == '__main__':
bazel_output_base = subprocess.run(['bazel', 'info', 'output_base'],
stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
workspace_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
targets = _get_inspection_targets_from_dir(workspace_root)
status_list = list(map(lambda x: _inspect_file(bazel_output_base, x), targets))
sys.exit(-1 if any(status_list) else 0)