-
Notifications
You must be signed in to change notification settings - Fork 127
/
updater.py
122 lines (107 loc) · 3.99 KB
/
updater.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
import datetime
import glob
import os
import platform
import shutil
import sys
from distutils.dir_util import copy_tree
from zipfile import ZipFile
import requests
start_dir = os.getcwd()
WORKER_URL = "https://github.com/glinscott/fishtest/archive/master.zip"
def do_restart():
"""Restarts the worker, using the same arguments"""
args = sys.argv[:]
args.insert(0, sys.executable)
if sys.platform == "win32":
args = ['"{}"'.format(arg) for arg in args]
os.chdir(start_dir)
os.execv(sys.executable, args) # This does not return!
def update(restart=True, test=False):
worker_dir = os.path.dirname(os.path.realpath(__file__))
update_dir = os.path.join(worker_dir, "update")
if not os.path.exists(update_dir):
os.makedirs(update_dir)
worker_zip = os.path.join(update_dir, "wk.zip")
with open(worker_zip, "wb+") as f:
f.write(requests.get(WORKER_URL).content)
zip_file = ZipFile(worker_zip)
zip_file.extractall(update_dir)
zip_file.close()
prefix = os.path.commonprefix([n.filename for n in zip_file.infolist()])
worker_src = os.path.join(update_dir, prefix, "worker")
if not test:
# Delete the "packages" folder to only have new files after an upgrade.
packages_dir = os.path.join(worker_dir, "packages")
if os.path.exists(packages_dir):
try:
shutil.rmtree(packages_dir)
except Exception as e:
print(
"Failed to delete the folder {}:\n".format(packages_dir),
e,
sep="",
file=sys.stderr,
)
copy_tree(worker_src, worker_dir)
else:
file_list = os.listdir(worker_src)
shutil.rmtree(update_dir)
# Rename the testing_dir to backup possible user custom files
# and to trigger the download of updated files.
# The worker runs games from the "testing" folder so change the folder.
os.chdir(worker_dir)
testing_dir = os.path.join(worker_dir, "testing")
if os.path.exists(testing_dir):
time_stamp = str(datetime.datetime.timestamp(datetime.datetime.utcnow()))
bkp_testing_dir = os.path.join(worker_dir, "_testing_" + time_stamp)
shutil.move(testing_dir, bkp_testing_dir)
os.makedirs(testing_dir)
# Delete old engine binaries
engines = glob.glob(os.path.join(bkp_testing_dir, "stockfish_*"))
for engine in engines:
try:
os.remove(engine)
except Exception as e:
print(
"Failed to delete the engine binary {}:\n".format(engine),
e,
sep="",
file=sys.stderr,
)
# Delete old networks.
networks = glob.glob(os.path.join(bkp_testing_dir, "nn-*.nnue"))
for network in networks:
try:
os.remove(network)
except Exception as e:
print(
"Failed to delete the network file {}:\n".format(network),
e,
sep="",
file=sys.stderr,
)
# Clean up old folder backups (keeping the num_bkps most recent).
bkp_dirs = glob.glob(os.path.join(worker_dir, "_testing_*"))
num_bkps = 3
if len(bkp_dirs) > num_bkps:
bkp_dirs.sort(key=os.path.getmtime)
for old_bkp_dir in bkp_dirs[:-num_bkps]:
try:
shutil.rmtree(old_bkp_dir)
except Exception as e:
print(
"Failed to remove the old backup folder {}:\n".format(
old_bkp_dir
),
e,
sep="",
file=sys.stderr,
)
print("start_dir: {}".format(start_dir))
if restart:
do_restart()
if test:
return file_list
if __name__ == "__main__":
update(False)