From: whitequark Date: Fri, 23 Aug 2019 01:10:51 +0000 (+0000) Subject: build.run: add BuildPlan.digest(), useful for caching. X-Git-Tag: locally_working~2 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=9350620f89db7875c18ab37f0032c03f83248289;p=nmigen.git build.run: add BuildPlan.digest(), useful for caching. --- diff --git a/nmigen/build/run.py b/nmigen/build/run.py index c512b1f..d92ab17 100644 --- a/nmigen/build/run.py +++ b/nmigen/build/run.py @@ -6,6 +6,7 @@ import sys import subprocess import tempfile import zipfile +import hashlib __all__ = ["BuildPlan", "BuildProducts", "LocalBuildProducts"] @@ -32,6 +33,21 @@ class BuildPlan: assert isinstance(filename, str) and filename not in self.files self.files[filename] = content + def digest(self, size=64): + """ + Compute a `digest`, a short byte sequence deterministically and uniquely identifying + this build plan. + """ + hasher = hashlib.blake2b(digest_size=size) + for filename in sorted(self.files): + hasher.update(filename.encode("utf-8")) + content = self.files[filename] + if isinstance(content, str): + content = content.encode("utf-8") + hasher.update(content) + hasher.update(self.script.encode("utf-8")) + return hasher.digest() + def archive(self, file): """ Archive files from the build plan into ``file``, which can be either a filename, or