Debian Codegraph @

misc/tools/kemi/lua/kemiluacheck.py

raw
1 # tool to generate the modules.md content
2 #
3
4 import os, json, sys, time, fnmatch, re, importlib
5
6 PATH_GENERATED_OBJ = "KSR.luacheckrc"
7
8 class ModuleObjGenerator(object):
9
10 # Contains the output until it should be written to disk
11 object_string = ""
12
13 def execute(self, data):
14 # Validate that we got some methods back. 155 is an arbitrary large number.
15 if len(data) < 1:
16 print("ERR: Invalid data")
17 exit()
18
19 functions_parsed = self.parse_function_list(data)
20 self.output_obj(functions_parsed)
21
22 print ("File created successfully at " + PATH_GENERATED_OBJ)
23
24 def parse_function_list(self, functions):
25 data = {}
26
27 for elem in functions:
28 module = elem["module"]
29
30 # TODO: What about the hdr, pv, x sub-module?
31 if module == "":
32 module = "_core"
33
34 if module not in data:
35 data[module] = []
36
37 data[module].append({"name": elem["name"], "return": elem["ret"], "params": elem["params"]})
38
39 return data
40
41 def output_obj(self, data):
42
43 for key in sorted(data):
44 methods = data[key]
45 # Sort the functions by name alphabetically
46 methods = sorted(methods, key = lambda k: k['name'])
47 self.object_module_content(key, methods)
48
49 self.obj_write()
50
51 return True
52
53 def object_module_content(self, module, methods):
54 if module != "_core":
55 self.object_string += "\t\t\t\t" + module + " = {\n\t\t\t\t\tfields= {\n"
56
57 for value in methods:
58 if module != "_core":
59 self.object_string += "\t\t\t\t\t\t" + value["name"] + " = {},\n"
60 else:
61 self.object_string += "\t\t\t\t" + value["name"] + " = {},\n"
62
63 if module != "_core":
64 self.object_string += "\t\t\t\t\t},\n\t\t\t\t},\n"
65 return True
66
67 def obj_write(self):
68 f = open(PATH_GENERATED_OBJ, "w")
69 f.write("stds.KSR = {\n\tread_globals = {\n\t\tKSR = {\n\t\t\tfields = {\n")
70 f.write(self.object_string)
71 f.write("\t\t\t\tx = {\n\t\t\t\t\tfields= {\n")
72 f.write("\t\t\t\t\t\tdrop= {},\n")
73 f.write("\t\t\t\t\t\texit= {},\n")
74 f.write("\t\t\t\t\t\tmodf= {},\n")
75 f.write("\t\t\t\t\t},\n\t\t\t\t},\n")
76 f.write("\t\t\t}\n\t\t}\n\t}\n}\n")
77 f.write("\nstd = \"min+KSR\"\n")
78 f.close()
79 return True
80
81 def read_file_to_string(self, filename):
82 path = self.PATH_MODULES_DOCS + filename
83 if os.path.isfile(path):
84 with open(path, 'r') as myfile:
85 return myfile.read() + "\n"
86 return ""
87
88
89 class KemiFileExportParser(object):
90 # These functions are created by a macro so makes the parsing somewhat tricky,
91 # for now they are statically defined
92 macro_functions = {
93 "t_set_auto_inv_100": "int state",
94 "t_set_disable_6xx": "int state",
95 "t_set_disable_failover": "int state",
96 "t_set_no_e2e_cancel_reason": "int state",
97 "t_set_disable_internal_reply": "int state"
98 }
99
100 # These files export the KEMI functions in a special way so we map them manually
101 # TODO: Discuss with @miconda if core/HDR/pv/x should be added as well or not
102 special_exports = [
103 {"filename": "kemi.c", "export": "_sr_kemi_core", "folder": "/src/core/"},
104 {"filename": "kemi.c", "export": "_sr_kemi_hdr", "folder": "/src/core/"},
105 {"filename": "kemi.c", "export": "_sr_kemi_pv", "folder": "/src/core/"},
106 #{"filename": "app_lua_mod.c", "export": "sr_kemi_app_lua_rpc_exports", "folder": "/modules/app_lua/"}
107 ]
108
109 def generate_kemi_export_list(self, source_path):
110 files = self.list_c_files_in_directory(source_path)
111 lists = []
112
113 for file in files:
114 with open(file, 'r', encoding='utf-8', errors='ignore') as f:
115 lines = f.readlines()
116
117 export_name = self.find_c_file_kemi_export(file, lines)
118 if export_name:
119 export_functions = self.extract_c_file_kemi_export_lines(file, lines, export_name)
120 lists = lists + export_functions
121 print ("Found ", len(export_functions), "functions", "Total:", len(lists))
122
123 # Handle some special files separately
124 for elem in self.special_exports:
125 file = source_path + elem["folder"] + elem["filename"]
126 with open(file) as f:
127 lines = f.readlines()
128 lists = lists + self.extract_c_file_kemi_export_lines(file, lines, elem["export"])
129
130 return lists
131
132 def find_c_file_kemi_export(self, filename, lines):
133 param = None
134
135 for line in lines:
136 if line.find("sr_kemi_modules_add") >= 0:
137 line = line.lstrip(" ")
138 line = line.lstrip("\t")
139 if line.find("sr_kemi_modules_add") == 0:
140 param = line[line.find("(") + 1:line.find(")")]
141 print ("INFO: ---- Found export", filename, param)
142 break
143 else:
144 if line != "int sr_kemi_modules_add(sr_kemi_t *klist)\n":
145 print ("ERR: Possible error at line: ", filename, line)
146 exit()
147
148 return param
149
150 def extract_c_file_kemi_export_lines(self, filename, lines, export_name):
151 list_functions = []
152 find_start = True
153
154 for line in lines:
155 if find_start and line.find("static sr_kemi_t " + export_name + "[]") >= 0:
156 find_start = False
157 elif not find_start:
158 if line.find("};") >= 0:
159 break
160 line = line.lstrip(" \t")
161 line = line.rstrip()
162 list_functions.append(line)
163
164 if len(list_functions) < 1:
165 print ("ERR: Couldn't parse file for exported functions: ", export_name)
166 exit()
167
168 parsed_list = self.parse_kemi_export_c_lines(filename, list_functions)
169
170 return parsed_list
171
172 def parse_kemi_export_c_lines(self, filename, lines):
173 elements = []
174 function_lines = []
175 temp_function = ""
176
177 # We look for str_init which would be the start of each exported function
178 for line in lines:
179 if line.find("str_init") >= 0:
180 if temp_function != "":
181 function_lines.append(temp_function)
182 temp_function = ""
183 temp_function += line
184
185 if temp_function != "":
186 function_lines.append(temp_function)
187
188 # Now we parse each exported function to extract its declaration
189 for func in function_lines:
190 function_lines_split = func.split(",{")
191
192 if len(function_lines_split) < 2:
193 print ("ERR: Incorrect function line", func)
194 exit()
195
196 declarations = function_lines_split[0].split(",")
197 params = function_lines_split[1]
198
199 # Extract the content from the definitions
200 val_module = declarations[0]
201 val_module = val_module[val_module.find('("') + 2:val_module.find('")')]
202 val_function = declarations[1]
203 val_function = val_function[val_function.find('("') + 2:val_function.find('")')]
204
205 if declarations[2] == "SR_KEMIP_INT":
206 val_return = "int"
207 elif declarations[2] == "SR_KEMIP_STR":
208 val_return = "string"
209 elif declarations[2] == "SR_KEMIP_NONE":
210 val_return = "void"
211 elif declarations[2] == "SR_KEMIP_BOOL":
212 val_return = "bool"
213 elif declarations[2] == "SR_KEMIP_XVAL":
214 val_return = "xval"
215 else:
216 print("ERR: Invalid return value", declarations[2], func)
217 exit()
218
219 val_c_function = declarations[3].strip()
220
221 # Count how many parameters the KEMI C function expects
222 val_params = []
223 itr = 0
224 for val in params.rstrip("},").split(","):
225 itr += 1
226 # KEMI function has a maximum of 6 params
227 if itr > 6:
228 break
229 pm = val.strip()
230 if pm == "SR_KEMIP_INT":
231 val_params.append("int")
232 elif pm == "SR_KEMIP_STR":
233 val_params.append("str")
234 elif pm == "SR_KEMIP_NONE":
235 continue
236 else:
237 print("Invalid return value", declarations[2], func)
238 exit()
239
240 if itr != 6:
241 print("ERR: Couldn't iterate the params: ", params)
242 exit()
243
244 param_string = self.find_c_function_params(filename, val_c_function, val_return)
245 param_string = self.prettify_params_list(val_function, param_string, val_params)
246
247 elements.append({"module": val_module, "name": val_function, "ret": val_return, "params": param_string})
248
249 return elements
250
251 def prettify_params_list(self, function_name, function_declaration, kemi_types):
252 # Validate the quantity and types of declaration vs export
253 if function_declaration == "" and len(kemi_types) == 0:
254 return ""
255
256 params = function_declaration.split(",")
257
258 if params[0].find("sip_msg_t") >= 0 or params[0].find("struct sip_msg") >= 0:
259 params.pop(0)
260
261 if len(params) != len(kemi_types):
262 print("ERR: Mismatching quantity of params. Declaration for", function_name, ":", function_declaration, "KEMI:", kemi_types)
263 exit()
264
265 for declared, type in zip(params, kemi_types):
266 declared = declared.replace("*", "")
267 declared = declared.strip().split(" ")[0]
268 if declared != type:
269 print("ERR: Mismatching type of params for", function_name, ":", function_declaration, " | ", kemi_types, " | Declared: ", declared, " Type: ", type)
270 exit()
271
272 param_string = ""
273
274 for param in params:
275 param = param.strip()
276 param = param.replace("*", "")
277 if param[:3] == "str":
278 temp = param.split(" ")
279 param = "p_" + temp[1]
280 param_string += param + ", "
281
282 # Clean up the presentation of the params
283 param_string = param_string.rstrip(", ")
284 return param_string
285
286 def find_c_function_params(self, filename, function_name, return_type):
287 # First we try with the same file to find the declaration
288 param_string = self.search_file_for_function_declaration(filename, function_name, return_type)
289 # If we couldn't find it, let's try all files in the same folder as the first file
290 if param_string:
291 return param_string
292 else:
293 files = self.list_c_files_in_directory(os.path.dirname(filename))
294 for file in files:
295 param_string = self.search_file_for_function_declaration(file, function_name, return_type)
296 if param_string:
297 return param_string
298
299 if function_name in self.macro_functions:
300 return self.macro_functions[function_name]
301
302 print("ERR: Couldn't find the function declaration", filename, function_name, return_type)
303 exit()
304
305 def search_file_for_function_declaration(self, filename, function_name, return_type):
306 # print "Searching file", filename, "for", function_name
307 with open(filename, 'r', encoding='utf-8', errors='ignore') as f:
308 lines = f.readlines()
309
310 param_string = None
311 found = False
312 temp_string = ""
313 return_match = return_type
314
315 # KEMI has some magic where most functions actually return INTs but KEMI maps them to void/bool
316 if return_type == "void" or return_type == "bool":
317 return_match = "int"
318
319 if return_type == "xval":
320 return_match = "sr_kemi_xval_t([ \t])*\*"
321
322 # Look for declarations in format: static? return_type function_name(
323 r = re.compile("^(?:static )?" + return_match + "[ \t]*(" + function_name + ")[ \t]*\(")
324 for line in lines:
325 m = r.match(line)
326 if m:
327 found = True
328 if found:
329 temp_string += line
330 if line.find("{") >= 0:
331 param_string = temp_string[temp_string.find('(') + 1:temp_string.find(')')]
332 break
333
334 return param_string
335
336 def list_c_files_in_directory(self, path):
337 matches = []
338 for root, dirnames, filenames in os.walk(path):
339 for filename in fnmatch.filter(filenames, '*.c'):
340 matches.append(os.path.join(root, filename))
341 return matches
342
343
344 if __name__ == "__main__":
345 try:
346 if not os.path.isdir(sys.argv[1]):
347 raise Exception('Not a valid directory')
348 except:
349 print("Please provide the path to the Kamailio src folder as the first argument")
350 exit()
351
352 print("Parsing the source")
353 parser = KemiFileExportParser()
354 data = parser.generate_kemi_export_list(sys.argv[1].rstrip("/"))
355 fgen = ModuleObjGenerator()
356 fgen.execute(data)
357 print("Done")