PyCG 0.0.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- PyCG/__init__.py +0 -0
- PyCG/__main__.py +89 -0
- PyCG/formats/__init__.py +23 -0
- PyCG/formats/as_graph.py +33 -0
- PyCG/formats/base.py +23 -0
- PyCG/formats/fasten.py +273 -0
- PyCG/formats/simple.py +33 -0
- PyCG/machinery/__init__.py +0 -0
- PyCG/machinery/callgraph.py +59 -0
- PyCG/machinery/classes.py +78 -0
- PyCG/machinery/definitions.py +230 -0
- PyCG/machinery/imports.py +235 -0
- PyCG/machinery/key_err.py +31 -0
- PyCG/machinery/modules.py +64 -0
- PyCG/machinery/pointers.py +149 -0
- PyCG/machinery/scopes.py +154 -0
- PyCG/processing/__init__.py +0 -0
- PyCG/processing/base.py +547 -0
- PyCG/processing/cgprocessor.py +252 -0
- PyCG/processing/keyerrprocessor.py +101 -0
- PyCG/processing/postprocessor.py +342 -0
- PyCG/processing/preprocessor.py +424 -0
- PyCG/pycg.py +263 -0
- PyCG/tests/base.py +25 -0
- PyCG/tests/callgraph_test.py +87 -0
- PyCG/tests/definitions_test.py +90 -0
- PyCG/tests/fasten_format_test.py +319 -0
- PyCG/tests/imports_test.py +189 -0
- PyCG/tests/pointers_test.py +112 -0
- PyCG/tests/scopes_test.py +217 -0
- PyCG/utils/__init__.py +22 -0
- PyCG/utils/common.py +45 -0
- PyCG/utils/constants.py +42 -0
- pycg-0.0.8.dist-info/METADATA +161 -0
- pycg-0.0.8.dist-info/RECORD +37 -0
- pycg-0.0.8.dist-info/WHEEL +4 -0
- pycg-0.0.8.dist-info/licenses/LICENCE +201 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2020 Vitalis Salis.
|
|
3
|
+
#
|
|
4
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
|
5
|
+
# or more contributor license agreements. See the NOTICE file
|
|
6
|
+
# distributed with this work for additional information
|
|
7
|
+
# regarding copyright ownership. The ASF licenses this file
|
|
8
|
+
# to you under the Apache License, Version 2.0 (the
|
|
9
|
+
# "License"); you may not use this file except in compliance
|
|
10
|
+
# with the License. You may obtain a copy of the License at
|
|
11
|
+
#
|
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
#
|
|
14
|
+
# Unless required by applicable law or agreed to in writing,
|
|
15
|
+
# software distributed under the License is distributed on an
|
|
16
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
17
|
+
# KIND, either express or implied. See the License for the
|
|
18
|
+
# specific language governing permissions and limitations
|
|
19
|
+
# under the License.
|
|
20
|
+
#
|
|
21
|
+
from pycg import utils
|
|
22
|
+
from pycg.machinery.pointers import LiteralPointer, NamePointer
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class DefinitionManager(object):
|
|
26
|
+
def __init__(self):
|
|
27
|
+
self.defs = {}
|
|
28
|
+
|
|
29
|
+
def create(self, ns, def_type):
|
|
30
|
+
if not ns or not isinstance(ns, str):
|
|
31
|
+
raise DefinitionError("Invalid namespace argument")
|
|
32
|
+
if def_type not in Definition.types:
|
|
33
|
+
raise DefinitionError("Invalid def type argument")
|
|
34
|
+
if self.get(ns):
|
|
35
|
+
raise DefinitionError("Definition already exists")
|
|
36
|
+
|
|
37
|
+
self.defs[ns] = Definition(ns, def_type)
|
|
38
|
+
return self.defs[ns]
|
|
39
|
+
|
|
40
|
+
def assign(self, ns, defi):
|
|
41
|
+
self.defs[ns] = Definition(ns, defi.get_type())
|
|
42
|
+
self.defs[ns].merge(defi)
|
|
43
|
+
|
|
44
|
+
# if it is a function def, we need to create a return pointer
|
|
45
|
+
if defi.is_function_def():
|
|
46
|
+
return_ns = utils.join_ns(ns, utils.constants.RETURN_NAME)
|
|
47
|
+
self.defs[return_ns] = Definition(return_ns, utils.constants.NAME_DEF)
|
|
48
|
+
self.defs[return_ns].get_name_pointer().add(
|
|
49
|
+
utils.join_ns(defi.get_ns(), utils.constants.RETURN_NAME)
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
return self.defs[ns]
|
|
53
|
+
|
|
54
|
+
def get(self, ns):
|
|
55
|
+
if ns in self.defs:
|
|
56
|
+
return self.defs[ns]
|
|
57
|
+
|
|
58
|
+
def get_defs(self):
|
|
59
|
+
return self.defs
|
|
60
|
+
|
|
61
|
+
def handle_function_def(self, parent_ns, fn_name):
|
|
62
|
+
full_ns = utils.join_ns(parent_ns, fn_name)
|
|
63
|
+
defi = self.get(full_ns)
|
|
64
|
+
if not defi:
|
|
65
|
+
defi = self.create(full_ns, utils.constants.FUN_DEF)
|
|
66
|
+
defi.decorator_names = set()
|
|
67
|
+
|
|
68
|
+
return_ns = utils.join_ns(full_ns, utils.constants.RETURN_NAME)
|
|
69
|
+
if not self.get(return_ns):
|
|
70
|
+
self.create(return_ns, utils.constants.NAME_DEF)
|
|
71
|
+
|
|
72
|
+
return defi
|
|
73
|
+
|
|
74
|
+
def handle_class_def(self, parent_ns, cls_name):
|
|
75
|
+
full_ns = utils.join_ns(parent_ns, cls_name)
|
|
76
|
+
defi = self.get(full_ns)
|
|
77
|
+
if not defi:
|
|
78
|
+
defi = self.create(full_ns, utils.constants.CLS_DEF)
|
|
79
|
+
|
|
80
|
+
return defi
|
|
81
|
+
|
|
82
|
+
def transitive_closure(self):
|
|
83
|
+
closured = {}
|
|
84
|
+
|
|
85
|
+
def dfs(defi):
|
|
86
|
+
name_pointer = defi.get_name_pointer()
|
|
87
|
+
new_set = set()
|
|
88
|
+
# bottom
|
|
89
|
+
if closured.get(defi.get_ns(), None) is not None:
|
|
90
|
+
return closured[defi.get_ns()]
|
|
91
|
+
|
|
92
|
+
if not name_pointer.get():
|
|
93
|
+
new_set.add(defi.get_ns())
|
|
94
|
+
|
|
95
|
+
closured[defi.get_ns()] = new_set
|
|
96
|
+
|
|
97
|
+
for name in name_pointer.get():
|
|
98
|
+
if not self.defs.get(name, None):
|
|
99
|
+
continue
|
|
100
|
+
items = dfs(self.defs[name])
|
|
101
|
+
if not items:
|
|
102
|
+
items = set([name])
|
|
103
|
+
new_set = new_set.union(items)
|
|
104
|
+
|
|
105
|
+
closured[defi.get_ns()] = new_set
|
|
106
|
+
return closured[defi.get_ns()]
|
|
107
|
+
|
|
108
|
+
for ns, current_def in self.defs.items():
|
|
109
|
+
if closured.get(current_def, None) is None:
|
|
110
|
+
dfs(current_def)
|
|
111
|
+
|
|
112
|
+
return closured
|
|
113
|
+
|
|
114
|
+
def complete_definitions(self):
|
|
115
|
+
# THE MOST expensive part of this tool's process
|
|
116
|
+
# TODO: IMPROVE COMPLEXITY
|
|
117
|
+
def update_pointsto_args(pointsto_args, arg, name):
|
|
118
|
+
changed_something = False
|
|
119
|
+
if arg == pointsto_args:
|
|
120
|
+
return False
|
|
121
|
+
for pointsto_arg in pointsto_args:
|
|
122
|
+
if not self.defs.get(pointsto_arg, None):
|
|
123
|
+
continue
|
|
124
|
+
if pointsto_arg == name:
|
|
125
|
+
continue
|
|
126
|
+
pointsto_arg_def = self.defs[pointsto_arg].get_name_pointer()
|
|
127
|
+
if pointsto_arg_def == pointsto_args:
|
|
128
|
+
continue
|
|
129
|
+
|
|
130
|
+
# sometimes we may end up with a cycle
|
|
131
|
+
if pointsto_arg in arg:
|
|
132
|
+
arg.remove(pointsto_arg)
|
|
133
|
+
|
|
134
|
+
for item in arg:
|
|
135
|
+
if item not in pointsto_arg_def.get():
|
|
136
|
+
if self.defs.get(item, None) is not None:
|
|
137
|
+
changed_something = True
|
|
138
|
+
# HACK: this check shouldn't be needed
|
|
139
|
+
# if we remove this the following breaks:
|
|
140
|
+
# x = lambda x: x + 1
|
|
141
|
+
# x(1)
|
|
142
|
+
# since on line 184 we don't discriminate between
|
|
143
|
+
# literal values and name values
|
|
144
|
+
if not self.defs.get(item, None):
|
|
145
|
+
continue
|
|
146
|
+
pointsto_arg_def.add(item)
|
|
147
|
+
return changed_something
|
|
148
|
+
|
|
149
|
+
for i in range(len(self.defs)):
|
|
150
|
+
changed_something = False
|
|
151
|
+
for ns, current_def in self.defs.items():
|
|
152
|
+
# the name pointer of the definition we're currently iterating
|
|
153
|
+
current_name_pointer = current_def.get_name_pointer()
|
|
154
|
+
# iterate the names the current definition points to items
|
|
155
|
+
# for name in current_name_pointer.get():
|
|
156
|
+
for name in current_name_pointer.get().copy():
|
|
157
|
+
# get the name pointer of the points to name
|
|
158
|
+
if not self.defs.get(name, None):
|
|
159
|
+
continue
|
|
160
|
+
if name == ns:
|
|
161
|
+
continue
|
|
162
|
+
|
|
163
|
+
pointsto_name_pointer = self.defs[name].get_name_pointer()
|
|
164
|
+
# iterate the arguments of the definition
|
|
165
|
+
# we're currently iterating
|
|
166
|
+
for arg_name, arg in current_name_pointer.get_args().items():
|
|
167
|
+
pos = current_name_pointer.get_pos_of_name(arg_name)
|
|
168
|
+
if pos is not None:
|
|
169
|
+
pointsto_args = pointsto_name_pointer.get_pos_arg(pos)
|
|
170
|
+
if not pointsto_args:
|
|
171
|
+
pointsto_name_pointer.add_pos_arg(pos, None, arg)
|
|
172
|
+
continue
|
|
173
|
+
else:
|
|
174
|
+
pointsto_args = pointsto_name_pointer.get_arg(arg_name)
|
|
175
|
+
if not pointsto_args:
|
|
176
|
+
pointsto_name_pointer.add_arg(arg_name, arg)
|
|
177
|
+
continue
|
|
178
|
+
changed_something = changed_something or update_pointsto_args(
|
|
179
|
+
pointsto_args, arg, current_def.get_ns()
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
if not changed_something:
|
|
183
|
+
break
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
class Definition(object):
|
|
187
|
+
types = [
|
|
188
|
+
utils.constants.FUN_DEF,
|
|
189
|
+
utils.constants.MOD_DEF,
|
|
190
|
+
utils.constants.NAME_DEF,
|
|
191
|
+
utils.constants.CLS_DEF,
|
|
192
|
+
utils.constants.EXT_DEF,
|
|
193
|
+
]
|
|
194
|
+
|
|
195
|
+
def __init__(self, fullns, def_type):
|
|
196
|
+
self.fullns = fullns
|
|
197
|
+
self.points_to = {"lit": LiteralPointer(), "name": NamePointer()}
|
|
198
|
+
self.def_type = def_type
|
|
199
|
+
|
|
200
|
+
def get_type(self):
|
|
201
|
+
return self.def_type
|
|
202
|
+
|
|
203
|
+
def is_function_def(self):
|
|
204
|
+
return self.def_type == utils.constants.FUN_DEF
|
|
205
|
+
|
|
206
|
+
def is_ext_def(self):
|
|
207
|
+
return self.def_type == utils.constants.EXT_DEF
|
|
208
|
+
|
|
209
|
+
def is_callable(self):
|
|
210
|
+
return self.is_function_def() or self.is_ext_def()
|
|
211
|
+
|
|
212
|
+
def get_lit_pointer(self):
|
|
213
|
+
return self.points_to["lit"]
|
|
214
|
+
|
|
215
|
+
def get_name_pointer(self):
|
|
216
|
+
return self.points_to["name"]
|
|
217
|
+
|
|
218
|
+
def get_name(self):
|
|
219
|
+
return self.fullns.split(".")[-1]
|
|
220
|
+
|
|
221
|
+
def get_ns(self):
|
|
222
|
+
return self.fullns
|
|
223
|
+
|
|
224
|
+
def merge(self, to_merge):
|
|
225
|
+
for name, pointer in to_merge.points_to.items():
|
|
226
|
+
self.points_to[name].merge(pointer)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
class DefinitionError(Exception):
|
|
230
|
+
pass
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2020 Vitalis Salis.
|
|
3
|
+
#
|
|
4
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
|
5
|
+
# or more contributor license agreements. See the NOTICE file
|
|
6
|
+
# distributed with this work for additional information
|
|
7
|
+
# regarding copyright ownership. The ASF licenses this file
|
|
8
|
+
# to you under the Apache License, Version 2.0 (the
|
|
9
|
+
# "License"); you may not use this file except in compliance
|
|
10
|
+
# with the License. You may obtain a copy of the License at
|
|
11
|
+
#
|
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
#
|
|
14
|
+
# Unless required by applicable law or agreed to in writing,
|
|
15
|
+
# software distributed under the License is distributed on an
|
|
16
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
17
|
+
# KIND, either express or implied. See the License for the
|
|
18
|
+
# specific language governing permissions and limitations
|
|
19
|
+
# under the License.
|
|
20
|
+
#
|
|
21
|
+
import copy
|
|
22
|
+
import importlib
|
|
23
|
+
import os
|
|
24
|
+
import sys
|
|
25
|
+
from importlib import abc
|
|
26
|
+
|
|
27
|
+
from pycg import utils
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def get_custom_loader(ig_obj):
|
|
31
|
+
"""
|
|
32
|
+
Closure which returns a custom loader
|
|
33
|
+
that modifies an ImportManager object
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
class CustomLoader(abc.SourceLoader):
|
|
37
|
+
def __init__(self, fullname, path):
|
|
38
|
+
self.fullname = fullname
|
|
39
|
+
self.path = path
|
|
40
|
+
|
|
41
|
+
ig_obj.create_edge(self.fullname)
|
|
42
|
+
if not ig_obj.get_node(self.fullname):
|
|
43
|
+
ig_obj.create_node(self.fullname)
|
|
44
|
+
ig_obj.set_filepath(self.fullname, self.path)
|
|
45
|
+
|
|
46
|
+
def get_filename(self, fullname):
|
|
47
|
+
return self.path
|
|
48
|
+
|
|
49
|
+
def get_data(self, filename):
|
|
50
|
+
return ""
|
|
51
|
+
|
|
52
|
+
return CustomLoader
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class ImportManager(object):
|
|
56
|
+
def __init__(self):
|
|
57
|
+
self.import_graph = dict()
|
|
58
|
+
self.current_module = ""
|
|
59
|
+
self.input_file = ""
|
|
60
|
+
self.mod_dir = None
|
|
61
|
+
self.old_path_hooks = None
|
|
62
|
+
self.old_path = None
|
|
63
|
+
|
|
64
|
+
def set_pkg(self, input_pkg):
|
|
65
|
+
self.mod_dir = input_pkg
|
|
66
|
+
|
|
67
|
+
def get_mod_dir(self):
|
|
68
|
+
return self.mod_dir
|
|
69
|
+
|
|
70
|
+
def get_node(self, name):
|
|
71
|
+
if name in self.import_graph:
|
|
72
|
+
return self.import_graph[name]
|
|
73
|
+
|
|
74
|
+
def create_node(self, name):
|
|
75
|
+
if not name or not isinstance(name, str):
|
|
76
|
+
raise ImportManagerError("Invalid node name")
|
|
77
|
+
|
|
78
|
+
if self.get_node(name):
|
|
79
|
+
raise ImportManagerError("Can't create a node a second time")
|
|
80
|
+
|
|
81
|
+
self.import_graph[name] = {"filename": "", "imports": set()}
|
|
82
|
+
return self.import_graph[name]
|
|
83
|
+
|
|
84
|
+
def create_edge(self, dest):
|
|
85
|
+
if not dest or not isinstance(dest, str):
|
|
86
|
+
raise ImportManagerError("Invalid node name")
|
|
87
|
+
|
|
88
|
+
node = self.get_node(self._get_module_path())
|
|
89
|
+
if not node:
|
|
90
|
+
raise ImportManagerError("Can't add edge to a non existing node")
|
|
91
|
+
|
|
92
|
+
node["imports"].add(dest)
|
|
93
|
+
|
|
94
|
+
def _clear_caches(self):
|
|
95
|
+
importlib.invalidate_caches()
|
|
96
|
+
sys.path_importer_cache.clear()
|
|
97
|
+
# TODO: maybe not do that since it empties the whole cache
|
|
98
|
+
for name in self.import_graph:
|
|
99
|
+
if name in sys.modules:
|
|
100
|
+
del sys.modules[name]
|
|
101
|
+
|
|
102
|
+
def _get_module_path(self):
|
|
103
|
+
return self.current_module
|
|
104
|
+
|
|
105
|
+
def set_current_mod(self, name, fname):
|
|
106
|
+
self.current_module = name
|
|
107
|
+
self.input_file = os.path.abspath(fname)
|
|
108
|
+
|
|
109
|
+
def get_filepath(self, modname):
|
|
110
|
+
if modname in self.import_graph:
|
|
111
|
+
return self.import_graph[modname]["filename"]
|
|
112
|
+
|
|
113
|
+
def set_filepath(self, node_name, filename):
|
|
114
|
+
if not filename or not isinstance(filename, str):
|
|
115
|
+
raise ImportManagerError("Invalid node name")
|
|
116
|
+
|
|
117
|
+
node = self.get_node(node_name)
|
|
118
|
+
if not node:
|
|
119
|
+
raise ImportManagerError("Node does not exist")
|
|
120
|
+
|
|
121
|
+
node["filename"] = os.path.abspath(filename)
|
|
122
|
+
|
|
123
|
+
def get_imports(self, modname):
|
|
124
|
+
if modname not in self.import_graph:
|
|
125
|
+
return []
|
|
126
|
+
return self.import_graph[modname]["imports"]
|
|
127
|
+
|
|
128
|
+
def _is_init_file(self):
|
|
129
|
+
return self.input_file.endswith("__init__.py")
|
|
130
|
+
|
|
131
|
+
def _handle_import_level(self, name, level):
|
|
132
|
+
# add a dot for each level
|
|
133
|
+
package = self._get_module_path().split(".")
|
|
134
|
+
if level > len(package):
|
|
135
|
+
raise ImportError("Attempting import beyond top level package")
|
|
136
|
+
|
|
137
|
+
mod_name = ("." * level) + name
|
|
138
|
+
# When an __init__ file is analyzed,
|
|
139
|
+
# then the module name doesn't contain
|
|
140
|
+
# the __init__ part in it,
|
|
141
|
+
# so special care must be taken for levels.
|
|
142
|
+
if self._is_init_file() and level >= 1:
|
|
143
|
+
if level != 1:
|
|
144
|
+
level -= 1
|
|
145
|
+
package = package[:-level]
|
|
146
|
+
else:
|
|
147
|
+
package = package[:-level]
|
|
148
|
+
|
|
149
|
+
return mod_name, ".".join(package)
|
|
150
|
+
|
|
151
|
+
def _do_import(self, mod_name, package):
|
|
152
|
+
if mod_name in sys.modules:
|
|
153
|
+
self.create_edge(mod_name)
|
|
154
|
+
return sys.modules[mod_name]
|
|
155
|
+
|
|
156
|
+
try:
|
|
157
|
+
module_spec = importlib.util.find_spec(mod_name, package=package)
|
|
158
|
+
except ModuleNotFoundError:
|
|
159
|
+
module_spec = None
|
|
160
|
+
|
|
161
|
+
if module_spec is None:
|
|
162
|
+
return importlib.import_module(mod_name, package=package)
|
|
163
|
+
|
|
164
|
+
return importlib.util.module_from_spec(module_spec)
|
|
165
|
+
|
|
166
|
+
def handle_import(self, name, level):
|
|
167
|
+
# We currently don't support builtin modules because they're frozen.
|
|
168
|
+
# Add an edge and continue.
|
|
169
|
+
# TODO: identify a way to include frozen modules
|
|
170
|
+
root = name.split(".")[0]
|
|
171
|
+
if root in sys.builtin_module_names:
|
|
172
|
+
self.create_edge(root)
|
|
173
|
+
return
|
|
174
|
+
|
|
175
|
+
# Import the module
|
|
176
|
+
try:
|
|
177
|
+
mod_name, package = self._handle_import_level(name, level)
|
|
178
|
+
except ImportError:
|
|
179
|
+
return
|
|
180
|
+
|
|
181
|
+
parent = ".".join(mod_name.split(".")[:-1])
|
|
182
|
+
parent_name = ".".join(name.split(".")[:-1])
|
|
183
|
+
combos = [
|
|
184
|
+
(mod_name, package),
|
|
185
|
+
(parent, package),
|
|
186
|
+
(utils.join_ns(package, name), ""),
|
|
187
|
+
(utils.join_ns(package, parent_name), ""),
|
|
188
|
+
]
|
|
189
|
+
|
|
190
|
+
mod = None
|
|
191
|
+
for mn, pkg in combos:
|
|
192
|
+
try:
|
|
193
|
+
mod = self._do_import(mn, pkg)
|
|
194
|
+
break
|
|
195
|
+
except Exception:
|
|
196
|
+
continue
|
|
197
|
+
|
|
198
|
+
if not mod:
|
|
199
|
+
return
|
|
200
|
+
|
|
201
|
+
if not hasattr(mod, "__file__") or not mod.__file__:
|
|
202
|
+
return
|
|
203
|
+
if self.mod_dir not in mod.__file__:
|
|
204
|
+
return
|
|
205
|
+
fname = mod.__file__
|
|
206
|
+
if fname.endswith("__init__.py"):
|
|
207
|
+
fname = os.path.split(fname)[0]
|
|
208
|
+
|
|
209
|
+
return utils.to_mod_name(os.path.relpath(fname, self.mod_dir))
|
|
210
|
+
|
|
211
|
+
def get_import_graph(self):
|
|
212
|
+
return self.import_graph
|
|
213
|
+
|
|
214
|
+
def install_hooks(self):
|
|
215
|
+
loader = get_custom_loader(self)
|
|
216
|
+
self.old_path_hooks = copy.deepcopy(sys.path_hooks)
|
|
217
|
+
self.old_path = copy.deepcopy(sys.path)
|
|
218
|
+
|
|
219
|
+
loader_details = loader, importlib.machinery.all_suffixes()
|
|
220
|
+
sys.path_hooks.insert(
|
|
221
|
+
0, importlib.machinery.FileFinder.path_hook(loader_details)
|
|
222
|
+
)
|
|
223
|
+
sys.path.insert(0, os.path.abspath(self.mod_dir))
|
|
224
|
+
|
|
225
|
+
self._clear_caches()
|
|
226
|
+
|
|
227
|
+
def remove_hooks(self):
|
|
228
|
+
sys.path_hooks = self.old_path_hooks
|
|
229
|
+
sys.path = self.old_path
|
|
230
|
+
|
|
231
|
+
self._clear_caches()
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
class ImportManagerError(Exception):
|
|
235
|
+
pass
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2021 Vitalis Salis.
|
|
3
|
+
#
|
|
4
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
|
5
|
+
# or more contributor license agreements. See the NOTICE file
|
|
6
|
+
# distributed with this work for additional information
|
|
7
|
+
# regarding copyright ownership. The ASF licenses this file
|
|
8
|
+
# to you under the Apache License, Version 2.0 (the
|
|
9
|
+
# "License"); you may not use this file except in compliance
|
|
10
|
+
# with the License. You may obtain a copy of the License at
|
|
11
|
+
#
|
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
#
|
|
14
|
+
# Unless required by applicable law or agreed to in writing,
|
|
15
|
+
# software distributed under the License is distributed on an
|
|
16
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
17
|
+
# KIND, either express or implied. See the License for the
|
|
18
|
+
# specific language governing permissions and limitations
|
|
19
|
+
# under the License.
|
|
20
|
+
#
|
|
21
|
+
class KeyErrors(object):
|
|
22
|
+
def __init__(self):
|
|
23
|
+
self.key_errs = []
|
|
24
|
+
|
|
25
|
+
def add(self, filename, lineno, namespace, key):
|
|
26
|
+
self.key_errs.append({
|
|
27
|
+
"filename": filename, "lineno": lineno, "namespace": namespace, "key": key
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
def get(self):
|
|
31
|
+
return self.key_errs
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2020 Vitalis Salis.
|
|
3
|
+
#
|
|
4
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
|
5
|
+
# or more contributor license agreements. See the NOTICE file
|
|
6
|
+
# distributed with this work for additional information
|
|
7
|
+
# regarding copyright ownership. The ASF licenses this file
|
|
8
|
+
# to you under the Apache License, Version 2.0 (the
|
|
9
|
+
# "License"); you may not use this file except in compliance
|
|
10
|
+
# with the License. You may obtain a copy of the License at
|
|
11
|
+
#
|
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
#
|
|
14
|
+
# Unless required by applicable law or agreed to in writing,
|
|
15
|
+
# software distributed under the License is distributed on an
|
|
16
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
17
|
+
# KIND, either express or implied. See the License for the
|
|
18
|
+
# specific language governing permissions and limitations
|
|
19
|
+
# under the License.
|
|
20
|
+
#
|
|
21
|
+
class ModuleManager:
|
|
22
|
+
def __init__(self):
|
|
23
|
+
self.internal = {}
|
|
24
|
+
self.external = {}
|
|
25
|
+
|
|
26
|
+
def create(self, name, fname, external=False):
|
|
27
|
+
mod = Module(name, fname)
|
|
28
|
+
if external:
|
|
29
|
+
self.external[name] = mod
|
|
30
|
+
else:
|
|
31
|
+
self.internal[name] = mod
|
|
32
|
+
return mod
|
|
33
|
+
|
|
34
|
+
def get(self, name):
|
|
35
|
+
if name in self.internal:
|
|
36
|
+
return self.internal[name]
|
|
37
|
+
if name in self.external:
|
|
38
|
+
return self.external[name]
|
|
39
|
+
|
|
40
|
+
def get_internal_modules(self):
|
|
41
|
+
return self.internal
|
|
42
|
+
|
|
43
|
+
def get_external_modules(self):
|
|
44
|
+
return self.external
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Module:
|
|
48
|
+
def __init__(self, name, filename):
|
|
49
|
+
self.name = name
|
|
50
|
+
self.filename = filename
|
|
51
|
+
self.methods = dict()
|
|
52
|
+
|
|
53
|
+
def get_name(self):
|
|
54
|
+
return self.name
|
|
55
|
+
|
|
56
|
+
def get_filename(self):
|
|
57
|
+
return self.filename
|
|
58
|
+
|
|
59
|
+
def get_methods(self):
|
|
60
|
+
return self.methods
|
|
61
|
+
|
|
62
|
+
def add_method(self, method, first=None, last=None):
|
|
63
|
+
if not self.methods.get(method, None):
|
|
64
|
+
self.methods[method] = dict(name=method, first=first, last=last)
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2020 Vitalis Salis.
|
|
3
|
+
#
|
|
4
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
|
5
|
+
# or more contributor license agreements. See the NOTICE file
|
|
6
|
+
# distributed with this work for additional information
|
|
7
|
+
# regarding copyright ownership. The ASF licenses this file
|
|
8
|
+
# to you under the Apache License, Version 2.0 (the
|
|
9
|
+
# "License"); you may not use this file except in compliance
|
|
10
|
+
# with the License. You may obtain a copy of the License at
|
|
11
|
+
#
|
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
#
|
|
14
|
+
# Unless required by applicable law or agreed to in writing,
|
|
15
|
+
# software distributed under the License is distributed on an
|
|
16
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
17
|
+
# KIND, either express or implied. See the License for the
|
|
18
|
+
# specific language governing permissions and limitations
|
|
19
|
+
# under the License.
|
|
20
|
+
#
|
|
21
|
+
class Pointer(object):
|
|
22
|
+
def __init__(self):
|
|
23
|
+
self.values = set()
|
|
24
|
+
|
|
25
|
+
def add(self, item):
|
|
26
|
+
self.values.add(item)
|
|
27
|
+
|
|
28
|
+
def add_set(self, s):
|
|
29
|
+
self.values = self.values.union(s)
|
|
30
|
+
|
|
31
|
+
def get(self):
|
|
32
|
+
return self.values
|
|
33
|
+
|
|
34
|
+
def merge(self, pointer):
|
|
35
|
+
self.values = self.values.union(pointer.values)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class LiteralPointer(Pointer):
|
|
39
|
+
STR_LIT = "STRING"
|
|
40
|
+
INT_LIT = "INTEGER"
|
|
41
|
+
UNK_LIT = "UNKNOWN"
|
|
42
|
+
|
|
43
|
+
# no need to add the actual item
|
|
44
|
+
def add(self, item):
|
|
45
|
+
if isinstance(item, str):
|
|
46
|
+
self.values.add(item)
|
|
47
|
+
elif isinstance(item, int):
|
|
48
|
+
self.values.add(item)
|
|
49
|
+
else:
|
|
50
|
+
self.values.add(self.UNK_LIT)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class NamePointer(Pointer):
|
|
54
|
+
def __init__(self):
|
|
55
|
+
super().__init__()
|
|
56
|
+
self.pos_to_name = {}
|
|
57
|
+
self.name_to_pos = {}
|
|
58
|
+
self.args = {}
|
|
59
|
+
|
|
60
|
+
def _sanitize_pos(self, pos):
|
|
61
|
+
try:
|
|
62
|
+
int(pos)
|
|
63
|
+
except ValueError:
|
|
64
|
+
raise PointerError("Invalid position for argument")
|
|
65
|
+
|
|
66
|
+
return pos
|
|
67
|
+
|
|
68
|
+
def get_or_create(self, name):
|
|
69
|
+
if name not in self.args:
|
|
70
|
+
self.args[name] = set()
|
|
71
|
+
return self.args[name]
|
|
72
|
+
|
|
73
|
+
def add_arg(self, name, item):
|
|
74
|
+
self.get_or_create(name)
|
|
75
|
+
if isinstance(item, str):
|
|
76
|
+
self.args[name].add(item)
|
|
77
|
+
elif isinstance(item, set):
|
|
78
|
+
self.args[name] = self.args[name].union(item)
|
|
79
|
+
else:
|
|
80
|
+
raise Exception()
|
|
81
|
+
|
|
82
|
+
def add_lit_arg(self, name, item):
|
|
83
|
+
arg = self.get_or_create(name)
|
|
84
|
+
if isinstance(item, str):
|
|
85
|
+
arg.add(LiteralPointer.STR_LIT)
|
|
86
|
+
elif isinstance(item, int):
|
|
87
|
+
arg.add(LiteralPointer.INT_LIT)
|
|
88
|
+
else:
|
|
89
|
+
arg.add(LiteralPointer.UNK_LIT)
|
|
90
|
+
|
|
91
|
+
def add_pos_arg(self, pos, name, item):
|
|
92
|
+
pos = self._sanitize_pos(pos)
|
|
93
|
+
if not name:
|
|
94
|
+
if self.pos_to_name.get(pos, None):
|
|
95
|
+
name = self.pos_to_name[pos]
|
|
96
|
+
else:
|
|
97
|
+
name = str(pos)
|
|
98
|
+
self.pos_to_name[pos] = name
|
|
99
|
+
self.name_to_pos[name] = pos
|
|
100
|
+
|
|
101
|
+
self.add_arg(name, item)
|
|
102
|
+
|
|
103
|
+
def add_name_arg(self, name, item):
|
|
104
|
+
self.add_arg(name, item)
|
|
105
|
+
|
|
106
|
+
def add_pos_lit_arg(self, pos, name, item):
|
|
107
|
+
pos = self._sanitize_pos(pos)
|
|
108
|
+
if not name:
|
|
109
|
+
name = str(pos)
|
|
110
|
+
self.pos_to_name[pos] = name
|
|
111
|
+
self.name_to_pos[name] = pos
|
|
112
|
+
self.add_lit_arg(name, item)
|
|
113
|
+
|
|
114
|
+
def get_pos_arg(self, pos):
|
|
115
|
+
pos = self._sanitize_pos(pos)
|
|
116
|
+
name = self.pos_to_name.get(pos, None)
|
|
117
|
+
return self.get_arg(name)
|
|
118
|
+
|
|
119
|
+
def get_arg(self, name):
|
|
120
|
+
if self.args.get(name, None):
|
|
121
|
+
return self.args[name]
|
|
122
|
+
|
|
123
|
+
def get_args(self):
|
|
124
|
+
return self.args
|
|
125
|
+
|
|
126
|
+
def get_pos_args(self):
|
|
127
|
+
args = {}
|
|
128
|
+
for pos, name in self.pos_to_name.items():
|
|
129
|
+
args[pos] = self.args[name]
|
|
130
|
+
return args
|
|
131
|
+
|
|
132
|
+
def get_pos_of_name(self, name):
|
|
133
|
+
if name in self.name_to_pos:
|
|
134
|
+
return self.name_to_pos[name]
|
|
135
|
+
|
|
136
|
+
def get_pos_names(self):
|
|
137
|
+
return self.pos_to_name
|
|
138
|
+
|
|
139
|
+
def merge(self, pointer):
|
|
140
|
+
super().merge(pointer)
|
|
141
|
+
if hasattr(pointer, "get_pos_names"):
|
|
142
|
+
for pos, name in pointer.get_pos_names().items():
|
|
143
|
+
self.pos_to_name[pos] = name
|
|
144
|
+
for name, arg in pointer.get_args().items():
|
|
145
|
+
self.add_arg(name, arg)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class PointerError(Exception):
|
|
149
|
+
pass
|