chessbaseLinux/scripts/rodent4-manifest.py

83 lines
3.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""The files of Rodent IV that ChessPenguin downloads from the authors' repository
(personalities and opening books, docs/decisions.md «Rodent IV»), with sizes and SHA-256,
and the books each personality opens, written as crates/play/src/rodent4_files.rs (the
desktop and the phone download from it).
git clone https://github.com/nescitus/rodent-iv /tmp/rodent-iv
git -C /tmp/rodent-iv checkout <COMMIT>
python3 -I scripts/rodent4-manifest.py /tmp/rodent-iv
An unpacked archive of the commit (no git) is read too, with the commit given:
python3 -I scripts/rodent4-manifest.py /tmp/rodent-iv-<COMMIT> <COMMIT>
"""
import hashlib
import os
import subprocess
import sys
repo = sys.argv[1]
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if len(sys.argv) > 2:
commit = sys.argv[2]
else:
commit = subprocess.run(["git", "-C", repo, "rev-parse", "HEAD"], capture_output=True, text=True, check=True).stdout.strip()
files = []
for top in ("personalities", "books"):
for dirpath, _, names in os.walk(os.path.join(repo, top)):
for n in sorted(names):
path = os.path.join(dirpath, n)
rel = os.path.relpath(path, repo).replace(os.sep, "/")
# Chess960 is not played here.
if rel.endswith("guide_960.bin"):
continue
data = open(path, "rb").read()
files.append((rel, len(data), hashlib.sha256(data).hexdigest()))
files.sort()
# The personalities of the list the engine offers (basic.ini, PERSONALITY_SET=all) and the
# books each one opens, named as its file names them (under books/).
personalities = []
section = None
for line in open(os.path.join(repo, "personalities", "basic.ini"), encoding="latin-1"):
line = line.strip()
if line.startswith("PERSONALITY_SET="):
section = line.split("=", 1)[1]
elif section == "all" and "=" in line and not line.startswith(("#", ";")):
name, file = line.split("=", 1)
# basic.ini names one more than the repository has (classicist.txt).
if not os.path.isfile(os.path.join(repo, "personalities", file)):
continue
books = {}
for opt in open(os.path.join(repo, "personalities", file), encoding="latin-1"):
words = opt.split()
if len(words) >= 5 and words[:2] == ["setoption", "name"] and words[3] == "value":
if words[2] in ("GuideBookFile", "MainBookFile"):
books[words[2]] = words[4]
personalities.append((name, books["GuideBookFile"], books["MainBookFile"]))
personalities.sort()
out = [
"//! Generated by scripts/rodent4-manifest.py: the personalities and opening books of",
"//! Rodent IV as they are in the authors' repository at `COMMIT`, with their sizes and",
"//! SHA-256 (each is checked as it is downloaded).",
"",
f'pub const COMMIT: &str = "{commit}";',
"",
"/// (path in the repository, bytes, SHA-256).",
"pub const FILES: &[(&str, u64, &str)] = &[",
]
out += [f' ("{p}", {s}, "{h}"),' for p, s, h in files]
out += [
"];",
"",
"/// The personalities of the engine's list: (name, guide book, main book), the books",
"/// as the personality's file names them (under `books/`).",
"pub const PERSONALITY_BOOKS: &[(&str, &str, &str)] = &[",
]
out += [f' ("{n}", "{g}", "{m}"),' for n, g, m in personalities]
out += ["];", ""]
open(os.path.join(root, "crates", "play", "src", "rodent4_files.rs"), "w").write("\n".join(out))
print(len(files), "files,", sum(s for _, s, _ in files) // 1_000_000, "MB, commit", commit)