-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_extractor.py
More file actions
67 lines (50 loc) · 1.92 KB
/
function_extractor.py
File metadata and controls
67 lines (50 loc) · 1.92 KB
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
import ast
import os
import shutil
import json
from pathlib import Path
from zipfile import ZipFile
def __get_function(function_node) -> dict:
function = {'function': function_node.name}
#print("\tFunction name:", function_node.name)
#print("\tArgs:")
args = function_node.args.args
function['args'] = []
for arg in args:
#print("\t\tParameter name:", arg.arg)
function['args'].append(arg.arg)
return function
def __get_modules(handler_path) -> list:
counter: int = 0
modules = []
for filename in os.listdir(handler_path):
if filename.endswith('.py'):
with open(os.path.join(handler_path, filename)) as file:
node = ast.parse(file.read())
#print()
#print('Module:', filename)
modules.append({'module': filename, 'functions': []})
functions = [n for n in node.body if isinstance(n, ast.FunctionDef)]
for function in functions:
modules[counter]['functions'].append(__get_function(function))
counter += 1
return modules
def __empty_temp_folder():
folder: str = 'temp'
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
def get_function_names(zip_path) -> json:
zip_filename: str = Path(zip_path).stem
ZipFile(zip_path, 'r').extractall(os.path.dirname(zip_path))
os.rename(zip_path, zip_path+'.input')
folder: str = os.path.join(os.path.dirname(zip_path), zip_filename)
result = {'project': zip_filename, 'modules': __get_modules(folder)}
#__empty_temp_folder()
return json.dumps(result)