-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPyFuncList.py
97 lines (83 loc) · 2.93 KB
/
getPyFuncList.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
import importlib
import sys
import platform
import pyflink
# import ast
# from pathlib import Path
# from typing import Callable, AnyStr
# notFuncName: list = ["__call__", "eval"]
# udfNameList: list = []
# is_udf: Callable[[AnyStr], bool] = lambda func: isinstance(getattr(importlib.import_module(path.stem), func),
# pyflink.table.udf.UserDefinedFunctionWrapper)
#
# # filePath: str = sys.argv[1]
# # if str is None:
# # raise Exception("请输入文件路径")
# # path = Path(filePath)
# # if path.parent.name != "":
# # sys.path.append(path.parent.__str__())
# with open(filePath, 'r', encoding='utf8') as f:
# tree = ast.parse(f.read())
#
# for node in ast.walk(tree):
# if isinstance(node, ast.FunctionDef):
# try:
# if node.args.args[0].arg == "self":
# continue
# except Exception as e:
# pass
# if notFuncName.__contains__(node.name):
# continue
#
# if not is_udf(node.name):
# continue
# udfNameList.append(node.name)
#
# try:
# if isinstance(node.targets[0], ast.Name) and is_udf(node.targets[0].id):
# udfNameList.append(node.targets[0].id)
# except Exception as e:
# pass
import os
if len(sys.argv) < 2:
raise Exception("Please enter the file path")
project_path: str = sys.argv[1]
udf_name_list = set()
def list_modules(root_dir):
"""返回给定目录下所有模块和子模块的名称"""
modules = []
for dirpath, _, filenames in os.walk(root_dir):
for filename in filenames:
if filename.endswith(".py"):
p_ = project_path.replace(os.sep, ".")
module_name = os.path.splitext(os.path.join(dirpath, filename))[0].replace(os.sep, ".").replace(
p_ + ".", "")
modules.append(module_name.replace(root_dir, ""))
return modules
if __name__ == '__main__':
modules = list_modules(project_path)
sys.path.append(project_path)
for module_name in modules:
try:
module = importlib.import_module(module_name)
for m in dir(module):
if isinstance(getattr(module, m), pyflink.table.udf.UserDefinedFunctionWrapper):
udf_name_list.add(module.__name__ + "." + m)
except Exception as e:
pass
print(str.join(",", udf_name_list))
# import pkgutil
# for _, module_name, _ in pkgutil.walk_packages([""]):
# if module_name == os.path.splitext(os.path.basename(__file__))[0]:
# continue
# try:
# print(module_name)
# module = importlib.import_module(module_name)
# for m in dir(module):
# if isinstance(getattr(module, m), pyflink.table.udf.UserDefinedFunctionWrapper):
# udfNameList.add(module.__name__ + "." + m)
#
# except Exception as e:
# pass
#
# print(str.join(",", udfNameList))