kaiten-cli 0.3.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.
- kaiten_cli/__init__.py +5 -0
- kaiten_cli/__main__.py +9 -0
- kaiten_cli/agent_gateway.py +577 -0
- kaiten_cli/app.py +1636 -0
- kaiten_cli/completion.py +570 -0
- kaiten_cli/discovery.py +39 -0
- kaiten_cli/errors.py +79 -0
- kaiten_cli/i18n.py +92 -0
- kaiten_cli/locales/ru.json +2967 -0
- kaiten_cli/localized_click.py +160 -0
- kaiten_cli/models.py +202 -0
- kaiten_cli/profiles.py +316 -0
- kaiten_cli/registry/__init__.py +260 -0
- kaiten_cli/registry/addons.py +392 -0
- kaiten_cli/registry/audit_and_analytics.py +451 -0
- kaiten_cli/registry/automation_schema.py +44 -0
- kaiten_cli/registry/automation_schemas.json +7417 -0
- kaiten_cli/registry/automations.py +342 -0
- kaiten_cli/registry/base.py +62 -0
- kaiten_cli/registry/blockers.py +320 -0
- kaiten_cli/registry/boards.py +520 -0
- kaiten_cli/registry/card_relations.py +376 -0
- kaiten_cli/registry/card_types.py +363 -0
- kaiten_cli/registry/cards.py +1329 -0
- kaiten_cli/registry/charts.py +539 -0
- kaiten_cli/registry/checklists.py +600 -0
- kaiten_cli/registry/columns.py +558 -0
- kaiten_cli/registry/comments.py +224 -0
- kaiten_cli/registry/custom_directories.py +980 -0
- kaiten_cli/registry/custom_properties.py +1460 -0
- kaiten_cli/registry/dashboards.py +564 -0
- kaiten_cli/registry/documents.py +761 -0
- kaiten_cli/registry/external_links.py +128 -0
- kaiten_cli/registry/files.py +738 -0
- kaiten_cli/registry/github_addon.py +613 -0
- kaiten_cli/registry/iterations.py +397 -0
- kaiten_cli/registry/lanes.py +205 -0
- kaiten_cli/registry/live_contracts.py +361 -0
- kaiten_cli/registry/members.py +336 -0
- kaiten_cli/registry/module_docs.py +108 -0
- kaiten_cli/registry/projects.py +369 -0
- kaiten_cli/registry/query.py +129 -0
- kaiten_cli/registry/restrictions.py +249 -0
- kaiten_cli/registry/roles_and_groups.py +1102 -0
- kaiten_cli/registry/scim.py +263 -0
- kaiten_cli/registry/service_desk.py +1270 -0
- kaiten_cli/registry/snapshot.py +187 -0
- kaiten_cli/registry/spaces.py +272 -0
- kaiten_cli/registry/subscribers.py +168 -0
- kaiten_cli/registry/tags.py +193 -0
- kaiten_cli/registry/time_logs.py +353 -0
- kaiten_cli/registry/tree.py +352 -0
- kaiten_cli/registry/utilities.py +287 -0
- kaiten_cli/registry/webhooks.py +265 -0
- kaiten_cli/runtime/__init__.py +1 -0
- kaiten_cli/runtime/automation_validation.py +115 -0
- kaiten_cli/runtime/behaviors.py +787 -0
- kaiten_cli/runtime/cache.py +733 -0
- kaiten_cli/runtime/client.py +387 -0
- kaiten_cli/runtime/endpoints.py +59 -0
- kaiten_cli/runtime/executor.py +269 -0
- kaiten_cli/runtime/fs_security.py +168 -0
- kaiten_cli/runtime/input.py +310 -0
- kaiten_cli/runtime/output.py +59 -0
- kaiten_cli/runtime/public_validation.py +35 -0
- kaiten_cli/runtime/snapshot_build.py +527 -0
- kaiten_cli/runtime/snapshot_common.py +363 -0
- kaiten_cli/runtime/snapshot_query.py +306 -0
- kaiten_cli/runtime/snapshot_store.py +1192 -0
- kaiten_cli/runtime/snapshots.py +62 -0
- kaiten_cli/runtime/sqlite_errors.py +23 -0
- kaiten_cli/runtime/support/__init__.py +1 -0
- kaiten_cli/runtime/support/addons.py +953 -0
- kaiten_cli/runtime/support/audit.py +70 -0
- kaiten_cli/runtime/support/batch.py +234 -0
- kaiten_cli/runtime/support/card_move_url.py +213 -0
- kaiten_cli/runtime/support/cards.py +174 -0
- kaiten_cli/runtime/support/checklists.py +21 -0
- kaiten_cli/runtime/support/company_users.py +117 -0
- kaiten_cli/runtime/support/dashboards.py +35 -0
- kaiten_cli/runtime/support/documents.py +175 -0
- kaiten_cli/runtime/support/files.py +561 -0
- kaiten_cli/runtime/support/markdown_export.py +525 -0
- kaiten_cli/runtime/support/pagination.py +167 -0
- kaiten_cli/runtime/support/projects.py +66 -0
- kaiten_cli/runtime/support/relations.py +86 -0
- kaiten_cli/runtime/support/restrictions.py +62 -0
- kaiten_cli/runtime/support/spaces.py +20 -0
- kaiten_cli/runtime/support/time_logs.py +61 -0
- kaiten_cli/runtime/support/tree.py +140 -0
- kaiten_cli/runtime/support/tree_sharing.py +447 -0
- kaiten_cli/runtime/trace.py +500 -0
- kaiten_cli/runtime/transforms.py +128 -0
- kaiten_cli/schema_docs.py +22 -0
- kaiten_cli/update_check.py +519 -0
- kaiten_cli-0.3.3.dist-info/METADATA +1110 -0
- kaiten_cli-0.3.3.dist-info/RECORD +99 -0
- kaiten_cli-0.3.3.dist-info/WHEEL +4 -0
- kaiten_cli-0.3.3.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
"""Best-effort update checks for interactive CLI runs.
|
|
2
|
+
|
|
3
|
+
The update path is deliberately isolated from normal command execution: it only
|
|
4
|
+
runs after a successful human-facing command, never changes JSON output, and
|
|
5
|
+
fails open when install metadata, Git, the network, or the installer is absent.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import contextlib
|
|
11
|
+
import hashlib
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
import re
|
|
15
|
+
import shutil
|
|
16
|
+
import subprocess
|
|
17
|
+
import sys
|
|
18
|
+
import tempfile
|
|
19
|
+
import time
|
|
20
|
+
from dataclasses import dataclass
|
|
21
|
+
from importlib import metadata
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
from typing import Any
|
|
24
|
+
from urllib.parse import urlencode, urlsplit, urlunsplit
|
|
25
|
+
|
|
26
|
+
import click
|
|
27
|
+
from platformdirs import user_cache_path
|
|
28
|
+
|
|
29
|
+
from kaiten_cli.i18n import tr
|
|
30
|
+
from kaiten_cli.runtime.fs_security import (
|
|
31
|
+
PRIVATE_FILE_MODE,
|
|
32
|
+
secure_directory,
|
|
33
|
+
secure_existing_file,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
PACKAGE_NAME = "kaiten-cli"
|
|
37
|
+
UPDATE_CHECK_ENV = "KAITEN_CLI_UPDATE_CHECK"
|
|
38
|
+
UPDATE_CACHE_SCHEMA_VERSION = 1
|
|
39
|
+
UPDATE_CHECK_INTERVAL_SECONDS = 24 * 60 * 60
|
|
40
|
+
GIT_CHECK_TIMEOUT_SECONDS = 8
|
|
41
|
+
UV_DETECTION_TIMEOUT_SECONDS = 2
|
|
42
|
+
|
|
43
|
+
_RELEASE_TAG_RE = re.compile(r"^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$")
|
|
44
|
+
_VERSION_RE = re.compile(r"^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$")
|
|
45
|
+
_COMMIT_RE = re.compile(r"^[0-9a-fA-F]{7,40}$")
|
|
46
|
+
_REMOTE_GIT_SCHEMES = frozenset({"git", "http", "https", "ssh"})
|
|
47
|
+
_FALSE_VALUES = frozenset({"0", "false", "no", "off"})
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass(frozen=True, slots=True)
|
|
51
|
+
class InstallSource:
|
|
52
|
+
"""A supported remote Git installation and its owning installer."""
|
|
53
|
+
|
|
54
|
+
manager: str
|
|
55
|
+
manager_executable: str | None
|
|
56
|
+
git_url: str
|
|
57
|
+
installed_version: str
|
|
58
|
+
requested_revision: str | None = None
|
|
59
|
+
subdirectory: str | None = None
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def pinned_release(self) -> bool:
|
|
63
|
+
return _parse_release_tag(self.requested_revision) is not None
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def immutable_commit(self) -> bool:
|
|
67
|
+
return bool(self.requested_revision and _COMMIT_RE.fullmatch(self.requested_revision))
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(frozen=True, slots=True)
|
|
71
|
+
class UpdateCandidate:
|
|
72
|
+
source: InstallSource
|
|
73
|
+
latest_tag: str
|
|
74
|
+
source_key: str
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def update_cache_path() -> Path:
|
|
78
|
+
return user_cache_path("kaiten-cli") / "update-check.json"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _normalize_package_name(value: str) -> str:
|
|
82
|
+
return re.sub(r"[-_.]+", "-", value).lower()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _read_json_text(value: str | None) -> dict[str, Any] | None:
|
|
86
|
+
if not value:
|
|
87
|
+
return None
|
|
88
|
+
try:
|
|
89
|
+
payload = json.loads(value)
|
|
90
|
+
except (TypeError, json.JSONDecodeError):
|
|
91
|
+
return None
|
|
92
|
+
return payload if isinstance(payload, dict) else None
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _read_json_file(path: Path) -> dict[str, Any] | None:
|
|
96
|
+
try:
|
|
97
|
+
payload = json.loads(path.read_text(encoding="utf-8"))
|
|
98
|
+
except (OSError, json.JSONDecodeError):
|
|
99
|
+
return None
|
|
100
|
+
return payload if isinstance(payload, dict) else None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _pipx_metadata(prefix: Path) -> dict[str, Any] | None:
|
|
104
|
+
payload = _read_json_file(prefix / "pipx_metadata.json")
|
|
105
|
+
if payload is None:
|
|
106
|
+
return None
|
|
107
|
+
main_package = payload.get("main_package")
|
|
108
|
+
if not isinstance(main_package, dict):
|
|
109
|
+
return None
|
|
110
|
+
package = main_package.get("package")
|
|
111
|
+
if not isinstance(package, str) or _normalize_package_name(package) != PACKAGE_NAME:
|
|
112
|
+
return None
|
|
113
|
+
suffix = main_package.get("suffix")
|
|
114
|
+
if suffix not in (None, ""):
|
|
115
|
+
return None
|
|
116
|
+
return payload
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _path_is_within(path: Path, parent: Path) -> bool:
|
|
120
|
+
try:
|
|
121
|
+
path.resolve().relative_to(parent.resolve())
|
|
122
|
+
except (OSError, ValueError):
|
|
123
|
+
return False
|
|
124
|
+
return True
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _uv_tool_executable(prefix: Path) -> str | None:
|
|
128
|
+
executable = shutil.which("uv")
|
|
129
|
+
if executable is None:
|
|
130
|
+
return None
|
|
131
|
+
try:
|
|
132
|
+
result = subprocess.run(
|
|
133
|
+
[executable, "tool", "dir"],
|
|
134
|
+
capture_output=True,
|
|
135
|
+
text=True,
|
|
136
|
+
timeout=UV_DETECTION_TIMEOUT_SECONDS,
|
|
137
|
+
check=False,
|
|
138
|
+
)
|
|
139
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
140
|
+
return None
|
|
141
|
+
if result.returncode != 0 or not result.stdout.strip():
|
|
142
|
+
return None
|
|
143
|
+
return executable if _path_is_within(prefix, Path(result.stdout.strip())) else None
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _git_transport_url(value: str) -> str:
|
|
147
|
+
return value[4:] if value.startswith("git+") else value
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _is_remote_git_url(value: str) -> bool:
|
|
151
|
+
parsed = urlsplit(_git_transport_url(value))
|
|
152
|
+
return bool(parsed.hostname and parsed.scheme.lower() in _REMOTE_GIT_SCHEMES)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def detect_install_source() -> InstallSource | None:
|
|
156
|
+
"""Return a supported install source, or ``None`` for local/unknown installs."""
|
|
157
|
+
|
|
158
|
+
try:
|
|
159
|
+
distribution = metadata.distribution(PACKAGE_NAME)
|
|
160
|
+
except metadata.PackageNotFoundError:
|
|
161
|
+
return None
|
|
162
|
+
|
|
163
|
+
direct_url = _read_json_text(distribution.read_text("direct_url.json"))
|
|
164
|
+
if direct_url is None or isinstance(direct_url.get("dir_info"), dict):
|
|
165
|
+
return None
|
|
166
|
+
|
|
167
|
+
vcs_info = direct_url.get("vcs_info")
|
|
168
|
+
git_url = direct_url.get("url")
|
|
169
|
+
if (
|
|
170
|
+
not isinstance(vcs_info, dict)
|
|
171
|
+
or vcs_info.get("vcs") != "git"
|
|
172
|
+
or not isinstance(git_url, str)
|
|
173
|
+
or not _is_remote_git_url(git_url)
|
|
174
|
+
):
|
|
175
|
+
return None
|
|
176
|
+
|
|
177
|
+
requested_revision = vcs_info.get("requested_revision")
|
|
178
|
+
if not isinstance(requested_revision, str) or not requested_revision.strip():
|
|
179
|
+
requested_revision = None
|
|
180
|
+
subdirectory = direct_url.get("subdirectory")
|
|
181
|
+
if not isinstance(subdirectory, str) or not subdirectory.strip():
|
|
182
|
+
subdirectory = None
|
|
183
|
+
|
|
184
|
+
prefix = Path(sys.prefix)
|
|
185
|
+
pipx = _pipx_metadata(prefix)
|
|
186
|
+
if pipx is not None:
|
|
187
|
+
manager = "pipx"
|
|
188
|
+
manager_executable = shutil.which("pipx")
|
|
189
|
+
else:
|
|
190
|
+
uv_executable = _uv_tool_executable(prefix)
|
|
191
|
+
if uv_executable is not None:
|
|
192
|
+
manager = "uv"
|
|
193
|
+
manager_executable = uv_executable
|
|
194
|
+
else:
|
|
195
|
+
manager = "pip"
|
|
196
|
+
manager_executable = sys.executable
|
|
197
|
+
|
|
198
|
+
return InstallSource(
|
|
199
|
+
manager=manager,
|
|
200
|
+
manager_executable=manager_executable,
|
|
201
|
+
git_url=_git_transport_url(git_url),
|
|
202
|
+
installed_version=distribution.version,
|
|
203
|
+
requested_revision=requested_revision,
|
|
204
|
+
subdirectory=subdirectory,
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _parse_release_tag(value: str | None) -> tuple[int, int, int] | None:
|
|
209
|
+
if value is None:
|
|
210
|
+
return None
|
|
211
|
+
match = _RELEASE_TAG_RE.fullmatch(value.strip())
|
|
212
|
+
if match is None:
|
|
213
|
+
return None
|
|
214
|
+
return tuple(int(part) for part in match.groups())
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _parse_installed_version(value: str) -> tuple[int, int, int] | None:
|
|
218
|
+
match = _VERSION_RE.fullmatch(value.strip())
|
|
219
|
+
if match is None:
|
|
220
|
+
return None
|
|
221
|
+
return tuple(int(part) for part in match.groups())
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _latest_remote_release(git_url: str) -> tuple[bool, str | None]:
|
|
225
|
+
git = shutil.which("git")
|
|
226
|
+
if git is None:
|
|
227
|
+
return False, None
|
|
228
|
+
env = os.environ.copy()
|
|
229
|
+
env["GIT_TERMINAL_PROMPT"] = "0"
|
|
230
|
+
env["SSH_ASKPASS_REQUIRE"] = "never"
|
|
231
|
+
try:
|
|
232
|
+
result = subprocess.run(
|
|
233
|
+
[git, "ls-remote", "--tags", "--refs", git_url, "refs/tags/v*"],
|
|
234
|
+
capture_output=True,
|
|
235
|
+
text=True,
|
|
236
|
+
timeout=GIT_CHECK_TIMEOUT_SECONDS,
|
|
237
|
+
check=False,
|
|
238
|
+
env=env,
|
|
239
|
+
)
|
|
240
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
241
|
+
return False, None
|
|
242
|
+
if result.returncode != 0:
|
|
243
|
+
return False, None
|
|
244
|
+
|
|
245
|
+
releases: list[tuple[tuple[int, int, int], str]] = []
|
|
246
|
+
for line in result.stdout.splitlines():
|
|
247
|
+
parts = line.split()
|
|
248
|
+
if len(parts) != 2 or not parts[1].startswith("refs/tags/"):
|
|
249
|
+
continue
|
|
250
|
+
tag = parts[1].removeprefix("refs/tags/")
|
|
251
|
+
version = _parse_release_tag(tag)
|
|
252
|
+
if version is not None:
|
|
253
|
+
releases.append((version, tag))
|
|
254
|
+
if not releases:
|
|
255
|
+
return True, None
|
|
256
|
+
return True, max(releases)[1]
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def _display_git_source(git_url: str) -> str:
|
|
260
|
+
parsed = urlsplit(git_url)
|
|
261
|
+
if not parsed.hostname:
|
|
262
|
+
return "remote Git source"
|
|
263
|
+
host = parsed.hostname
|
|
264
|
+
if parsed.port is not None:
|
|
265
|
+
host = f"{host}:{parsed.port}"
|
|
266
|
+
return f"{host}{parsed.path}"
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _source_key(source: InstallSource) -> str:
|
|
270
|
+
display = _display_git_source(source.git_url).lower()
|
|
271
|
+
material = f"{display}\0{source.subdirectory or ''}".encode()
|
|
272
|
+
return hashlib.sha256(material).hexdigest()
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def _empty_cache() -> dict[str, Any]:
|
|
276
|
+
return {"schema_version": UPDATE_CACHE_SCHEMA_VERSION, "sources": {}}
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def _load_cache() -> dict[str, Any]:
|
|
280
|
+
path = update_cache_path()
|
|
281
|
+
try:
|
|
282
|
+
secure_existing_file(path)
|
|
283
|
+
except OSError:
|
|
284
|
+
return _empty_cache()
|
|
285
|
+
payload = _read_json_file(path)
|
|
286
|
+
if (
|
|
287
|
+
payload is None
|
|
288
|
+
or payload.get("schema_version") != UPDATE_CACHE_SCHEMA_VERSION
|
|
289
|
+
or not isinstance(payload.get("sources"), dict)
|
|
290
|
+
):
|
|
291
|
+
return _empty_cache()
|
|
292
|
+
return payload
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def _save_cache(payload: dict[str, Any]) -> None:
|
|
296
|
+
path = update_cache_path()
|
|
297
|
+
temporary_path: Path | None = None
|
|
298
|
+
descriptor: int | None = None
|
|
299
|
+
try:
|
|
300
|
+
secure_directory(path.parent)
|
|
301
|
+
descriptor, temporary_name = tempfile.mkstemp(prefix=f".{path.name}.", dir=path.parent)
|
|
302
|
+
temporary_path = Path(temporary_name)
|
|
303
|
+
fchmod = getattr(os, "fchmod", None)
|
|
304
|
+
if fchmod is not None:
|
|
305
|
+
fchmod(descriptor, PRIVATE_FILE_MODE)
|
|
306
|
+
else: # pragma: no cover - Windows fallback
|
|
307
|
+
temporary_path.chmod(PRIVATE_FILE_MODE)
|
|
308
|
+
with os.fdopen(descriptor, "w", encoding="utf-8") as stream:
|
|
309
|
+
descriptor = None
|
|
310
|
+
json.dump(payload, stream, ensure_ascii=False, separators=(",", ":"))
|
|
311
|
+
stream.write("\n")
|
|
312
|
+
stream.flush()
|
|
313
|
+
os.fsync(stream.fileno())
|
|
314
|
+
temporary_path.replace(path)
|
|
315
|
+
secure_existing_file(path)
|
|
316
|
+
except OSError:
|
|
317
|
+
return
|
|
318
|
+
finally:
|
|
319
|
+
if descriptor is not None:
|
|
320
|
+
os.close(descriptor)
|
|
321
|
+
if temporary_path is not None:
|
|
322
|
+
with contextlib.suppress(OSError):
|
|
323
|
+
temporary_path.unlink(missing_ok=True)
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def _record_for(cache: dict[str, Any], source_key: str) -> dict[str, Any]:
|
|
327
|
+
sources = cache.setdefault("sources", {})
|
|
328
|
+
existing = sources.get(source_key)
|
|
329
|
+
if isinstance(existing, dict):
|
|
330
|
+
return existing
|
|
331
|
+
record: dict[str, Any] = {}
|
|
332
|
+
sources[source_key] = record
|
|
333
|
+
return record
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def check_for_update(source: InstallSource, *, now: float | None = None) -> UpdateCandidate | None:
|
|
337
|
+
"""Return a newer stable release while respecting check and prompt TTLs."""
|
|
338
|
+
|
|
339
|
+
installed_version = _parse_installed_version(source.installed_version)
|
|
340
|
+
if installed_version is None or source.immutable_commit:
|
|
341
|
+
return None
|
|
342
|
+
|
|
343
|
+
current_time = time.time() if now is None else now
|
|
344
|
+
key = _source_key(source)
|
|
345
|
+
cache = _load_cache()
|
|
346
|
+
record = _record_for(cache, key)
|
|
347
|
+
checked_at = record.get("checked_at")
|
|
348
|
+
latest_tag = record.get("latest_tag")
|
|
349
|
+
cache_is_fresh = isinstance(checked_at, (int, float)) and (
|
|
350
|
+
current_time - float(checked_at) < UPDATE_CHECK_INTERVAL_SECONDS
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
if not cache_is_fresh:
|
|
354
|
+
succeeded, fetched_tag = _latest_remote_release(source.git_url)
|
|
355
|
+
record["checked_at"] = current_time
|
|
356
|
+
if succeeded:
|
|
357
|
+
latest_tag = fetched_tag
|
|
358
|
+
record["latest_tag"] = fetched_tag
|
|
359
|
+
_save_cache(cache)
|
|
360
|
+
|
|
361
|
+
if not isinstance(latest_tag, str):
|
|
362
|
+
return None
|
|
363
|
+
latest_version = _parse_release_tag(latest_tag)
|
|
364
|
+
if latest_version is None or latest_version <= installed_version:
|
|
365
|
+
return None
|
|
366
|
+
|
|
367
|
+
prompted_tag = record.get("prompted_tag")
|
|
368
|
+
prompted_at = record.get("prompted_at")
|
|
369
|
+
if (
|
|
370
|
+
prompted_tag == latest_tag
|
|
371
|
+
and isinstance(prompted_at, (int, float))
|
|
372
|
+
and current_time - float(prompted_at) < UPDATE_CHECK_INTERVAL_SECONDS
|
|
373
|
+
):
|
|
374
|
+
return None
|
|
375
|
+
return UpdateCandidate(source=source, latest_tag=latest_tag, source_key=key)
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
def _record_prompted(candidate: UpdateCandidate, *, now: float | None = None) -> None:
|
|
379
|
+
cache = _load_cache()
|
|
380
|
+
record = _record_for(cache, candidate.source_key)
|
|
381
|
+
record["prompted_tag"] = candidate.latest_tag
|
|
382
|
+
record["prompted_at"] = time.time() if now is None else now
|
|
383
|
+
_save_cache(cache)
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
def _git_install_spec(source: InstallSource, revision: str | None) -> str:
|
|
387
|
+
url = urlunsplit(urlsplit(source.git_url))
|
|
388
|
+
spec = f"git+{url}"
|
|
389
|
+
if revision:
|
|
390
|
+
spec = f"{spec}@{revision}"
|
|
391
|
+
if source.subdirectory:
|
|
392
|
+
spec = f"{spec}#{urlencode({'subdirectory': source.subdirectory})}"
|
|
393
|
+
return spec
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def build_update_command(source: InstallSource, latest_tag: str) -> list[str] | None:
|
|
397
|
+
"""Build a shell-free installer command for the detected install mode."""
|
|
398
|
+
|
|
399
|
+
executable = source.manager_executable
|
|
400
|
+
if executable is None:
|
|
401
|
+
return None
|
|
402
|
+
if source.pinned_release:
|
|
403
|
+
specification = _git_install_spec(source, latest_tag)
|
|
404
|
+
if source.manager == "pipx":
|
|
405
|
+
return [executable, "install", "--force", specification]
|
|
406
|
+
if source.manager == "uv":
|
|
407
|
+
return [executable, "tool", "install", "--force", specification]
|
|
408
|
+
return [executable, "-m", "pip", "install", "--upgrade", specification]
|
|
409
|
+
|
|
410
|
+
if source.manager == "pipx":
|
|
411
|
+
return [executable, "upgrade", PACKAGE_NAME]
|
|
412
|
+
if source.manager == "uv":
|
|
413
|
+
return [executable, "tool", "upgrade", PACKAGE_NAME]
|
|
414
|
+
return [
|
|
415
|
+
executable,
|
|
416
|
+
"-m",
|
|
417
|
+
"pip",
|
|
418
|
+
"install",
|
|
419
|
+
"--upgrade",
|
|
420
|
+
_git_install_spec(source, source.requested_revision),
|
|
421
|
+
]
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def _perform_update(candidate: UpdateCandidate) -> int:
|
|
425
|
+
command = build_update_command(candidate.source, candidate.latest_tag)
|
|
426
|
+
if command is None:
|
|
427
|
+
click.echo(
|
|
428
|
+
tr(
|
|
429
|
+
"Unable to update automatically: {value_0} is not available on PATH.",
|
|
430
|
+
value_0=candidate.source.manager,
|
|
431
|
+
),
|
|
432
|
+
err=True,
|
|
433
|
+
)
|
|
434
|
+
return 1
|
|
435
|
+
try:
|
|
436
|
+
result = subprocess.run(command, check=False)
|
|
437
|
+
except OSError as exc:
|
|
438
|
+
click.echo(
|
|
439
|
+
tr(
|
|
440
|
+
"Automatic update failed: {value_0} could not be started ({value_1}).",
|
|
441
|
+
value_0=candidate.source.manager,
|
|
442
|
+
value_1=type(exc).__name__,
|
|
443
|
+
),
|
|
444
|
+
err=True,
|
|
445
|
+
)
|
|
446
|
+
return 1
|
|
447
|
+
if result.returncode != 0:
|
|
448
|
+
click.echo(
|
|
449
|
+
tr(
|
|
450
|
+
"Automatic update failed in {value_0} (exit code {value_1}).",
|
|
451
|
+
value_0=candidate.source.manager,
|
|
452
|
+
value_1=result.returncode,
|
|
453
|
+
),
|
|
454
|
+
err=True,
|
|
455
|
+
)
|
|
456
|
+
return result.returncode if result.returncode > 0 else 1
|
|
457
|
+
click.echo(
|
|
458
|
+
tr(
|
|
459
|
+
"kaiten-cli {value_0} installed. The new version will be used on the next run.",
|
|
460
|
+
value_0=candidate.latest_tag.removeprefix("v"),
|
|
461
|
+
),
|
|
462
|
+
err=True,
|
|
463
|
+
)
|
|
464
|
+
return 0
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
def should_check_for_updates(
|
|
468
|
+
args: list[str], *, stdin: Any | None = None, stderr: Any | None = None
|
|
469
|
+
) -> bool:
|
|
470
|
+
"""Keep update traffic and prompts out of automation and exact-output modes."""
|
|
471
|
+
|
|
472
|
+
if not args or "--json" in args or "--no-update-check" in args:
|
|
473
|
+
return False
|
|
474
|
+
if any(argument in {"-h", "--help", "--version"} for argument in args):
|
|
475
|
+
return False
|
|
476
|
+
setting = os.environ.get(UPDATE_CHECK_ENV)
|
|
477
|
+
if setting is not None and setting.strip().lower() in _FALSE_VALUES:
|
|
478
|
+
return False
|
|
479
|
+
if os.environ.get("_KAITEN_COMPLETE"):
|
|
480
|
+
return False
|
|
481
|
+
input_stream = sys.stdin if stdin is None else stdin
|
|
482
|
+
error_stream = sys.stderr if stderr is None else stderr
|
|
483
|
+
return bool(input_stream.isatty() and error_stream.isatty())
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
def maybe_offer_update(args: list[str]) -> int:
|
|
487
|
+
"""Check and optionally install an update after a successful CLI command."""
|
|
488
|
+
|
|
489
|
+
if not should_check_for_updates(args):
|
|
490
|
+
return 0
|
|
491
|
+
source = detect_install_source()
|
|
492
|
+
if source is None:
|
|
493
|
+
return 0
|
|
494
|
+
candidate = check_for_update(source)
|
|
495
|
+
if candidate is None:
|
|
496
|
+
return 0
|
|
497
|
+
|
|
498
|
+
source_display = _display_git_source(source.git_url)
|
|
499
|
+
prompt = tr(
|
|
500
|
+
"kaiten-cli {version} is available (installed {installed}, source {source}). Update now with {manager}?",
|
|
501
|
+
version=candidate.latest_tag.removeprefix("v"),
|
|
502
|
+
installed=source.installed_version,
|
|
503
|
+
source=source_display,
|
|
504
|
+
manager=source.manager,
|
|
505
|
+
)
|
|
506
|
+
try:
|
|
507
|
+
confirmed = click.confirm(prompt, default=False, err=True)
|
|
508
|
+
except click.Abort:
|
|
509
|
+
confirmed = False
|
|
510
|
+
if not confirmed:
|
|
511
|
+
_record_prompted(candidate)
|
|
512
|
+
click.echo(
|
|
513
|
+
tr("Update skipped; this release can be offered again after 24 hours."), err=True
|
|
514
|
+
)
|
|
515
|
+
return 0
|
|
516
|
+
result = _perform_update(candidate)
|
|
517
|
+
if result == 0:
|
|
518
|
+
_record_prompted(candidate)
|
|
519
|
+
return result
|