File size: 5,240 Bytes
809dbd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Build a scoped public reproduction bundle and optional frozen RIR archives."""
from __future__ import annotations
import argparse
import shutil
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile
from .common import read,sha,write


def package(root,work=None,reuse_cache=False):
    repo=Path(__file__).resolve().parents[1]
    folder=root/'reproduction';folder.mkdir(exist_ok=True)
    modules=['__init__.py','common.py','motion.py','continuous.py','diagnostics.py','render.py','export.py','score.py','validate.py','Dockerfile','requirements.txt','fetch_cache.py','package.py','binaural.py']
    for name in modules:
        destination=folder/'dynamic_demo'/name;destination.parent.mkdir(parents=True,exist_ok=True)
        shutil.copyfile(repo/'dynamic_demo'/name,destination)
    for name in ['tests/test_protocol.py','tests/parity.mjs','tests/browser.cjs','tests/test_binaural.py','tests/test_diagnostics.py']:
        destination=folder/'dynamic_demo'/name;destination.parent.mkdir(parents=True,exist_ok=True)
        shutil.copyfile(repo/'dynamic_demo'/name,destination)
    for name in ['native/source_xyz.patch','configs/native_environment.json']:
        destination=folder/'generation'/name;destination.parent.mkdir(parents=True,exist_ok=True)
        shutil.copyfile(repo/'generation'/name,destination)
    cache_manifest=[]
    if work:
        cache=work/'rirs';recipe=read(root/'data/recipe.json')
        write(cache/'cache_context.json',dict(renderer=recipe['renderer'],foa_mapping=recipe['foa_mapping'],
            room_assets_sha256=recipe['room_assets_sha256']))
        cache_dir=root/'cache';cache_dir.mkdir(exist_ok=True)
        covered=set()
        if reuse_cache and (cache_dir/'manifest.json').exists():
            cache_manifest=read(cache_dir/'manifest.json')['archives']
            for record in cache_manifest:
                archive=root/record['path']
                if sha(archive)!=record['sha256']:raise ValueError('Reusable cache archive checksum mismatch')
                with ZipFile(archive) as z:
                    covered.update(z.namelist())
                    if 'cache_context.json' in z.namelist() and z.read('cache_context.json')!=(cache/'cache_context.json').read_bytes():
                        raise ValueError('Reusable archives belong to another room/renderer')
        else:
            for archive in cache_dir.glob('RIR-cache-*.zip'):archive.unlink()
        groups=[];part=[];size=0
        records=read(root/'data/rendering.json')['rir_records']
        paths=[cache/'cache_context.json']+[cache/(r['rir_id']+suffix) for r in records for suffix in ('.json','.npy')]
        for p in sorted(paths):
            if not p.is_file():raise ValueError('Required frozen RIR cache file missing: '+str(p))
            if p.name in covered:continue
            if part and size+p.stat().st_size>40*1024*1024:groups.append(part);part=[];size=0
            part.append(p);size+=p.stat().st_size
        if part:groups.append(part)
        for index,paths in enumerate(groups,len(cache_manifest)+1):
            archive=cache_dir/f'RIR-cache-{index:02d}.zip'
            with ZipFile(archive,'w',ZIP_DEFLATED,compresslevel=5) as z:
                for p in paths:z.write(p,p.name)
            cache_manifest.append(dict(path=str(archive.relative_to(root)),sha256=sha(archive),
                bytes=archive.stat().st_size,files=len(paths)))
        write(cache_dir/'manifest.json',dict(description='Optional frozen native RIR cache; unpack into WORK/rirs',
            archives=cache_manifest,rir_count=len(read(root/'data/rendering.json')['rir_records'])))
    zip_path=root/'reproduction.zip'
    with ZipFile(zip_path,'w',ZIP_DEFLATED,compresslevel=5) as z:
        # No paths outside this demo; no credentials, full benchmark, old releases or author operations.
        for p in sorted(root.rglob('*')):
            relative=p.relative_to(root)
            # The outer manifest hashes this zip; including it here would be stale/circular.
            if not p.is_file() or p in (zip_path,root/'manifest.json') or relative.parts[0] in ('cache','reproduction') or '__pycache__' in relative.parts:continue
            z.write(p,'demo/dynamic/'+str(relative))
        for p in sorted(folder.rglob('*')):
            if p.is_file():z.write(p,str(p.relative_to(folder)))
        z.write(root/'README.md','README.md')
    manifest=[]
    for p in sorted(root.rglob('*')):
        if p.is_file() and 'reproduction' not in p.relative_to(root).parts and p!=root/'manifest.json':
            manifest.append(dict(path=str(p.relative_to(root)),sha256=sha(p),bytes=p.stat().st_size))
    write(root/'manifest.json',dict(description='Only public demo files; frozen inputs and generated outputs',files=manifest))
    print('PACKAGED',zip_path.stat().st_size,'bytes;',len(cache_manifest),'optional cache archives')


if __name__=='__main__':
    p=argparse.ArgumentParser();p.add_argument('--root',type=Path,required=True);p.add_argument('--work',type=Path)
    p.add_argument('--reuse-cache',action='store_true',help='Keep verified same-room archives and append missing kernels')
    a=p.parse_args();package(a.root.resolve(),a.work.resolve() if a.work else None,a.reuse_cache)