Update SnapForm to version 0.10.10 and add technical documentation

- Bump SnapForm version in uv.lock from 0.9.30 to 0.10.10.
- Introduce comprehensive technical documentation for SnapForm, detailing features, usage instructions, and development guidelines.
This commit is contained in:
2026-08-08 19:05:42 +08:00
parent d08d0e9c1a
commit aee460fe7f
22 changed files with 3905 additions and 1472 deletions

View File

@@ -1,7 +1,9 @@
import importlib.util
import io
import json
import os
import subprocess
import tarfile
import tempfile
import time
import unittest
@@ -25,12 +27,24 @@ def load_bootstrap_runtime():
class BootstrapRuntimeTest(unittest.TestCase):
def create_bundled_sources(self, root):
sources = root / "sources"
sources.mkdir()
(sources / "depth-anything-3.tar.gz").write_bytes(b"da3")
(sources / "ben2.tar.gz").write_bytes(b"ben2")
return sources
def test_installs_da3_from_archives_without_gsplat(self):
bootstrap = load_bootstrap_runtime()
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
runtime_dir = root / "runtime"
runtime_dir.mkdir()
self.create_bundled_sources(root)
da3_uri = (
root / "sources" / "depth-anything-3.tar.gz"
).as_uri()
ben2_uri = (root / "sources" / "ben2.tar.gz").as_uri()
uv_path = root / "uv"
uv_path.write_bytes(b"uv")
args = SimpleNamespace(
@@ -42,7 +56,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
ben2_commit="test-ben2-commit",
status=str(root / "status.json"),
)
with patch.object(bootstrap, "preflight", return_value=({}, False)), patch.object(
with patch.object(bootstrap, "resolve_indexes", return_value=None), patch.object(
bootstrap,
"run",
) as mocked_run, patch.object(
@@ -64,13 +78,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
f"addict=={bootstrap.ADDICT_VERSION}",
da3_command,
)
self.assertTrue(
any(
"https://github.com/ByteDance-Seed/Depth-Anything-3/"
"archive/test-commit.tar.gz" in str(value)
for value in da3_command
)
)
self.assertIn(f"depth-anything-3 @ {da3_uri}", da3_command)
self.assertFalse(
any("git+" in str(value) for value in da3_command)
)
@@ -93,13 +101,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
for command in commands
if any("ben2 @" in str(value) for value in command)
)
self.assertTrue(
any(
"https://github.com/PramaLLC/BEN2/archive/"
"test-ben2-commit.tar.gz" in str(value)
for value in ben2_command
)
)
self.assertIn(f"ben2 @ {ben2_uri}", ben2_command)
self.assertFalse(
any("git+" in str(value) for value in ben2_command)
)
@@ -162,6 +164,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
root = Path(directory)
runtime_dir = root / "runtime"
runtime_dir.mkdir()
self.create_bundled_sources(root)
uv_path = root / "uv"
uv_path.write_bytes(b"uv")
args = SimpleNamespace(
@@ -181,7 +184,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
bootstrap.platform,
"machine",
return_value="arm64",
), patch.object(bootstrap, "preflight", return_value=({}, False)), patch.object(
), patch.object(bootstrap, "resolve_indexes", return_value=None), patch.object(
bootstrap,
"run",
), patch.object(
@@ -329,6 +332,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
root = Path(directory)
runtime_dir = root / "runtime"
runtime_dir.mkdir()
self.create_bundled_sources(root)
uv_path = root / "uv"
uv_path.write_bytes(b"uv")
args = SimpleNamespace(
@@ -364,7 +368,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
)
return []
with patch.object(bootstrap, "preflight", return_value=({}, False)), patch.object(
with patch.object(bootstrap, "resolve_indexes", return_value=None), patch.object(
bootstrap,
"run_stage",
side_effect=fake_run_stage,
@@ -425,7 +429,8 @@ class BootstrapRuntimeTest(unittest.TestCase):
"3/3 packages",
],
)
self.assertEqual(mocked_write.call_args_list[-1].args[1], 0.02)
self.assertEqual(mocked_write.call_args_list[0].args[1], 0.02)
self.assertLess(mocked_write.call_args_list[-1].args[1], 0.03)
first = mocked_write.call_args_list[0]
self.assertEqual(first.kwargs["stage"], 2)
self.assertEqual(first.kwargs["stages"], 8)
@@ -525,146 +530,79 @@ class BootstrapRuntimeTest(unittest.TestCase):
self.assertEqual(first.kwargs["stages"], 8)
self.assertEqual(first.kwargs["stage_label"], "pytorch deps")
def test_run_install_retries_once_with_mirror(self):
bootstrap = load_bootstrap_runtime()
previous = bootstrap._resolved_pypi_index
bootstrap._resolved_pypi_index = None
try:
with patch.object(
bootstrap,
"_run_install_once",
side_effect=[
subprocess.CalledProcessError(1, ["uv", "pip"]),
["numpy==1.26.4"],
],
) as mocked_install:
installed = bootstrap.run_install(
"status.json",
3,
8,
"ai deps",
["uv", "pip", "install", "x"],
)
finally:
bootstrap._resolved_pypi_index = previous
self.assertEqual(installed, ["numpy==1.26.4"])
self.assertEqual(mocked_install.call_count, 2)
self.assertEqual(
mocked_install.call_args_list[1].args[5],
{"PIP_INDEX_URL": bootstrap.PYPI_MIRRORS[0]},
)
def test_formats_elapsed_time(self):
bootstrap = load_bootstrap_runtime()
self.assertEqual(bootstrap.format_elapsed(12), "12s")
self.assertEqual(bootstrap.format_elapsed(83), "1m 23s")
def test_preflight_rejects_low_disk_space(self):
def test_resolve_indexes_uses_configured_pypi_index(self):
bootstrap = load_bootstrap_runtime()
args = SimpleNamespace(
backend="cu128",
runtime_dir=str(Path.cwd()),
bootstrap.resolve_indexes(
SimpleNamespace(pypi_index="https://pypi.tuna.tsinghua.edu.cn/simple")
)
with patch.object(
bootstrap.shutil,
"disk_usage",
return_value=SimpleNamespace(free=1024**3),
), patch.object(bootstrap, "reachable", return_value=True):
with self.assertRaisesRegex(RuntimeError, "disk space"):
bootstrap.preflight(args)
def test_preflight_rejects_unreachable_network(self):
bootstrap = load_bootstrap_runtime()
args = SimpleNamespace(
backend="cu128",
runtime_dir=str(Path.cwd()),
)
with patch.object(
bootstrap.shutil,
"disk_usage",
return_value=SimpleNamespace(free=100 * 1024**3),
), patch.object(bootstrap, "reachable", return_value=False):
with self.assertRaisesRegex(RuntimeError, "Cannot reach"):
bootstrap.preflight(args)
def test_preflight_passes_when_checks_ok(self):
bootstrap = load_bootstrap_runtime()
args = SimpleNamespace(
backend="cu128",
runtime_dir=str(Path.cwd()),
)
with patch.object(
bootstrap.shutil,
"disk_usage",
return_value=SimpleNamespace(free=100 * 1024**3),
), patch.object(bootstrap, "reachable", return_value=True):
resolved, used_mirror = bootstrap.preflight(args)
self.assertFalse(used_mirror)
self.assertEqual(
resolved["pypi"],
"https://pypi.org/simple/",
)
def test_preflight_falls_back_to_mirrors(self):
bootstrap = load_bootstrap_runtime()
args = SimpleNamespace(
backend="cu128",
runtime_dir=str(Path.cwd()),
)
def fake_reachable(url, timeout=bootstrap.PREFLIGHT_TIMEOUT):
if "github.com" in url:
return True
if "tuna" in url or "hf-mirror" in url or "aliyun" in url:
return True
return False
with patch.object(
bootstrap.shutil,
"disk_usage",
return_value=SimpleNamespace(free=100 * 1024**3),
), patch.object(
bootstrap,
"reachable",
side_effect=fake_reachable,
):
resolved, used_mirror = bootstrap.preflight(args)
self.assertTrue(used_mirror)
self.assertEqual(
resolved["pypi"],
bootstrap._resolved_pypi_index,
"https://pypi.tuna.tsinghua.edu.cn/simple",
)
self.assertEqual(resolved["hf"], "https://hf-mirror.com")
self.assertEqual(
resolved["torch"],
"https://mirrors.aliyun.com/pytorch-wheels/cu128",
)
self.assertEqual(
bootstrap._resolved_torch_index,
"https://mirrors.aliyun.com/pytorch-wheels/cu128",
)
def test_preflight_uses_environment_overrides(self):
def test_resolve_indexes_uses_environment_override(self):
bootstrap = load_bootstrap_runtime()
args = SimpleNamespace(
backend="cu128",
runtime_dir=str(Path.cwd()),
)
with patch.dict(
os.environ,
{
"PIP_INDEX_URL": "https://pypi.example/simple",
"HF_ENDPOINT": "https://hf.example",
"SNAPFORM_TORCH_INDEX": "https://torch.example/whl/cu128",
},
), patch.object(
bootstrap.shutil,
"disk_usage",
return_value=SimpleNamespace(free=100 * 1024**3),
), patch.object(bootstrap, "reachable", return_value=True):
resolved, used_mirror = bootstrap.preflight(args)
self.assertFalse(used_mirror)
self.assertEqual(resolved["pypi"], "https://pypi.example/simple")
self.assertEqual(resolved["hf"], "https://hf.example")
{"PIP_INDEX_URL": "https://pypi.example/simple"},
):
bootstrap.resolve_indexes(SimpleNamespace(pypi_index=None))
self.assertEqual(
resolved["torch"],
"https://torch.example/whl/cu128",
bootstrap._resolved_pypi_index,
"https://pypi.example/simple",
)
def test_child_environment_injects_resolved_mirrors(self):
def test_child_environment_injects_resolved_pypi_index(self):
bootstrap = load_bootstrap_runtime()
previous_pypi = bootstrap._resolved_pypi_index
previous_hf = bootstrap._resolved_hf_endpoint
bootstrap._resolved_pypi_index = (
"https://pypi.tuna.tsinghua.edu.cn/simple"
)
bootstrap._resolved_hf_endpoint = "https://hf-mirror.com"
try:
env = bootstrap.child_environment()
finally:
bootstrap._resolved_pypi_index = previous_pypi
bootstrap._resolved_hf_endpoint = previous_hf
self.assertEqual(
env["PIP_INDEX_URL"],
"https://pypi.tuna.tsinghua.edu.cn/simple",
)
self.assertEqual(env["HF_ENDPOINT"], "https://hf-mirror.com")
def test_child_environment_injects_registry_proxy(self):
bootstrap = load_bootstrap_runtime()
@@ -700,6 +638,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
root = Path(directory)
runtime_dir = root / "runtime"
runtime_dir.mkdir()
self.create_bundled_sources(root)
uv_path = root / "uv"
uv_path.write_bytes(b"uv")
args = SimpleNamespace(
@@ -711,7 +650,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
ben2_commit="test-ben2-commit",
status=str(root / "status.json"),
)
with patch.object(bootstrap, "preflight", return_value=({}, False)), patch.object(
with patch.object(bootstrap, "resolve_indexes", return_value=None), patch.object(
bootstrap,
"run",
), patch.object(
@@ -737,6 +676,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
root = Path(directory)
runtime_dir = root / "runtime"
runtime_dir.mkdir()
self.create_bundled_sources(root)
(runtime_dir / "partial-python.exe").write_text("junk")
args = SimpleNamespace(
uv="uv",
@@ -747,7 +687,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
ben2_commit="test-ben2-commit",
status=str(root / "status.json"),
)
with patch.object(bootstrap, "preflight", return_value=({}, False)), patch.object(
with patch.object(bootstrap, "resolve_indexes", return_value=None), patch.object(
bootstrap,
"run",
), patch.object(
@@ -765,6 +705,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
root = Path(directory)
runtime_dir = root / "runtime"
runtime_dir.mkdir(parents=True)
self.create_bundled_sources(root)
(runtime_dir / "Scripts").mkdir()
(runtime_dir / "Scripts" / "python.exe").write_text("python")
(runtime_dir / "snapform_ready.json").write_text(
@@ -781,7 +722,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
ben2_commit="test-ben2-commit",
status=str(root / "status.json"),
)
with patch.object(bootstrap, "preflight", return_value=({}, False)), patch.object(
with patch.object(bootstrap, "resolve_indexes", return_value=None), patch.object(
bootstrap,
"run",
), patch.object(
@@ -803,6 +744,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
root = Path(directory)
runtime_dir = root / "runtime"
runtime_dir.mkdir()
self.create_bundled_sources(root)
args = SimpleNamespace(
uv="uv",
runtime_dir=str(runtime_dir),
@@ -812,7 +754,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
ben2_commit="test-ben2-commit",
status=str(root / "status.json"),
)
with patch.object(bootstrap, "preflight", return_value=({}, False)), patch.object(
with patch.object(bootstrap, "resolve_indexes", return_value=None), patch.object(
bootstrap,
"run",
side_effect=fake_run,
@@ -826,12 +768,13 @@ class BootstrapRuntimeTest(unittest.TestCase):
self.assertEqual(status["progress"], 1.0)
self.assertIn("Runtime is ready", status["message"])
def test_install_downloads_missing_uv(self):
def test_install_fails_when_bundled_uv_missing(self):
bootstrap = load_bootstrap_runtime()
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
runtime_dir = root / "runtime"
runtime_dir.mkdir()
self.create_bundled_sources(root)
uv_path = root / "uv" / "uv.exe"
args = SimpleNamespace(
uv=str(uv_path),
@@ -842,7 +785,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
ben2_commit="test-ben2-commit",
status=str(root / "status.json"),
)
with patch.object(bootstrap, "preflight", return_value=({}, False)), patch.object(
with patch.object(bootstrap, "resolve_indexes", return_value=None), patch.object(
bootstrap,
"run",
), patch.object(
@@ -855,15 +798,101 @@ class BootstrapRuntimeTest(unittest.TestCase):
), patch.object(
bootstrap.shutil,
"rmtree",
), patch.object(bootstrap, "download_uv") as mocked_download:
):
with self.assertRaisesRegex(
RuntimeError,
"Bundled uv is missing",
):
bootstrap.install(args)
def test_install_uses_bundled_sources_and_uv_without_network(self):
bootstrap = load_bootstrap_runtime()
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
runtime_dir = root / "runtime"
runtime_dir.mkdir()
bundled = root / "bundled"
bundled.mkdir()
(bundled / "depth-anything-3.tar.gz").write_bytes(b"da3")
(bundled / "ben2.tar.gz").write_bytes(b"ben2")
target, archive_name = bootstrap.uv_download_target()
archive = bundled / f"uv-{target}.{archive_name}"
if archive_name.endswith("zip"):
with zipfile.ZipFile(archive, "w") as bundle:
bundle.writestr("uv/uv.exe", b"uv-binary")
else:
with tarfile.open(archive, "w:gz") as tar:
content = b"uv-binary"
info = tarfile.TarInfo("uv")
info.size = len(content)
tar.addfile(info, io.BytesIO(content))
uv_path = root / "uv" / ("uv.exe" if os.name == "nt" else "uv")
args = SimpleNamespace(
uv=str(uv_path),
runtime_dir=str(runtime_dir),
models_dir=str(root / "models"),
backend="cu128",
da3_commit="test-commit",
ben2_commit="test-ben2-commit",
status=str(root / "status.json"),
)
run_commands = []
install_commands = []
def fake_run(command, environment=None):
run_commands.append(command)
def fake_run_install(
status_path,
stage,
stages,
label,
command,
environment=None,
start=0.0,
end=1.0,
clock=None,
):
install_commands.append(command)
return []
with patch.object(
bootstrap,
"bundled_sources_directory",
return_value=bundled,
), patch.object(
bootstrap,
"resolve_indexes",
return_value=None,
), patch.object(
bootstrap,
"run",
side_effect=fake_run,
), patch.object(
bootstrap,
"run_install",
side_effect=fake_run_install,
):
bootstrap.install(args)
mocked_download.assert_called_once()
call_args = mocked_download.call_args
self.assertEqual(call_args.args[0], uv_path)
self.assertEqual(call_args.args[1], str(root / "status.json"))
self.assertEqual(call_args.kwargs["stage"], 1)
self.assertEqual(call_args.kwargs["stage_label"], "uv")
self.assertEqual(uv_path.read_bytes(), b"uv-binary")
all_commands = run_commands + install_commands
self.assertFalse(
any(
"github.com" in str(value)
for command in all_commands
for value in command
)
)
ai_command = next(
command
for command in install_commands
if any("depth-anything-3 @" in str(value) for value in command)
)
da3_uri = (root / "sources" / "depth-anything-3.tar.gz").as_uri()
ben2_uri = (root / "sources" / "ben2.tar.gz").as_uri()
self.assertIn(f"depth-anything-3 @ {da3_uri}", ai_command)
self.assertIn(f"ben2 @ {ben2_uri}", ai_command)
def test_install_reuses_existing_uv(self):
bootstrap = load_bootstrap_runtime()
@@ -871,6 +900,7 @@ class BootstrapRuntimeTest(unittest.TestCase):
root = Path(directory)
runtime_dir = root / "runtime"
runtime_dir.mkdir()
self.create_bundled_sources(root)
uv_path = root / "uv.exe"
uv_path.write_bytes(b"uv")
args = SimpleNamespace(
@@ -882,17 +912,15 @@ class BootstrapRuntimeTest(unittest.TestCase):
ben2_commit="test-ben2-commit",
status=str(root / "status.json"),
)
with patch.object(bootstrap, "preflight", return_value=({}, False)), patch.object(
with patch.object(bootstrap, "resolve_indexes", return_value=None), patch.object(
bootstrap,
"run",
), patch.object(
bootstrap,
"run_install",
), patch.object(bootstrap, "download_uv") as mocked_download:
):
bootstrap.install(args)
mocked_download.assert_not_called()
def test_extract_uv_unpacks_standalone_binary(self):
bootstrap = load_bootstrap_runtime()
with tempfile.TemporaryDirectory() as directory:
@@ -906,6 +934,112 @@ class BootstrapRuntimeTest(unittest.TestCase):
self.assertEqual(uv_path.read_bytes(), b"binary")
def test_sync_bundled_sources_copies_missing_archives(self):
bootstrap = load_bootstrap_runtime()
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
bundled = root / "bundled"
bundled.mkdir()
(bundled / "depth-anything-3.tar.gz").write_bytes(b"da3")
(bundled / "ben2.tar.gz").write_bytes(b"ben2")
sources = root / "sources"
bootstrap.sync_bundled_sources(bundled, sources)
self.assertEqual(
(sources / "depth-anything-3.tar.gz").read_bytes(),
b"da3",
)
self.assertEqual(
(sources / "ben2.tar.gz").read_bytes(),
b"ben2",
)
def test_install_stages_uses_local_source_archives(self):
bootstrap = load_bootstrap_runtime()
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
runtime_dir = root / "runtime"
runtime_dir.mkdir()
sources = root / "sources"
sources.mkdir()
(sources / "depth-anything-3.tar.gz").write_bytes(b"da3")
(sources / "ben2.tar.gz").write_bytes(b"ben2")
uv_path = root / "uv"
uv_path.write_bytes(b"uv")
args = SimpleNamespace(
uv=str(uv_path),
runtime_dir=str(runtime_dir),
models_dir=str(root / "models"),
backend="cu128",
da3_commit="test-commit",
ben2_commit="test-ben2-commit",
status=str(root / "status.json"),
)
stages = bootstrap.install_stages(
args,
runtime_dir,
root / "python",
uv_path,
)
ai_command = next(
command
for _label, command, _environment, kind in stages
if kind == "install"
and any("depth-anything-3" in str(value) for value in command)
)
da3_local = (sources / "depth-anything-3.tar.gz").as_uri()
ben2_local = (sources / "ben2.tar.gz").as_uri()
self.assertIn(f"depth-anything-3 @ {da3_local}", ai_command)
self.assertIn(f"ben2 @ {ben2_local}", ai_command)
def test_install_uv_extracts_bundled_archive(self):
bootstrap = load_bootstrap_runtime()
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
uv_path = root / "uv"
bundled = root / "bundled"
bundled.mkdir(parents=True)
target, archive_name = bootstrap.uv_download_target()
archive = bundled / f"uv-{target}.{archive_name}"
if archive_name.endswith("zip"):
with zipfile.ZipFile(archive, "w") as bundle:
bundle.writestr("uv/uv.exe", b"uv-binary")
else:
with tarfile.open(archive, "w:gz") as tar:
content = b"uv-binary"
info = tarfile.TarInfo("uv")
info.size = len(content)
tar.addfile(info, io.BytesIO(content))
with patch.object(
bootstrap,
"bundled_sources_directory",
return_value=bundled,
):
bootstrap.install_uv(uv_path, str(root / "status.json"))
self.assertEqual(uv_path.read_bytes(), b"uv-binary")
def test_install_uv_raises_when_bundled_missing(self):
bootstrap = load_bootstrap_runtime()
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
uv_path = root / "uv"
bundled = root / "bundled"
bundled.mkdir()
with patch.object(
bootstrap,
"bundled_sources_directory",
return_value=bundled,
):
with self.assertRaisesRegex(
RuntimeError,
"Bundled uv is missing",
):
bootstrap.install_uv(uv_path, str(root / "status.json"))
if __name__ == "__main__":
unittest.main()