blob: ace9f342aec4f25e3b4321e7ba316c1eea3f6bee [file] [view]
# Fuchsia Controller
`fuchsia-controller` (`fuchsia_controller_py`) is a host-side Python library for
interacting with Fuchsia target devices over FDomain/RCS and invoking FIDL
protocols directly from Python.
## Building Python Wheels for Interactive & Virtualenv Usage
`//src/developer/fuchsia-controller/BUILD.gn` defines PEP 427 Python wheel
targets that package `fuchsia-controller` and the Fuchsia FIDL SDK (`partner_idk`,
`prebuilt`, and `host_tool` FIDL libraries) so they can be installed into any
virtual environment pinned to `fuchsia-vendored-python`:
* `//src/developer/fuchsia-controller:fuchsia_controller_combined_wheel` (alias
`:fuchsia_controller_with_fidl_wheel`): Builds a single self-contained
`fuchsia_controller-*.whl` containing `fuchsia_controller_py`,
`fuchsia_controller_internal`, `fidl`, and all ~270 SDK `fidl_fuchsia_*`
Python modules.
* `//src/developer/fuchsia-controller:fuchsia_controller_wheel` and
`//src/developer/fuchsia-controller:fidl_sdk_wheel` (alias `:fidl_wheel`):
Separate wheels if you prefer to install `fuchsia-controller` and `fidl-sdk`
independently.
Build the combined wheel once:
```sh
fx build --host //src/developer/fuchsia-controller:fuchsia_controller_combined_wheel
```
The generated wheel is placed at:
```text
$(fx get-build-dir)/host_x64/obj/src/developer/fuchsia-controller/fuchsia_controller_combined_wheel/fuchsia_controller-0.1.0-*.whl
```
## Setting Up a Virtual Environment (`uv` or `venv`)
Because `fuchsia_controller_internal` is a native C++ extension compiled against
Fuchsia's prebuilt Python interpreter (`fuchsia-vendored-python`), the virtual
environment **must** use Fuchsia's vendored Python binary rather than the system
`/usr/bin/python3`.
### Option 1: Using `uv` (Recommended)
```sh
FUCHSIA_PYTHON="$(fx get-workspace-dir)/prebuilt/third_party/python3/linux-x64/bin/python3"
WHEEL_DIR="$(fx get-build-dir)/host_x64/obj/src/developer/fuchsia-controller/fuchsia_controller_combined_wheel"
# Create a persistent virtual environment and install the combined wheel:
uv venv /tmp/fc_venv --python "$FUCHSIA_PYTHON"
uv pip install --python /tmp/fc_venv/bin/python \
--no-index --find-links "$WHEEL_DIR" fuchsia-controller
# Launch an interactive Python REPL (python -m asyncio for top-level await):
/tmp/fc_venv/bin/python -m asyncio
# Or run a standalone script ephemerally in a single command without creating a named venv:
uv run --no-project --python "$FUCHSIA_PYTHON" \
--no-index --find-links "$WHEEL_DIR" \
--with fuchsia-controller my_script.py
```
### Option 2: Using `fuchsia-vendored-python -m venv`
```sh
WHEEL_DIR="$(fx get-build-dir)/host_x64/obj/src/developer/fuchsia-controller/fuchsia_controller_combined_wheel"
fuchsia-vendored-python -m venv /tmp/fc_venv
/tmp/fc_venv/bin/pip install --no-index --find-links "$WHEEL_DIR" fuchsia-controller
# Launch an interactive async REPL or run a script:
/tmp/fc_venv/bin/python -m asyncio
/tmp/fc_venv/bin/python my_script.py
```
## Interactive & Standalone Script Example
By default, `Context()` runs in ffx strict mode (`ffx --strict`): it uses only
the explicitly provided `config` with no isolate directory or ambient
environment lookups, and creates a local testing FDomain when `target=None`
without `target.default` in `config`. For non-strict discovery with a temporary
`IsolateDir`, use `Context.isolated()` (or `Context.detect()` to auto-detect the
ambient `ffx` environment, though note `Context.detect()` reads ambient build
and user config from the current working directory). The example below uses
`Context.isolated()` so standalone scripts stay isolated from ambient build/repo
state while explicitly forwarding `.ffx_user_config.json` settings:
```python
import asyncio
import json
import os
import pathlib
import subprocess
import sys
from typing import Any
from fuchsia_controller_py import Context
import fidl_fuchsia_buildinfo as f_buildinfo
import fidl_fuchsia_developer_remotecontrol as f_remotecontrol
def _user_ffx_config_path() -> pathlib.Path:
if sys.platform == "darwin":
base = pathlib.Path.home() / "Library/Preferences"
else:
base = pathlib.Path(
os.environ.get("XDG_CONFIG_HOME")
or pathlib.Path.home() / ".local/share"
)
return base / "Fuchsia/ffx/config/.ffx_user_config.json"
def create_device_context(target: str | None = None) -> Context:
"""Creates a Context preserving user-level ffx connectivity settings."""
user_cfg_path = _user_ffx_config_path()
config: dict[str, Any] = (
json.loads(user_cfg_path.read_text()) if user_cfg_path.exists() else {}
)
config.pop("target.default", None)
if isinstance(config.get("target"), dict):
config["target"].pop("default", None)
if not config["target"]:
config.pop("target", None)
else:
config.pop("target", None)
if not target:
out = subprocess.check_output(
["ffx", "target", "list", "--format", "json"], text=True
)
targets = json.loads(out)
defaults = [t for t in targets if t.get("is_default")]
chosen = defaults[0] if defaults else (targets[0] if targets else None)
if not chosen:
raise RuntimeError("No Fuchsia target found via `ffx target list`")
target = chosen["nodename"]
return Context.isolated(target=target, config=config)
async def main() -> None:
ctx = create_device_context()
# Query RemoteControl (RCS)
rcs = f_remotecontrol.RemoteControlClient(ctx.connect_remote_control_proxy())
ident = (await rcs.identify_host()).unwrap()
print(f"Connected to nodename: {ident.response.nodename}")
# Connect to any component capability by moniker and Marker
build_info = f_buildinfo.ProviderClient(
ctx.connect_device_proxy("core/build-info", f_buildinfo.ProviderMarker)
)
info = await build_info.get_build_info()
print(f"Product: {info.build_info.product_config}, Board: {info.build_info.board_config}")
if __name__ == "__main__":
asyncio.run(main())
```