PyCG 0.0.3__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.
- micro-benchmark/__init__.py +0 -0
- micro-benchmark/args_test.py +25 -0
- micro-benchmark/assignments_test.py +19 -0
- micro-benchmark/base.py +87 -0
- micro-benchmark/builtins_test.py +16 -0
- micro-benchmark/classes_test.py +73 -0
- micro-benchmark/create_pytests.py +42 -0
- micro-benchmark/decorators_test.py +28 -0
- micro-benchmark/dicts_test.py +43 -0
- micro-benchmark/direct_calls_test.py +19 -0
- micro-benchmark/dynamic_test.py +10 -0
- micro-benchmark/exceptions_test.py +16 -0
- micro-benchmark/external_test.py +25 -0
- micro-benchmark/functions_test.py +19 -0
- micro-benchmark/generators_test.py +25 -0
- micro-benchmark/imports_test.py +49 -0
- micro-benchmark/kwargs_test.py +16 -0
- micro-benchmark/lambdas_test.py +22 -0
- micro-benchmark/lists_test.py +31 -0
- micro-benchmark/mro_test.py +28 -0
- micro-benchmark/returns_test.py +19 -0
- micro-benchmark-key-errs/__init__.py +0 -0
- micro-benchmark-key-errs/assignments_test.py +16 -0
- micro-benchmark-key-errs/base.py +81 -0
- micro-benchmark-key-errs/classes_test.py +25 -0
- micro-benchmark-key-errs/create_pytests.py +42 -0
- micro-benchmark-key-errs/dicts_test.py +34 -0
- micro-benchmark-key-errs/kwargs_test.py +13 -0
- micro-benchmark-key-errs/lists_test.py +13 -0
- micro-benchmark-key-errs/parameters_test.py +16 -0
- pycg/__init__.py +0 -0
- pycg/__main__.py +104 -0
- pycg/formats/__init__.py +23 -0
- pycg/formats/as_graph.py +32 -0
- pycg/formats/base.py +23 -0
- pycg/formats/fasten.py +253 -0
- pycg/formats/simple.py +32 -0
- pycg/machinery/__init__.py +0 -0
- pycg/machinery/callgraph.py +59 -0
- pycg/machinery/classes.py +77 -0
- pycg/machinery/definitions.py +219 -0
- pycg/machinery/imports.py +220 -0
- pycg/machinery/key_err.py +34 -0
- pycg/machinery/modules.py +66 -0
- pycg/machinery/pointers.py +146 -0
- pycg/machinery/scopes.py +143 -0
- pycg/processing/__init__.py +0 -0
- pycg/processing/base.py +522 -0
- pycg/processing/cgprocessor.py +230 -0
- pycg/processing/keyerrprocessor.py +82 -0
- pycg/processing/postprocessor.py +308 -0
- pycg/processing/preprocessor.py +375 -0
- pycg/pycg.py +229 -0
- pycg/utils/__init__.py +22 -0
- pycg/utils/common.py +39 -0
- pycg/utils/constants.py +42 -0
- pycg-0.0.3.dist-info/LICENCE +201 -0
- pycg-0.0.3.dist-info/METADATA +150 -0
- pycg-0.0.3.dist-info/RECORD +62 -0
- pycg-0.0.3.dist-info/WHEEL +5 -0
- pycg-0.0.3.dist-info/entry_points.txt +3 -0
- pycg-0.0.3.dist-info/top_level.txt +3 -0
pycg/formats/simple.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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 .base import BaseFormatter
|
|
22
|
+
|
|
23
|
+
class Simple(BaseFormatter):
|
|
24
|
+
def __init__(self, cg_generator):
|
|
25
|
+
self.cg_generator = cg_generator
|
|
26
|
+
|
|
27
|
+
def generate(self):
|
|
28
|
+
output = self.cg_generator.output()
|
|
29
|
+
output_cg = {}
|
|
30
|
+
for node in output:
|
|
31
|
+
output_cg[node] = list(output[node])
|
|
32
|
+
return output_cg
|
|
File without changes
|
|
@@ -0,0 +1,59 @@
|
|
|
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 CallGraph(object):
|
|
22
|
+
def __init__(self):
|
|
23
|
+
self.cg = {}
|
|
24
|
+
self.modnames = {}
|
|
25
|
+
|
|
26
|
+
def add_node(self, name, modname=""):
|
|
27
|
+
if not isinstance(name, str):
|
|
28
|
+
raise CallGraphError("Only string node names allowed")
|
|
29
|
+
if not name:
|
|
30
|
+
raise CallGraphError("Empty node name")
|
|
31
|
+
|
|
32
|
+
if not name in self.cg:
|
|
33
|
+
self.cg[name] = set()
|
|
34
|
+
self.modnames[name] = modname
|
|
35
|
+
|
|
36
|
+
if name in self.cg and not self.modnames[name]:
|
|
37
|
+
self.modnames[name] = modname
|
|
38
|
+
|
|
39
|
+
def add_edge(self, src, dest):
|
|
40
|
+
self.add_node(src)
|
|
41
|
+
self.add_node(dest)
|
|
42
|
+
self.cg[src].add(dest)
|
|
43
|
+
|
|
44
|
+
def get(self):
|
|
45
|
+
return self.cg
|
|
46
|
+
|
|
47
|
+
def get_edges(self):
|
|
48
|
+
output = []
|
|
49
|
+
for src in self.cg:
|
|
50
|
+
for dst in self.cg[src]:
|
|
51
|
+
output.append([src, dst])
|
|
52
|
+
return output
|
|
53
|
+
|
|
54
|
+
def get_modules(self):
|
|
55
|
+
return self.modnames
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class CallGraphError(Exception):
|
|
59
|
+
pass
|
|
@@ -0,0 +1,77 @@
|
|
|
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 ClassManager:
|
|
22
|
+
def __init__(self):
|
|
23
|
+
self.names = {}
|
|
24
|
+
|
|
25
|
+
def get(self, name):
|
|
26
|
+
if name in self.names:
|
|
27
|
+
return self.names[name]
|
|
28
|
+
|
|
29
|
+
def create(self, name, module):
|
|
30
|
+
if not name in self.names:
|
|
31
|
+
cls = ClassNode(name, module)
|
|
32
|
+
self.names[name] = cls
|
|
33
|
+
return self.names[name]
|
|
34
|
+
|
|
35
|
+
def get_classes(self):
|
|
36
|
+
return self.names
|
|
37
|
+
|
|
38
|
+
class ClassNode:
|
|
39
|
+
def __init__(self, ns, module):
|
|
40
|
+
self.ns = ns
|
|
41
|
+
self.module = module
|
|
42
|
+
self.mro = [ns]
|
|
43
|
+
|
|
44
|
+
def add_parent(self, parent):
|
|
45
|
+
if isinstance(parent, str):
|
|
46
|
+
self.mro.append(parent)
|
|
47
|
+
elif isinstance(parent, list):
|
|
48
|
+
for item in parent:
|
|
49
|
+
self.mro.append(item)
|
|
50
|
+
self.fix_mro()
|
|
51
|
+
|
|
52
|
+
def fix_mro(self):
|
|
53
|
+
new_mro = []
|
|
54
|
+
for idx, item in enumerate(self.mro):
|
|
55
|
+
if self.mro[idx+1:].count(item) > 0:
|
|
56
|
+
continue
|
|
57
|
+
new_mro.append(item)
|
|
58
|
+
self.mro = new_mro
|
|
59
|
+
|
|
60
|
+
def get_mro(self):
|
|
61
|
+
return self.mro
|
|
62
|
+
|
|
63
|
+
def get_module(self):
|
|
64
|
+
return self.module
|
|
65
|
+
|
|
66
|
+
def compute_mro(self):
|
|
67
|
+
res = []
|
|
68
|
+
self.mro.reverse()
|
|
69
|
+
for parent in self.mro:
|
|
70
|
+
if not parent in res:
|
|
71
|
+
res.append(parent)
|
|
72
|
+
|
|
73
|
+
res.reverse()
|
|
74
|
+
self.mro = res
|
|
75
|
+
|
|
76
|
+
def clear_mro(self):
|
|
77
|
+
self.mro = [self.ns]
|
|
@@ -0,0 +1,219 @@
|
|
|
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.machinery.pointers import NamePointer, LiteralPointer
|
|
22
|
+
from pycg import utils
|
|
23
|
+
|
|
24
|
+
class DefinitionManager(object):
|
|
25
|
+
def __init__(self):
|
|
26
|
+
self.defs = {}
|
|
27
|
+
|
|
28
|
+
def create(self, ns, def_type):
|
|
29
|
+
if not ns or not isinstance(ns, str):
|
|
30
|
+
raise DefinitionError("Invalid namespace argument")
|
|
31
|
+
if not def_type in Definition.types:
|
|
32
|
+
raise DefinitionError("Invalid def type argument")
|
|
33
|
+
if self.get(ns):
|
|
34
|
+
raise DefinitionError("Definition already exists")
|
|
35
|
+
|
|
36
|
+
self.defs[ns] = Definition(ns, def_type)
|
|
37
|
+
return self.defs[ns]
|
|
38
|
+
|
|
39
|
+
def assign(self, ns, defi):
|
|
40
|
+
self.defs[ns] = Definition(ns, defi.get_type())
|
|
41
|
+
self.defs[ns].merge(defi)
|
|
42
|
+
|
|
43
|
+
# if it is a function def, we need to create a return pointer
|
|
44
|
+
if defi.is_function_def():
|
|
45
|
+
return_ns = utils.join_ns(ns, utils.constants.RETURN_NAME)
|
|
46
|
+
self.defs[return_ns] = Definition(return_ns, utils.constants.NAME_DEF)
|
|
47
|
+
self.defs[return_ns].get_name_pointer().add(
|
|
48
|
+
utils.join_ns(defi.get_ns(), utils.constants.RETURN_NAME))
|
|
49
|
+
|
|
50
|
+
return self.defs[ns]
|
|
51
|
+
|
|
52
|
+
def get(self, ns):
|
|
53
|
+
if ns in self.defs:
|
|
54
|
+
return self.defs[ns]
|
|
55
|
+
|
|
56
|
+
def get_defs(self):
|
|
57
|
+
return self.defs
|
|
58
|
+
|
|
59
|
+
def handle_function_def(self, parent_ns, fn_name):
|
|
60
|
+
full_ns = utils.join_ns(parent_ns, fn_name)
|
|
61
|
+
defi = self.get(full_ns)
|
|
62
|
+
if not defi:
|
|
63
|
+
defi = self.create(full_ns, utils.constants.FUN_DEF)
|
|
64
|
+
defi.decorator_names = set()
|
|
65
|
+
|
|
66
|
+
return_ns = utils.join_ns(full_ns, utils.constants.RETURN_NAME)
|
|
67
|
+
if not self.get(return_ns):
|
|
68
|
+
self.create(return_ns, utils.constants.NAME_DEF)
|
|
69
|
+
|
|
70
|
+
return defi
|
|
71
|
+
|
|
72
|
+
def handle_class_def(self, parent_ns, cls_name):
|
|
73
|
+
full_ns = utils.join_ns(parent_ns, cls_name)
|
|
74
|
+
defi = self.get(full_ns)
|
|
75
|
+
if not defi:
|
|
76
|
+
defi = self.create(full_ns, utils.constants.CLS_DEF)
|
|
77
|
+
|
|
78
|
+
return defi
|
|
79
|
+
|
|
80
|
+
def transitive_closure(self):
|
|
81
|
+
closured = {}
|
|
82
|
+
def dfs(defi):
|
|
83
|
+
name_pointer = defi.get_name_pointer()
|
|
84
|
+
new_set = set()
|
|
85
|
+
# bottom
|
|
86
|
+
if not closured.get(defi.get_ns(), None) == None:
|
|
87
|
+
return closured[defi.get_ns()]
|
|
88
|
+
|
|
89
|
+
if not name_pointer.get():
|
|
90
|
+
new_set.add(defi.get_ns())
|
|
91
|
+
|
|
92
|
+
closured[defi.get_ns()] = new_set
|
|
93
|
+
|
|
94
|
+
for name in name_pointer.get():
|
|
95
|
+
if not self.defs.get(name, None):
|
|
96
|
+
continue
|
|
97
|
+
items = dfs(self.defs[name])
|
|
98
|
+
if not items:
|
|
99
|
+
items = set([name])
|
|
100
|
+
new_set = new_set.union(items)
|
|
101
|
+
|
|
102
|
+
closured[defi.get_ns()] = new_set
|
|
103
|
+
return closured[defi.get_ns()]
|
|
104
|
+
|
|
105
|
+
for ns, current_def in self.defs.items():
|
|
106
|
+
if closured.get(current_def, None) == None:
|
|
107
|
+
dfs(current_def)
|
|
108
|
+
|
|
109
|
+
return closured
|
|
110
|
+
|
|
111
|
+
def complete_definitions(self):
|
|
112
|
+
# THE MOST expensive part of this tool's process
|
|
113
|
+
# TODO: IMPROVE COMPLEXITY
|
|
114
|
+
def update_pointsto_args(pointsto_args, arg):
|
|
115
|
+
changed_something = False
|
|
116
|
+
if arg == pointsto_args:
|
|
117
|
+
return False
|
|
118
|
+
for pointsto_arg in pointsto_args:
|
|
119
|
+
if not self.defs.get(pointsto_arg, None):
|
|
120
|
+
continue
|
|
121
|
+
pointsto_arg_def = self.defs[pointsto_arg].get_name_pointer()
|
|
122
|
+
if pointsto_arg_def == pointsto_args:
|
|
123
|
+
continue
|
|
124
|
+
|
|
125
|
+
# sometimes we may end up with a cycle
|
|
126
|
+
if pointsto_arg in arg:
|
|
127
|
+
arg.remove(pointsto_arg)
|
|
128
|
+
|
|
129
|
+
for item in arg:
|
|
130
|
+
if not item in pointsto_arg_def.get():
|
|
131
|
+
if self.defs.get(item, None) != None:
|
|
132
|
+
changed_something = True
|
|
133
|
+
# HACK: this check shouldn't be needed
|
|
134
|
+
# if we remove this the following breaks:
|
|
135
|
+
# x = lambda x: x + 1
|
|
136
|
+
# x(1)
|
|
137
|
+
# since on line 184 we don't discriminate between literal values and name values
|
|
138
|
+
if not self.defs.get(item, None):
|
|
139
|
+
continue
|
|
140
|
+
pointsto_arg_def.add(item)
|
|
141
|
+
return changed_something
|
|
142
|
+
|
|
143
|
+
for i in range(len(self.defs)):
|
|
144
|
+
changed_something = False
|
|
145
|
+
for ns, current_def in self.defs.items():
|
|
146
|
+
# the name pointer of the definition we're currently iterating
|
|
147
|
+
current_name_pointer = current_def.get_name_pointer()
|
|
148
|
+
# iterate the names the current definition points to items
|
|
149
|
+
for name in current_name_pointer.get():
|
|
150
|
+
# get the name pointer of the points to name
|
|
151
|
+
if not self.defs.get(name, None):
|
|
152
|
+
continue
|
|
153
|
+
pointsto_name_pointer = self.defs[name].get_name_pointer()
|
|
154
|
+
# iterate the arguments of the definition we're currently iterating
|
|
155
|
+
for arg_name, arg in current_name_pointer.get_args().items():
|
|
156
|
+
pos = current_name_pointer.get_pos_of_name(arg_name)
|
|
157
|
+
if not pos is None:
|
|
158
|
+
pointsto_args = pointsto_name_pointer.get_pos_arg(pos)
|
|
159
|
+
if not pointsto_args:
|
|
160
|
+
pointsto_name_pointer.add_pos_arg(pos, None, arg)
|
|
161
|
+
continue
|
|
162
|
+
else:
|
|
163
|
+
pointsto_args = pointsto_name_pointer.get_arg(arg_name)
|
|
164
|
+
if not pointsto_args:
|
|
165
|
+
pointsto_name_pointer.add_arg(arg_name, arg)
|
|
166
|
+
continue
|
|
167
|
+
changed_something = changed_something or update_pointsto_args(pointsto_args, arg)
|
|
168
|
+
|
|
169
|
+
if not changed_something:
|
|
170
|
+
break
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class Definition(object):
|
|
174
|
+
types = [
|
|
175
|
+
utils.constants.FUN_DEF,
|
|
176
|
+
utils.constants.MOD_DEF,
|
|
177
|
+
utils.constants.NAME_DEF,
|
|
178
|
+
utils.constants.CLS_DEF,
|
|
179
|
+
utils.constants.EXT_DEF
|
|
180
|
+
]
|
|
181
|
+
|
|
182
|
+
def __init__(self, fullns, def_type):
|
|
183
|
+
self.fullns = fullns
|
|
184
|
+
self.points_to = {
|
|
185
|
+
"lit": LiteralPointer(),
|
|
186
|
+
"name": NamePointer()
|
|
187
|
+
}
|
|
188
|
+
self.def_type = def_type
|
|
189
|
+
|
|
190
|
+
def get_type(self):
|
|
191
|
+
return self.def_type
|
|
192
|
+
|
|
193
|
+
def is_function_def(self):
|
|
194
|
+
return self.def_type == utils.constants.FUN_DEF
|
|
195
|
+
|
|
196
|
+
def is_ext_def(self):
|
|
197
|
+
return self.def_type == utils.constants.EXT_DEF
|
|
198
|
+
|
|
199
|
+
def is_callable(self):
|
|
200
|
+
return (self.is_function_def() or self.is_ext_def())
|
|
201
|
+
|
|
202
|
+
def get_lit_pointer(self):
|
|
203
|
+
return self.points_to["lit"]
|
|
204
|
+
|
|
205
|
+
def get_name_pointer(self):
|
|
206
|
+
return self.points_to["name"]
|
|
207
|
+
|
|
208
|
+
def get_name(self):
|
|
209
|
+
return self.fullns.split(".")[-1]
|
|
210
|
+
|
|
211
|
+
def get_ns(self):
|
|
212
|
+
return self.fullns
|
|
213
|
+
|
|
214
|
+
def merge(self, to_merge):
|
|
215
|
+
for name, pointer in to_merge.points_to.items():
|
|
216
|
+
self.points_to[name].merge(pointer)
|
|
217
|
+
|
|
218
|
+
class DefinitionError(Exception):
|
|
219
|
+
pass
|
|
@@ -0,0 +1,220 @@
|
|
|
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 sys
|
|
22
|
+
import ast
|
|
23
|
+
import os
|
|
24
|
+
import importlib
|
|
25
|
+
import copy
|
|
26
|
+
|
|
27
|
+
from pycg import utils
|
|
28
|
+
|
|
29
|
+
def get_custom_loader(ig_obj):
|
|
30
|
+
"""
|
|
31
|
+
Closure which returns a custom loader
|
|
32
|
+
that modifies an ImportManager object
|
|
33
|
+
"""
|
|
34
|
+
class CustomLoader(importlib.abc.SourceLoader):
|
|
35
|
+
def __init__(self, fullname, path):
|
|
36
|
+
self.fullname = fullname
|
|
37
|
+
self.path = path
|
|
38
|
+
|
|
39
|
+
ig_obj.create_edge(self.fullname)
|
|
40
|
+
if not ig_obj.get_node(self.fullname):
|
|
41
|
+
ig_obj.create_node(self.fullname)
|
|
42
|
+
ig_obj.set_filepath(self.fullname, self.path)
|
|
43
|
+
|
|
44
|
+
def get_filename(self, fullname):
|
|
45
|
+
return self.path
|
|
46
|
+
|
|
47
|
+
def get_data(self, filename):
|
|
48
|
+
return ""
|
|
49
|
+
|
|
50
|
+
return CustomLoader
|
|
51
|
+
|
|
52
|
+
class ImportManager(object):
|
|
53
|
+
def __init__(self):
|
|
54
|
+
self.import_graph = dict()
|
|
55
|
+
self.current_module = ""
|
|
56
|
+
self.input_file = ""
|
|
57
|
+
self.mod_dir = None
|
|
58
|
+
self.old_path_hooks = None
|
|
59
|
+
self.old_path = None
|
|
60
|
+
|
|
61
|
+
def set_pkg(self, input_pkg):
|
|
62
|
+
self.mod_dir = input_pkg
|
|
63
|
+
|
|
64
|
+
def get_mod_dir(self):
|
|
65
|
+
return self.mod_dir
|
|
66
|
+
|
|
67
|
+
def get_node(self, name):
|
|
68
|
+
if name in self.import_graph:
|
|
69
|
+
return self.import_graph[name]
|
|
70
|
+
|
|
71
|
+
def create_node(self, name):
|
|
72
|
+
if not name or not isinstance(name, str):
|
|
73
|
+
raise ImportManagerError("Invalid node name")
|
|
74
|
+
|
|
75
|
+
if self.get_node(name):
|
|
76
|
+
raise ImportManagerError("Can't create a node a second time")
|
|
77
|
+
|
|
78
|
+
self.import_graph[name] = {"filename": "", "imports": set()}
|
|
79
|
+
return self.import_graph[name]
|
|
80
|
+
|
|
81
|
+
def create_edge(self, dest):
|
|
82
|
+
if not dest or not isinstance(dest, str):
|
|
83
|
+
raise ImportManagerError("Invalid node name")
|
|
84
|
+
|
|
85
|
+
node = self.get_node(self._get_module_path())
|
|
86
|
+
if not node:
|
|
87
|
+
raise ImportManagerError("Can't add edge to a non existing node")
|
|
88
|
+
|
|
89
|
+
node["imports"].add(dest)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _clear_caches(self):
|
|
93
|
+
importlib.invalidate_caches()
|
|
94
|
+
sys.path_importer_cache.clear()
|
|
95
|
+
# TODO: maybe not do that since it empties the whole cache
|
|
96
|
+
for name in self.import_graph:
|
|
97
|
+
if name in sys.modules:
|
|
98
|
+
del sys.modules[name]
|
|
99
|
+
|
|
100
|
+
def _get_module_path(self):
|
|
101
|
+
return self.current_module
|
|
102
|
+
|
|
103
|
+
def set_current_mod(self, name, fname):
|
|
104
|
+
self.current_module = name
|
|
105
|
+
self.input_file = os.path.abspath(fname)
|
|
106
|
+
|
|
107
|
+
def get_filepath(self, modname):
|
|
108
|
+
if modname in self.import_graph:
|
|
109
|
+
return self.import_graph[modname]["filename"]
|
|
110
|
+
|
|
111
|
+
def set_filepath(self, node_name, filename):
|
|
112
|
+
if not filename or not isinstance(filename, str):
|
|
113
|
+
raise ImportManagerError("Invalid node name")
|
|
114
|
+
|
|
115
|
+
node = self.get_node(node_name)
|
|
116
|
+
if not node:
|
|
117
|
+
raise ImportManagerError("Node does not exist")
|
|
118
|
+
|
|
119
|
+
node["filename"] = os.path.abspath(filename)
|
|
120
|
+
|
|
121
|
+
def get_imports(self, modname):
|
|
122
|
+
if not modname in self.import_graph:
|
|
123
|
+
return []
|
|
124
|
+
return self.import_graph[modname]["imports"]
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _is_init_file(self):
|
|
128
|
+
return self.input_file.endswith("__init__.py")
|
|
129
|
+
|
|
130
|
+
def _handle_import_level(self, name, level):
|
|
131
|
+
# add a dot for each level
|
|
132
|
+
package = self._get_module_path().split(".")
|
|
133
|
+
if level > len(package):
|
|
134
|
+
raise ImportError("Attempting import beyond top level package")
|
|
135
|
+
|
|
136
|
+
mod_name = ("." * level) + name
|
|
137
|
+
# When an __init__ file is analyzed, then the module name doesn't contain
|
|
138
|
+
# the __init__ part in it, so special care must be taken for levels.
|
|
139
|
+
if self._is_init_file() and level >= 1:
|
|
140
|
+
if level != 1:
|
|
141
|
+
level -= 1
|
|
142
|
+
package = package[:-level]
|
|
143
|
+
else:
|
|
144
|
+
package = package[:-level]
|
|
145
|
+
|
|
146
|
+
return mod_name, ".".join(package)
|
|
147
|
+
|
|
148
|
+
def _do_import(self, mod_name, package):
|
|
149
|
+
if mod_name in sys.modules:
|
|
150
|
+
self.create_edge(mod_name)
|
|
151
|
+
return sys.modules[mod_name]
|
|
152
|
+
|
|
153
|
+
return importlib.import_module(mod_name, package=package)
|
|
154
|
+
|
|
155
|
+
def handle_import(self, name, level):
|
|
156
|
+
# We currently don't support builtin modules because they're frozen.
|
|
157
|
+
# Add an edge and continue.
|
|
158
|
+
# TODO: identify a way to include frozen modules
|
|
159
|
+
root = name.split(".")[0]
|
|
160
|
+
if root in sys.builtin_module_names:
|
|
161
|
+
self.create_edge(root)
|
|
162
|
+
return
|
|
163
|
+
|
|
164
|
+
# Import the module
|
|
165
|
+
try:
|
|
166
|
+
mod_name, package = self._handle_import_level(name, level)
|
|
167
|
+
except ImportError:
|
|
168
|
+
return
|
|
169
|
+
|
|
170
|
+
parent = ".".join(mod_name.split(".")[:-1])
|
|
171
|
+
parent_name = ".".join(name.split(".")[:-1])
|
|
172
|
+
combos = [(mod_name, package),
|
|
173
|
+
(parent, package),
|
|
174
|
+
(utils.join_ns(package, name), ""),
|
|
175
|
+
(utils.join_ns(package, parent_name), "")]
|
|
176
|
+
|
|
177
|
+
mod = None
|
|
178
|
+
for mn, pkg in combos:
|
|
179
|
+
try:
|
|
180
|
+
mod = self._do_import(mn, pkg)
|
|
181
|
+
break
|
|
182
|
+
except:
|
|
183
|
+
continue
|
|
184
|
+
|
|
185
|
+
if not mod:
|
|
186
|
+
return
|
|
187
|
+
|
|
188
|
+
if not hasattr(mod, "__file__") or not mod.__file__:
|
|
189
|
+
return
|
|
190
|
+
if self.mod_dir not in mod.__file__:
|
|
191
|
+
return
|
|
192
|
+
fname = mod.__file__
|
|
193
|
+
if fname.endswith("__init__.py"):
|
|
194
|
+
fname = os.path.split(fname)[0]
|
|
195
|
+
|
|
196
|
+
return utils.to_mod_name(
|
|
197
|
+
os.path.relpath(fname, self.mod_dir))
|
|
198
|
+
|
|
199
|
+
def get_import_graph(self):
|
|
200
|
+
return self.import_graph
|
|
201
|
+
|
|
202
|
+
def install_hooks(self):
|
|
203
|
+
loader = get_custom_loader(self)
|
|
204
|
+
self.old_path_hooks = copy.deepcopy(sys.path_hooks)
|
|
205
|
+
self.old_path = copy.deepcopy(sys.path)
|
|
206
|
+
|
|
207
|
+
loader_details = loader, importlib.machinery.all_suffixes()
|
|
208
|
+
sys.path_hooks.insert(0, importlib.machinery.FileFinder.path_hook(loader_details))
|
|
209
|
+
sys.path.insert(0, os.path.abspath(self.mod_dir))
|
|
210
|
+
|
|
211
|
+
self._clear_caches()
|
|
212
|
+
|
|
213
|
+
def remove_hooks(self):
|
|
214
|
+
sys.path_hooks = self.old_path_hooks
|
|
215
|
+
sys.path = self.old_path
|
|
216
|
+
|
|
217
|
+
self._clear_caches()
|
|
218
|
+
|
|
219
|
+
class ImportManagerError(Exception):
|
|
220
|
+
pass
|
|
@@ -0,0 +1,34 @@
|
|
|
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,
|
|
28
|
+
"lineno": lineno,
|
|
29
|
+
"namespace": namespace,
|
|
30
|
+
"key": key
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
def get(self):
|
|
34
|
+
return self.key_errs
|