-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.py
271 lines (205 loc) · 10.4 KB
/
analyzer.py
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
import pandas as pd
import os,ast,subprocess,argparse
import logging as lg
from math import pow
from tqdm import tqdm
lg.basicConfig(format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S')
def extract_dataframe(answer_path, ground_truth="", FROM_ESCAPE=False) -> pd.DataFrame:
if(FROM_ESCAPE == True):
with open(answer_path, 'r') as f:
list_answer = [elem.split("\t")[1].strip() for elem in f.readlines()]
f.close()
lg.debug("Extracted answers: " + str(len(list_answer)))
else:
with open(answer_path, 'r') as f:
list_answer = [elem.strip() for elem in f.readlines()]
f.close()
lg.debug("Extracted answers: " + str(len(list_answer)))
# with open(des_path, 'r') as f:
# #list_answer = ast.literal_eval(f.read())
# list_des = [elem.strip() for elem in f.readlines()]
# f.close()
# lg.debug("Extracted answers: " + str(len(list_des)))
if(ground_truth != ""):
if(FROM_ESCAPE == True):
with open(ground_truth, 'r') as f:
list_truth = [elem.split("\t")[1].strip() for elem in f.readlines()]
f.close()
lg.debug("Extracted truth: "+ str(len(list_truth)))
else:
with open(ground_truth, 'r') as f:
list_truth = [elem.strip() for elem in f.readlines()]
f.close()
lg.debug("Extracted truth: "+ str(len(list_truth)))
#df = pd.DataFrame(data={"Description": list_des,"Answer" : list_answer, 'Ground Truth': list_truth})
df = pd.DataFrame(data={"Answer" : list_answer, 'Ground Truth': list_truth})
lg.debug("Created dataframe: ")
return df
else:
#df = pd.DataFrame(data={"Description": list_des,"Answer" : list_answer})
df = pd.DataFrame(data={"Answer" : list_answer})
lg.debug("Created dataframe: ")
return df
def parse_output(answer) -> []:
lg.debug(answer)
current_dir = os.getcwd()
with open("buffer.ps1", 'w') as f:
f.write(answer)
f.close()
#pwsh o powershell a piacer del tuo WINDOWS
result = subprocess.run(f'pwsh {os.path.join(current_dir,"parser.ps1")} {os.path.join(current_dir,"buffer.ps1")}', stdout=subprocess.PIPE, text=True)
result = result.stdout.strip().split("--")
result = [elem.strip("|").strip().strip("|") for elem in result]
try:
result = [elem.split("|") for elem in result]
except:
pass
lg.debug(result)
return result
def add_results_compare(df, df_partial,FILE_CSV) -> pd.DataFrame:
l = df.shape[0]
N = df_partial.shape[0]
for i,row in tqdm(df.iterrows(),total=l, colour='blue'):
if(i>=N):
answer,truth = row['Answer'], row['Ground Truth']
answer_out = parse_output(answer)
truth_out = parse_output(truth)
if(answer_out == ['']):
answer_out = ['','','']
if(truth_out == ['']):
truth_out = ['','','']
lg.debug(f"It: {i+1}/{l} Len_ans_out: {len(answer_out)} Len_truth_out: {len(truth_out)}")
try:
df_partial.loc[len(df_partial.index)] = answer_out+truth_out
finally:
df_partial.to_csv(FILE_CSV, index=False)
df_out = pd.concat([df,df_partial], axis = 1)
if(os.path.exists("buffer.ps1")):
os.remove("buffer.ps1")
return df_out
def add_results_single(df,df_partial,FILE_CSV) -> pd.DataFrame:
l = df.shape[0]
N = df_partial.shape[0]
for i,row in tqdm(df.iterrows(), total=l, colour='blue'):
if(i>=N):
answer = row['Answer']
answer_out = parse_output(answer)
if(answer_out == ['']):
answer_out = ['','','']
lg.debug(f"It: {i+1}/{l} Len_ans_out: {len(answer_out)}")
try:
df_partial.loc[len(df_partial.index)] = answer_out
finally:
df_partial.to_csv(FILE_CSV, index=False)
df_out = pd.concat([df,df_partial], axis = 1)
if(os.path.exists("buffer.ps1")):
os.remove("buffer.ps1")
return df_out
def str2list(string):
if(string == [''] or string == '' or string == ['']):
return []
else:
return string[1:-1].split(",")
def calculate_syntax_metric_single(df) -> float:
l = df.shape[0]
count = 0
skip_error_rule = ['RedirectionNotSupported', "MissingFileSpecification"] #["The '<' operator is reserved for future use. "]
for i,row in df.iterrows():
if(type(row['ANSWER Rulename']) == str):
list_rulename = [elem.replace("'","").replace(" ","") for elem in str2list(row['ANSWER Rulename'])]
list_severity = [elem.replace("'","").replace(" ","") for elem in str2list(row['ANSWER Severity'])]
else:
list_rulename = [elem.replace("'","").replace(" ","") for elem in row['ANSWER Rulename']]
list_severity = [elem.replace("'","").replace(" ","") for elem in row['ANSWER Severity']]
if(list_rulename == [] or list_rulename == ['']):
continue
list_a = list(zip(list_rulename,list_severity))
list_a_filtered = []
for elem_a in list_a:
if(elem_a[1] == "ParseError" and elem_a[0] not in skip_error_rule):
list_a_filtered.append(elem_a)
for j,elem in enumerate(list_a_filtered):
if(elem[1] == 'ParseError'):
count += 1
lg.info(f"Answer: {row} {i}")
break
lg.info(f"Count valid ParseErrors: {count}/{l}")
return round((1-count/l)*100,2)
def calculate_syntax_metric_double(df) -> float:
l = df.shape[0]
count= 0
skip_error_rule = ['RedirectionNotSupported', "MissingFileSpecification"] #["The '<' operator is reserved for future use. "]
for i,row in df.iterrows():
if(type(row['ANSWER Rulename']) == str):
list_rulename = [elem.replace("'","").replace(" ","") for elem in str2list(row['ANSWER Rulename'])]
list_severity = [elem.replace("'","").replace(" ","") for elem in str2list(row['ANSWER Severity'])]
list_rulename_t = [elem.replace("'","").replace(" ","") for elem in str2list(row['TRUTH Rulename'])]
list_severity_t = [elem.replace("'","").replace(" ","") for elem in str2list(row['TRUTH Severity'])]
else:
list_rulename = [elem.replace("'","").replace(" ","") for elem in row['ANSWER Rulename']]
list_severity = [elem.replace("'","").replace(" ","") for elem in row['ANSWER Severity']]
list_rulename_t = [elem.replace("'","").replace(" ","") for elem in row['TRUTH Rulename']]
list_severity_t = [elem.replace("'","").replace(" ","") for elem in row['TRUTH Severity']]
list_a = list(zip(list_rulename, list_severity))
list_b = list(zip(list_rulename_t, list_severity_t))
list_a_filtered = []
for elem_a in list_a:
if(elem_a[1] == "ParseError" and elem_a[0] not in skip_error_rule):
list_a_filtered.append(elem_a)
list_b_filtered = []
for elem_b in list_b:
if(elem_b[1] == "ParseError" and elem_b[0] not in skip_error_rule):
list_b_filtered.append(elem_b)
list_equals = list(set(list_a_filtered) & set(list_b_filtered))
for j,elem in enumerate(list_a_filtered):
if(elem[1] == 'ParseError' and elem not in list_equals):
count += 1
lg.info(f"Answer: {elem} {i}")
break
lg.info(f"Count valid ParseErrors: {count}/{l}")
return round((1-count/l)*100,2)
if __name__ == '__main__':
print("""
__ __ __ ___ __ ___ __
|__) | | /__` |__| __ /__` \ / |\ | | /\ \_/ __ /\ |\ | /\ | \ / / |__ |__)
| |/\| .__/ | | .__/ | | \| | /~~\ / \ /~~\ | \| /~~\ |___ | /_ |___ | \
""")
parser = argparse.ArgumentParser(description="Python NLP wrapper for powershell syntax analysis through PSScript Analyzer")
#parser.add_argument("DESCRIPTION_PATH", help="Description text file path from the model")
parser.add_argument("OUT_FILE", help="Output csv file", nargs='?',const="output.csv")
parser.add_argument("ANSWER_PATH", help="Answers text file path from the model")
parser.add_argument("GROUND_TRUTH", help="Ground truth text file path",nargs='?', default="")
parser.add_argument("FROM_ESCAPE", help="Output files from ESCAPE", type=bool, nargs="?", default=False)
parser.add_argument("-v", help="Verbose", nargs='?', type=int, const=1, default=0)
args = parser.parse_args()
#DESCRIPTION_PATH = args.DESCRIPTION_PATH
ANSWER_PATH = args.ANSWER_PATH
GROUND_TRUTH = args.GROUND_TRUTH
FROM_ESCAPE= args.FROM_ESCAPE
OUT_FILE = args.OUT_FILE
VERBOSE = args.v
if(VERBOSE):
lg.getLogger().setLevel(lg.DEBUG)
lg.debug(vars(args))
else:
lg.getLogger().setLevel(lg.INFO)
N = 0
if(GROUND_TRUTH != ""):
if((os.path.exists(OUT_FILE))):
df_partial = pd.read_csv(OUT_FILE)
else:
df_partial = pd.DataFrame(columns=["ANSWER Rulename",'ANSWER Message','ANSWER Severity',
"TRUTH Rulename",'TRUTH Message','TRUTH Severity'])
df = extract_dataframe(ANSWER_PATH,GROUND_TRUTH,FROM_ESCAPE)
df_out = add_results_compare(df, df_partial, OUT_FILE)
print("Syntax metric single: ",calculate_syntax_metric_single(df_out))
print("Syntax metric double: ",calculate_syntax_metric_double(df_out))
else:
if((os.path.exists(OUT_FILE))):
df_partial = pd.read_csv(OUT_FILE)
else:
df_partial = pd.DataFrame(columns=["ANSWER Rulename",'ANSWER Message','ANSWER Severity'])
df = extract_dataframe(ANSWER_PATH)
df_out = add_results_single(df,df_partial,OUT_FILE)
print("Syntax metric single: ",calculate_syntax_metric_single(df_out))
df_out.to_csv(OUT_FILE, index=False)