ProgramLab / build /verify_context.py
YaoTang23's picture
ProgramLab
af2930e
Raw History Blame Contribute Delete
1.9 kB
import hashlib
import json
from pathlib import Path
import subprocess
import sys
import tarfile
manifest, iid, archive, destination = sys.argv[1:]
record = json.loads(Path(manifest).read_text())['contexts'][iid]
archive = Path(archive)
destination = Path(destination).resolve()
def digest(path):
value = hashlib.sha256()
with path.open('rb') as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b''):
value.update(chunk)
return value.hexdigest()
assert digest(archive) == record['archive_sha256'], 'Context archive hash mismatch'
with tarfile.open(archive) as bundle:
bundle.extractall(destination, filter='data')
gold = destination/'executable'
assert gold.is_file() and not gold.is_symlink()
assert digest(gold) == record['gold_sha256'], 'Reference hash mismatch'
assert (destination/'.git/HEAD').is_file(), 'Missing Git HEAD'
git = ['git', '-C', str(destination)]
assert subprocess.run(git+['rev-parse','--verify','HEAD'],capture_output=True).returncode == 0
for command in (['show','HEAD:executable'], ['ls-files','--error-unmatch','executable']):
assert subprocess.run(git+command,capture_output=True).returncode != 0, 'Reference tracked by Git'
history = subprocess.run(git+['rev-list','--all','--objects','--','executable'],capture_output=True)
assert history.returncode == 0 and not history.stdout.strip(), 'Reference path in Git history'
for path in destination.rglob('*'):
if path.name == '.git' and path.is_dir() and (path/'HEAD').is_file():
check = subprocess.run(['git','--git-dir',str(path),'cat-file','-e',record['gold_git_blob']],capture_output=True)
assert check.returncode != 0, 'Reference blob in a Git object database'
if path.is_file() and path != gold:
assert digest(path) != record['gold_sha256'], 'Duplicate reference file: '+str(path)
print(record['gold_sha256'],record['gold_git_blob'])