kdev-cli 0.1.4__py3-none-any.whl → 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.
- kdev/api.py +125 -24
- kdev/auth.py +161 -22
- kdev/bootstrap.py +47 -0
- kdev/cli.py +85 -22
- kdev/cloudflared.py +7 -6
- kdev/commands/_common.py +9 -6
- kdev/commands/account.py +21 -5
- kdev/commands/box.py +505 -91
- kdev/commands/doctor.py +91 -29
- kdev/commands/home.py +364 -57
- kdev/commands/settings.py +35 -5
- kdev/commands/tunnel.py +17 -12
- kdev/commands/workspace.py +41 -10
- kdev/config.py +13 -0
- kdev/errors.py +12 -0
- kdev/kdev_box.py +14 -0
- kdev/menu.py +372 -0
- kdev/notebook.py +55 -22
- kdev/persistence.py +218 -16
- kdev/quota.py +34 -2
- kdev/safety.py +18 -1
- kdev/session.py +277 -25
- kdev/sshcfg.py +51 -5
- kdev/ui.py +951 -124
- kdev/wizard.py +10 -4
- kdev_cli-0.2.0.dist-info/METADATA +195 -0
- kdev_cli-0.2.0.dist-info/RECORD +34 -0
- {kdev_cli-0.1.4.dist-info → kdev_cli-0.2.0.dist-info}/WHEEL +1 -1
- kdev_cli-0.1.4.dist-info/METADATA +0 -610
- kdev_cli-0.1.4.dist-info/RECORD +0 -33
- {kdev_cli-0.1.4.dist-info → kdev_cli-0.2.0.dist-info}/entry_points.txt +0 -0
- {kdev_cli-0.1.4.dist-info → kdev_cli-0.2.0.dist-info}/licenses/LICENSE +0 -0
kdev/api.py
CHANGED
|
@@ -11,6 +11,8 @@ from __future__ import annotations
|
|
|
11
11
|
|
|
12
12
|
import json
|
|
13
13
|
import logging
|
|
14
|
+
import random
|
|
15
|
+
import threading
|
|
14
16
|
import time
|
|
15
17
|
from collections.abc import Iterator
|
|
16
18
|
from dataclasses import dataclass
|
|
@@ -81,6 +83,41 @@ class Creds:
|
|
|
81
83
|
RETRY_STATUS = frozenset({429, 500, 502, 503, 504})
|
|
82
84
|
|
|
83
85
|
|
|
86
|
+
_pool: httpx.Client | None = None
|
|
87
|
+
_pool_lock = threading.Lock()
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _post(url: str, **kwargs: Any) -> httpx.Response:
|
|
91
|
+
"""POST over one shared keep-alive pool: a fresh TLS handshake per call
|
|
92
|
+
cost ~0.3s each, and a status check makes several calls in a row. Tests
|
|
93
|
+
patch `httpx.post`, so a patched one is honoured."""
|
|
94
|
+
global _pool
|
|
95
|
+
if httpx.post is not _REAL_POST:
|
|
96
|
+
return httpx.post(url, **kwargs)
|
|
97
|
+
with _pool_lock:
|
|
98
|
+
if _pool is None:
|
|
99
|
+
_pool = httpx.Client(timeout=120.0, limits=httpx.Limits(max_keepalive_connections=8))
|
|
100
|
+
return _pool.post(url, **kwargs)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
_REAL_POST = httpx.post
|
|
104
|
+
|
|
105
|
+
#: Extra tries a retryable call gets when Kaggle answers 429.
|
|
106
|
+
RATE_LIMIT_EXTRA = 2
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _backoff(r: httpx.Response, attempt: int) -> float:
|
|
110
|
+
"""Kaggle's Retry-After when it sends one (capped), else exponential with
|
|
111
|
+
jitter -- so threads that were limited together do not return together."""
|
|
112
|
+
try:
|
|
113
|
+
after = float((getattr(r, "headers", None) or {}).get("retry-after", ""))
|
|
114
|
+
except ValueError:
|
|
115
|
+
after = 0.0
|
|
116
|
+
if after > 0:
|
|
117
|
+
return min(after, 30.0)
|
|
118
|
+
return 2**attempt + random.uniform(0, 1)
|
|
119
|
+
|
|
120
|
+
|
|
84
121
|
def call(
|
|
85
122
|
creds: Creds,
|
|
86
123
|
method: str,
|
|
@@ -89,16 +126,23 @@ def call(
|
|
|
89
126
|
attempts: int = 3,
|
|
90
127
|
) -> dict:
|
|
91
128
|
url = f"{BASE}/{service}/{method}"
|
|
92
|
-
|
|
93
|
-
|
|
129
|
+
# Two budgets. Failures (no answer, 5xx) get the caller's `attempts`.
|
|
130
|
+
# Rate limiting is Kaggle asking us to slow down, not failing: a call that
|
|
131
|
+
# may be retried at all gets RATE_LIMIT_EXTRA more tries for a 429 only,
|
|
132
|
+
# so being offline still fails fast.
|
|
133
|
+
tries = 0
|
|
134
|
+
limited = 0
|
|
135
|
+
while True:
|
|
136
|
+
tries += 1
|
|
137
|
+
last = tries >= attempts
|
|
94
138
|
started = time.monotonic()
|
|
95
139
|
try:
|
|
96
|
-
r =
|
|
140
|
+
r = _post(url, json=payload or {}, headers=creds.headers, timeout=120.0)
|
|
97
141
|
except httpx.HTTPError as e:
|
|
98
|
-
log.debug("%s as %s: %s (attempt %d)", method, creds.username, e,
|
|
142
|
+
log.debug("%s as %s: %s (attempt %d)", method, creds.username, e, tries)
|
|
99
143
|
if last:
|
|
100
144
|
raise KaggleError(f"{method}: could not reach Kaggle ({e})") from e
|
|
101
|
-
time.sleep(2**
|
|
145
|
+
time.sleep(2 ** (tries - 1))
|
|
102
146
|
continue
|
|
103
147
|
log.debug(
|
|
104
148
|
"%s as %s -> %d in %.2fs",
|
|
@@ -107,8 +151,13 @@ def call(
|
|
|
107
151
|
r.status_code,
|
|
108
152
|
time.monotonic() - started,
|
|
109
153
|
)
|
|
154
|
+
if r.status_code == 429 and attempts > 1 and limited < RATE_LIMIT_EXTRA:
|
|
155
|
+
limited += 1
|
|
156
|
+
tries -= 1 # a rate-limited try does not spend the failure budget
|
|
157
|
+
time.sleep(_backoff(r, limited))
|
|
158
|
+
continue
|
|
110
159
|
if r.status_code in RETRY_STATUS and not last:
|
|
111
|
-
time.sleep(
|
|
160
|
+
time.sleep(_backoff(r, tries - 1))
|
|
112
161
|
continue
|
|
113
162
|
break
|
|
114
163
|
if r.status_code == 401:
|
|
@@ -121,16 +170,18 @@ def call(
|
|
|
121
170
|
raise KaggleError(f"{method}: Kaggle sent something that is not JSON", r.status_code) from e
|
|
122
171
|
# The API returns 200 with an `error` field for validation failures.
|
|
123
172
|
if isinstance(body, dict) and body.get("error"):
|
|
124
|
-
raise KaggleError(f"{method}: {body['error']}")
|
|
173
|
+
raise KaggleError(f"{method}: {body['error']}", r.status_code)
|
|
125
174
|
return body
|
|
126
175
|
|
|
127
176
|
|
|
128
177
|
def _message(r: httpx.Response) -> str:
|
|
129
178
|
"""Kaggle's own error message, not the JSON envelope around it."""
|
|
130
179
|
try:
|
|
131
|
-
|
|
180
|
+
body = r.json()
|
|
132
181
|
except ValueError:
|
|
133
|
-
|
|
182
|
+
body = None
|
|
183
|
+
# A proxy or an outage page can answer with any JSON at all, or none.
|
|
184
|
+
err = body.get("error") if isinstance(body, dict) else None
|
|
134
185
|
if isinstance(err, dict) and err.get("message"):
|
|
135
186
|
return f"{err['message']} (HTTP {r.status_code})"
|
|
136
187
|
return f"HTTP {r.status_code}: {r.text[:200]}"
|
|
@@ -163,17 +214,21 @@ def save_kernel(
|
|
|
163
214
|
}
|
|
164
215
|
if machine_shape:
|
|
165
216
|
payload["machineShape"] = machine_shape
|
|
166
|
-
|
|
217
|
+
# This creates a run. A lost response is not permission to create another.
|
|
218
|
+
return call(creds, "SaveKernel", payload, attempts=1)
|
|
167
219
|
|
|
168
220
|
|
|
169
221
|
#: The most ListKernelSessionOutput accepts per page (it says so on a 400).
|
|
170
222
|
OUTPUT_PAGE = 500
|
|
223
|
+
#: The first page of a prefix read: enough for `.kdev/` when it sorts first.
|
|
224
|
+
PREFIX_PAGE = 50
|
|
171
225
|
|
|
172
226
|
|
|
173
|
-
def session_output(creds: Creds, slug: str, version: str = "") -> list[dict]:
|
|
227
|
+
def session_output(creds: Creds, slug: str, version: str = "", *, prefix: str = "") -> list[dict]:
|
|
174
228
|
"""Every file a saved version left in /kaggle/working, with signed URLs.
|
|
175
229
|
|
|
176
|
-
`version` is a label like "v7"; empty means the latest.
|
|
230
|
+
`version` is a label like "v7"; empty means the latest. With `prefix`,
|
|
231
|
+
paging stops once every name under it has been listed (see `_past`). Older versions stay
|
|
177
232
|
readable, which is what lets a restore build on the last good one when the
|
|
178
233
|
newest was cut short. A notebook cannot mount its own output (Kaggle
|
|
179
234
|
rejects the data source), so this listing is the only way back.
|
|
@@ -181,8 +236,13 @@ def session_output(creds: Creds, slug: str, version: str = "") -> list[dict]:
|
|
|
181
236
|
user, _, kslug = slug.partition("/")
|
|
182
237
|
out = []
|
|
183
238
|
token = None
|
|
239
|
+
# A prefix read wants the first names only: a small page answers faster.
|
|
240
|
+
# If that page does not get past the prefix (a `.git/` or `.cache/` at the
|
|
241
|
+
# root sorts before `.kdev/`), start over at the full size rather than
|
|
242
|
+
# walk thousands of names fifty at a time.
|
|
243
|
+
size = PREFIX_PAGE if prefix else OUTPUT_PAGE
|
|
184
244
|
while True:
|
|
185
|
-
payload = {"userName": user, "kernelSlug": kslug, "pageSize":
|
|
245
|
+
payload = {"userName": user, "kernelSlug": kslug, "pageSize": size}
|
|
186
246
|
if version:
|
|
187
247
|
payload["versionLabel"] = version
|
|
188
248
|
if token:
|
|
@@ -195,6 +255,21 @@ def session_output(creds: Creds, slug: str, version: str = "") -> list[dict]:
|
|
|
195
255
|
token = resp.get("nextPageToken")
|
|
196
256
|
if not token:
|
|
197
257
|
return out
|
|
258
|
+
if size == PREFIX_PAGE and not _past(resp.get("files") or [], prefix):
|
|
259
|
+
out, token, size = [], None, OUTPUT_PAGE
|
|
260
|
+
continue
|
|
261
|
+
if prefix and _past(resp.get("files") or [], prefix):
|
|
262
|
+
# Only the names under `prefix` must be complete, and Kaggle lists
|
|
263
|
+
# names in order (measured): a sorted page that ends beyond the
|
|
264
|
+
# prefix means no later page holds one. The listing returned is
|
|
265
|
+
# partial -- complete under `prefix`, a sample of the rest.
|
|
266
|
+
return out
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _past(page: list[dict], prefix: str) -> bool:
|
|
270
|
+
"""Whether a page is sorted and ends after every name starting `prefix`."""
|
|
271
|
+
names = [str(f.get("fileName") or "") for f in page]
|
|
272
|
+
return bool(names) and names == sorted(names) and names[-1] > prefix + "\U0010ffff"
|
|
198
273
|
|
|
199
274
|
|
|
200
275
|
#: The status of a notebook that has never been run. Kaggle answers that with
|
|
@@ -203,19 +278,24 @@ def session_output(creds: Creds, slug: str, version: str = "") -> list[dict]:
|
|
|
203
278
|
NEVER_RUN = "NEVER_RUN"
|
|
204
279
|
|
|
205
280
|
|
|
206
|
-
def session_status(creds: Creds, slug: str) -> dict:
|
|
281
|
+
def session_status(creds: Creds, slug: str, version: str = "") -> dict:
|
|
207
282
|
"""{"status": ..., "failureMessage": ...} -- the second is why it stopped.
|
|
208
283
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
284
|
+
Pass a saved version label such as "v7" to ask about that run instead of
|
|
285
|
+
the latest one.
|
|
286
|
+
|
|
287
|
+
A 404 for the latest run means the notebook has never run. For an explicit
|
|
288
|
+
historical version it means Kaggle has no status for that version.
|
|
212
289
|
"""
|
|
213
290
|
user, _, kslug = slug.partition("/")
|
|
291
|
+
payload = {"userName": user, "kernelSlug": kslug}
|
|
292
|
+
if version:
|
|
293
|
+
payload["versionLabel"] = version
|
|
214
294
|
try:
|
|
215
|
-
return call(creds, "GetKernelSessionStatus",
|
|
295
|
+
return call(creds, "GetKernelSessionStatus", payload)
|
|
216
296
|
except KaggleError as e:
|
|
217
297
|
if e.status == 404:
|
|
218
|
-
return {"status": NEVER_RUN}
|
|
298
|
+
return {"status": "UNKNOWN" if version else NEVER_RUN}
|
|
219
299
|
raise
|
|
220
300
|
|
|
221
301
|
|
|
@@ -236,7 +316,12 @@ def quota(creds: Creds) -> dict:
|
|
|
236
316
|
|
|
237
317
|
|
|
238
318
|
def stream_logs(
|
|
239
|
-
creds: Creds,
|
|
319
|
+
creds: Creds,
|
|
320
|
+
slug: str,
|
|
321
|
+
wait_seconds: int = 300,
|
|
322
|
+
idle: float | None = None,
|
|
323
|
+
*,
|
|
324
|
+
version: str = "",
|
|
240
325
|
) -> Iterator[str]:
|
|
241
326
|
"""Yield log lines from a running session.
|
|
242
327
|
|
|
@@ -251,6 +336,8 @@ def stream_logs(
|
|
|
251
336
|
"kernelSlug": kslug,
|
|
252
337
|
"waitForLogsUrlSeconds": min(wait_seconds, 300),
|
|
253
338
|
}
|
|
339
|
+
if version:
|
|
340
|
+
payload["versionLabel"] = version
|
|
254
341
|
with httpx.stream(
|
|
255
342
|
"POST",
|
|
256
343
|
f"{BASE}/{KERNELS}/GetKernelSessionLogsStream",
|
|
@@ -273,7 +360,15 @@ def stream_logs(
|
|
|
273
360
|
if idle is None:
|
|
274
361
|
raise
|
|
275
362
|
else:
|
|
276
|
-
|
|
363
|
+
try:
|
|
364
|
+
entries = json.loads(r.read() or b"[]")
|
|
365
|
+
except ValueError as e:
|
|
366
|
+
raise KaggleError(
|
|
367
|
+
"logs: Kaggle sent something that is not JSON", r.status_code
|
|
368
|
+
) from e
|
|
369
|
+
if not isinstance(entries, list):
|
|
370
|
+
entries = [entries]
|
|
371
|
+
for entry in entries:
|
|
277
372
|
yield _log_text(entry)
|
|
278
373
|
|
|
279
374
|
|
|
@@ -314,7 +409,10 @@ def share_with_group(
|
|
|
314
409
|
bindings = [dict(b) for b in policy.get("bindings") or []]
|
|
315
410
|
|
|
316
411
|
def is_our_group(member: dict) -> bool:
|
|
317
|
-
|
|
412
|
+
# GetIamPolicy leaves `group.slug` null and puts it on `group.avatar`
|
|
413
|
+
# (see policy_groups); a binding we wrote ourselves has it at the top.
|
|
414
|
+
g = member.get("group") or {}
|
|
415
|
+
return group_slug in (g.get("slug"), (g.get("avatar") or {}).get("slug"))
|
|
318
416
|
|
|
319
417
|
for b in bindings:
|
|
320
418
|
b["members"] = [m for m in (b.get("members") or []) if not is_our_group(m)]
|
|
@@ -360,10 +458,13 @@ def list_shared_kernels(creds: Creds) -> list[dict]:
|
|
|
360
458
|
return resp.get("kernels") or []
|
|
361
459
|
|
|
362
460
|
|
|
363
|
-
def get_kernel(creds: Creds, slug: str) -> dict:
|
|
461
|
+
def get_kernel(creds: Creds, slug: str, version: str = "") -> dict:
|
|
364
462
|
"""The kernel's metadata. Note the response key is `metadata`, not `kernel`."""
|
|
365
463
|
user, _, kslug = slug.partition("/")
|
|
366
|
-
|
|
464
|
+
payload = {"userName": user, "kernelSlug": kslug}
|
|
465
|
+
if version:
|
|
466
|
+
payload["versionLabel"] = version
|
|
467
|
+
resp = call(creds, "GetKernel", payload)
|
|
367
468
|
return resp.get("metadata") or {}
|
|
368
469
|
|
|
369
470
|
|
kdev/auth.py
CHANGED
|
@@ -5,7 +5,8 @@ refuses it outright and the endpoints kdev needs (ListKernels, quota, session
|
|
|
5
5
|
status) answer 401. Only a Bearer access token works.
|
|
6
6
|
|
|
7
7
|
Tokens expire after 12 hours, but the credentials file also carries a
|
|
8
|
-
long-lived refresh token,
|
|
8
|
+
long-lived refresh token, which kdev trades for a new access token when the
|
|
9
|
+
stored one is within an hour of expiring. kdev keeps one
|
|
9
10
|
credentials file per profile so several teammates' accounts can coexist -- the
|
|
10
11
|
official CLI only ever has one, at ~/.kaggle/credentials.json.
|
|
11
12
|
"""
|
|
@@ -13,18 +14,31 @@ official CLI only ever has one, at ~/.kaggle/credentials.json.
|
|
|
13
14
|
from __future__ import annotations
|
|
14
15
|
|
|
15
16
|
import json
|
|
17
|
+
import os
|
|
16
18
|
import shutil
|
|
17
19
|
import subprocess
|
|
18
20
|
import sys
|
|
21
|
+
import threading
|
|
22
|
+
from datetime import UTC, datetime, timedelta
|
|
19
23
|
from pathlib import Path
|
|
20
24
|
|
|
25
|
+
from . import api
|
|
21
26
|
from . import config as _config
|
|
22
27
|
|
|
23
28
|
KAGGLE_CREDENTIALS = Path.home() / ".kaggle" / "credentials.json"
|
|
29
|
+
#: The RPC service that turns a refresh token into an access token.
|
|
30
|
+
ACCOUNTS = "users.AccountService"
|
|
24
31
|
|
|
25
32
|
|
|
26
33
|
class AuthError(RuntimeError):
|
|
27
|
-
|
|
34
|
+
"""This account cannot authenticate: not signed in, or its sign-in is
|
|
35
|
+
no longer accepted. Signing in again fixes it."""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class AuthUnavailable(AuthError):
|
|
39
|
+
"""The token could not be refreshed right now -- offline, a proxy, Kaggle
|
|
40
|
+
down. Signing in again would not help, so callers must not treat this as
|
|
41
|
+
"signed out"."""
|
|
28
42
|
|
|
29
43
|
|
|
30
44
|
def creds_dir() -> Path:
|
|
@@ -75,10 +89,8 @@ def login(profile: str) -> str:
|
|
|
75
89
|
if not data.get("refresh_token"):
|
|
76
90
|
raise AuthError("Credentials are missing a refresh token.")
|
|
77
91
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
dest.write_text(json.dumps(data, indent=2))
|
|
81
|
-
dest.chmod(0o600)
|
|
92
|
+
_write_private(creds_path(profile), data)
|
|
93
|
+
invalidate(profile)
|
|
82
94
|
|
|
83
95
|
# Put the shared file back so kdev does not disturb the user's own CLI login.
|
|
84
96
|
if before is not None:
|
|
@@ -87,24 +99,150 @@ def login(profile: str) -> str:
|
|
|
87
99
|
return data.get("username") or ""
|
|
88
100
|
|
|
89
101
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
102
|
+
#: Refresh a token this long before it expires. Longer than any one wait
|
|
103
|
+
#: that holds a single `Creds` (a box boot or a save: up to 30 min), so a
|
|
104
|
+
#: token never lapses halfway through and fails as "signed out".
|
|
105
|
+
REFRESH_MARGIN = timedelta(hours=1)
|
|
106
|
+
#: What kagglesdk asks for; Kaggle's answer says what it actually granted.
|
|
107
|
+
TOKEN_LIFETIME = timedelta(hours=12)
|
|
108
|
+
|
|
109
|
+
#: profile creds path -> (file identity, token, expiry). One process reads an
|
|
110
|
+
#: account's token dozens of times; minting one costs a round trip each.
|
|
111
|
+
_cache: dict[str, tuple[tuple[int, int, int], str, datetime]] = {}
|
|
112
|
+
_locks: dict[str, threading.Lock] = {}
|
|
113
|
+
_locks_guard = threading.Lock()
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _lock_for(key: str) -> threading.Lock:
|
|
117
|
+
# One lock per account, so several accounts still mint in parallel.
|
|
118
|
+
with _locks_guard:
|
|
119
|
+
return _locks.setdefault(key, threading.Lock())
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _identity(path: Path) -> tuple[int, int, int]:
|
|
123
|
+
"""Changes whenever the file is replaced -- a re-login, another kdev."""
|
|
124
|
+
st = path.stat()
|
|
125
|
+
return st.st_ino, st.st_mtime_ns, st.st_size
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _expiry(value: object) -> datetime | None:
|
|
129
|
+
if not value or not isinstance(value, str):
|
|
130
|
+
return None
|
|
95
131
|
try:
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
132
|
+
when = datetime.fromisoformat(value)
|
|
133
|
+
except ValueError:
|
|
134
|
+
return None
|
|
135
|
+
return when if when.tzinfo else when.replace(tzinfo=UTC)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _fresh(expires: datetime) -> bool:
|
|
139
|
+
return expires - datetime.now(UTC) > REFRESH_MARGIN
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def invalidate(profile: str) -> None:
|
|
143
|
+
"""Forget the cached token, after the account's file was replaced."""
|
|
144
|
+
_cache.pop(str(creds_path(profile)), None)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _write_private(path: Path, data: dict) -> None:
|
|
148
|
+
"""Write JSON through a temp file and a rename, 0600 from the start.
|
|
149
|
+
|
|
150
|
+
Several threads, or kdev processes, can refresh one account at once; a
|
|
151
|
+
half-written file would sign the account out.
|
|
152
|
+
"""
|
|
153
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
154
|
+
tmp = path.with_name(f".{path.name}.{os.getpid()}.{threading.get_ident()}.tmp")
|
|
155
|
+
fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
156
|
+
try:
|
|
157
|
+
with os.fdopen(fd, "w") as f:
|
|
158
|
+
json.dump(data, f, indent=2)
|
|
159
|
+
os.chmod(tmp, 0o600)
|
|
160
|
+
tmp.replace(path)
|
|
161
|
+
except BaseException:
|
|
162
|
+
tmp.unlink(missing_ok=True)
|
|
163
|
+
raise
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _mint(profile: str, refresh_token: str) -> tuple[str, datetime]:
|
|
167
|
+
"""A new access token from the refresh token. Nothing is written here.
|
|
168
|
+
|
|
169
|
+
The same request kagglesdk's `KaggleCredentials.generate_access_token`
|
|
170
|
+
builds (users.AccountService/GenerateAccessToken), sent directly: the SDK's
|
|
171
|
+
refresh path saves the result to ~/.kaggle/credentials.json -- the user's
|
|
172
|
+
own CLI login -- sends with no timeout, and attaches whatever other
|
|
173
|
+
credentials it finds in the environment.
|
|
174
|
+
"""
|
|
175
|
+
payload = {
|
|
176
|
+
"refreshToken": refresh_token,
|
|
177
|
+
"apiVersion": "API_VERSION_V1",
|
|
178
|
+
"expirationDuration": f"{int(TOKEN_LIFETIME.total_seconds())}s",
|
|
179
|
+
}
|
|
180
|
+
try:
|
|
181
|
+
resp = api.call(api.Creds(profile), "GenerateAccessToken", payload, service=ACCOUNTS)
|
|
182
|
+
except api.KaggleError as e:
|
|
183
|
+
if e.status in (400, 401, 403, 404):
|
|
184
|
+
raise AuthError(
|
|
185
|
+
f"Kaggle no longer accepts {profile}'s sign-in. Run: kdev account add {profile}"
|
|
186
|
+
) from e
|
|
187
|
+
raise AuthUnavailable(
|
|
188
|
+
f"Could not reach Kaggle to refresh {profile}'s sign-in ({e})."
|
|
189
|
+
) from e
|
|
190
|
+
token = str(resp.get("token") or "")
|
|
105
191
|
if not token:
|
|
106
|
-
raise
|
|
107
|
-
|
|
192
|
+
raise AuthUnavailable(f"Kaggle sent no access token for {profile}.")
|
|
193
|
+
try:
|
|
194
|
+
seconds = int(resp.get("expiresIn") or 0)
|
|
195
|
+
except (TypeError, ValueError):
|
|
196
|
+
seconds = 0
|
|
197
|
+
lifetime = timedelta(seconds=seconds) if seconds > 0 else TOKEN_LIFETIME
|
|
198
|
+
return token, datetime.now(UTC) + lifetime
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def access_token(profile: str) -> str:
|
|
202
|
+
"""A valid access token for this profile, refreshing it when near expiry.
|
|
203
|
+
|
|
204
|
+
Read once per process and kept, so the dozens of calls one command makes
|
|
205
|
+
share a token. A refresh lands in the profile's own file, never in the
|
|
206
|
+
official CLI's.
|
|
207
|
+
"""
|
|
208
|
+
path = creds_path(profile)
|
|
209
|
+
key = str(path)
|
|
210
|
+
with _lock_for(key):
|
|
211
|
+
try:
|
|
212
|
+
ident = _identity(path)
|
|
213
|
+
except FileNotFoundError:
|
|
214
|
+
_cache.pop(key, None)
|
|
215
|
+
raise AuthError(
|
|
216
|
+
f"{profile} is not signed in. Run: kdev account add {profile}"
|
|
217
|
+
) from None
|
|
218
|
+
except OSError as e:
|
|
219
|
+
raise AuthError(f"Could not read credentials for {profile!r}: {e}") from e
|
|
220
|
+
hit = _cache.get(key)
|
|
221
|
+
if hit and hit[0] == ident and _fresh(hit[2]):
|
|
222
|
+
return hit[1]
|
|
223
|
+
|
|
224
|
+
try:
|
|
225
|
+
data = json.loads(path.read_text())
|
|
226
|
+
except (OSError, ValueError) as e:
|
|
227
|
+
raise AuthError(f"Could not read credentials for {profile!r}: {e}") from e
|
|
228
|
+
if not isinstance(data, dict) or not data.get("refresh_token"):
|
|
229
|
+
raise AuthError(
|
|
230
|
+
f"{profile}'s credentials have no refresh token. Run: kdev account add {profile}"
|
|
231
|
+
)
|
|
232
|
+
token = str(data.get("access_token") or "")
|
|
233
|
+
expires = _expiry(data.get("access_token_expiration"))
|
|
234
|
+
if not token or expires is None or not _fresh(expires):
|
|
235
|
+
token, expires = _mint(profile, str(data["refresh_token"]))
|
|
236
|
+
data["access_token"] = token
|
|
237
|
+
data["access_token_expiration"] = expires.isoformat()
|
|
238
|
+
try:
|
|
239
|
+
_write_private(path, data)
|
|
240
|
+
ident = _identity(path)
|
|
241
|
+
except OSError:
|
|
242
|
+
# The token is good either way; the next process mints again.
|
|
243
|
+
pass
|
|
244
|
+
_cache[key] = (ident, token, expires)
|
|
245
|
+
return token
|
|
108
246
|
|
|
109
247
|
|
|
110
248
|
def signed_in(profile: str) -> bool:
|
|
@@ -113,3 +251,4 @@ def signed_in(profile: str) -> bool:
|
|
|
113
251
|
|
|
114
252
|
def forget(profile: str) -> None:
|
|
115
253
|
creds_path(profile).unlink(missing_ok=True)
|
|
254
|
+
invalidate(profile)
|
kdev/bootstrap.py
CHANGED
|
@@ -113,6 +113,7 @@ def export_env():
|
|
|
113
113
|
ssh_env.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
114
114
|
ssh_env.write_text("".join(f"{k}={v}\n" for k, v in env.items()))
|
|
115
115
|
ssh_env.chmod(0o600)
|
|
116
|
+
_pam_path(env.get("PATH", ""))
|
|
116
117
|
|
|
117
118
|
# Also kept as a sourceable file, for scripts that start from a clean env.
|
|
118
119
|
pathlib.Path("/root/.kdev_env.sh").write_text(
|
|
@@ -124,9 +125,26 @@ def export_env():
|
|
|
124
125
|
f.write(hook)
|
|
125
126
|
|
|
126
127
|
|
|
128
|
+
def _pam_path(path):
|
|
129
|
+
"""sshd applies ~/.ssh/environment, then PAM's pam_env sets PATH again from
|
|
130
|
+
/etc/environment -- measured: a non-interactive ssh had no /opt/bin, so no
|
|
131
|
+
nvidia-smi, and VS Code's server no CUDA. Give pam_env the notebook's PATH."""
|
|
132
|
+
if not path or '"' in path:
|
|
133
|
+
return
|
|
134
|
+
etc = pathlib.Path("/etc/environment")
|
|
135
|
+
try:
|
|
136
|
+
lines = etc.read_text().splitlines() if etc.exists() else []
|
|
137
|
+
kept = [ln for ln in lines if not ln.startswith("PATH=")]
|
|
138
|
+
etc.write_text("\n".join(kept + [f'PATH="{path}"']) + "\n")
|
|
139
|
+
except OSError as e:
|
|
140
|
+
print(f"could not set PATH for ssh sessions: {e}", flush=True)
|
|
141
|
+
|
|
142
|
+
|
|
127
143
|
def add_env(name, value):
|
|
128
144
|
"""Add one variable to every future ssh session."""
|
|
129
145
|
os.environ[name] = value
|
|
146
|
+
if name == "PATH":
|
|
147
|
+
_pam_path(value)
|
|
130
148
|
with pathlib.Path("/root/.ssh/environment").open("a") as f:
|
|
131
149
|
f.write(f"{name}={value}\n")
|
|
132
150
|
with pathlib.Path("/root/.kdev_env.sh").open("a") as f:
|
|
@@ -222,6 +240,30 @@ def snapshot(dest):
|
|
|
222
240
|
sh(f"git -C {dest} push", check=False)
|
|
223
241
|
|
|
224
242
|
|
|
243
|
+
END_WARNING_MINUTES = (15, 5)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def warn_ending(previous_left, left, warned, pts_dir="/dev/pts"):
|
|
247
|
+
"""Announce each session-end threshold once, in logs and open terminals."""
|
|
248
|
+
for minutes in END_WARNING_MINUTES:
|
|
249
|
+
threshold = minutes * 60
|
|
250
|
+
if minutes in warned or not (previous_left >= threshold >= left):
|
|
251
|
+
continue
|
|
252
|
+
print(f"KDEV_ENDING minutes_left={minutes}", flush=True)
|
|
253
|
+
message = f"\r\nkdev: session ends in {minutes} minutes\r\n".encode()
|
|
254
|
+
# VS Code terminals may not be recorded in utmp, so wall misses them.
|
|
255
|
+
for tty in pathlib.Path(pts_dir).glob("[0-9]*"):
|
|
256
|
+
try:
|
|
257
|
+
fd = os.open(tty, os.O_WRONLY | os.O_NOCTTY | os.O_NONBLOCK)
|
|
258
|
+
try:
|
|
259
|
+
os.write(fd, message)
|
|
260
|
+
finally:
|
|
261
|
+
os.close(fd)
|
|
262
|
+
except OSError:
|
|
263
|
+
pass # a terminal we cannot write to must never end the session
|
|
264
|
+
warned.add(minutes)
|
|
265
|
+
|
|
266
|
+
|
|
225
267
|
def end_run():
|
|
226
268
|
"""Make sure nothing below this cell runs once the session is over.
|
|
227
269
|
|
|
@@ -279,6 +321,9 @@ def main():
|
|
|
279
321
|
signal.signal(signal.SIGTERM, lambda *_: stop.__setitem__("now", True))
|
|
280
322
|
next_checkpoint = time.time() + CHECKPOINT_SECONDS
|
|
281
323
|
next_meta = time.time() + META_SECONDS
|
|
324
|
+
warned = set()
|
|
325
|
+
# Seed from the full duration to catch thresholds crossed before the first tick.
|
|
326
|
+
previous_left = CFG["hold_seconds"]
|
|
282
327
|
try:
|
|
283
328
|
stop_file = pathlib.Path("/kaggle/working/.kdev-stop")
|
|
284
329
|
while time.time() < deadline and not stop["now"]:
|
|
@@ -309,6 +354,8 @@ def main():
|
|
|
309
354
|
# A heartbeat keeps the log stream flowing so the CLI can tell the
|
|
310
355
|
# difference between 'alive and idle' and 'dead'.
|
|
311
356
|
left = int(deadline - time.time())
|
|
357
|
+
warn_ending(previous_left, left, warned)
|
|
358
|
+
previous_left = left
|
|
312
359
|
print(f"KDEV_ALIVE seconds_left={left}", flush=True)
|
|
313
360
|
# Short sleeps so a stop request is noticed promptly.
|
|
314
361
|
for _ in range(6):
|