-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-benchmark.py
executable file
·220 lines (192 loc) · 7.21 KB
/
new-benchmark.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
#!/usr/bin/env python3
from smtrecords import dbobj, config
import sqlalchemy
from sqlalchemy.orm import sessionmaker
import sys
import os
import os.path
import hashlib
import re
if config.remotecopy:
import paramiko
import shutil
re_status = re.compile("set-info\s+:status\s+(\w+)")
def process_benchmark(path):
status = None
hasher = hashlib.sha256()
with open(path, 'rb') as casedata:
buf = casedata.read()
for line in buf.decode('UTF-8').split("\r\n"):
match = re_status.search(line)
if match:
if status is None:
status = match.group(1)
else:
print("warning: case %s has multiple (set-info :status) commands, setting status to unknown")
status = "unknown"
hasher.update(buf)
return (hasher.hexdigest(), status)
def ensure_subdirectories(remotebase, path, sftp):
if path == '':
return
check = remotebase + "/" + path
try:
sftp.stat(check)
except FileNotFoundError:
(head, tail) = os.path.split(path)
ensure_subdirectories(remotebase, head, sftp)
sftp.mkdir(check)
def read_back_and_confirm(sftp, remotecase, expectedhash):
try:
hasher = hashlib.sha256()
with sftp.file(remotecase, 'rb') as remotefile:
buf = remotefile.read()
hasher.update(buf)
if hasher.hexdigest() != expectedhash:
return False
# OK
return True
except:
return False
## Entry point
if len(sys.argv) != 3:
print("Usage: " + sys.argv[0] + " benchmark-name /path/to/benchmark/files")
sys.exit(1)
name = sys.argv[1]
path = sys.argv[2]
engine = dbobj.mk_engine()
Session = sessionmaker(bind=engine)
session = Session()
# check if the named benchmark already exists
if session.query(dbobj.Benchmark).filter(dbobj.Benchmark.name == name).count() > 0:
print("Benchmark '" + name + "' already exists.")
sys.exit(1)
remotepath = config.workbase + "/benchmarks/%s" % (name,)
benchmark = dbobj.Benchmark(name=name, path=remotepath)
if config.remotecopy:
print("Opening connection to %s for file transfer" % (config.remotehost,))
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(config.remotehost)
except paramiko.BadHostKeyException as e:
print("Bad host key:")
print(e)
session.rollback()
sys.exit(1)
except paramiko.AuthenticationException as e:
print("Authentication failure:")
print(e)
session.rollback()
sys.exit(1)
except paramiko.SSHException as e:
print("SSH connection error:")
print(e)
session.rollback()
sys.exit(1)
except Exception as e:
print("An exception occurred while connecting:")
print(e)
session.rollback()
sys.exit(1)
sftp = ssh.open_sftp()
# the remote path may already exist if we are resuming a previous attempt
try:
sftp.stat(remotepath)
except FileNotFoundError:
sftp.mkdir(remotepath)
print("Created %s" % (remotepath,))
print("Copying files", end='', flush=True)
# iterate over the given path, look for files ending in .smt2
ncases = 0
for root, dirs, files in os.walk(path):
for f in files:
if f.endswith(".smt2"):
fullPathToCase = os.path.join(root, f)
casename = os.path.relpath(fullPathToCase, path)
ncases += 1
(checksum, status) = process_benchmark(fullPathToCase)
case = dbobj.Case(path=casename, checksum=checksum, status=status)
benchmark.cases.append(case)
(head, tail) = os.path.split(casename)
ensure_subdirectories(remotepath, head, sftp)
remotecase = remotepath + "/" + casename
# see if the file already exists
exists = False
try:
sftp.stat(remotecase)
exists = True
if not read_back_and_confirm(sftp, remotecase, checksum):
print("Failed to verify %s! Aborting." % (remotecase,))
sftp.close()
ssh.close()
session.rollback()
sys.exit(1)
except FileNotFoundError:
exists = False
try:
if not exists:
sftp.put(fullPathToCase, remotecase)
if not read_back_and_confirm(sftp, remotecase, checksum):
print("Failed to verify %s! Aborting." % (remotecase,))
sftp.close()
ssh.close()
session.rollback()
sys.exit(1)
except Exception as e:
print("An exception occurred while copying %s:" % (casename,))
print(e)
sftp.close()
ssh.close()
session.rollback()
sys.exit(1)
print(".", end='', flush=True)
if ncases % 100 == 0:
print("(%d)" % (ncases,))
sftp.close()
ssh.close()
print("")
else: # config.remotecopy = False
try:
os.makedirs(remotepath, exist_ok = True)
# iterate over the given path, look for files ending in .smt2
ncases = 0
for root, dirs, files in os.walk(path):
for f in files:
if f.endswith(".smt2"):
fullPathToCase = os.path.join(root, f)
casename = os.path.relpath(fullPathToCase, path)
ncases += 1
(checksum, status) = process_benchmark(fullPathToCase)
case = dbobj.Case(path=casename, checksum=checksum, status=status)
benchmark.cases.append(case)
(head, tail) = os.path.split(casename)
os.makedirs(os.path.join(remotepath, head), exist_ok = True)
remotecase = os.path.join(remotepath, casename)
# see if the file already exists
exists = os.path.exists(remotecase)
try:
if not exists:
shutil.copy(fullPathToCase, remotecase)
except Exception as e:
print("An exception occurred while copying %s:" % (casename,))
print(e)
session.rollback()
sys.exit(1)
print(".", end='', flush=True)
if ncases % 100 == 0:
print("(%d)" % (ncases,))
print("")
except Exception as e:
print("Failed to create benchmark:")
print(e)
session.rollback()
sys.exit(1)
if ncases == 0:
print("No SMT2 files found in benchmark path. Exiting.")
print("The database has not been modified.")
session.rollback()
sys.exit(1)
session.add(benchmark)
session.commit()
print("Benchmark %s created with %d cases." % (name, ncases))