File size: 1,901 Bytes
af2930e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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'])