Skip to content

Commit

Permalink
Try a backup copy if the downloaded cutechess-cli is not working.
Browse files Browse the repository at this point in the history
Logic:

1) Check if cutechess-cli exists and is working.

2) If not: download it.

3) Check if downloaded cutechess-cli is working.

4) If not: try to restore a backup copy.

5) If successful check if the backup copy is working.

6) If not: bail out.

Do not treat MacOS specially in the updater.
  • Loading branch information
vdbergh committed Jun 25, 2022
1 parent 5f98246 commit 95ca00f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
49 changes: 37 additions & 12 deletions worker/games.py
Expand Up @@ -257,7 +257,7 @@ def required_net(engine):
return net


def verify_required_cutechess(testing_dir, cutechess):
def verify_required_cutechess(testing_dir, cutechess, quiet=False):
print(
"Obtaining version info for {} ...".format(os.path.join(testing_dir, cutechess))
)
Expand All @@ -282,22 +282,27 @@ def verify_required_cutechess(testing_dir, cutechess):
minor = int(m.group(2))
patch = int(m.group(3))
except (OSError, subprocess.SubprocessError) as e:
raise FatalException("Unable to run cutechess-cl. Error: {}".format(str(e)))
if not quiet:
print("Unable to run cutechess-cli. Error: {}".format(str(e)))
return False

if p.returncode != 0:
raise FatalException(
print(
"Unable to run cutechess-cli. Return code: {}. Error: {}".format(
format_return_code(p.returncode), errors
)
)
return False

if major + minor + patch == 0:
raise FatalException("Unable to find the version of cutechess-cli.")
print("Unable to find the version of cutechess-cli.")
return False

if (major, minor) < (1, 2):
raise FatalException(
"Requires cutechess 1.2 or higher, found version doesn't match"
)
print("Requires cutechess 1.2 or higher, found version doesn't match")
return False

return True


def required_net_from_source():
Expand Down Expand Up @@ -1143,7 +1148,7 @@ def run_games(worker_info, password, remote, run, task_id, pgn_file):
if "start" in task:
print("Variable task sizes used. Opening offset = {}".format(opening_offset))
start_game_index = opening_offset + input_total_games
run_seed = int(hashlib.sha1(run["_id"].encode("utf-8")).hexdigest(), 16) % (2**30)
run_seed = int(hashlib.sha1(run["_id"].encode("utf-8")).hexdigest(), 16) % (2 ** 30)

# Format options according to cutechess syntax.
def parse_options(s):
Expand All @@ -1167,10 +1172,10 @@ def parse_options(s):
if not os.path.exists(testing_dir):
os.makedirs(testing_dir)

# Download cutechess if missing in the directory.
# Download cutechess if not working or missing in the directory.
cutechess = "cutechess-cli" + EXE_SUFFIX
os.chdir(testing_dir)
if not os.path.exists(cutechess):
if not verify_required_cutechess(testing_dir, cutechess, quiet=True):
if len(EXE_SUFFIX) > 0:
zipball = "cutechess-cli-win.zip"
elif IS_MACOS:
Expand All @@ -1184,8 +1189,28 @@ def parse_options(s):
os.remove(zipball)
os.chmod(cutechess, os.stat(cutechess).st_mode | stat.S_IEXEC)

# Verify that cutechess is working and has the required minimum version.
verify_required_cutechess(testing_dir, cutechess)
# Verify that cutechess is working and has the required minimum version.
if not verify_required_cutechess(testing_dir, cutechess):
print(
"The downloaded cutechess-cli is not working. Trying to restore a backup copy ..."
)
bkp_cutechess_clis = sorted(
glob.glob(os.path.join(worker_dir, "_testing_*", cutechess)),
key=os.path.getctime,
)
if bkp_cutechess_clis:
bkp_cutechess_cli = bkp_cutechess_clis[-1]
shutil.copy(bkp_cutechess_cli, testing_dir)
if not verify_required_cutechess(testing_dir, cutechess):
print(
"The backup copy {} doesn't work either ...".format(
bkp_cutechess_cli
)
)
raise FatalException("No suitable cutechess-cli found")
else:
print("No backup copy found")
raise FatalException("No suitable cutechess-cli found")

# Clean up old engines (keeping the num_bkps most recent).
engines = glob.glob(os.path.join(testing_dir, "stockfish_*" + EXE_SUFFIX))
Expand Down
4 changes: 0 additions & 4 deletions worker/updater.py
Expand Up @@ -68,10 +68,6 @@ def update(restart=True, test=False):
bkp_testing_dir = os.path.join(worker_dir, "_testing_" + time_stamp)
shutil.move(testing_dir, bkp_testing_dir)
os.makedirs(testing_dir)
# Copy the user built MacOS cutechess-cli
# until we will have an official MacOS build to download
if "darwin" in platform.system().lower():
shutil.copy2(os.path.join(bkp_testing_dir, "cutechess-cli"), testing_dir)
# Delete old engine binaries
engines = glob.glob(os.path.join(bkp_testing_dir, "stockfish_*"))
for engine in engines:
Expand Down

0 comments on commit 95ca00f

Please sign in to comment.