cpg2py 1.0.0__py3-none-any.whl → 1.0.2__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.
- cpg2py/abc/__init__.py +6 -0
- cpg2py/abc/edge.py +39 -0
- cpg2py/abc/graph.py +98 -0
- cpg2py/abc/node.py +26 -0
- cpg2py/abc/storage.py +153 -0
- cpg2py/cpg/__init__.py +3 -0
- cpg2py/cpg/edge.py +32 -0
- cpg2py/cpg/graph.py +97 -0
- cpg2py/cpg/node.py +65 -0
- {cpg2py-1.0.0.dist-info → cpg2py-1.0.2.dist-info}/METADATA +1 -1
- cpg2py-1.0.2.dist-info/RECORD +15 -0
- cpg2py-1.0.0.dist-info/RECORD +0 -6
- {cpg2py-1.0.0.dist-info → cpg2py-1.0.2.dist-info}/LICENSE +0 -0
- {cpg2py-1.0.0.dist-info → cpg2py-1.0.2.dist-info}/WHEEL +0 -0
- {cpg2py-1.0.0.dist-info → cpg2py-1.0.2.dist-info}/top_level.txt +0 -0
cpg2py/abc/__init__.py
ADDED
cpg2py/abc/edge.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import abc
|
|
3
|
+
from .storage import Storage
|
|
4
|
+
from typing import Any, Dict, Optional, Tuple
|
|
5
|
+
|
|
6
|
+
class AbcEdgeQuerier(abc.ABC):
|
|
7
|
+
|
|
8
|
+
def __init__(self, graph: Storage, f_nid: str, t_nid: str, e_type: int = 0) -> None:
|
|
9
|
+
self.__graph: Storage = graph
|
|
10
|
+
self.__edge_id: Tuple[str, str, str] = (str(f_nid), str(t_nid), str(e_type))
|
|
11
|
+
if not graph.contains_edge(self.__edge_id):
|
|
12
|
+
raise Exception('CANNOT FIND THE NODE ID {nid} IN GRAPH')
|
|
13
|
+
return None
|
|
14
|
+
|
|
15
|
+
@property
|
|
16
|
+
def edge_id(self) -> Tuple[str, str, int]:
|
|
17
|
+
return self.__edge_id
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def from_nid(self) -> str:
|
|
21
|
+
return self.__edge_id[0]
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def to_nid(self) -> str:
|
|
25
|
+
return self.__edge_id[1]
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def edge_type(self) -> str:
|
|
29
|
+
return self.__edge_id[0]
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def properties(self) -> Optional[Dict[str, Any]]:
|
|
33
|
+
return self.__graph.get_edge_props(self.__edge_id)
|
|
34
|
+
|
|
35
|
+
def get_property(self, *prop_names: str) -> Optional[Any]:
|
|
36
|
+
prop_values = (self.__graph.get_edge_prop(self.__edge_id, p_name) for p_name in prop_names)
|
|
37
|
+
return next((value for value in prop_values if value is not None), None)
|
|
38
|
+
|
|
39
|
+
pass
|
cpg2py/abc/graph.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from .edge import AbcEdgeQuerier
|
|
5
|
+
from .node import AbcNodeQuerier
|
|
6
|
+
|
|
7
|
+
from typing import Callable, Deque, List, Optional, Iterable
|
|
8
|
+
from collections import deque
|
|
9
|
+
from .storage import Storage
|
|
10
|
+
import abc
|
|
11
|
+
|
|
12
|
+
class AbcGraphQuerier(abc.ABC):
|
|
13
|
+
|
|
14
|
+
__NodeCondition = Callable[[AbcNodeQuerier], bool]
|
|
15
|
+
__EdgeCondition = Callable[[AbcEdgeQuerier], bool]
|
|
16
|
+
__always_true = lambda _: True
|
|
17
|
+
|
|
18
|
+
__NodesResult = Iterable[AbcNodeQuerier]
|
|
19
|
+
__EdgesResult = Iterable[AbcEdgeQuerier]
|
|
20
|
+
|
|
21
|
+
def __init__(self, target: Storage, maxdepth: int=-1) -> None:
|
|
22
|
+
self.__graph: Storage = target
|
|
23
|
+
self.__maxdepth: int = maxdepth
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def storage(self) -> Storage:
|
|
27
|
+
return self.__graph
|
|
28
|
+
|
|
29
|
+
@abc.abstractmethod
|
|
30
|
+
def node(self, whose_id_is: str) -> Optional[AbcNodeQuerier]:
|
|
31
|
+
raise NotImplementedError
|
|
32
|
+
|
|
33
|
+
@abc.abstractmethod
|
|
34
|
+
def edge(self, fid, tid, eid) -> Optional[AbcEdgeQuerier]:
|
|
35
|
+
raise NotImplementedError
|
|
36
|
+
|
|
37
|
+
def nodes(self, who_satisifies: __NodeCondition=__always_true)-> __NodesResult:
|
|
38
|
+
for nid in self.__graph.get_nodes():
|
|
39
|
+
cur_node = self.node(whose_id_is=nid)
|
|
40
|
+
if cur_node and who_satisifies(cur_node): yield cur_node
|
|
41
|
+
pass
|
|
42
|
+
|
|
43
|
+
def first_node(self, who_satisifies: __NodeCondition=__always_true)-> Optional[AbcNodeQuerier]:
|
|
44
|
+
return next(self.nodes(who_satisifies), None)
|
|
45
|
+
|
|
46
|
+
def edges(self, who_satisifies: __EdgeCondition = __always_true) -> __EdgesResult:
|
|
47
|
+
for from_id, to_id, edge_id in self.__graph.get_edges():
|
|
48
|
+
cur_edge = self.edge(from_id, to_id, edge_id)
|
|
49
|
+
if cur_edge and who_satisifies(cur_edge): yield cur_edge
|
|
50
|
+
pass
|
|
51
|
+
|
|
52
|
+
def succ(self, of: AbcNodeQuerier, who_satisifies: __EdgeCondition = __always_true) -> __NodesResult:
|
|
53
|
+
for src, dst, type in self.__graph.out_edges(of.id):
|
|
54
|
+
if not who_satisifies(self.edge(src, dst, type)): continue
|
|
55
|
+
yield self.node(whose_id_is=dst)
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
def prev(self, of: AbcNodeQuerier, who_satisifies: __EdgeCondition = __always_true) -> __NodesResult:
|
|
59
|
+
for src, dst, type in self.__graph.in_edges(of.id):
|
|
60
|
+
if not who_satisifies(self.edge(src, dst, type)): continue
|
|
61
|
+
yield self.node(whose_id_is=src)
|
|
62
|
+
pass
|
|
63
|
+
|
|
64
|
+
def __bfs_search(self, root: AbcNodeQuerier, condition: __EdgeCondition, reverse: bool) -> __NodesResult:
|
|
65
|
+
'''
|
|
66
|
+
return the nodes from (any edge relation) src node by the bfs order (src node will not included)
|
|
67
|
+
'''
|
|
68
|
+
if root is None: return
|
|
69
|
+
visited_nids: List[str] = list()
|
|
70
|
+
nodes_queue: Deque[AbcNodeQuerier] = deque([root, None])
|
|
71
|
+
depth = self.__maxdepth
|
|
72
|
+
while depth != 0 and len(nodes_queue) > 1:
|
|
73
|
+
cur_node = nodes_queue.popleft()
|
|
74
|
+
if cur_node == None:
|
|
75
|
+
nodes_queue.append(None)
|
|
76
|
+
depth -= 1
|
|
77
|
+
elif cur_node.id not in visited_nids:
|
|
78
|
+
visited_nids.append(cur_node.id)
|
|
79
|
+
if not reverse: n_nodes = self.succ(cur_node, condition)
|
|
80
|
+
else: n_nodes = self.prev(cur_node, condition)
|
|
81
|
+
nodes_queue.extend(n_nodes)
|
|
82
|
+
if root.id != cur_node.id: yield cur_node
|
|
83
|
+
pass
|
|
84
|
+
|
|
85
|
+
def descendants(self, src: AbcNodeQuerier, condition: __EdgeCondition = __always_true) -> __NodesResult:
|
|
86
|
+
'''
|
|
87
|
+
return the result of descendants from (any edge relation) src node by the bfs order (src node will not included)
|
|
88
|
+
'''
|
|
89
|
+
return self.__bfs_search(src, condition, reverse=False)
|
|
90
|
+
|
|
91
|
+
def ancestors(self, src: AbcNodeQuerier, condition: __EdgeCondition = __always_true) -> __NodesResult:
|
|
92
|
+
'''
|
|
93
|
+
return the result of ancestors from (any edge relation) src node by the bfs order (src node will not included)
|
|
94
|
+
'''
|
|
95
|
+
return self.__bfs_search(src, condition, reverse=True)
|
|
96
|
+
|
|
97
|
+
pass
|
|
98
|
+
|
cpg2py/abc/node.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from typing import Any, Dict, Optional
|
|
2
|
+
from .storage import Storage
|
|
3
|
+
import abc
|
|
4
|
+
|
|
5
|
+
class AbcNodeQuerier(abc.ABC):
|
|
6
|
+
|
|
7
|
+
def __init__(self, graph: Storage, nid: str) -> None:
|
|
8
|
+
self.__nid: str = str(nid)
|
|
9
|
+
self.__graph: Storage = graph
|
|
10
|
+
if not graph.contains_node(self.__nid):
|
|
11
|
+
raise Exception(f'CANNOT FIND THE NODE ID {nid} IN GRAPH')
|
|
12
|
+
return None
|
|
13
|
+
|
|
14
|
+
@property
|
|
15
|
+
def node_id(self) -> str:
|
|
16
|
+
return self.__nid
|
|
17
|
+
|
|
18
|
+
@property
|
|
19
|
+
def properties(self) -> Optional[Dict[str, Any]]:
|
|
20
|
+
return self.__graph.get_node_props(self.__nid)
|
|
21
|
+
|
|
22
|
+
def get_property(self, *prop_names: str) -> Optional[Any]:
|
|
23
|
+
prop_values = (self.__graph.get_node_prop(self.__nid, p_name) for p_name in prop_names)
|
|
24
|
+
return next((value for value in prop_values if value is not None), None)
|
|
25
|
+
|
|
26
|
+
pass
|
cpg2py/abc/storage.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
from typing import Optional, Iterable, Dict, Tuple
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Storage:
|
|
5
|
+
""" A directed multi-graph implementation supporting multiple edges between nodes. """
|
|
6
|
+
|
|
7
|
+
__NodeID = str
|
|
8
|
+
__EdgeID = Tuple[str, str, str]
|
|
9
|
+
__Property = Dict[str, any]
|
|
10
|
+
|
|
11
|
+
def __init__(self):
|
|
12
|
+
""" Initializes an empty directed graph. """
|
|
13
|
+
self.__nodes = dict()
|
|
14
|
+
self.__edges = dict()
|
|
15
|
+
self.__struct = dict()
|
|
16
|
+
|
|
17
|
+
################################ GRAPH STRUCTURE APIs ################################
|
|
18
|
+
|
|
19
|
+
def add_node(self, nid: __NodeID) -> bool:
|
|
20
|
+
""" Adds a node to the graph. """
|
|
21
|
+
nid = str(nid)
|
|
22
|
+
if nid in self.__nodes: return False
|
|
23
|
+
self.__nodes[nid] = {}
|
|
24
|
+
self.__struct[nid] = []
|
|
25
|
+
return True
|
|
26
|
+
|
|
27
|
+
def contains_node(self, nid: __NodeID) -> bool:
|
|
28
|
+
""" Checks if a node exists in the graph. """
|
|
29
|
+
nid = str(nid)
|
|
30
|
+
return nid in self.__nodes
|
|
31
|
+
|
|
32
|
+
def add_edge(self, eid: __EdgeID) -> bool:
|
|
33
|
+
""" Adds a directed edge from `start` to `end` with an optional edge type. """
|
|
34
|
+
eid = (str(eid[0]), str(eid[1]), str(eid[2]))
|
|
35
|
+
if eid in self.__edges: return False
|
|
36
|
+
if eid[0] not in self.__nodes: return False
|
|
37
|
+
if eid[1] not in self.__nodes: return False
|
|
38
|
+
self.__edges[eid] = {}
|
|
39
|
+
self.__struct[eid[0]].append(eid)
|
|
40
|
+
self.__struct[eid[1]].append(eid)
|
|
41
|
+
return True
|
|
42
|
+
|
|
43
|
+
def contains_edge(self, eid: __EdgeID) -> bool:
|
|
44
|
+
""" Checks if an edge exists in the graph. """
|
|
45
|
+
eid = (str(eid[0]), str(eid[1]), str(eid[2]))
|
|
46
|
+
return eid in self.__edges
|
|
47
|
+
|
|
48
|
+
def out_edges(self, nid: __NodeID) -> Iterable[__EdgeID]:
|
|
49
|
+
""" Returns a list of outgoing edges from a given node. """
|
|
50
|
+
nid = str(nid)
|
|
51
|
+
return (eid for eid in self.__struct.get(nid) if eid[0] == nid)
|
|
52
|
+
|
|
53
|
+
def in_edges(self, nid: __NodeID) -> Iterable[__EdgeID]:
|
|
54
|
+
""" Returns a list of incoming edges to a given node. """
|
|
55
|
+
nid = str(nid)
|
|
56
|
+
return (eid for eid in self.__struct.get(nid) if eid[1] == nid)
|
|
57
|
+
|
|
58
|
+
def successors(self, nid: __NodeID) -> Iterable[__NodeID]:
|
|
59
|
+
""" Returns all successor nodes of a given node. """
|
|
60
|
+
nid = str(nid)
|
|
61
|
+
return (eid[1] for eid in self.__struct.get(nid) if eid[0] == nid)
|
|
62
|
+
|
|
63
|
+
def predecessors(self, nid: __NodeID) -> Iterable[__NodeID]:
|
|
64
|
+
""" Returns all predecessor nodes of a given node. """
|
|
65
|
+
nid = str(nid)
|
|
66
|
+
return (eid[0] for eid in self.__struct.get(nid) if eid[1] == nid)
|
|
67
|
+
|
|
68
|
+
################################ GRAPH PROPERTIES APIs ################################
|
|
69
|
+
|
|
70
|
+
def set_node_props(self, node: __NodeID, props: __Property) -> bool:
|
|
71
|
+
""" Sets the properties of a node. """
|
|
72
|
+
node = str(node)
|
|
73
|
+
if node not in self.__nodes: return False
|
|
74
|
+
prev_data: dict = self.__nodes[node]
|
|
75
|
+
prev_data.update({str(k) : v for k, v in props.items()})
|
|
76
|
+
return True
|
|
77
|
+
|
|
78
|
+
def get_node_props(self, node: __NodeID) -> Optional[__Property]:
|
|
79
|
+
""" Returns the properties of a node. """
|
|
80
|
+
node = str(node)
|
|
81
|
+
print(self.__nodes)
|
|
82
|
+
return self.__nodes.get(node, None)
|
|
83
|
+
|
|
84
|
+
def set_node_prop(self, node: __NodeID, key: str, value: any) -> bool:
|
|
85
|
+
""" Sets the properties of a node. """
|
|
86
|
+
node, key = str(node), str(key)
|
|
87
|
+
if node not in self.__nodes: return False
|
|
88
|
+
self.__nodes[node][key] = value
|
|
89
|
+
return True
|
|
90
|
+
|
|
91
|
+
def get_node_prop(self, node: __NodeID, key: str) -> Optional[any]:
|
|
92
|
+
""" Returns the properties of a node. """
|
|
93
|
+
node, key = str(node), str(key)
|
|
94
|
+
return self.__nodes.get(node, {}).get(key, None)
|
|
95
|
+
|
|
96
|
+
def set_edge_props(self, eid: __EdgeID, props: __Property) -> bool:
|
|
97
|
+
""" Sets the properties of an edge. """
|
|
98
|
+
eid = (str(eid[0]), str(eid[1]), str(eid[2]))
|
|
99
|
+
if eid not in self.__edges: return False
|
|
100
|
+
prev_data: dict = self.__edges[eid]
|
|
101
|
+
prev_data.update({str(k) : v for k, v in props.items()})
|
|
102
|
+
return True
|
|
103
|
+
|
|
104
|
+
def get_edge_props(self, eid: __EdgeID) -> Optional[__Property]:
|
|
105
|
+
""" Returns the properties of an edge. """
|
|
106
|
+
eid = (str(eid[0]), str(eid[1]), str(eid[2]))
|
|
107
|
+
return self.__edges.get(eid)
|
|
108
|
+
|
|
109
|
+
def set_edge_prop(self, eid: __EdgeID, key: str, value: any) -> bool:
|
|
110
|
+
""" Sets the properties of an edge. """
|
|
111
|
+
eid = (str(eid[0]), str(eid[1]), str(eid[2]))
|
|
112
|
+
key = str(key)
|
|
113
|
+
if eid not in self.__edges: return False
|
|
114
|
+
self.__edges[eid][key] = value
|
|
115
|
+
return
|
|
116
|
+
|
|
117
|
+
def get_edge_prop(self, eid: __EdgeID, key: str) -> Optional[__Property]:
|
|
118
|
+
""" Returns the properties of an edge. """
|
|
119
|
+
eid = (str(eid[0]), str(eid[1]), str(eid[2]))
|
|
120
|
+
key = str(key)
|
|
121
|
+
return self.__edges.get(eid, {}).get(key, None)
|
|
122
|
+
|
|
123
|
+
def __repr__(self):
|
|
124
|
+
""" Returns a string representation of the graph. """
|
|
125
|
+
return f"MultiDiGraph(nodes={len(self.nodes)}, edges={len(self.get_edges())})"
|
|
126
|
+
|
|
127
|
+
################################ GRAPH COMMON APIs ################################
|
|
128
|
+
|
|
129
|
+
def get_nodes(self) -> Iterable[__NodeID]:
|
|
130
|
+
""" Returns a list of all nodes in the graph. """
|
|
131
|
+
return self.__nodes.keys()
|
|
132
|
+
|
|
133
|
+
def get_edges(self) -> Iterable[__EdgeID]:
|
|
134
|
+
""" Returns a list of all edges in the graph. """
|
|
135
|
+
return self.__edges.keys()
|
|
136
|
+
|
|
137
|
+
def remove_node(self, nid: __NodeID) -> bool:
|
|
138
|
+
""" Removes a node from the graph. """
|
|
139
|
+
nid = str(nid)
|
|
140
|
+
if nid not in self.__nodes: return False
|
|
141
|
+
self.__nodes.pop(nid)
|
|
142
|
+
for eid in self.__struct[nid]:
|
|
143
|
+
self.__edges.pop(eid)
|
|
144
|
+
return True
|
|
145
|
+
|
|
146
|
+
def remove_edge(self, eid: __EdgeID) -> bool:
|
|
147
|
+
""" Removes an edge from the graph. """
|
|
148
|
+
eid = (str(eid[0]), str(eid[1]), str(eid[2]))
|
|
149
|
+
if eid not in self.__edges: return False
|
|
150
|
+
self.__struct[eid[0]].remove(eid)
|
|
151
|
+
self.__struct[eid[1]].remove(eid)
|
|
152
|
+
self.__edges.pop(eid)
|
|
153
|
+
return True
|
cpg2py/cpg/__init__.py
ADDED
cpg2py/cpg/edge.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from typing import Optional, Tuple
|
|
3
|
+
from enum import Enum
|
|
4
|
+
from ..abc import AbcGraphQuerier, AbcEdgeQuerier
|
|
5
|
+
|
|
6
|
+
class _Edge(AbcEdgeQuerier):
|
|
7
|
+
|
|
8
|
+
def __init__(self, graph: AbcGraphQuerier, f_nid: str, t_nid: str, e_type: str) -> None:
|
|
9
|
+
super().__init__(graph, f_nid, t_nid, e_type)
|
|
10
|
+
return None
|
|
11
|
+
|
|
12
|
+
@property
|
|
13
|
+
def id(self) -> Tuple[str, str, str]:
|
|
14
|
+
return self.edge_id
|
|
15
|
+
|
|
16
|
+
@property
|
|
17
|
+
def start(self) -> Optional[int]:
|
|
18
|
+
start_str = str(self.get_property('start', 'start:START_ID'))
|
|
19
|
+
return int(start_str) if start_str.isnumeric() else int(self.__from_id)
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def end(self) -> Optional[int]:
|
|
23
|
+
end_str = str(self.get_property('end', 'end:END_ID'))
|
|
24
|
+
return int(end_str) if end_str.isnumeric() else int(self.__to_id)
|
|
25
|
+
|
|
26
|
+
@property
|
|
27
|
+
def type(self) -> Optional[str]:
|
|
28
|
+
return self.get_property('type', 'type:TYPE')
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
def var(self) -> Optional[str]:
|
|
32
|
+
return self.get_property('var')
|
cpg2py/cpg/graph.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from .edge import _Edge
|
|
5
|
+
from .node import _Node
|
|
6
|
+
from ..abc import AbcGraphQuerier, Storage
|
|
7
|
+
|
|
8
|
+
from typing import Callable, Iterable, Optional
|
|
9
|
+
import json, functools
|
|
10
|
+
|
|
11
|
+
class _Graph(AbcGraphQuerier):
|
|
12
|
+
'''
|
|
13
|
+
OPG is Object Property Diagram used by ODgen and FAST
|
|
14
|
+
'''
|
|
15
|
+
__EdgeCondition = Callable[[_Edge], bool]
|
|
16
|
+
__always_true = lambda _: True
|
|
17
|
+
|
|
18
|
+
def __init__(self, target: Storage) -> None:
|
|
19
|
+
super().__init__(target)
|
|
20
|
+
return None
|
|
21
|
+
|
|
22
|
+
def node(self, whose_id_is: str) -> Optional[_Node]:
|
|
23
|
+
try:
|
|
24
|
+
return _Node(self.storage, whose_id_is)
|
|
25
|
+
except Exception as e: print(
|
|
26
|
+
f'✘ {_Graph} ERROR:'
|
|
27
|
+
f'Cannot find node with id {whose_id_is}.'
|
|
28
|
+
f'(exception is {e})'
|
|
29
|
+
)
|
|
30
|
+
return None
|
|
31
|
+
|
|
32
|
+
def edge(self, fid: str, tid: str, eid:str) -> Optional[_Edge]:
|
|
33
|
+
try:
|
|
34
|
+
return _Edge(self.storage, fid, tid, eid)
|
|
35
|
+
except Exception as e: print(
|
|
36
|
+
f'✘ {_Graph} ERROR:'
|
|
37
|
+
f'Cannot find edge from {fid} to {tid}, and eid is {str(eid)}.'
|
|
38
|
+
f'(exception is {e})'
|
|
39
|
+
)
|
|
40
|
+
return None
|
|
41
|
+
|
|
42
|
+
@functools.lru_cache()
|
|
43
|
+
def topfile_node(self, of_nid: str) -> _Node:
|
|
44
|
+
'''
|
|
45
|
+
find the top file node from the input node.
|
|
46
|
+
'''
|
|
47
|
+
of_node = self.node(of_nid)
|
|
48
|
+
if of_node.type == "File": return of_node
|
|
49
|
+
if 'TOPLEVEL_FILE' in of_node.flags: return of_node
|
|
50
|
+
parents = self.prev(of_node, lambda e: e.type in ["PARENT_OF", "ENTRY", "EXIT"])
|
|
51
|
+
for pre in parents:
|
|
52
|
+
top_file = self.topfile_node(pre.id)
|
|
53
|
+
if top_file is not None: return top_file
|
|
54
|
+
raise Exception(f'❌ INNER ERROR(500): CANNOT FIND THE TOPFILE.')
|
|
55
|
+
|
|
56
|
+
def succ(self, of: _Node, who_satisifies: __EdgeCondition = __always_true) -> Iterable[_Node]:
|
|
57
|
+
'''
|
|
58
|
+
return the next nodes connected with the input one.
|
|
59
|
+
'''
|
|
60
|
+
return super().succ(of.id, who_satisifies)
|
|
61
|
+
|
|
62
|
+
def prev(self, of, who_satisifies = __always_true) -> Iterable[_Node]:
|
|
63
|
+
'''
|
|
64
|
+
return the previous nodes connected with the input one.
|
|
65
|
+
'''
|
|
66
|
+
return super().prev(of, who_satisifies)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def children(self, of: _Node, extra: __EdgeCondition = __always_true) -> Iterable[_Node]:
|
|
70
|
+
'''
|
|
71
|
+
return the next nodes connected with the input one.
|
|
72
|
+
The edge type between them is PARENT_OF
|
|
73
|
+
'''
|
|
74
|
+
return self.succ(of, lambda e: extra(e) and (e.type == "PARENT_OF"))
|
|
75
|
+
|
|
76
|
+
def parent(self, of: _Node, extra:__EdgeCondition = __always_true) -> Iterable[_Node]:
|
|
77
|
+
'''
|
|
78
|
+
return the prev nodes connected with the input one.
|
|
79
|
+
The edge type between them is PARENT_OF
|
|
80
|
+
'''
|
|
81
|
+
return self.prev(of, lambda e: extra(e) and (e.type == "PARENT_OF"))
|
|
82
|
+
|
|
83
|
+
def flow_to(self, of: _Node, extra: __EdgeCondition = __always_true) -> Iterable[_Node]:
|
|
84
|
+
'''
|
|
85
|
+
return the next nodes connected with the input one.
|
|
86
|
+
The edge type between them is FLOW_TO
|
|
87
|
+
'''
|
|
88
|
+
return self.succ(of, lambda e: extra(e) and (e.type == "FLOWS_TO"))
|
|
89
|
+
|
|
90
|
+
def flow_from(self, of: _Node, extra: __EdgeCondition = __always_true) -> Iterable[_Node]:
|
|
91
|
+
'''
|
|
92
|
+
return the previous nodes connected with the input one.
|
|
93
|
+
The edge type between them is FLOW_TO
|
|
94
|
+
'''
|
|
95
|
+
return self.prev(of, lambda e: extra(e) and (e.type == "FLOWS_TO"))
|
|
96
|
+
|
|
97
|
+
pass
|
cpg2py/cpg/node.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from typing import List, Optional
|
|
3
|
+
|
|
4
|
+
from ..abc import AbcNodeQuerier, AbcGraphQuerier
|
|
5
|
+
|
|
6
|
+
class _Node(AbcNodeQuerier):
|
|
7
|
+
|
|
8
|
+
def __init__(self, graph: AbcGraphQuerier, nid: str) -> None:
|
|
9
|
+
super().__init__(graph, nid)
|
|
10
|
+
return None
|
|
11
|
+
|
|
12
|
+
@property
|
|
13
|
+
def id(self) -> str: return self.node_id
|
|
14
|
+
|
|
15
|
+
@property
|
|
16
|
+
def code(self) -> Optional[str]:
|
|
17
|
+
return self.get_property('code')
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def label(self) -> Optional[str]:
|
|
21
|
+
return self.get_property('labels:label', 'labels')
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def flags(self) -> List[str]:
|
|
25
|
+
flags_str = self.get_property('flags:string_array', 'flags:string[]', 'flags')
|
|
26
|
+
return str(flags_str).split(' ') if flags_str is not None else []
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def line_num(self) -> Optional[int]:
|
|
30
|
+
linenum_str = str(self.get_property('lineno:int', 'lineno'))
|
|
31
|
+
return int(linenum_str) if linenum_str.isnumeric() else None
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def children_num(self) -> Optional[int]:
|
|
35
|
+
num_str = str(self.get_property('childnum:int', 'childnum'))
|
|
36
|
+
return int(num_str) if num_str.isnumeric() else None
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def func_id(self) -> Optional[int]:
|
|
40
|
+
fid_str = str(self.get_property('funcid:int', 'funcid'))
|
|
41
|
+
return int(fid_str) if fid_str.isnumeric() else None
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def class_name(self) -> Optional[str]:
|
|
45
|
+
return self.get_property('classname')
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def namespace(self) -> Optional[str]:
|
|
49
|
+
return self.get_property('namespace')
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def name(self) -> Optional[str]: return self.get_property('name')
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def end_num(self) -> Optional[int]:
|
|
56
|
+
end_str = str(self.get_property('endlineno:int', 'endlineno'))
|
|
57
|
+
return int(end_str) if end_str.isnumeric() else None
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def comment(self) -> Optional[str]:
|
|
61
|
+
return self.get_property('doccomment')
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def type(self) -> Optional[str]:
|
|
65
|
+
return self.get_property('type')
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
cpg2py/__init__.py,sha256=bjd1EjYmNa0N8DW4tSQg-YpzzkqgtBVR2FrgDZReY10,1557
|
|
2
|
+
cpg2py/abc/__init__.py,sha256=HgDXsJkGcPQvr6ac3hMrUU9TuQibMaP7-oXbXLs_iLI,207
|
|
3
|
+
cpg2py/abc/edge.py,sha256=zLlRebDKT2qK5XOJq23_UUCESgGsvgalbXdx_uUcKks,1240
|
|
4
|
+
cpg2py/abc/graph.py,sha256=DbeHN6qsNaqcqL9cUquJWC16BihDxy1MEhyuL-qVMBI,3902
|
|
5
|
+
cpg2py/abc/node.py,sha256=E2EQv_KisEPuslFaglZZvSBm-VY5LYCFBOFY1yiPzDY,844
|
|
6
|
+
cpg2py/abc/storage.py,sha256=BF82Vs_7FxGYSPiM_6JwQVzmaqMzGy2r-WSk8pQQclY,5976
|
|
7
|
+
cpg2py/cpg/__init__.py,sha256=fO59Yd7OISyxdjdZDJ5zFM41r4cYKawOsvpqV4gWrGM,48
|
|
8
|
+
cpg2py/cpg/edge.py,sha256=_ERU_lS4_5AxlOiuX73JvTZBSr9o8ae0q-kWwO82580,1029
|
|
9
|
+
cpg2py/cpg/graph.py,sha256=n4EjAhhjzl0XRJBwlAmK5AcSrECiqhjA6dXt2Ucf7IM,3448
|
|
10
|
+
cpg2py/cpg/node.py,sha256=-2xl8c-eSbe4Kv4xPAEf6POlCYOV4j0voxJAKDZj3Cc,2037
|
|
11
|
+
cpg2py-1.0.2.dist-info/LICENSE,sha256=vTjbt7iL1hUilI8E87FoQerEDa9nbpeip26iA6bguHI,1066
|
|
12
|
+
cpg2py-1.0.2.dist-info/METADATA,sha256=dCiQ4H2zcFC9AfFVOsSAkzFJDsKccGwcuyGYCnj9LRs,7077
|
|
13
|
+
cpg2py-1.0.2.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
|
14
|
+
cpg2py-1.0.2.dist-info/top_level.txt,sha256=xDY8faKh5Rczvsqb5Jt9Sq-Y7EOImh7jh-m1oVTnH5k,7
|
|
15
|
+
cpg2py-1.0.2.dist-info/RECORD,,
|
cpg2py-1.0.0.dist-info/RECORD
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
cpg2py/__init__.py,sha256=bjd1EjYmNa0N8DW4tSQg-YpzzkqgtBVR2FrgDZReY10,1557
|
|
2
|
-
cpg2py-1.0.0.dist-info/LICENSE,sha256=vTjbt7iL1hUilI8E87FoQerEDa9nbpeip26iA6bguHI,1066
|
|
3
|
-
cpg2py-1.0.0.dist-info/METADATA,sha256=ESCorHBwLXChyZNEH5MdekKZiD59VFc1g6IYs8-YsfM,7077
|
|
4
|
-
cpg2py-1.0.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
|
5
|
-
cpg2py-1.0.0.dist-info/top_level.txt,sha256=xDY8faKh5Rczvsqb5Jt9Sq-Y7EOImh7jh-m1oVTnH5k,7
|
|
6
|
-
cpg2py-1.0.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|