kdev-cli 0.1.3__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.
- kdev/__init__.py +7 -0
- kdev/__main__.py +3 -0
- kdev/api.py +408 -0
- kdev/auth.py +115 -0
- kdev/bootstrap.py +510 -0
- kdev/cli.py +162 -0
- kdev/cloudflared.py +179 -0
- kdev/commands/__init__.py +5 -0
- kdev/commands/_common.py +51 -0
- kdev/commands/account.py +148 -0
- kdev/commands/box.py +694 -0
- kdev/commands/doctor.py +127 -0
- kdev/commands/home.py +92 -0
- kdev/commands/settings.py +164 -0
- kdev/commands/tunnel.py +78 -0
- kdev/commands/workspace.py +210 -0
- kdev/config.py +181 -0
- kdev/errors.py +25 -0
- kdev/kdev_box.py +538 -0
- kdev/notebook.py +261 -0
- kdev/persistence.py +314 -0
- kdev/quota.py +102 -0
- kdev/safety.py +72 -0
- kdev/session.py +284 -0
- kdev/sshcfg.py +72 -0
- kdev/tunnel.py +259 -0
- kdev/ui.py +666 -0
- kdev/wizard.py +251 -0
- kdev_cli-0.1.3.dist-info/METADATA +598 -0
- kdev_cli-0.1.3.dist-info/RECORD +33 -0
- kdev_cli-0.1.3.dist-info/WHEEL +4 -0
- kdev_cli-0.1.3.dist-info/entry_points.txt +2 -0
- kdev_cli-0.1.3.dist-info/licenses/LICENSE +21 -0
kdev/cloudflared.py
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""Locate, or fetch, the local `cloudflared` binary.
|
|
2
|
+
|
|
3
|
+
It is a Go binary, not a Python package, so it cannot be a project dependency.
|
|
4
|
+
There is no official PyPI distribution -- `pycloudflared` is an unofficial
|
|
5
|
+
one-person wrapper -- so kdev fetches Cloudflare's own release directly, the
|
|
6
|
+
same source Homebrew pulls from.
|
|
7
|
+
|
|
8
|
+
A system install always wins; the managed copy is a fallback so that
|
|
9
|
+
`uv tool install kdev-cli` leaves nothing for the user to go and install by hand.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import hashlib
|
|
15
|
+
import os
|
|
16
|
+
import platform
|
|
17
|
+
import shutil
|
|
18
|
+
import stat
|
|
19
|
+
import subprocess
|
|
20
|
+
import tarfile
|
|
21
|
+
import tempfile
|
|
22
|
+
import urllib.request
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
|
|
25
|
+
from .errors import KdevError
|
|
26
|
+
|
|
27
|
+
RELEASE = "https://github.com/cloudflare/cloudflared/releases/latest/download"
|
|
28
|
+
|
|
29
|
+
#: Kept on `latest` deliberately: bootstrap.py fetches `latest` on the Kaggle
|
|
30
|
+
#: side too, and a stale pinned client against a fresh connector is a real
|
|
31
|
+
#: failure mode. Override with KDEV_CLOUDFLARED_URL to pin a version.
|
|
32
|
+
_ASSETS = {
|
|
33
|
+
("Darwin", "arm64"): "cloudflared-darwin-arm64.tgz",
|
|
34
|
+
("Darwin", "x86_64"): "cloudflared-darwin-amd64.tgz",
|
|
35
|
+
("Linux", "x86_64"): "cloudflared-linux-amd64",
|
|
36
|
+
("Linux", "aarch64"): "cloudflared-linux-arm64",
|
|
37
|
+
("Linux", "arm64"): "cloudflared-linux-arm64",
|
|
38
|
+
("Linux", "armv7l"): "cloudflared-linux-arm",
|
|
39
|
+
("Windows", "AMD64"): "cloudflared-windows-amd64.exe",
|
|
40
|
+
("Windows", "x86_64"): "cloudflared-windows-amd64.exe",
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def bin_dir() -> Path:
|
|
45
|
+
root = os.environ.get("KDEV_DATA_DIR")
|
|
46
|
+
if root:
|
|
47
|
+
return Path(root) / "bin"
|
|
48
|
+
if platform.system() == "Windows":
|
|
49
|
+
base = Path(os.environ.get("LOCALAPPDATA", Path.home() / "AppData" / "Local"))
|
|
50
|
+
return base / "kdev" / "bin"
|
|
51
|
+
return Path(os.environ.get("XDG_DATA_HOME", Path.home() / ".local" / "share")) / "kdev" / "bin"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def managed_path() -> Path:
|
|
55
|
+
name = "cloudflared.exe" if platform.system() == "Windows" else "cloudflared"
|
|
56
|
+
return bin_dir() / name
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def asset_name() -> str:
|
|
60
|
+
key = (platform.system(), platform.machine())
|
|
61
|
+
if key not in _ASSETS:
|
|
62
|
+
raise KdevError(
|
|
63
|
+
f"No cloudflared build for {key[0]}/{key[1]}. "
|
|
64
|
+
"Install it yourself and put it on PATH: https://pkg.cloudflare.com"
|
|
65
|
+
)
|
|
66
|
+
return _ASSETS[key]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def find() -> Path | None:
|
|
70
|
+
"""System install first, then the copy kdev manages."""
|
|
71
|
+
system = shutil.which("cloudflared")
|
|
72
|
+
if system:
|
|
73
|
+
return Path(system)
|
|
74
|
+
managed = managed_path()
|
|
75
|
+
return managed if managed.exists() and os.access(managed, os.X_OK) else None
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def version(path: Path) -> str:
|
|
79
|
+
try:
|
|
80
|
+
out = subprocess.run([str(path), "--version"], capture_output=True, text=True, timeout=20)
|
|
81
|
+
return (out.stdout or out.stderr).strip().splitlines()[0]
|
|
82
|
+
except (OSError, subprocess.SubprocessError, IndexError):
|
|
83
|
+
return "unknown"
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def install(progress=None) -> Path:
|
|
87
|
+
"""Download the official release for this platform into kdev's data dir."""
|
|
88
|
+
asset = asset_name()
|
|
89
|
+
url = os.environ.get("KDEV_CLOUDFLARED_URL", f"{RELEASE}/{asset}")
|
|
90
|
+
dest = managed_path()
|
|
91
|
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
92
|
+
|
|
93
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
94
|
+
tmp_path = Path(tmp) / asset
|
|
95
|
+
_download(url, tmp_path, progress)
|
|
96
|
+
if asset.endswith(".tgz"):
|
|
97
|
+
# The macOS tarballs hold a single bare `cloudflared` at the root.
|
|
98
|
+
with tarfile.open(tmp_path) as tf:
|
|
99
|
+
member = next(
|
|
100
|
+
(
|
|
101
|
+
m
|
|
102
|
+
for m in tf.getmembers()
|
|
103
|
+
if m.isfile() and Path(m.name).name.startswith("cloudflared")
|
|
104
|
+
),
|
|
105
|
+
None,
|
|
106
|
+
)
|
|
107
|
+
if member is None:
|
|
108
|
+
raise KdevError(f"No cloudflared binary inside {asset}")
|
|
109
|
+
# Extract only the one member we want, and never let the archive
|
|
110
|
+
# choose where it lands.
|
|
111
|
+
handle = tf.extractfile(member)
|
|
112
|
+
if handle is None:
|
|
113
|
+
raise KdevError(f"Could not read cloudflared out of {asset}")
|
|
114
|
+
extracted = Path(tmp) / "cloudflared"
|
|
115
|
+
with handle, extracted.open("wb") as out:
|
|
116
|
+
shutil.copyfileobj(handle, out)
|
|
117
|
+
shutil.move(str(extracted), dest)
|
|
118
|
+
else:
|
|
119
|
+
shutil.move(str(tmp_path), dest)
|
|
120
|
+
|
|
121
|
+
dest.chmod(dest.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
|
|
122
|
+
|
|
123
|
+
# A binary that downloads but will not run is worse than no binary: it fails
|
|
124
|
+
# later, inside ssh's ProxyCommand, where the error is unreadable.
|
|
125
|
+
if version(dest) == "unknown":
|
|
126
|
+
dest.unlink(missing_ok=True)
|
|
127
|
+
raise KdevError(f"Downloaded cloudflared from {url} but it would not execute.")
|
|
128
|
+
return dest
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _download(url: str, dest: Path, progress=None) -> str:
|
|
132
|
+
sha = hashlib.sha256()
|
|
133
|
+
with urllib.request.urlopen(url, timeout=120) as r:
|
|
134
|
+
total = int(r.headers.get("Content-Length") or 0)
|
|
135
|
+
done = 0
|
|
136
|
+
with dest.open("wb") as f:
|
|
137
|
+
while chunk := r.read(262144):
|
|
138
|
+
f.write(chunk)
|
|
139
|
+
sha.update(chunk)
|
|
140
|
+
done += len(chunk)
|
|
141
|
+
if progress:
|
|
142
|
+
progress(done, total)
|
|
143
|
+
return sha.hexdigest()
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def ensure(auto: bool = True, progress=None) -> Path:
|
|
147
|
+
"""Return a usable cloudflared, downloading it if allowed."""
|
|
148
|
+
found = find()
|
|
149
|
+
if found:
|
|
150
|
+
return found
|
|
151
|
+
if not auto:
|
|
152
|
+
raise KdevError(
|
|
153
|
+
"cloudflared not found. Run `kdev doctor --install` or "
|
|
154
|
+
"install it yourself: brew install cloudflared"
|
|
155
|
+
)
|
|
156
|
+
return install(progress)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def proxy_command(path: Path | None = None) -> str:
|
|
160
|
+
"""ssh runs ProxyCommand through /bin/sh, so the path must survive a shell.
|
|
161
|
+
|
|
162
|
+
A bare `cloudflared` would only resolve for a system install; kdev's managed
|
|
163
|
+
copy lives outside PATH, so the absolute path is written instead -- quoted,
|
|
164
|
+
because home directories and app-support paths do contain spaces.
|
|
165
|
+
"""
|
|
166
|
+
# Never downloads: rendering an ssh config must not reach the network.
|
|
167
|
+
# A bare name is the honest fallback -- if no cloudflared is resolvable,
|
|
168
|
+
# ssh will say so, which beats stalling on a 19 MB fetch nobody asked for.
|
|
169
|
+
path = path or find()
|
|
170
|
+
if path is None:
|
|
171
|
+
return "cloudflared access ssh --hostname %h"
|
|
172
|
+
p = str(path)
|
|
173
|
+
if any(c in p for c in " \t\"'\\$`"):
|
|
174
|
+
p = (
|
|
175
|
+
'"'
|
|
176
|
+
+ p.replace("\\", "\\\\").replace('"', '\\"').replace("$", "\\$").replace("`", "\\`")
|
|
177
|
+
+ '"'
|
|
178
|
+
)
|
|
179
|
+
return f"{p} access ssh --hostname %h"
|
kdev/commands/_common.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Small pieces every command needs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from .. import config, ui, wizard
|
|
6
|
+
from ..errors import KdevError
|
|
7
|
+
|
|
8
|
+
MAX_HOURS = 12.0 # Kaggle's ceiling for CPU/GPU sessions
|
|
9
|
+
TPU_HOURS = 9.0
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def cap_for(gpu: str) -> float:
|
|
13
|
+
return TPU_HOURS if gpu == "tpu" else MAX_HOURS
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def load_ready(interactive: bool) -> config.Config:
|
|
17
|
+
"""The config, running setup first if nothing is configured yet."""
|
|
18
|
+
cfg = config.load()
|
|
19
|
+
if cfg.profiles:
|
|
20
|
+
return cfg
|
|
21
|
+
if not interactive:
|
|
22
|
+
raise KdevError("kdev is not set up on this machine.", "Run `kdev setup` in a terminal.")
|
|
23
|
+
return wizard.run()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def need_notebook(cfg: config.Config, override: str = "") -> str:
|
|
27
|
+
if not cfg.profiles:
|
|
28
|
+
raise KdevError("kdev is not set up on this machine.", "Run: kdev setup")
|
|
29
|
+
target = override or cfg.notebook
|
|
30
|
+
if not target:
|
|
31
|
+
raise KdevError(
|
|
32
|
+
"No workspace notebook yet.",
|
|
33
|
+
"Join your group's: kdev workspace join or make one: "
|
|
34
|
+
"kdev workspace create --group <slug>",
|
|
35
|
+
)
|
|
36
|
+
return target
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def context(cfg: config.Config, account: str = "", notebook: str = "") -> list[tuple[str, str]]:
|
|
40
|
+
"""The facts a header shows: who, which notebook, which tunnel."""
|
|
41
|
+
return [
|
|
42
|
+
("account", account or cfg.active or "—"),
|
|
43
|
+
("notebook", notebook or cfg.notebook or "—"),
|
|
44
|
+
("tunnel", cfg.tunnel_hostname or "quick"),
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def box_label(cfg: config.Config) -> str:
|
|
49
|
+
if not cfg.default_hours:
|
|
50
|
+
return "not set"
|
|
51
|
+
return f"{ui.gpu_label(cfg.default_gpu or 't4')} · {cfg.default_hours:g}h"
|
kdev/commands/account.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"""Kaggle accounts on this machine, and the weekly quota each has left."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import typer
|
|
6
|
+
|
|
7
|
+
from .. import auth, config, quota, ui, wizard
|
|
8
|
+
from ..errors import KdevError
|
|
9
|
+
|
|
10
|
+
app = typer.Typer(
|
|
11
|
+
help="Kaggle accounts on this machine, and their quota.",
|
|
12
|
+
no_args_is_help=False,
|
|
13
|
+
invoke_without_command=True,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@app.callback()
|
|
18
|
+
def main(
|
|
19
|
+
ctx: typer.Context,
|
|
20
|
+
as_json: bool = typer.Option(False, "--json", help="Print JSON instead."),
|
|
21
|
+
) -> None:
|
|
22
|
+
"""List the accounts with the GPU and TPU hours each has left this week."""
|
|
23
|
+
if ctx.invoked_subcommand:
|
|
24
|
+
return
|
|
25
|
+
cfg = config.load()
|
|
26
|
+
if not cfg.profiles:
|
|
27
|
+
raise KdevError("No accounts yet.", "Sign one in: kdev account add")
|
|
28
|
+
with ui.spinner("reading quota…"):
|
|
29
|
+
rows = quota.rows(cfg)
|
|
30
|
+
if as_json:
|
|
31
|
+
ui.emit_json(
|
|
32
|
+
[
|
|
33
|
+
{
|
|
34
|
+
"account": r.name,
|
|
35
|
+
"username": r.username,
|
|
36
|
+
"active": r.active,
|
|
37
|
+
"gpu_seconds_left": r.gpu_left,
|
|
38
|
+
"gpu_seconds_total": r.gpu_total,
|
|
39
|
+
"tpu_seconds_left": r.tpu_left,
|
|
40
|
+
"error": r.error or None,
|
|
41
|
+
}
|
|
42
|
+
for r in rows
|
|
43
|
+
]
|
|
44
|
+
)
|
|
45
|
+
return
|
|
46
|
+
ui.console.print(ui.accounts_table(rows))
|
|
47
|
+
total = sum(r.gpu_left for r in rows if not r.error)
|
|
48
|
+
ui.blank()
|
|
49
|
+
ui.hint(
|
|
50
|
+
f"{ui.fmt_hours(total)} of GPU left across {len(rows)} account(s) "
|
|
51
|
+
f"{ui.g('bullet')} {ui.g('live')} runs the next box"
|
|
52
|
+
)
|
|
53
|
+
broken = [r.name for r in rows if r.error]
|
|
54
|
+
if broken:
|
|
55
|
+
ui.blank()
|
|
56
|
+
ui.next_steps([(f"kdev account add {n}", "sign it in again") for n in broken])
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@app.command("add")
|
|
60
|
+
def add(name: str = typer.Argument("", help="A short name for the account, e.g. alice.")) -> None:
|
|
61
|
+
"""Sign a Kaggle account in (opens a browser). Re-run to refresh one."""
|
|
62
|
+
cfg = config.load()
|
|
63
|
+
if not name and not ui.interactive():
|
|
64
|
+
raise KdevError("Name the account.", "kdev account add <name>")
|
|
65
|
+
if name:
|
|
66
|
+
try:
|
|
67
|
+
name = config.valid_profile_name(name)
|
|
68
|
+
except ValueError as e:
|
|
69
|
+
raise KdevError(str(e)) from e
|
|
70
|
+
if name in cfg.profiles:
|
|
71
|
+
_refresh(cfg, name)
|
|
72
|
+
return
|
|
73
|
+
name = wizard.sign_in(cfg, name)
|
|
74
|
+
config.save(cfg)
|
|
75
|
+
ui.ok(
|
|
76
|
+
f"added {name} ({cfg.profiles[name].username})"
|
|
77
|
+
+ ("; it runs the next box" if cfg.active == name else "")
|
|
78
|
+
)
|
|
79
|
+
ui.blank()
|
|
80
|
+
ui.next_steps([("kdev account", "see every account's quota")])
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _refresh(cfg: config.Config, name: str) -> None:
|
|
84
|
+
"""Sign an existing account in again, without risking its current token.
|
|
85
|
+
|
|
86
|
+
The sign-in lands in a scratch slot first: if the browser was logged in as
|
|
87
|
+
someone else, the account's working token must not have been replaced.
|
|
88
|
+
"""
|
|
89
|
+
scratch = f"{name}.signing-in"
|
|
90
|
+
ui.hint("Your browser will open to sign in to Kaggle.")
|
|
91
|
+
try:
|
|
92
|
+
username = auth.login(scratch)
|
|
93
|
+
except auth.AuthError as e:
|
|
94
|
+
auth.forget(scratch)
|
|
95
|
+
raise KdevError(str(e), f"Try again: kdev account add {name}") from e
|
|
96
|
+
expected = cfg.profiles[name].username
|
|
97
|
+
if username != expected:
|
|
98
|
+
auth.forget(scratch)
|
|
99
|
+
raise KdevError(
|
|
100
|
+
f"The browser signed in as {username}, not {expected}.",
|
|
101
|
+
f"Switch Kaggle accounts in the browser, then: kdev account add {name}",
|
|
102
|
+
)
|
|
103
|
+
auth.creds_path(scratch).replace(auth.creds_path(name))
|
|
104
|
+
ui.ok(f"signed {name} in again")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
@app.command("use")
|
|
108
|
+
def use(name: str = typer.Argument("", help="The account to make active.")) -> None:
|
|
109
|
+
"""Choose which account runs the next box."""
|
|
110
|
+
cfg = config.load()
|
|
111
|
+
if not cfg.profiles:
|
|
112
|
+
raise KdevError("No accounts yet.", "Sign one in: kdev account add")
|
|
113
|
+
if not name:
|
|
114
|
+
if not ui.interactive():
|
|
115
|
+
raise KdevError("Name the account.", "kdev account use <name>")
|
|
116
|
+
with ui.spinner("reading quota…"):
|
|
117
|
+
rows = quota.rows(cfg)
|
|
118
|
+
name = ui.pick_profile(rows, 0)
|
|
119
|
+
cfg.profile(name)
|
|
120
|
+
cfg.active = name
|
|
121
|
+
config.save(cfg)
|
|
122
|
+
ui.ok(f"{name} runs the next box")
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@app.command("remove")
|
|
126
|
+
def remove(
|
|
127
|
+
name: str = typer.Argument(..., help="The account to remove."),
|
|
128
|
+
yes: bool = typer.Option(False, "--yes", "-y", help="Do not ask."),
|
|
129
|
+
) -> None:
|
|
130
|
+
"""Sign an account out and forget it on this machine."""
|
|
131
|
+
cfg = config.load()
|
|
132
|
+
prof = cfg.profile(name)
|
|
133
|
+
if len(cfg.profiles) == 1:
|
|
134
|
+
raise KdevError(
|
|
135
|
+
"That is the only account; kdev needs one.", "Add another first: kdev account add"
|
|
136
|
+
)
|
|
137
|
+
if (
|
|
138
|
+
not yes
|
|
139
|
+
and ui.interactive()
|
|
140
|
+
and not ui.confirm(f"Remove {name} ({prof.username}) from this machine?", default=False)
|
|
141
|
+
):
|
|
142
|
+
return
|
|
143
|
+
auth.forget(name)
|
|
144
|
+
del cfg.profiles[name]
|
|
145
|
+
if cfg.active == name:
|
|
146
|
+
cfg.active = next(iter(cfg.profiles))
|
|
147
|
+
config.save(cfg)
|
|
148
|
+
ui.ok(f"removed {name}; {cfg.active} runs the next box")
|