-
Notifications
You must be signed in to change notification settings - Fork 1
/
patchfiles_compiler.py
71 lines (58 loc) · 2.59 KB
/
patchfiles_compiler.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
import json
import shutil
import os
metadata = "result.json"
FINAL_PATCHES_LOC = "/home/scantist_jl/projects/nvd-patch-getter/final_patches"
FOLDERNAME = "final_patches"
def main():
local_dir_check_create(FOLDERNAME)
with open(metadata, 'r', encoding="utf-8") as file:
json_data = json.load(file)
for dict1 in json_data['result']:
if dict1["similarity_score"]:
#ignore html in new patch files
for file in dict1["new_patch_file"]:
with open(file, 'r') as readfile:
if "<!DOCTYPE html>" in readfile:
pass
for score in dict1["similarity_score"]:
if score > 0.99:
#old and new patch are extremely similar, take the old patch
copy_old_patch(dict1)
elif score < 0.99 and len(dict1["similarity_score"]) == 1:
#only 1 new patch is found, and there are some difference,
#take the new patch file
copy_new_patch(dict1)
elif score < 0.99 and len(dict1["similarity_score"]) > 1:
#more than 1 new patch is found regardless of the score
#submit the file with the highest match
max_score_idx = dict1["similarity_score"].index(max(dict1["similarity_score"]))
copy_one_new_patch(dict1, max_score_idx)
else:
# no patch is found, skip
pass
#copy_old_patch(dict1)
def copy_one_new_patch(dict1, max_score_idx):
filename = dict1["new_patch_file"][max_score_idx]
id_name = filename.split("/")[-1].split("_")[0]
patch_filename = FINAL_PATCHES_LOC + '/' + id_name + '.patch'
shutil.copy(dict1["new_patch_file"][max_score_idx], patch_filename)
def copy_old_patch(dict1):
shutil.copy(dict1["prev_patch_file"][0], FINAL_PATCHES_LOC)
def copy_new_patch(dict1):
old_filename = dict1["new_patch_file"][0]
id_name = old_filename.split("/")[-1].split("_")[0]
patch_filename = FINAL_PATCHES_LOC + '/' + id_name + '.patch'
shutil.copy(dict1["new_patch_file"][0], patch_filename)
def local_dir_check_create(FOLDERNAME):
# Check if current directory has folder named "patches"
# Create if it does not exist
curr_dir = os.getcwd()
target_dir = os.path.join(curr_dir, FOLDERNAME)
print(target_dir)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
else:
pass
if __name__ == "__main__":
main()