File size: 1,496 Bytes
1209c2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5adf069
1209c2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Streaming integrity checks; no model imports, weights loading, or network calls."""
import hashlib
import json
from pathlib import Path


def file_sha256(path):
    digest = hashlib.sha256()
    with Path(path).open('rb') as stream:
        for chunk in iter(lambda: stream.read(8 * 1024 * 1024), b''):
            digest.update(chunk)
    return digest.hexdigest()


def verify_package(directory):
    root = Path(directory).resolve()
    manifest = json.loads((root / 'manifest.json').read_text())
    if manifest['model'] != 'Fev-4B' or manifest['version'] not in ('0.1.0', '0.2.0', '0.2.1'):
        raise ValueError('Unsupported package manifest')
    files = manifest.get('files')
    if not isinstance(files, dict) or not files:
        raise ValueError('No files in package manifest')
    for name, expected in files.items():
        relative = Path(name)
        if relative.is_absolute() or '..' in relative.parts:
            raise ValueError('Unsafe path in manifest')
        path = (root / relative).resolve()
        if not path.is_relative_to(root) or not path.is_file():
            raise ValueError('Missing or external package file: ' + name)
        if path.stat().st_size != expected['bytes'] or file_sha256(path) != expected['sha256']:
            raise ValueError('Package checksum mismatch: ' + name)
    return {'model': manifest['model'], 'version': manifest['version'],
            'verifiedFiles': len(files), 'totalBytes': sum(s['bytes'] for s in files.values())}