-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_visibility.py
executable file
·83 lines (73 loc) · 2.72 KB
/
test_visibility.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
#!/usr/bin/env python3
#
# Copyright 2024 AVSystem <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import collections
import os
import os.path
import re
import sys
from enum import Enum
FileType = Enum('FileType', 'PUBLIC_HEADER PRIVATE_HEADER PRIVATE_SOURCE')
def classify_file(filename) -> FileType:
if filename.endswith('.h'):
if '/include_public/' in filename:
return FileType.PUBLIC_HEADER
else:
return FileType.PRIVATE_HEADER
else:
return FileType.PRIVATE_SOURCE
def make_replacement(pattern, replacement='', flags=re.MULTILINE):
return (pattern, replacement, flags)
replacements = [
# joins lines on trailing backslash
make_replacement(r'\\\s*\n', replacement=' '),
# removes leading spaces
make_replacement(r'^\s*'),
# removes block comments (incl. multiline)
make_replacement(r'/\*.*?\*/', flags=re.MULTILINE|re.DOTALL),
# removes line comments
make_replacement(r'//.*$'),
# removes preprocessor directives
make_replacement(r'^#.*$'),
# removes trailing spaces
make_replacement(r'\s*$'),
# removes extern function declarations
make_replacement(r'^extern .*\);$'),
# removes empty lines
make_replacement(r'^\s*$')
]
if __name__ == '__main__':
if len(sys.argv) < 2:
print('Error: you must provide path to the file to check')
sys.exit(1)
filename = os.path.realpath(sys.argv[1])
filetype = classify_file(filename)
with open(filename, 'r') as fp:
contents = fp.read()
for pattern, replacement, flags in replacements:
contents = re.sub(pattern, replacement, contents, flags=flags).strip()
lines = contents.split()
valid = False
if len(lines) == 0:
valid = True
elif filetype == FileType.PRIVATE_HEADER:
valid = all((lines[0] == 'VISIBILITY_PRIVATE_HEADER_BEGIN',
lines[-1] == 'VISIBILITY_PRIVATE_HEADER_END'))
elif filetype == FileType.PUBLIC_HEADER:
valid = all((re.match(r'^VISIBILITY', lines[0]) is None,
re.match(r'^VISIBILITY', lines[-1]) is None))
elif filetype == FileType.PRIVATE_SOURCE:
valid = lines[0] == 'VISIBILITY_SOURCE_BEGIN'
sys.exit(0 if valid else 1)