workbox 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
workbox/ws.py ADDED
@@ -0,0 +1,101 @@
1
+ """WebSocket framing (RFC 6455) for the box gateway's /ssh route and `workbox connect`, standard library only.
2
+
3
+ Only what a byte pipe needs: binary frames, ping and pong, close. Clients mask what they send, servers don't.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ import asyncio
9
+ import base64
10
+ import hashlib
11
+ import os
12
+ import struct
13
+
14
+ GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
15
+ BINARY, CLOSE, PING, PONG = 0x2, 0x8, 0x9, 0xA
16
+ MAX_FRAME = 1 << 20
17
+
18
+
19
+ def accept_key(key: str) -> str:
20
+ """The Sec-WebSocket-Accept answer to a client's Sec-WebSocket-Key."""
21
+ return base64.b64encode(hashlib.sha1((key + GUID).encode()).digest()).decode()
22
+
23
+
24
+ def new_key() -> str:
25
+ return base64.b64encode(os.urandom(16)).decode()
26
+
27
+
28
+ def frame(opcode: int, payload: bytes, mask: bool) -> bytes:
29
+ head = bytes([0x80 | opcode])
30
+ length = len(payload)
31
+ bit = 0x80 if mask else 0
32
+ if length < 126:
33
+ head += bytes([bit | length])
34
+ elif length < 1 << 16:
35
+ head += bytes([bit | 126]) + struct.pack("!H", length)
36
+ else:
37
+ head += bytes([bit | 127]) + struct.pack("!Q", length)
38
+ if not mask:
39
+ return head + payload
40
+ key = os.urandom(4)
41
+ return head + key + bytes(byte ^ key[index % 4] for index, byte in enumerate(payload))
42
+
43
+
44
+ async def read_frame(reader: asyncio.StreamReader) -> tuple[int, bytes]:
45
+ """One frame: its opcode and payload, unmasked. Raises IncompleteReadError when the stream ends."""
46
+ first, second = await reader.readexactly(2)
47
+ opcode = first & 0x0F
48
+ length = second & 0x7F
49
+ if length == 126:
50
+ (length,) = struct.unpack("!H", await reader.readexactly(2))
51
+ elif length == 127:
52
+ (length,) = struct.unpack("!Q", await reader.readexactly(8))
53
+ if length > MAX_FRAME:
54
+ raise ValueError("WebSocket frame too large")
55
+ key = await reader.readexactly(4) if second & 0x80 else None
56
+ payload = await reader.readexactly(length)
57
+ if key:
58
+ payload = bytes(byte ^ key[index % 4] for index, byte in enumerate(payload))
59
+ return opcode, payload
60
+
61
+
62
+ async def pipe_ws_to_stream(reader: asyncio.StreamReader, ws_writer: asyncio.StreamWriter, target: asyncio.StreamWriter, mask: bool) -> None:
63
+ """Writes the payloads of the WebSocket's data frames to `target`, answering pings, until it closes."""
64
+ try:
65
+ while True:
66
+ opcode, payload = await read_frame(reader)
67
+ if opcode == CLOSE:
68
+ break
69
+ if opcode == PING:
70
+ ws_writer.write(frame(PONG, payload, mask))
71
+ await ws_writer.drain()
72
+ continue
73
+ if opcode == PONG:
74
+ continue
75
+ target.write(payload)
76
+ await target.drain()
77
+ except (asyncio.IncompleteReadError, ConnectionError, ValueError):
78
+ pass
79
+ finally:
80
+ close(target)
81
+
82
+
83
+ async def pipe_stream_to_ws(source: asyncio.StreamReader, ws_writer: asyncio.StreamWriter, mask: bool) -> None:
84
+ """Sends what `source` gives as binary frames, then a close frame."""
85
+ try:
86
+ while chunk := await source.read(1 << 16):
87
+ ws_writer.write(frame(BINARY, chunk, mask))
88
+ await ws_writer.drain()
89
+ ws_writer.write(frame(CLOSE, b"", mask))
90
+ await ws_writer.drain()
91
+ except ConnectionError:
92
+ pass
93
+ finally:
94
+ close(ws_writer)
95
+
96
+
97
+ def close(writer: asyncio.StreamWriter) -> None:
98
+ try:
99
+ writer.close()
100
+ except Exception: # noqa: BLE001 - already closed
101
+ pass
@@ -0,0 +1,62 @@
1
+ Metadata-Version: 2.5
2
+ Name: workbox
3
+ Version: 0.2.0
4
+ Summary: Workbox: SSH into your GPU boxes, and log metrics and samples from training jobs.
5
+ Classifier: Environment :: Console
6
+ Classifier: Operating System :: OS Independent
7
+ Classifier: Programming Language :: Python :: 3
8
+ Requires-Python: >=3.10
9
+ Description-Content-Type: text/markdown
10
+
11
+ # workbox
12
+
13
+ The Python package for Workbox: GPU boxes with Jupyter, VS Code, a terminal
14
+ and SSH, and training jobs that keep everything they make.
15
+
16
+ ```bash
17
+ pip install workbox # or: uv tool install workbox
18
+ ```
19
+
20
+ It has no dependencies, and works on Python 3.10 and later.
21
+
22
+ ## SSH into your boxes
23
+
24
+ Sign in once on each computer. It opens your browser to approve the computer, makes a key for Workbox and adds
25
+ `Host *.workbox` to `~/.ssh/config`:
26
+
27
+ ```bash
28
+ workbox login --url https://your-workbox
29
+ ```
30
+
31
+ Then connect to a running box by its name, from the terminal or from your editor:
32
+
33
+ ```bash
34
+ workbox boxes # your boxes and their names
35
+ workbox ssh tinystories # or: ssh tinystories.workbox
36
+ scp data.csv tinystories.workbox:~/work/
37
+ ```
38
+
39
+ In VS Code or Cursor, use Remote-SSH and connect to `tinystories.workbox`. A running job's machine is
40
+ `<project>-job-<number>.workbox`, for example `workbox ssh tinystories-job-42`.
41
+
42
+ Each connection gets a certificate for that one box, valid for 12 hours, and a one-use pass through the box's HTTPS
43
+ port. No port is left open and no key stays on the box. `workbox logout` signs the computer out; you can also sign
44
+ computers out from your account in Workbox. You need OpenSSH (`ssh` and `ssh-keygen`), which macOS, Linux and
45
+ Windows 10 or later have.
46
+
47
+ ## Metrics and samples from your training code
48
+
49
+ ```python
50
+ import workbox
51
+
52
+ workbox.log(step=step, loss=loss, lr=lr) # live charts on the job's page
53
+ workbox.sample(step=step, text=story) # or image="plot.png", or a PIL image
54
+ ```
55
+
56
+ Inside a Workbox job these are reported as the job runs. Anywhere else (your laptop, Colab) they do nothing, so the
57
+ same code runs everywhere. They never raise: a training run never fails over a chart.
58
+
59
+ ## What else is in it
60
+
61
+ The same package runs inside every Workbox image: `workbox-runner` runs a job and reports to Workbox, and
62
+ `workbox-box` runs a box (its gateway, tools and SSH). You don't run those yourself.
@@ -0,0 +1,10 @@
1
+ workbox/__init__.py,sha256=jLsExK5aCOVXwxuazZ7WdYk5hdrxKL1TgsucNqOe10c,3774
2
+ workbox/box.py,sha256=D5Q2Zg96Ye7lHkXCJsPgiswKMgLn2YqwFpow_M0Bev8,10718
3
+ workbox/cli.py,sha256=Gc1RNGmkHbYFLNNvw9dKTc2UflEQ0qmmI5AYpmHg9n4,10315
4
+ workbox/gateway.py,sha256=xjB1vflrKtu5on70yIUukDj5l5VjEVCObDQiNHbKf5E,16640
5
+ workbox/runner.py,sha256=f7o9hapEfDB4LuCO36-aJ3u2el_YrSrP2dWmaYeQEhc,27123
6
+ workbox/ws.py,sha256=iNmpVKa1ASNZpWRE2fCSfeLJAJ6n313CnXb3yS6osZk,3472
7
+ workbox-0.2.0.dist-info/METADATA,sha256=Lfg8520yGJgTYxzkHA8uTu4TXpkyJn6-FSrCJq7OyDU,2307
8
+ workbox-0.2.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
9
+ workbox-0.2.0.dist-info/entry_points.txt,sha256=ZjK38nQQC1yUTYhtPtbLJU3EkuOxsdSQE064LhIkHE8,113
10
+ workbox-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,4 @@
1
+ [console_scripts]
2
+ workbox = workbox.cli:main
3
+ workbox-box = workbox.box:main
4
+ workbox-runner = workbox.runner:main