-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-validation-solver.py
executable file
·128 lines (113 loc) · 3.71 KB
/
new-validation-solver.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
#!/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 shutil
if config.remotecopy:
import paramiko
def read_back_and_confirm(sftp, remotePath, checksum):
try:
hasher = hashlib.sha256()
with sftp.file(remotePath, 'rb') as remoteFile:
for chunk in iter(lambda: remoteFile.read(4096), b""):
hasher.update(chunk)
if checksum != hasher.hexdigest():
return False
return True
except:
return False
## Entry point
if len(sys.argv) < 3:
print("Usage: " + sys.argv[0] + " solver-name /path/to/binary [arguments...]")
sys.exit(1)
solvername = sys.argv[1]
solverpath = sys.argv[2]
if len(sys.argv) > 3:
solverargs = " ".join(sys.argv[3:])
else:
solverargs = ""
engine = dbobj.mk_engine()
Session = sessionmaker(bind=engine)
session = Session()
# check if the named validation solver is already registered
existingVersion = session.query(dbobj.ValidationSolver).filter(dbobj.ValidationSolver.name == solvername).one_or_none()
if existingVersion is not None:
print("Validation solver {} already exists.".format(solvername))
session.rollback()
sys.exit(0)
checksum = ""
try:
with open(solverpath, 'rb') as f:
hasher = hashlib.sha256()
for chunk in iter(lambda: f.read(4096), b""):
hasher.update(chunk)
checksum = hasher.hexdigest()
except Exception as e:
print("An exception occurred while reading the solver from %s:" % (solverpath,))
print(e)
session.rollback()
sys.exit(1)
print("checksum %s" % (checksum,))
# path is a relative path
vSolverEntry = dbobj.ValidationSolver(name=solvername, path=solvername, checksum=checksum, command_line=solverargs)
remoteFilename = os.path.join(config.validationsolverbase, solvername)
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 Exception as e:
print("An exception occurred while connecting:")
print(e)
session.rollback()
sys.exit(1)
sftp = ssh.open_sftp()
# create validation solver directory if it doesn't exist
try:
sftp.stat(config.validationsolverbase)
except FileNotFoundError:
sftp.mkdir(config.validationsolverbase)
# copy validation solver and verify
try:
sftp.put(solverpath, remoteFilename)
if not read_back_and_confirm(sftp, remoteFilename, checksum):
print("Failed to verify solver! Removing file and aborting.")
try:
#sftp.remove(remoteBaseDir + "/" + remoteSolverPath)
pass
except:
pass
sftp.close()
ssh.close()
session.rollback()
sys.exit(1)
except Exception as e:
print("Could not copy solver to remote server:")
print(e)
sftp.close()
ssh.close()
session.rollback()
sys.exit(1)
sftp.close()
ssh.close()
else: # config.remotecopy = False
print("Copying solver to {}".format(remoteFilename))
try:
os.makedirs(config.validationsolverbase, exist_ok=True)
shutil.copyfile(solverpath, remoteFilename)
mode = os.stat(remoteFilename).st_mode
mode |= (mode & 0o444) >> 2
os.chmod(remoteFilename, mode)
except Exception as e:
print("Failed to copy solver:")
print(e)
session.rollback()
sys.exit(1)
session.add(vSolverEntry)
session.commit()
print("Created solver {}.".format(solvername))