Skip to content

armgit / vork

Public Imported from GitHub
0

Headless 3D and VFX engine for agents. Research preview.

6.7 KB
Raw Blame
#!/usr/bin/env python3
"""Publish ONLY to a fresh loopback Verdaccio and serve a local Python index.
Needs Node >=22.18, npm, Python >=3.11 (venv/pip). No real-registry writes.
"""
import argparse
import functools
import http.server
import json
import os
from pathlib import Path
import platform
import shutil
import socket
import subprocess
import sys
import threading
import time
import urllib.request

ROOT = Path(__file__).resolve().parents[1]
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('--dist', type=Path, required=True)
p.add_argument('--work', type=Path, required=True, help='new directory outside the repository')
p.add_argument('--gpu', action='store_true')
p.add_argument('--init', action='store_true', help='also test project creation; needs native archives containing vork init')
a = p.parse_args()
a.dist = a.dist.resolve()
a.work = a.work.resolve()
a.work.mkdir(parents=True, exist_ok=False)
target = {('Linux', 'x86_64'): 'linux-x64', ('Darwin', 'arm64'): 'darwin-arm64', ('Windows', 'AMD64'): 'windows-x64'}[(platform.system(), platform.machine())]
npm = shutil.which('npm')
env = os.environ.copy()
env.pop('VORK_BIN', None)
env['npm_config_cache'] = str(a.work / 'npm-cache')

def run(command, cwd=a.work):
    print('+', *map(str, command), flush=True)
    subprocess.run(list(map(str, command)), cwd=cwd, env=env, check=True)

# Download the test server from npm; never upload to its upstream registry.
run([npm, 'install', '--prefix', a.work / 'tools', '--registry', 'https://registry.npmjs.org', '[email protected]'])
with socket.socket() as sock:
    sock.bind(('127.0.0.1', 0))
    port = sock.getsockname()[1]
url = f'http://127.0.0.1:{port}'
registry = ['--registry', url, f'--@voyager-so:registry={url}']
config = a.work / 'verdaccio.yaml'
# JSON is YAML, including properly quoted Windows paths.
config.write_text(json.dumps({'storage': str(a.work / 'storage'), 'max_body_size': '300mb',
    'auth': {'htpasswd': {'file': str(a.work / 'htpasswd')}}, 'uplinks': {},
    'packages': {'@voyager-so/*': {'access': '$all', 'publish': '$all'}, 'create-vork': {'access': '$all', 'publish': '$all'}},
    'log': {'type': 'stdout', 'format': 'pretty', 'level': 'warn'}}))
server_log = (a.work / 'verdaccio.log').open('w')
server = subprocess.Popen([shutil.which('node'), str(a.work / 'tools/node_modules/verdaccio/bin/verdaccio'), '--config', str(config), '--listen', f'127.0.0.1:{port}'], stdout=server_log, stderr=subprocess.STDOUT, env=env)
index = a.work / 'index'
index.mkdir()
# A PEP 503 simple index; only our freshly generated artifacts are visible.
for wheel in a.dist.glob('*.whl'):
    name = wheel.name.split('-')[0].replace('_', '-')
    project = index / 'simple' / name
    project.mkdir(parents=True, exist_ok=True)
    shutil.copy2(wheel, index / wheel.name)
    (project / 'index.html').write_text(f'<a href="../../{wheel.name}">{wheel.name}</a>\n')
handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory=str(index))
http = http.server.ThreadingHTTPServer(('127.0.0.1', 0), handler)
threading.Thread(target=http.serve_forever, daemon=True).start()
try:
    for _ in range(100):
        try:
            urllib.request.urlopen(url + '/-/ping', timeout=1).close()
            break
        except OSError:
            if server.poll() is not None:
                raise RuntimeError('Verdaccio exited; see verdaccio.log')
            time.sleep(.1)
    else:
        raise RuntimeError('Verdaccio did not start')
    auth = f'--//127.0.0.1:{port}/:_authToken=private-test'
    patterns = [f'voyager-so-vork-{t}-*.tgz' for t in ['linux-x64', 'darwin-arm64', 'windows-x64'] if list(a.dist.glob(f'voyager-so-vork-{t}-*.tgz'))]
    if f'voyager-so-vork-{target}-*.tgz' not in patterns:
        raise RuntimeError('host native artifact missing')
    for pattern in patterns + ['voyager-so-vork-[0-9]*.tgz', 'create-vork-*.tgz']:
        files = list(a.dist.glob(pattern))
        if len(files) != 1:
            raise RuntimeError(f'expected one artifact: {pattern}')
        run([npm, 'publish', files[0], *registry, '--ignore-scripts', '--access', 'public', auth])
    project = a.work / 'project'
    project.mkdir()
    if a.init:
        run([npm, 'create', 'vork', 'starter project', *registry, '--yes', '--', '--language', 'typescript'], a.work)
        run([npm, 'exec', '--offline', '--', 'vork', 'help'], a.work / 'starter project')
        run([shutil.which('node'), 'scene.ts'], a.work / 'starter project')
    run([npm, 'init', '-y'], project)
    run([npm, 'install', '@voyager-so/vork', *registry, '--ignore-scripts'], project)
    installed = sorted(p.name for p in (project / 'node_modules/@voyager-so').iterdir())
    assert installed == sorted(['vork', 'vork-' + target]), installed
    if len(patterns) == 3:
        shutil.rmtree(project / 'node_modules')
        run([npm, 'ci', *registry, '--ignore-scripts'], project)
    else:
        print('npm ci portability check needs all three native tarballs; host install only.', flush=True)
    run([sys.executable, '-m', 'venv', project / '.venv'])
    python = project / '.venv' / ('Scripts/python.exe' if os.name == 'nt' else 'bin/python')
    run([python, '-m', 'pip', 'install', '--index-url', f'http://127.0.0.1:{http.server_address[1]}/simple/', '--no-cache-dir', 'vork'], project)
    run([python, ROOT / 'scripts/test-registry-install', *(['--gpu'] if a.gpu else [])], project)
    if a.init:
        run([npm, 'exec', '--offline', '--', 'vork', 'init', 'python starter', '--language', 'python'], project)
        run([python, 'scene.py'], project / 'python starter')
        # Kill the sole npm server: prove cached project creation has no network fallback.
        server.terminate()
        server.wait(timeout=15)
        run([npm, 'create', 'vork', 'offline starter', *registry, '--offline', '--yes'], a.work)
        run([npm, 'exec', '--offline', '--', 'vork', 'help'], a.work / 'offline starter')
    if sys.platform == 'darwin':
        # Verify both package-manager extractions, including every dylib and slangc.
        for root in [project / 'node_modules/@voyager-so', project / '.venv']:
            for file in root.rglob('*'):
                if file.is_file() and 'runtime' in file.relative_to(root).parts and (file.name in ('vork', 'slangc') or file.suffix == '.dylib'):
                    run(['codesign', '--verify', '--strict', '--verbose=2', file])
                    identity = subprocess.run(['codesign', '-dv', '--verbose=4', str(file)], capture_output=True, text=True, check=True)
                    print(identity.stderr, flush=True)
                    if 'Authority=Developer ID Application:' not in identity.stderr:
                        raise RuntimeError(f'not Developer ID signed: {file}')
finally:
    server.terminate()
    server.wait(timeout=15)
    server_log.close()
    http.shutdown()