Skip to content

Properly manage assets (importlib.resources paths) #329

Description

@jatentaki

Current status

Even with #328, we're still importing assets via Path(__file__).resolve() / "relative" / "path" instead of the proper importlib.resources discovery. This is not yet known to be buggy but can easily cause hiccups in non-standard installs.

Fix

The proper pattern looks like below (note the context management is another layer of defensive programming as we have technically no guarantees about assets living for the whole duration of the deployment - perhaps in practice it's fine to just assume they do)

from contextlib import ExitStack
from importlib.resources import as_file, files

WEB_DIR_RESOURCE = files("omnidreams.webrtc").joinpath("web")


async def _close_package_resources(app: web.Application) -> None:
    app["package_resource_stack"].close()


def create_app(
    *,
    request_session_url: str,
    session_manager: WebRTCSessionManager | None = None,
) -> web.Application:
    manager = session_manager or OmnidreamsWebRTCSessionManager()

    resource_stack = ExitStack()
    web_dir = resource_stack.enter_context(as_file(WEB_DIR_RESOURCE))

    app = create_webrtc_app(
        web_dir=web_dir,
        session_manager=manager,
        preload_name="Omnidreams",
        request_session_url=request_session_url,
    )
    app["package_resource_stack"] = resource_stack
    app.on_cleanup.append(_close_package_resources)
    return app

Broader picture

More generally, we abuse Path(__file__) in multiple places, a quick list from codex is below

Likely Packaging Issues

  • integrations/omnidreams/omnidreams/webrtc/server.py REPO_ASSETS_DIR = Path(file).resolve().parents[4] / "assets" serves repo-level /assets/. This is the closest sibling to your current PR concern: outside a checkout, those logo/assets will disappear. Fix by bundling the needed assets under omnidreams.webrtc.web and serving them from package resources.

  • integrations/lingbot/lingbot/runner.py and integrations/lingbot/lingbot/webrtc/session.py Both derive repo root and default example data to assets/example_data/lingbot_world. This should probably be a cache dir, not repo root, unless the examples are intentionally only checkout-local.

  • integrations/hy_worldplay/hy_worldplay/runner.py Uses repo root for data_local/hy_worldplay. Same category: better as FLASHDREAMS_CACHE_DIR / user cache / explicit CLI path.

  • integrations/omnidreams/omnidreams/native/omnidreams_singleview.py Walks to sibling omnidreams_singleview sources. prepare.py explicitly documents this as source-checkout-only for perf/native paths, so it may be intentional, but it is not wheel-packaged runtime.

  • flashdreams/flashdreams/quality/video_quality/run_regression.py Defaults to configs/video_quality_cases.yml relative to CWD/repo root. Fine for repo tooling, fragile as installed CLI/library behavior.

Package-Local But Not Resource-Based

These use declared package data but still access it via file instead of importlib.resources:

  • integrations/omnidreams/omnidreams/webrtc/server.py for web/
  • integrations/omnidreams/omnidreams/interactive_drive/cli.py for configs/assets
  • integrations/lingbot/lingbot/webrtc/server.py for web/
  • integrations/flashvsr/flashvsr/corrector.py for CUDA csrc
  • integrations/omnidreams/ludus-renderer/ludus_renderer/* for _cpp JIT sources

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    Fields

    No fields configured for Task.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions