-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_ast_extractor.py
More file actions
290 lines (228 loc) · 9.36 KB
/
path_ast_extractor.py
File metadata and controls
290 lines (228 loc) · 9.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import random
import os
from typed_ast import ast27 as ast
from itertools import combinations
import hashlib
class RemoveLoadNode(ast.NodeTransformer):
'''
Removes all occurrences of the ast.Load node from the AST.
'''
def generic_visit(self, node):
"""
Called if no explicit visitor function exists for a node.
"""
for field, old_value in ast.iter_fields(node):
# For singular nodes
if isinstance(old_value, ast.AST):
new_node = self.visit(old_value)
if new_node is None:
setattr(node, field, None) # Set field to None if Load node is encountered and removed
elif new_node != old_value:
setattr(node, field, new_node)
# For lists of nodes
elif isinstance(old_value, list):
new_values = []
for value in old_value:
if isinstance(value, ast.AST):
value = self.visit(value)
if value is not None: # Add to the list only if it's not a Load node
new_values.append(value)
old_value[:] = new_values
return node
def visit_Load(self, node):
return None # Removes the Load node by returning None
# Determine parent for each node
def set_parents(node, parent=None):
for child in ast.iter_child_nodes(node):
child.parent = parent
set_parents(child, child)
def get_leaf_nodes(node):
if not list(ast.iter_child_nodes(node)):
return [node]
else:
leaves = []
for child in ast.iter_child_nodes(node):
leaves.extend(get_leaf_nodes(child))
return leaves
def get_path_upwards(node, target_ancestor):
path = []
while node.parent and node != target_ancestor:
node = node.parent
path.append(node)
return path
def find_common_ancestor(node1, node2):
ancestors_node1 = set()
current = node1
while hasattr(current, "parent"):
ancestors_node1.add(current)
current = current.parent
current = node2
while hasattr(current, "parent"):
if current in ancestors_node1:
return current
current = current.parent
return None
def get_code_snippet(node, code):
lines = code.splitlines()
# Handling for Compare nodes to get the entire comparison as a snippet
if isinstance(node, ast.Compare):
left_value = get_code_snippet(node.left, code)
op = get_code_snippet(node.ops[0], code) # For simplicity, just handle the first operator
comparator = get_code_snippet(node.comparators[0], code) # Similarly, handle the first comparator
return f"{left_value} {op} {comparator}"
if hasattr(node, 'lineno') and hasattr(node, 'col_offset') and hasattr(node, 'end_lineno') and hasattr(node, 'end_col_offset'):
# Extract code snippet based on lineno and col_offset attributes
start_line = lines[node.lineno - 1][node.col_offset:]
end_line = lines[node.end_lineno - 1][:node.end_col_offset]
middle_lines = lines[node.lineno:node.end_lineno - 1]
return ' '.join([start_line] + middle_lines + [end_line]).strip()
if isinstance(node, ast.Eq):
return "=="
# Mapping based on provided list
attribute_mapping = {
ast.Attribute: "value",
ast.Subscript: "value",
ast.List: "elts",
ast.Tuple: "elts",
ast.Slice: "lower",
ast.ExtSlice: "dims",
ast.Index: "value",
ast.ExceptHandler: "name",
ast.TypeIgnore: "lineno",
ast.Module: "body",
ast.Interactive: "body",
ast.Expression: "body",
ast.FunctionType: ["argtypes", "returns"],
ast.Suite: "body",
ast.FunctionDef: "name",
ast.ClassDef: "name",
ast.Return: "value",
ast.Delete: "targets",
ast.Assign: "targets",
ast.AugAssign: "target",
ast.Print: "dest",
ast.For: "target",
ast.While: "test",
ast.If: "test",
ast.With: "context_expr",
ast.Raise: "type",
ast.TryExcept: "body",
ast.TryFinally: "body",
ast.Assert: "test",
ast.Import: "names",
ast.ImportFrom: "module",
ast.Exec: "body",
ast.Global: "names",
ast.Expr: "value",
ast.BoolOp: "op",
ast.BinOp: "left",
ast.UnaryOp: "op",
ast.Lambda: "args",
ast.IfExp: "test",
ast.Dict: "keys",
ast.Set: "elts",
ast.ListComp: "elt",
ast.SetComp: "elt",
ast.DictComp: "key",
ast.GeneratorExp: "elt",
ast.Yield: "value",
ast.Compare: "left",
ast.Call: "func",
ast.Repr: "value",
ast.Num: "n",
ast.Str: "s",
ast.Name : "id"
}
attr_name = attribute_mapping.get(type(node))
if attr_name:
if isinstance(attr_name, list):
# If there are multiple attributes, combine their values (this is a basic assumption, might need adjustment)
return " ".join([str(getattr(node, name)) for name in attr_name if hasattr(node, name)])
elif hasattr(node, attr_name):
return str(getattr(node, attr_name))
return str(type(node).__name__) # Fallback to node type if we can't extract snippet
def extract_paths(module, leaf_nodes, code):
comb = list(combinations(leaf_nodes, 2))
all_paths = []
for node1, node2 in comb:
common_ancestor = find_common_ancestor(node1, node2)
path1UP = [str(type(node).__name__) for node in get_path_upwards(node1, common_ancestor)]
path2UP = [str(type(node).__name__) for node in get_path_upwards(node2, common_ancestor)]
# For the leaf nodes, we extract the actual code snippet
path1UP.insert(0, get_code_snippet(node1, code))
path2UP.insert(0, get_code_snippet(node2, code))
#print(path1UP[0])
#print(path2UP[0])
path2UP.reverse()
all_paths.append((path1UP, path2UP))
return all_paths
def concatenate_paths(paths):
concatenated = []
for path1, path2 in paths:
new_path = []
for node in path1[:-1]:
new_path.append(node)
new_path.append('->')
for node in path2:
new_path.append(node)
new_path.append('<-')
new_path = new_path[:-1]
concatenated.append(new_path)
return concatenated
def sanitize_string(s):
return s.replace(" ", "").replace(",", "").replace("\n", "").replace("\t", "")
def hash_paths(concatenated_paths):
method_ina_line = []
for path in concatenated_paths:
real_path_length = (len(path) - 2) // 2
if real_path_length < 9:
path_string = str(path[1:-1]).encode('utf-8')
path_hash = str(hashlib.sha256(path_string).hexdigest())
formatted_hash = f'b"{path_hash}"'
first_node = sanitize_string(str(path[0]))
last_node = sanitize_string(str(path[-1]))
path_final = f"{first_node},{formatted_hash},{last_node} "
method_ina_line.append(path_final)
return method_ina_line
def create_ast_dataset(origin, destination):
curr=os.getcwd()
origin= os.path.join(curr, origin)
destination_folder = os.path.join(curr, destination)
destination_file = os.path.join(destination_folder, "datasetgrezzo.txt")
os.makedirs(destination_folder, exist_ok=True)
l=0
with open(destination_file, 'w',encoding="utf-8") as dataset:
for root, dirs, files in os.walk(origin, topdown = False):
for name in files:
file_int=""
target_example=""
strexample=""
example=""
target=os.path.split(os.path.split(root)[1])[1]
print(target)
doc= os.path.join(root, name)
try: # Start of try block
with open(doc, 'rb') as f:
contents = f.read().decode("utf-8")
module = ast.parse(contents)
module = RemoveLoadNode().visit(module)
set_parents(module)
leaf_nodes = get_leaf_nodes(module)
paths = extract_paths(module, leaf_nodes, contents)
concatenated_paths = concatenate_paths(paths)
example = hash_paths(concatenated_paths)
example=set(example)
if len(example)!=0:
if len(example)>200: ###definisce il numero di path max per riga di esempio
l=l+1
example = random.sample(example, 200)
strexample = ''.join(example)
file_int = strexample + file_int
if len(file_int)>0:
target_example= target + " " + file_int +'\n'
dataset.write(target_example) #scrivere su txt
except Exception as e: # Handle the exception
print(f"An error occurred processing the file: {doc}")
print(f"Error: {e}")
print("dataset created")
print("numero example>200", l)