-
Notifications
You must be signed in to change notification settings - Fork 1
/
bazel.py
92 lines (71 loc) · 2.63 KB
/
bazel.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/python3
import re
RULE = re.compile('^(\w+)\($')
NAME = re.compile('\s+name = "(.+)"')
CRATE_NAME = re.compile('\s+crate_name = "(.+)"')
CRATE = re.compile('\s+crate = "(.+)"')
SRCS_GLOB = re.compile('\s+srcs = (glob\(\[.+),')
SRCS_NO_GLOB = re.compile('\s+srcs = (\[.+\]),')
def loads(text):
lines = text.split('\n')
result = []
entry = {}
for line in lines:
if match := RULE.match(line):
entry['rule'] = match.group(1)
continue
if match := NAME.match(line):
entry['name'] = match.group(1)
continue
if match := SRCS_GLOB.match(line):
entry['srcs'] = match.group(1)
continue
if match := SRCS_NO_GLOB.match(line):
entry['srcs'] = match.group(1)
continue
if match := CRATE_NAME.match(line):
entry['crate_name'] = match.group(1)
continue
if match := CRATE.match(line):
entry['crate'] = match.group(1)
continue
if line == ')':
if entry.get('rule'):
result.append(entry)
entry = {}
continue
return result
def is_bazelized_bin_or_lib(package_name, data):
def _check(name, data):
binaries_or_libs = [
x for x in data if x.get('rule') in ['rust_library', 'rust_binary', 'rust_proc_macro', 'rust_canister']
]
for x in binaries_or_libs:
if name in [x.get('name'), x.get('crate_name')]:
return True
return False
crate_name = package_name.replace('-', '_')
return _check(package_name, data) or _check(crate_name, data)
def is_bazelized_test(package_name, data):
def _check(name, data):
binaries_or_libs = [
x for x in data if x.get('rule') in ['rust_library', 'rust_binary', 'rust_proc_macro']
]
tests_or_suites = [
x for x in data if x.get('rule') in ['rust_test', 'rust_test_suite']
]
for test in tests_or_suites:
test_crate = test.get('crate')
if test_crate is None:
if 'tests/' in test.get('srcs', ''):
return True
if 'test/' in test.get('srcs', ''):
return True
continue
test_crate = test_crate.replace(':', '')
for bin in binaries_or_libs:
if test_crate == bin.get('name') and name in [bin.get('name'), bin.get('crate_name')]:
return True
return False
crate_name = package_name.replace('-', '_')
return _check(package_name, data) or _check(crate_name, data)