Skip to main content

virtx for Python

A virtx client over the C ABI of libvirtx_core (virtx-core/include/virtx.h), reached with ctypes: the package has no extension module of its own, and a wheel carries the library for its platform beside its Python files.

The package also installs the virtx command, the one in libvirtx_core: uvx virtx images.

A build, pull or push of an image, and a process in a sandbox, is a job the server runs apart from the request that started it. The method that starts one answers a StartedJob, sent when it is first awaited, iterated or completed: complete follows the job to its end and answers what it ended with; iterated, it yields the job's log, each entry numbered so a reader can resume after one, until its end entry; awaited, it is the job. Stopping early leaves the job running; ImageJob.remove or SandboxJob.remove cancels it.

import asyncio
from virtx import Client, Recipe, VirtxError


async def main():
    with Client.spawn() as client:
        recipe = Recipe("alpine:3.20").run("apk add jq")
        try:
            digest = await client.build_image(recipe, "jq:latest").complete(print=True)
            print("built", digest)
        except VirtxError as failed:
            print("failed", failed)


asyncio.run(main())

complete(print=True) prints each line of the log as it is read. An image job that fails raises its VirtxError, from complete and from its log alike, in place of an ImageJobEnd.

Awaited, the same call answers the job, an ImageJob that carries its client, and its log is read after with its log, from its start or after an entry: by another reader, or after a reconnect.

job = await client.pull_image("alpine:3.20")
async for entry in job.log(after=1):  # from its second entry
    ...

A sandbox runs commands as jobs the same way: create_sandbox answers a Sandbox that carries its client, and its exec starts one: completed, it answers the exit code; iterated, it yields its output, a line as it is written, then how it ended. Awaited, it is a SandboxJob that carries its client too: what signals the process, writes to its stdin and reads its log again. One value awaited and then iterated reads the log of the one job it started.

from virtx import SandboxJobEnd, Stderr, Stdout

sandbox = await client.create_sandbox("alpine:3.20")
print("exited", await sandbox.exec(["uname", "-a"]).complete(print=True))
run = sandbox.exec(["echo", "hi"])
job = await run
print("started", job.id)
async for entry in run:
    match entry.entry:
        case Stdout(line) | Stderr(line):
            print(line)
        case SandboxJobEnd(code):
            print("exited", code)
await sandbox.remove()
cat = await sandbox.exec(["cat"], stdin=True)
await cat.write_stdin("hello\n", eof=True)
async for entry in cat.log():
    ...

A sandbox's port is reached through a PortForwarder, opened over the client with the url create_sandbox answered for it: an address on this machine's loopback, for any TCP client, until it is closed.

from virtx import PortForwarder

sandbox = await client.create_sandbox("alpine:3.20", ports=[8000])
with PortForwarder.open(client, sandbox.ports[0].url) as forwarder:
    print("reach it at", forwarder.address)

A volume is answered the same way, as a Volume that carries its client. It and a Sandbox read and write their files, with a path relative to their root. A sandbox's are its root as its processes see it, while its VM runs. A file is written and read whole, or read in part; a directory lists its entries and goes with everything in it.

volume = await client.create_volume("data")
await volume.create_dir("in")
await volume.write_file("in/notes.txt", b"hello")
for entry in await volume.read_dir("in"):
    print(entry.name, entry.metadata.size)
print(await volume.read_file("in/notes.txt"))

What acts on a sandbox, a volume or a sandbox job is its handle. One known only by its id or name is had again with get_sandbox, get_volume or a sandbox's job: await (await client.get_volume("data")).read_file("in/notes.txt").

The library is the one given to virtx.load(path), or $VIRTX_CORE_LIB, or the copy in the package, or the system loader's search for libvirtx_core. One process opens one, so another module sharing a client opens virtx.library_path() and takes the pointer from client.clone().into_raw().

Tests

uv run pytest

The tests build libvirtx_core with cargo first, and run against the real servers, which cargo xtask fixture builds and serves over a home of the run's own for them. They boot machines, so they need cargo xtask init and cargo xtask build kernel first.

Metadata

Release files for virtx 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for virtx 0.2.0
File Actions
virtx-0.2.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for virtx-0.2.0-py3-none-win_amd64.whl
virtx-0.2.0-py3-none-manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.28+ x86-64 Details for virtx-0.2.0-py3-none-manylinux_2_28_x86_64.whl
virtx-0.2.0-py3-none-manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.28+ ARM64 Details for virtx-0.2.0-py3-none-manylinux_2_28_aarch64.whl
virtx-0.2.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for virtx-0.2.0-py3-none-macosx_11_0_arm64.whl

Total release size: 18.2 MB

Release files / virtx-0.2.0-py3-none-win_amd64.whl

Download URL virtx-0.2.0-py3-none-win_amd64.whl
Size 4.2 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
83413b517106cb2b8c597953272207bac2f4165cfb811886730ec5881907065c
BLAKE2b-256 checksum
How to use checksums
b37418fce4fdf6cea987639528307da55ccf16c28b82a6e88d3e7cdbe899dcd2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / virtx-0.2.0-py3-none-manylinux_2_28_x86_64.whl

Download URL virtx-0.2.0-py3-none-manylinux_2_28_x86_64.whl
Size 4.8 MB
Tags Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
1601df1dd50f64eb9cd95a4f666d62ce42db716e09a4a80d7ad07ced210514cd
BLAKE2b-256 checksum
How to use checksums
86c4fac8e38584cf591153a1adee1a8479291581146daba9d32fbf947d32bfe6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / virtx-0.2.0-py3-none-manylinux_2_28_aarch64.whl

Download URL virtx-0.2.0-py3-none-manylinux_2_28_aarch64.whl
Size 4.8 MB
Tags Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
68c997eeb9a25ec9653f0e22970524fda0351f2d0bab7f6a9550a25864193654
BLAKE2b-256 checksum
How to use checksums
eaad7317617fd4669cb3a4350c62aac2bce73857fbdca97f81261f6c0a9ae54f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / virtx-0.2.0-py3-none-macosx_11_0_arm64.whl

Download URL virtx-0.2.0-py3-none-macosx_11_0_arm64.whl
Size 4.3 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4f5640710e9c932857b0d4eb559f8addb295bffbc84eb462a0a074f0ce7e8b55
BLAKE2b-256 checksum
How to use checksums
040f2374f3b1191d61ca31a6ab563e6c354955c175a0e689fc420746be764b5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for virtx 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page