forked from aas-integration/integration-test2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
186 lines (154 loc) · 5.24 KB
/
common.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
import os, subprocess, traceback, sys, json
from threading import Timer
from contextlib import contextmanager
WORKING_DIR = os.path.dirname(os.path.realpath(__file__))
LIBS_DIR = os.path.join(WORKING_DIR, "libs")
CORPUS_DIR = os.path.join(WORKING_DIR, "corpus")
CORPUS_INFO = None
TOOLS_DIR = os.path.join(WORKING_DIR, "tools")
DLJC_BINARY = os.path.join(TOOLS_DIR, "do-like-javac", "dljc")
DLJC_OUTPUT_DIR = "dljc-out"
SIMPROG_DIR = os.path.join(WORKING_DIR, "simprog")
def run_cmd(cmd, print_output=False, timeout=None):
def kill_proc(proc, stats):
stats['timed_out'] = True
proc.kill()
stats = {'timed_out': False,
'output': ''}
timer = None
if print_output:
print ("Running %s" % ' '.join(cmd))
try:
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if timeout:
timer = Timer(timeout, kill_proc, [process, stats])
timer.start()
for line in iter(process.stdout.readline, b''):
stats['output'] = stats['output'] + line
if print_output:
sys.stdout.write(line)
sys.stdout.flush()
process.stdout.close()
process.wait()
stats['return_code'] = process.returncode
if timer:
timer.cancel()
except:
print ('calling {cmd} failed\n{trace}'.format(cmd=' '.join(cmd),trace=traceback.format_exc()))
return stats
@contextmanager
def cd(newdir):
prevdir = os.getcwd()
os.chdir(os.path.expanduser(newdir))
try:
yield
finally:
os.chdir(prevdir)
def mkdir(newdir):
if not os.path.isdir(newdir):
os.makedirs(newdir)
def get_method_from_daikon_out(daikon_out):
arr1 = daikon_out.split('.')
arr2 = arr1[1].split(':::')
method = arr2[0]
return method
def find_dot_name(method_name, method_file):
with open(method_file, "r") as fi:
for line in fi:
line = line.rstrip()
arr = line.split('\t')
method_sig = arr[0]
dot_name = arr[1]
if method_name in method_sig:
return dot_name
return None
def get_dot_path(project_name, dot_name):
return os.path.join(get_project_dir(project_name), DLJC_OUTPUT_DIR, '_target_classes', dot_name)
def get_jar(jar_name):
path = os.path.join(LIBS_DIR, jar_name)
if os.path.isfile(path):
return path
else:
return None
def get_method_summary_from_dot_path(dot_path):
arr = dot_path.split(os.sep)
dot_name = arr[-1]
dot_dir = arr[:-1]
proj_dir = arr[:-3]
method_arr = dot_dir + ["methods.txt"]
method_file = os.path.join("/", *method_arr)
sourceline_arr = dot_dir + ["sourcelines.txt"]
sourceline_file = os.path.join("/", *sourceline_arr)
mf = open(method_file, "r")
sf = open(sourceline_file, "r")
dot_to_method_dict = {}
method_to_source_dict = {}
for line in mf:
line = line.rstrip()
arr = line.split('\t')
method_sig = arr[0]
dot = arr[1]
dot_to_method_dict[dot] = method_sig
mf.close()
for line in sf:
line = line.rstrip()
arr = line.split('\t')
method_sig = arr[0]
source_file_name = arr[1]
method_to_source_dict[method_sig] = source_file_name
sf.close()
method_sig = dot_to_method_dict[dot_name]
source_file = method_to_source_dict[method_sig]
source_path = os.path.join("/", *(proj_dir + ["src/main", source_file]))
new_method_sig = method_sig[1:-1]
sig_arr = new_method_sig.split(' ')
return source_path+"::"+sig_arr[2]+"::"+sig_arr[1]
def get_corpus_info():
global CORPUS_INFO
if not CORPUS_INFO:
with open(os.path.join(WORKING_DIR, 'corpus.json')) as f:
CORPUS_INFO = json.loads(f.read())
return CORPUS_INFO
def get_project_dir(project_name):
project = project_info(project_name)
if 'build-dir' in project:
return os.path.join(CORPUS_DIR, project['name'], project['build-dir'])
else:
return os.path.join(CORPUS_DIR, project['name'])
def get_kernel_path(project_name):
return os.path.join(get_project_dir(project_name), DLJC_OUTPUT_DIR, '_target_classes', 'kernel.txt')
def get_method_path(project_name):
return os.path.join(get_project_dir(project_name), DLJC_OUTPUT_DIR, '_target_classes', 'methods.txt')
def get_project_list():
return get_corpus_info()['projects'].keys()
def project_info(project_name):
return get_corpus_info()['projects'][project_name]
def get_simprog(py_file):
return os.path.join(SIMPROG_DIR, py_file)
def clean_project(project):
project_dir = get_project_dir(project)
with cd(project_dir):
clean_command = project_info(project)['clean'].strip().split()
run_cmd(clean_command)
def run_dljc(project, tools, options=[], timelimit=1800.0):
project_dir = get_project_dir(project)
with cd(project_dir):
build_command = project_info(project)['build'].strip().split()
dljc_command = [DLJC_BINARY,
'-o', DLJC_OUTPUT_DIR,
'-t', ','.join(tools)]
dljc_command.extend(options)
dljc_command.append('--')
dljc_command.extend(build_command)
run_cmd(dljc_command, print_output=True, timeout=timelimit)
CHECKER_ENV_SETUP = False
def setup_checker_framework_env():
global CHECKER_ENV_SETUP
if CHECKER_ENV_SETUP:
return
jsr308 = TOOLS_DIR
os.environ['JSR308'] = jsr308
afu = os.path.join(jsr308, 'annotation-tools', 'annotation-file-utilities')
os.environ['AFU'] = afu
os.environ['PATH'] += ':' + os.path.join(afu, 'scripts')
CHECKER_ENV_SETUP = True