-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlive_parse_cutechess_cli_log.py_000000000000002.copy
executable file
·130 lines (124 loc) · 5.43 KB
/
live_parse_cutechess_cli_log.py_000000000000002.copy
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
#!/usr/bin/env python3
import chess, sys, os, json, time, re
SLEEP_TIME = 0.1
DEFAULT_DIGITS = 3
LAST_BYTES_SIZE = 100000
SI = ((10**3, "k"),
(10**6, "M"),
(10**9, "G"),
(10**12, "T"),
(10**15, "P"),
(10**18, "E"),
(10**21, "Z"),
(10**24, "Y"))
SIN = ((10**3, "k"),
(10**6, "M"),
(10**9, "B"),
(10**12, "T"),
(10**15, "P"),
(10**18, "E"),
(10**21, "Z"),
(10**24, "Y"))
def str_SI(no, digits=DEFAULT_DIGITS, units=SI, postfix=""):
if postfix:
gap = " "
else:
gap = ""
last_unit = ""
last_mag = 1
for i in range(len(units)):
mag, unit = units[i]
if no<mag:
return "%.*g%s%s%s" % (digits, no/last_mag, gap, last_unit, postfix)
last_mag, last_unit = mag, unit
return "%.*g%s%s%s" % (digits, no/last_mag, gap, last_unit, postfix)
def get_size(filename):
return os.stat(filename).st_size
if __name__=="__main__":
logfile = sys.argv[1]
current_size = get_size(logfile)
if current_size >= LAST_BYTES_SIZE:
with open(logfile) as fp:
fp.seek(current_size - LAST_BYTES_SIZE)
s = fp.read(LAST_BYTES_SIZE)
m = re.match(r".*([<>].*position.*)", s, re.DOTALL)
if m:
current_size -= len(m.group(1)) + LAST_BYTES_SIZE - len(s)
b = None
while True:
size = get_size(logfile)
if size < current_size: #New tournament started
current_size = 0
b = None
continue
elif size == current_size:
time.sleep(SLEEP_TIME)
else:
with open(logfile) as fp:
fp.seek(current_size)
for line in fp:
if not line: break
if line[-1]!="\n":
time.sleep(SLEEP_TIME)
break
current_size += len(line)
if ":" not in line: continue
cs, uci = line.split(":")[:2]
m = re.match(r".*[<>](.*?)\(", cs)
if not m: continue
name = m.group(1)
l = uci.split()
if l[0]=="position":
if len(l)>=2 and l[1]=="startpos":
b = chess.Board()
if len(l)>=4 and l[2]=="moves":
for m in l[3:]:
b.push_uci(m)
elif l[0]=="info" and b and "pv" in l:
move_lst = [chess.Move.from_uci(m) for m in l[l.index("pv")+1:]]
try:
pv_str = b.variation_san(move_lst)
except ValueError:
pv_str = ""
#{"engine": "Stockfish 030519", "eval": 0.63, "pv": "5...g4 6. Nh4 f3 7. Nc3 Be7 8. Be3 Bxh4 9. gxh4 Qxh4+ 10. Kd2 Nc6 11. h3 Nf6 12. Bd3 gxh3 13. Qxf3 Bg4 14. Qf4 Nh5 15. Qh2 O-O-O 16. Rag1 f5 17. exf5 Nf6 18. Qf4 Rhg8 19. Bc4 d5 20. Be2 Qh5 21. Rg3 Bxe2 22. Rgxh3 Qf7 23. Nxe2 Rde8 24. Nc3 h5 25. Kc1 Rg4 26. Qf2 Qg7 27. a3 a6 28. Kb1 Rg2 29. Qf3 Qg4 30. Qxg4 Rxg4 31. Bf2 Nxd4", "depth": "33/56", "speed": "125Mn/s", "tbhits": "26", "nodes": 2905276005}
score = depth = seldepth = speed = tbhits = ""
nodes = 0
time_s = 0.0
color = 0
fail = 0
plynum = len(b.move_stack)
if b.turn==chess.BLACK:
color = 1
for i, token in enumerate(l):
if b.turn==chess.BLACK:
color = 1
if token=="score":
score_no = int(l[i+2])
if b.turn==chess.BLACK: score_no = -score_no
if l[i+1]=="cp":
score = "%.2f" % (score_no/100,)
elif l[i+1]=="mate":
score = "#%i" % (score_no,)
elif token=="depth":
depth = l[i+1]
elif token=="seldepth":
seldepth = l[i+1]
elif token=="nodes":
nodes = str_SI(int(l[i+1]), units=SIN)
elif token=="time":
time_s = int(l[i+1])/1000
elif token=="tbhits":
tbhits = str_SI(int(l[i+1]))
elif token=="nps":
speed = str_SI(int(l[i+1]), postfix="nps")
if seldepth:
depth += "/" + seldepth
else:
depth += "/" + depth
#print(json_str)
if fail == 0:
json_str = json.dumps({"color": color, "engine": name, "eval": score, "pv": pv_str, "depth": depth, "speed": speed, "tbhits": tbhits, "time": time_s, "nodes": nodes, "plynum": plynum})
with open("liveengineeval.json", "w") as fp_out: fp_out.write(json_str + "\n")
if current_size >= size:
break
current_size = size