File size: 2,398 Bytes
0e239c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
"""Run inside the untouched agent image; output contains no evaluator answers."""
import hashlib
import json
from pathlib import Path
import subprocess


def git(path, *args, check=True):
    p = subprocess.run(['git', '-C', str(path), *args], text=True, capture_output=True)
    if check and p.returncode:
        raise RuntimeError(f'git {args[0]} failed: {p.returncode}')
    return p


manifest = json.loads(Path('/opt/crossswe/instance.json').read_text())
out = {'manifest': manifest, 'repositories': []}
for r in manifest['repositories']:
    path = Path(r['path'])
    base = r['base_commit']
    head = git(path, 'rev-parse', 'HEAD').stdout.strip()
    tree = git(path, 'rev-parse', 'HEAD^{tree}').stdout.strip()
    status = git(path, 'status', '--porcelain=v1', '--untracked-files=all').stdout
    objects = git(path, 'cat-file', '--batch-all-objects', '--batch-check=%(objectname) %(objecttype)').stdout.splitlines()
    entries = git(path, 'ls-tree', '-r', '-t', 'HEAD').stdout.splitlines()
    expected = {base, tree} | {line.split()[2] for line in entries}
    actual = {line.split()[0] for line in objects}
    assert head == base and tree == r['tree_sha1'] and not status
    assert actual == expected, 'Unexpected Git objects in agent image'
    assert [line.split()[0] for line in objects if line.endswith(' commit')] == [base]
    assert not (path / '.git/objects/info/alternates').exists()
    assert git(path, 'remote').stdout.strip() == ''
    listing = []
    for file in sorted(path.rglob('*')):
        rel = file.relative_to(path)
        if '.git' in rel.parts or (not file.is_file() and not file.is_symlink()):
            continue
        if file.is_symlink():
            import os
            listing.append((str(rel), 'symlink', os.readlink(file)))
        else:
            listing.append((str(rel), file.stat().st_mode & 0o777, hashlib.sha256(file.read_bytes()).hexdigest()))
    out['repositories'].append({'key': r['key'], 'head': head, 'tree': tree,
        'clean': not status, 'git_object_count': len(actual), 'only_base_objects': True,
        'files': len(listing), 'file_inventory_sha256': hashlib.sha256(json.dumps(listing).encode()).hexdigest()})
out['node'] = subprocess.check_output(['node', '--version'], text=True).strip()
out['npm'] = subprocess.check_output(['npm', '--version'], text=True).strip()
print(json.dumps(out, indent=2, sort_keys=True))