-
Notifications
You must be signed in to change notification settings - Fork 0
/
EulerVerify.py
166 lines (129 loc) · 4.86 KB
/
EulerVerify.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
import glob
import re
import subprocess
import hashlib
import threading
import sys
import time
import argparse
import inspect
starttime = time.time()
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def ok(str):
print(bcolors.OKGREEN + str + bcolors.ENDC)
def warning(str):
print(bcolors.WARNING + str + bcolors.ENDC)
def fail(str):
print(bcolors.FAIL + str + bcolors.ENDC)
class EulerVerify:
def __init__(self):
self.files = {}
self.scan()
def scan(self):
files = glob.glob("./solutions/*.py")
def match(s): return re.match("./solutions/([0-9]*).py", s)
self.files = dict((int(match(file).group(1)), file)
for file in files if match(file))
with open("hashes.txt") as f:
self.hashes = [line.strip() for line in f.readlines()]
def executeAll(self, timeout=10, commit_id=None):
failed = False
for i in range(max(self.files.keys())):
if i in self.files:
if not self.execute(i, timeout, commit_id):
failed = True
if failed:
exit(1)
def execute(self, num, timeout=10, commit_id=None):
if num in self.files:
if commit_id and not self.file_changed(num, commit_id):
return
print("Running Euler Problem #%d" % (num))
self.solution = ""
p = subprocess.Popen(["python3", "solutions/"+str(num)+".py"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
t = time.time()
self.out = ""
self.err = ""
def target():
out, err = p.communicate("")
lines = str(out, "UTF-8").split("\n")
if len(lines) >= 2:
self.solution = lines[-2]
self.out = str(out, "UTF-8")
self.err = str(err, "UTF-8")
thread = threading.Thread(target=target)
thread.start()
thread.join(timeout)
t = time.time()-t
if thread.is_alive():
fail("\tThread exceeded %d second time limit" % (timeout))
p.terminate()
thread.join()
return False
else:
hash = hashlib.md5(self.solution.encode('utf-8')).hexdigest()
if self.hashes[num-1] == hash:
if t < 1:
ok("\tPassed in %.3f" % t)
else:
warning("\tPassed in %.3f" % t)
return True
else:
fail("\tWrong: Solution %s: %s != %s"
% (self.solution, self.hashes[num-1], hash))
print(self.out)
print(self.err)
return False
def _verify(self, value):
t = time.time()-starttime
file = inspect.stack()[2][1]
def match(s): return re.match("solutions/([0-9]*).py", s)
num = int(match(file).group(1))
hash = hashlib.md5(str(value).encode("utf-8")).hexdigest()
if self.hashes[num-1] == hash:
if t < 1:
ok("Passed in %.3f" % t)
else:
warning("Passed in %.3f" % t)
else:
fail("Wrong: Solution %s: " % (value))
fail("Hashes: %s != %s" % (self.hashes[num-1], hash))
print(value)
exit()
def file_changed(self, num, commit_id):
p = subprocess.Popen(["git", "diff",
commit_id, "--",
str(num)+".py"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate("")
return len(out) != 0
def verify(value):
EulerVerify()._verify(value)
if len(sys.argv) > 1:
timeout = 10
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('option', type=str, metavar="option", nargs=1,
help='number or all')
parser.add_argument('-t', dest='timeout', default=[10],
type=int, metavar="seconds", nargs=1,
help='timeout for problem execution')
parser.add_argument('-c', dest='commit_id', default=[None],
type=str, metavar="commit", nargs=1,
help='only execute scripts changed'
+ 'since this commit it')
args = parser.parse_args()
if "all" in args.option:
EulerVerify().executeAll(args.timeout[0], args.commit_id[0])
if args.option[0].isdigit():
EulerVerify().execute(int(args.option[0]), args.commit_id[0])