-
Notifications
You must be signed in to change notification settings - Fork 0
/
nusmv-brute-force.py
144 lines (113 loc) · 4.45 KB
/
nusmv-brute-force.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
import datetime
import os
import shutil
import time
from subprocess import Popen
from util.smvFileParser import extractChecks
from util.argumentParser import parseArguments
from util.configFileParser import parseConfig
import re
def setupWorkDir(outPath: str):
if outPath is None:
shutil.rmtree('temp', ignore_errors=True)
os.mkdir('temp')
return 'temp'
return outPath
def checkForFailedChecks(subDir: str = 'temp/'):
outFileRegex = re.compile('out[0-9]+\\.txt')
allOutputFiles = [f for f in os.listdir(subDir) if outFileRegex.match(f)]
failedChecks = []
passedChecks = []
for outputFile in allOutputFiles:
with open(subDir + outputFile, 'r') as openOutputFile:
fileContent = openOutputFile.read()
if 'as demonstrated by the following execution sequence' in fileContent:
failedChecks.append(outputFile)
else:
passedChecks.append(outputFile)
passedChecksNumbers = []
for passedCheck in passedChecks:
checkNumber = re.search('out([0-9]+).txt', passedCheck)
passedChecksNumbers.append(checkNumber.group(1))
print('The following checks passed: ' + ','.join(passedChecksNumbers))
failedChecksNumbers = []
for failedCheck in failedChecks:
checkNumber = re.search('out([0-9]+).txt', failedCheck)
failedChecksNumbers.append(checkNumber.group(1))
os.rename(subDir + failedCheck, subDir + failedCheck + '.failed')
print('The following checks failed: ' + ','.join(failedChecksNumbers))
smvPath, nusmvPath, outPath, configFile = parseArguments()
if smvPath is None or nusmvPath is None:
# require config
configSmvPath: str
configNusmvPath: str
if configFile is not None:
configSmvPath, configNusmvPath = parseConfig(configFile)
else:
configSmvPath, configNusmvPath = parseConfig()
if smvPath is None:
smvPath = configSmvPath
if nusmvPath is None:
nusmvPath = configNusmvPath
workDirectory = setupWorkDir(outPath)
with open(smvPath, 'r') as smvFile:
smvFileContent = smvFile.read()
smvWithoutChecks, smvChecks = extractChecks(smvFileContent)
with open(os.path.join(workDirectory, 'smvWithoutChecks.smv'), 'w') as smvWithoutChecksFile:
smvWithoutChecksFile.writelines(smvWithoutChecks)
for index, check in enumerate(smvChecks):
print("Id %i: %s" % (index, check))
selectedChecksString = input('Select checks to run, e.g.: (1,3,7) or leave empty to run all: ').strip()
if selectedChecksString is not "":
selectedChecksList = list(map(lambda check: int(check.strip()), selectedChecksString.split(",")))
assert len(selectedChecksList) > 0
smvChecks = [smvChecks[i] for i in selectedChecksList]
fileNames = []
for index, check in enumerate(smvChecks):
print("Id %i: %s" % (index, check))
fileName = os.path.join(workDirectory, ("smvWithCheck%i.smv" % index))
fileNames.append(os.path.abspath(fileName))
with open(fileName, 'w') as svmCheckFile:
svmCheckFile.writelines(smvWithoutChecks)
svmCheckFile.write(check)
pipes = []
processes = []
openFiles = []
startTime = time.time_ns()
print("Starting processes at " + datetime.datetime.now().strftime("%H:%M:%S"))
for index, path in enumerate(fileNames):
fileName = os.path.join(workDirectory, ('out%i.txt' % index))
outFile = open(fileName, 'w')
openFiles.append(outFile)
process = Popen([nusmvPath, path], stdout=outFile, stderr=outFile)
processes.append(process)
try:
while True:
totalProcesses = len(processes)
finishedProcesses = []
ongoingProcesses = []
for index, process in enumerate(processes):
if process.poll() is None:
ongoingProcesses.append(index)
else:
finishedProcesses.append(index)
if len(ongoingProcesses) is 0:
print()
break
print("Active processes: %s; finished: %s" % (
','.join(str(proc) for proc in ongoingProcesses), ','.join(str(proc) for proc in finishedProcesses)),
end='\r')
time.sleep(1)
except KeyboardInterrupt:
for index, process in enumerate(processes):
try:
process.terminate()
except OSError:
pass
process.wait()
openFiles[index].close()
for openFile in openFiles:
openFile.close()
afterTime = time.time_ns()
print("Finished after %f ms" % ((afterTime - startTime) / 1000000))
checkForFailedChecks()