Skip to content
TKTail

Purpose-built scripts and the TKTail workflow - the source

scripts/suite_digest.py

SHA-256 (LF line endings, as in the source ZIP): 27c4e0543087d0329bc1ef352d2fd629a757fe3e7e11a8d88d7b8117fdce07d6 · MIT license (the benchmark suite only)

"""competitive-bench-v1 - the identity of the suite's source: every file of the suite (not results, build output or the
package's own manifests), its path and SHA-256, in path order; and one digest over that list. run.py records it in each
run's environment.json; the packager refuses to publish a source ZIP whose files have another digest - the published
code is exactly the code that produced the published numbers.
"""
import hashlib
import os

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
EXCLUDED_DIRS = {'bin', 'obj', '__pycache__', '.vs', 'results', 'published'}  # published/: the runs on tktail.dev (data)
EXCLUDED_FILES = {'BENCHMARK-MANIFEST.json', 'SOURCE-SHA256SUMS.txt'}
INCLUDED_IN_RESULTS = {'README.md'}  # results/README.md describes the result files


def source_files(root=ROOT):
    """(relative path with '/', absolute path) of every source file, in path order."""
    out = []
    for folder, dirs, files in os.walk(root):
        rel_folder = os.path.relpath(folder, root).replace(os.sep, '/')
        top = rel_folder.split('/')[0]
        dirs[:] = sorted(d for d in dirs if d not in EXCLUDED_DIRS or (d == 'results' and rel_folder == '.'))
        for name in sorted(files):
            rel = name if rel_folder == '.' else f'{rel_folder}/{name}'
            if name in EXCLUDED_FILES or name.endswith(('.pyc', '.user', '.suo')):
                continue
            if top == 'results' and name not in INCLUDED_IN_RESULTS:
                continue
            out.append((rel, os.path.join(folder, name)))
    return sorted(out)


def normalized(path):
    """The file's bytes with CRLF as LF - the suite's files are text, and a git checkout may change line endings; the
    source ZIP holds exactly these bytes."""
    with open(path, 'rb') as f:
        return f.read().replace(b'\r\n', b'\n')


def file_sha256(path):
    return hashlib.sha256(normalized(path)).hexdigest()


def digest(root=ROOT):
    """(the suite digest, [(path, sha256)]) over the LF-normalized files."""
    files = [(rel, file_sha256(path)) for rel, path in source_files(root)]
    h = hashlib.sha256()
    for rel, sha in files:
        h.update(f'{sha}  {rel}\n'.encode('utf-8'))
    return h.hexdigest(), files