cpg2py 1.0.5__py3-none-any.whl → 1.2.0__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.
@@ -0,0 +1,180 @@
1
+ Metadata-Version: 2.4
2
+ Name: cpg2py
3
+ Version: 1.2.0
4
+ Summary: A graph-based data structure designed for querying CSV files in Joern format in Python
5
+ Author-email: samhsu-dev <yxu166@jhu.edu>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 samhsu-dev
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/samhsu-dev/cpg2py
29
+ Project-URL: Repository, https://github.com/samhsu-dev/cpg2py
30
+ Project-URL: Documentation, https://github.com/samhsu-dev/cpg2py
31
+ Keywords: Joern,CPG,Graph,CSV
32
+ Classifier: Development Status :: 4 - Beta
33
+ Classifier: Intended Audience :: Developers
34
+ Classifier: License :: OSI Approved :: MIT License
35
+ Classifier: Programming Language :: Python :: 3
36
+ Classifier: Programming Language :: Python :: 3.8
37
+ Classifier: Programming Language :: Python :: 3.9
38
+ Classifier: Programming Language :: Python :: 3.10
39
+ Classifier: Programming Language :: Python :: 3.11
40
+ Classifier: Programming Language :: Python :: 3.12
41
+ Classifier: Programming Language :: Python :: 3.13
42
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
43
+ Requires-Python: >=3.8
44
+ Description-Content-Type: text/markdown
45
+ License-File: LICENSE
46
+ Provides-Extra: test
47
+ Requires-Dist: pytest>=7.4.0; extra == "test"
48
+ Requires-Dist: pytest-cov>=4.0.0; extra == "test"
49
+ Provides-Extra: dev
50
+ Requires-Dist: pytest>=7.4.0; extra == "dev"
51
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
52
+ Requires-Dist: black>=23.0.0; extra == "dev"
53
+ Requires-Dist: isort>=5.12.0; extra == "dev"
54
+ Requires-Dist: pylint>=2.17.0; extra == "dev"
55
+ Dynamic: license-file
56
+
57
+ # cpg2py
58
+
59
+ Python graph query engine for Code Property Graphs from Joern CSV exports. Directed multi-graph with generic ABCs for custom node/edge/graph types.
60
+
61
+ **Features**: Load from `nodes.csv` + `rels.csv`; query/update nodes and edges (`get_property`, `set_property`, `set_properties`); traverse succ/prev/children/parent/flow_to/flow_from; JSON persistence (`save_json`, `load_json`, `storage_from_json`). Concrete types: `CpgGraph`, `CpgNode`, `CpgEdge`.
62
+
63
+ ---
64
+
65
+ ## Installation
66
+
67
+ ```bash
68
+ pip install cpg2py
69
+ ```
70
+
71
+ From source (e.g. with [uv](https://github.com/astral-sh/uv)):
72
+
73
+ ```bash
74
+ git clone https://github.com/samhsu-dev/cpg2py.git && cd cpg2py
75
+ uv sync --dev
76
+ uv run pytest tests/
77
+ ```
78
+
79
+ ---
80
+
81
+ ## Input format
82
+
83
+ - **nodes.csv**: tab-delimited; must include node id (e.g. `id:int` or `id`). Other columns become node properties.
84
+ - **rels.csv**: tab-delimited; columns `start`, `end`, `type` (or `start:str`, `end:str`, `type:str`).
85
+
86
+ ---
87
+
88
+ ## Usage
89
+
90
+ **Load from CSV**
91
+
92
+ ```python
93
+ from pathlib import Path
94
+ from cpg2py import cpg_graph, CpgGraph, CpgNode, CpgEdge
95
+
96
+ graph: CpgGraph = cpg_graph(Path("nodes.csv"), Path("rels.csv"))
97
+ ```
98
+
99
+ **Nodes and edges** (edge identified by `(from_id, to_id, edge_type)`; `edge_type` is string)
100
+
101
+ ```python
102
+ node: CpgNode = graph.node("2")
103
+ node.name
104
+ node.set_property("name", "x")
105
+ node.set_properties({"k": "v"})
106
+
107
+ edge: CpgEdge = graph.edge("2", "3", "ENTRY")
108
+ edge.from_nid, edge.to_nid, edge.type
109
+ edge.set_property("weight", 0.5)
110
+ ```
111
+
112
+ **Traversal**
113
+
114
+ ```python
115
+ graph.succ(node) # successors
116
+ graph.prev(node) # predecessors
117
+ graph.children(node)
118
+ graph.parent(node)
119
+ graph.flow_to(node)
120
+ graph.flow_from(node)
121
+ graph.topfile_node("5") # top-level file node for given node ID
122
+ ```
123
+
124
+ **Filtered iteration** (optional predicate)
125
+
126
+ ```python
127
+ graph.nodes(lambda n: n.type == "Function")
128
+ graph.edges(lambda e: e.edge_type == "FLOWS_TO")
129
+ graph.succ(node, who_satisifies=lambda e: e.edge_type == "PARENT_OF")
130
+ graph.descendants(node, condition=...)
131
+ graph.ancestors(node, condition=...)
132
+ ```
133
+
134
+ **JSON persistence**
135
+
136
+ ```python
137
+ graph.storage.save_json("graph.json")
138
+
139
+ storage = Storage()
140
+ storage.load_json("graph.json")
141
+ graph2 = CpgGraph(storage)
142
+
143
+ # or
144
+ storage = storage_from_json(Path("graph.json"))
145
+ ```
146
+
147
+ JSON schema: `{"nodes": { "<id>": { "<key>": <value>, ... }, ... }, "edges": [ {"from": str, "to": str, "type": str, "props": {...} }, ... ]}`. See [design.md](docs/design.md).
148
+
149
+ ---
150
+
151
+ ## Extending (ABCs)
152
+
153
+ Implement `AbcGraphQuerier[MyNode, MyEdge]`, `AbcNodeQuerier`, `AbcEdgeQuerier`; inject `Storage`. Full interface and contracts: [docs/design.md](docs/design.md).
154
+
155
+ Minimal custom graph:
156
+
157
+ ```python
158
+ from cpg2py import AbcGraphQuerier, AbcNodeQuerier, AbcEdgeQuerier, Storage
159
+ from typing import Optional
160
+
161
+ class MyNode(AbcNodeQuerier): pass
162
+ class MyEdge(AbcEdgeQuerier): pass
163
+
164
+ class MyGraph(AbcGraphQuerier[MyNode, MyEdge]):
165
+ def node(self, whose_id_is: str) -> Optional[MyNode]:
166
+ return MyNode(self.storage, whose_id_is)
167
+ def edge(self, fid: str, tid: str, eid: str) -> Optional[MyEdge]:
168
+ return MyEdge(self.storage, fid, tid, eid)
169
+
170
+ g = MyGraph(Storage())
171
+ ```
172
+
173
+ ---
174
+
175
+ Interface specifications (classes, methods, signatures, validation): [docs/design.md](docs/design.md).
176
+
177
+ ---
178
+
179
+ ## License
180
+ MIT.
@@ -0,0 +1,17 @@
1
+ cpg2py/__init__.py,sha256=j_4bRx0t-P10QnOXahQ89avNJne_1mVio0Gd62JIRLs,3548
2
+ cpg2py/_exceptions.py,sha256=OlnptpATmuzRQS0HoC24MoDPcPrY01uGMhVPJdK7Zuo,1376
3
+ cpg2py/_logger.py,sha256=HjJAhs4ZAB_IMoBMVRe3KPGS5GRygGy0eNncvIqu8ls,1309
4
+ cpg2py/_abc/__init__.py,sha256=XN7RKxMpnZqvjDAUWxXXLGKY1jrPpKBAxUbq-pydwh4,208
5
+ cpg2py/_abc/edge.py,sha256=Co3TXbfR2kd7G5oXFVEO0YjDND0zzFecf_R6yRHcyzw,2956
6
+ cpg2py/_abc/graph.py,sha256=LDF2ylbJYQubUV2LboJlkiQaqWtLW3LsyKBkTJOl4Gs,7795
7
+ cpg2py/_abc/node.py,sha256=ZnMf0TTi2Pv69AhzmomI8a8z8_05WgzVpK9p8VisTr8,2275
8
+ cpg2py/_abc/storage.py,sha256=E8lB6uo4zMCvgFmg6LJ5x00AoAFT-gha8nY3FxcJAQE,9252
9
+ cpg2py/_cpg/__init__.py,sha256=BYNrxYo0G3XoIaUN9llEYkZoCstYJgt88OrJZCkQW7E,126
10
+ cpg2py/_cpg/edge.py,sha256=bqzv9dmAVMFSyDmyCLhE0SKIWZZ9gu9UWLC3j6dfhAQ,886
11
+ cpg2py/_cpg/graph.py,sha256=Aks2VsDolKdT1NdEz9Vn5ABIWBTPDGvEq7PbrehLVuI,5674
12
+ cpg2py/_cpg/node.py,sha256=wSBCTCvA8UWQZG9CJn0QyBlQPt9PiXlPd0B2Ddlr7BI,1945
13
+ cpg2py-1.2.0.dist-info/licenses/LICENSE,sha256=ZAjdPt8K7uO5xDb4RyEuUTNZtFGx9_sfSQCy0OP64cU,1067
14
+ cpg2py-1.2.0.dist-info/METADATA,sha256=a-sNUByG1k8a0xX_HFQKRmINP8LDcJ9FMuz5RcqjzNU,5915
15
+ cpg2py-1.2.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
16
+ cpg2py-1.2.0.dist-info/top_level.txt,sha256=xDY8faKh5Rczvsqb5Jt9Sq-Y7EOImh7jh-m1oVTnH5k,7
17
+ cpg2py-1.2.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.8.0)
2
+ Generator: setuptools (80.10.2)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 Yichao Xu
3
+ Copyright (c) 2025 samhsu-dev
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
cpg2py/abc/edge.py DELETED
@@ -1,39 +0,0 @@
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 DELETED
@@ -1,98 +0,0 @@
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 DELETED
@@ -1,26 +0,0 @@
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 DELETED
@@ -1,153 +0,0 @@
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 DELETED
@@ -1,3 +0,0 @@
1
- from .graph import _Graph
2
-
3
- __all__ = ['_Graph']
cpg2py/cpg/edge.py DELETED
@@ -1,31 +0,0 @@
1
- from __future__ import annotations
2
- from typing import Optional, Tuple
3
- from ..abc import AbcGraphQuerier, AbcEdgeQuerier
4
-
5
- class _Edge(AbcEdgeQuerier):
6
-
7
- def __init__(self, graph: AbcGraphQuerier, f_nid: str, t_nid: str, e_type: str) -> None:
8
- super().__init__(graph, f_nid, t_nid, e_type)
9
- return None
10
-
11
- @property
12
- def id(self) -> Tuple[str, str, str]:
13
- return self.edge_id
14
-
15
- @property
16
- def start(self) -> Optional[int]:
17
- start_str = str(self.get_property('start', 'start:START_ID'))
18
- return int(start_str) if start_str.isnumeric() else int(self.__from_id)
19
-
20
- @property
21
- def end(self) -> Optional[int]:
22
- end_str = str(self.get_property('end', 'end:END_ID'))
23
- return int(end_str) if end_str.isnumeric() else int(self.__to_id)
24
-
25
- @property
26
- def type(self) -> Optional[str]:
27
- return self.get_property('type', 'type:TYPE')
28
-
29
- @property
30
- def var(self) -> Optional[str]:
31
- return self.get_property('var')
cpg2py/cpg/graph.py DELETED
@@ -1,97 +0,0 @@
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