Skip to content

Commit 6e50f98

Browse files
committed
add scripts for apisan
1 parent 017de78 commit 6e50f98

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed

apisan/auto_apisan.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import os
2+
import sys
3+
import subprocess
4+
import json
5+
import time
6+
7+
# return a dict list. dict: {name: file_name, path: full_path}
8+
def get_all_file_path(in_dir):
9+
res = []
10+
out_list = list()
11+
for path in os.listdir(in_dir):
12+
# print(in_dir)
13+
# check if current path is a file
14+
orig_path = os.path.join(in_dir, path)
15+
# print(orig_path)
16+
# if not os.path.isfile(orig_path):
17+
# print('in')
18+
out_dict = dict()
19+
out_dict['name'] = path
20+
out_dict['path'] = orig_path
21+
res.append(out_dict)
22+
return res
23+
24+
def read_json(in_path):
25+
# in_list = list()
26+
out_list = list()
27+
with open(in_path, 'r') as f:
28+
tmp_list = f.readlines()
29+
for line in tmp_list:
30+
line = line.strip('\n')
31+
line_json = json.loads(line)
32+
out_list.append(line_json)
33+
return out_list
34+
35+
def get_repo_name(link):
36+
link = link.replace('.git', '')
37+
name = link.strip('/').split('/')[-1]
38+
return name
39+
40+
if __name__ == '__main__':
41+
if len(sys.argv) != 3:
42+
print("wrong input")
43+
print('Usage: python3 ./auto_apisan.py <in_software_dir> <out_dir>')
44+
exit(1)
45+
46+
# TODO check if more?
47+
apisan_cmd = ['apisan check --checker=rvchk', 'apisan check --checker=cpair', 'apisan check --checker=args', 'apisan check --checker=intovfl', 'apisan check --checker=cond', 'apisan check --checker=fsb']
48+
49+
in_dir = sys.argv[1]
50+
out_dir = sys.argv[2]
51+
out_log = out_dir + '/apisan-log'
52+
faild_log = out_dir + '/apisan-faild-log'
53+
success_log = out_dir + '/apisan-success-log'
54+
software_path = in_dir + '/apisan-in'
55+
software_list = list()
56+
57+
if not os.path.exists(out_dir):
58+
os.mkdir(out_dir)
59+
60+
with open(software_path, 'r') as f:
61+
tmp = f.read().strip('\n')
62+
software_list = tmp.split('\n')
63+
for software in software_list:
64+
out_dict = dict()
65+
out_dict['repo'] = software
66+
out_dict['status'] = 'begin'
67+
out_dict['faild_stage'] = ''
68+
software_dir = in_dir + '/' + software
69+
out_software_dir = out_dir + '/' + software
70+
if not os.path.exists(out_software_dir):
71+
os.mkdir(out_software_dir)
72+
73+
os.chdir(software_dir)
74+
with open(out_log, 'a') as f:
75+
f.write('Parse' + software + '\n')
76+
for cmd in apisan_cmd:
77+
start = time.time()
78+
check_name = cmd.strip(' ').split('=')[-1]
79+
out_path = out_software_dir + '/' + check_name
80+
all_cmd = cmd + ' > ' + out_path
81+
out_dict['status'] = all_cmd
82+
with open(out_log, 'a') as f:
83+
f.write('Exec' + all_cmd + '\n')
84+
return_info = subprocess.Popen(all_cmd, shell=True, stderr=subprocess.PIPE)
85+
try:
86+
out, err = return_info.communicate(timeout=18000)
87+
except:
88+
89+
with open(faild_log, 'a') as f:
90+
out_dict['faild_stage'] = 'timeout'
91+
f.write(json.dumps(out_dict))
92+
f.write('\n')
93+
with open(out_log, 'a') as f:
94+
f.write('Cost too much time. Break!\n\n')
95+
# os.remove(ql_path)
96+
continue
97+
end = time.time()
98+
info = err.decode("utf-8","ignore")
99+
if return_info.returncode != 0:
100+
with open(faild_log, 'a') as f:
101+
out_dict['faild_stage'] = 'run wrong'
102+
f.write(json.dumps(out_dict))
103+
f.write('\n')
104+
with open(out_log, 'a') as f:
105+
f.write('Error: ' + info + '\n\n')
106+
continue
107+
108+
else:
109+
out_dict['status'] = 'finish'
110+
with open(success_log, 'a') as f:
111+
f.write(json.dumps(out_dict))
112+
f.write('\n')
113+
with open(out_log, 'a') as f:
114+
f.write('Success! Time: ' + str(end-start) + 's\n\n')
115+
116+
117+

auto_clone.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import os
2+
import sys
3+
import json
4+
5+
def read_json(in_path):
6+
# in_list = list()
7+
out_list = list()
8+
with open(in_path, 'r') as f:
9+
tmp_list = f.readlines()
10+
for line in tmp_list:
11+
line = line.strip('\n')
12+
line_json = json.loads(line)
13+
out_list.append(line_json)
14+
return out_list
15+
16+
def get_repo_name(link):
17+
link = link.replace('.git', '')
18+
name = link.strip('/').split('/')[-1]
19+
return name
20+
21+
if __name__ == '__main__':
22+
if len(sys.argv) != 3:
23+
print("wrong input")
24+
print('Usage: python3 ./auto_clone.py <https_link_path> <out_dir>')
25+
exit(1)
26+
27+
in_path = sys.argv[1]
28+
out_dir = sys.argv[2]
29+
out_log = out_dir + '/clone-log'
30+
faild_log = out_dir + '/clong-faild-log'
31+
32+
in_list = read_json(in_path)
33+
os.chdir(out_dir)
34+
for item in in_list:
35+
os.chdir(out_dir)
36+
repo_link = item['github link']
37+
commit_id = item['commit id']
38+
repo_name = get_repo_name(repo_link)
39+
cmd = 'git clone --recursive ' + repo_link
40+
with open(out_log, 'a') as f:
41+
f.write('Exec: ' + cmd + '\n')
42+
re = os.system(cmd)
43+
if not re:
44+
with open(out_log, 'a') as f:
45+
f.write('Success\n')
46+
else:
47+
item['faild_stage'] = 'clone'
48+
with open(faild_log, 'a') as f:
49+
f.write(item + '\n')
50+
51+
os.chdir(out_dir + '/' + repo_name)
52+
cmd = 'git checkout ' + str(commit_id)
53+
with open(out_log, 'a') as f:
54+
f.write('Exec: ' + cmd + '\n')
55+
re = os.system(cmd)
56+
if not re:
57+
with open(out_log, 'a') as f:
58+
f.write('Success\n')
59+
else:
60+
item['faild_stage'] = 'checkout'
61+
with open(faild_log, 'a') as f:
62+
f.write(item + '\n')
63+
64+

parse_csv.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import csv
2+
import sys
3+
import json
4+
# from openpyxl import Workbook
5+
# from openpyxl import load_workbook
6+
# import openpyxl
7+
8+
def get_data_excel(in_path):
9+
wb= openpyxl.load_workbook(in_path)
10+
# 第二步选取表单
11+
sheet = wb.active
12+
# 按行获取数据转换成列表
13+
rows_data = list(sheet.rows)
14+
# 获取表单的表头信息(第一行),也就是列表的第一个元素
15+
titles = [title.value for title in rows_data.pop(0)]
16+
# print(titles)
17+
18+
# 整个表格最终转换出来的字典数据列表
19+
all_row_dict = []
20+
# 遍历出除了第一行的其他行
21+
for a_row in rows_data:
22+
the_row_data = [cell.value for cell in a_row]
23+
# 将表头和该条数据内容,打包成一个字典
24+
row_dict = dict(zip(titles, the_row_data))
25+
# print(row_dict)
26+
all_row_dict.append(row_dict)
27+
return all_row_dict
28+
29+
def get_data_csv(in_path):
30+
out_list = list()
31+
with open(in_path, 'r') as file_csv:
32+
# fieldnames = ("field1","field2")
33+
reader = csv.DictReader(file_csv)
34+
for info in reader:
35+
# print(info)
36+
out_list.append(info)
37+
38+
return out_list
39+
40+
if __name__ == '__main__':
41+
if len(sys.argv) != 2:
42+
print('python3 ./parse_csv.py in_path')
43+
exit(1)
44+
in_path = sys.argv[1]
45+
out_path = in_path.split('.')[0]
46+
# in_path = '/home/jhliu/data/GT-no-keyword.csv'
47+
# out_path = '/home/jhliu//data/GT-no-keyword'
48+
json_list = get_data_csv(in_path)
49+
# print(json_list)
50+
for info in json_list:
51+
# print(info)
52+
with open(out_path, 'a') as f:
53+
f.write(json.dumps(info))
54+
f.write('\n')

0 commit comments

Comments
 (0)