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
+
0 commit comments