-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathlib_search.py
302 lines (273 loc) · 8.54 KB
/
lib_search.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# Copyright (c) 2020-2022 Intel Corporation
#
# 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 os
import sys
import re
COPYRIGHT = re.compile(r'(C|c)opyright (\((c|C)\) )?(20(0|1|2)([0-9])-)?(20(0|1|2)([0-9]),)*20(0|1|2)([0-9])(,)? ([a-z]|[A-Z]|[0-9]| )*')
FORBIDDEN_FUNCTIONS = re.compile(r'setjmp\(|longjmp\(|getwd\(|strlen\(|wcslen\(|gets\(|strcpy\(|wcscpy\(|strcat\(|wcscat\(|sprintf\(|vsprintf\(|asctime\(')
def check_header(fd):
detected = False
try:
for line in fd:
if COPYRIGHT.findall(line):
detected = True
break
except:
print("ERROR: Cannot parse file:" + str(fd))
return detected
def check_function(fd):
# Add space separated exceptions for given file in the dictionary
fix_applied = {"./src/test/ensemble_flow_custom_node_tests.cpp":"size_t strLen = std::strlen(str);size_t prefixLen = std::strlen(prefix);",}
detected = False
try:
for line in fd:
found = FORBIDDEN_FUNCTIONS.findall(line)
if found:
if line.trim() in fix_applied[fd.name]:
#It's ok fix and check was applied and verified
continue
detected = True
print("ERROR: Forbidden function detected in:" + str(fd.name))
print("Line start:" + str(line) + "End")
print("Function:" + str(found))
break
except:
print("ERROR: Cannot parse file:" + str(fd))
return detected
def check_dir(start_dir):
no_header = []
exclude_files = [
'.user.bazelrc',
'.bandit',
'.bin',
'.dockerignore',
'.git',
'.groovy',
'.jpeg',
'.jpg',
'.json',
'.md',
'.npy',
'.pdf',
'.png',
'.proto',
'.pytest_cache',
'.svg',
'.tar.gz',
'.venv',
'.wav',
'.log',
'.xml',
'Doxyfile',
'LICENSE',
'REST_age_gender.ipynb',
'__pycache__',
'abseil_gcc_8.5_constant_expression.patch',
'azure_sdk.patch',
'cb.patch',
'bazel-',
'check_coverage.bat',
'cleanup_jenkins.bat',
'genhtml',
'clang-format',
'client_requirements.txt',
'cppclean_src',
'cppclean_test',
'docx',
'dummy.xml',
'forbidden_functions.txt',
'gif',
'index.html',
'input_images.txt',
'third_party/python/BUILD',
'libevent/BUILD',
'listen.patch',
'metrics_output.out',
'missing_headers.txt',
'net_http.patch',
'partial.patch',
'ovms_drogon_trantor.patch',
'opencv_cmake_flags.txt',
'ovms-c/dist',
'requirements.txt',
'requirements_win.txt',
'resnet_images.txt',
"resnet_labels.txt",
'rest_sdk_v2.10.16.patch',
'tf.patch',
'tf_graph_info_multilinecomment.patch',
'tftext.patch',
'upb_platform_fix.patch',
'upb_warning_turn_off.patch',
'partial_2.18.patch',
'tf_2.18_logging.patch',
'vehicle_images.txt',
'bazel_rules_apple.patch',
"go.sum",
"mwaitpkg.patch",
'saved_model.pb',
'tflite',
"yarn.lock",
"BUILD.bazel",
"package.json",
"graph.pbtxt",
"graph_gpu.pbtxt",
"holistic_tracking.pbtxt",
"ssdlite_object_detection_labelmap.txt",
"build_dependencies.sh",
"iris_tracking.pbtxt",
"graph_two_inputs_model.pbtxt",
"aipc.txt",
"internal_tests",
"pugixml_v1.13_flags.patch",
"rag_demo.ipynb",
".bazelversion",
"lib_files.txt",
"lib_files_python.txt",
"lib_custom_nodes_files",
"spelling-whitelist.txt",
"results.txt",
]
exclude_directories = ['/dist/', 'release_files/thirdparty-licenses']
for (d_path, _, file_set) in os.walk(start_dir):
for f_name in file_set:
skip = False
for excluded in exclude_directories:
if excluded in d_path:
skip = True
print('Warning - Skipping directory - ' + d_path + ' for file - ' + f_name)
break
if skip:
continue
fpath = os.path.join(d_path, f_name)
if not [test for test in exclude_files if test in fpath]:
with open(fpath, 'r') as fd:
header_detected = check_header(fd)
if not header_detected:
no_header.append(fpath)
return no_header
def check_func(start_dir):
not_ok = []
exclude_files = [
'.bin',
'.git',
'.groovy',
'.jpeg',
'.jpg',
'.json' ,
'.npy',
'.png',
'.pytest_cache',
'.svg',
'.tar.gz',
'.venv',
'.vscode',
'.xml',
'Doxyfile',
'REST_age_gender.ipynb',
'__pycache__',
'abseil_gcc_8.5_constant_expression.patch',
'azure_sdk.patch',
'bazel-',
'boost.LICENSE.txt',
'c-ares.LICENSE.txt',
'check_coverage.bat',
'genhtml',
'clang-format',
'client_requirements.txt',
'docx',
'forbidden_functions.txt',
'input_images.txt',
'third_party/python/BUILD',
'libevent/BUILD',
'libuuid.LICENSE.txt',
'license.txt',
'listen.patch',
'md',
'metrics_output.out',
'missing_headers.txt',
'missing_headers.txt',
'net_http.patch',
'partial.patch',
'ovms_drogon_trantor.patch',
'openvino.LICENSE.txt',
'ovms-c/dist',
'requirements.txt',
'requirements_win.txt',
'rest_sdk_v2.10.16.patch',
'tf.patch',
'tf_graph_info_multilinecomment.patch',
'tftext.patch',
'partial_2.18.patch',
'tf_2.18_logging.patch',
'zlib.LICENSE.txt',
'bazel_rules_apple.patch',
'yarn.lock',
'BUILD.bazel',
'package.json',
'graph.pbtxt',
'graph_gpu.pbtxt',
"build_dependencies.sh",
"iris_tracking.pbtxt",
"internal_tests",
'cleanup_jenkins.bat',
".bazelversion",
]
exclude_directories = ['/dist/']
for (d_path, _, file_set) in os.walk(start_dir):
for f_name in file_set:
skip = False
for excluded in exclude_directories:
if excluded in d_path:
skip = True
print('Warning - Skipping directory - ' + d_path + ' for file - ' + f_name)
break
if skip:
continue
fpath = os.path.join(d_path, f_name)
if not [test for test in exclude_files if test in fpath]:
with open(fpath, 'r') as fd:
detected = check_function(fd)
if detected:
not_ok.append(fpath)
return not_ok
def main():
if len(sys.argv) < 2:
print('Provide start dir!')
else:
start_dir = sys.argv[1]
print('Provided start dir:' + start_dir)
if len(sys.argv) > 2 and sys.argv[2] == 'functions':
print("Check for forbidden functions")
forbidden_func = check_func(start_dir)
if len(forbidden_func) == 0:
print('Success: All files checked for forbidden functions')
else:
print('#########################')
print('## Forbidden functions detected:')
for forbid_func in forbidden_func:
print(f'{forbid_func}')
else:
print("Check for missing headers")
no_header_set = check_dir(start_dir)
if len(no_header_set) == 0:
print('Success: All files have headers')
else:
print('#########################')
print('## No header files detected:')
for no_header in no_header_set:
print(f'{no_header}')
if __name__ == '__main__':
main()