itqan-cli 0.1.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.
- itqan_cli/__init__.py +1 -0
- itqan_cli/__main__.py +5 -0
- itqan_cli/browse.py +344 -0
- itqan_cli/client.py +334 -0
- itqan_cli/credentials.py +91 -0
- itqan_cli/downloader.py +220 -0
- itqan_cli/exceptions.py +69 -0
- itqan_cli/init_template.py +118 -0
- itqan_cli/lockfile.py +285 -0
- itqan_cli/main.py +535 -0
- itqan_cli/manifest.py +274 -0
- itqan_cli/semver.py +205 -0
- itqan_cli-0.1.0.dist-info/METADATA +147 -0
- itqan_cli-0.1.0.dist-info/RECORD +17 -0
- itqan_cli-0.1.0.dist-info/WHEEL +4 -0
- itqan_cli-0.1.0.dist-info/entry_points.txt +3 -0
- itqan_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
itqan_cli/main.py
ADDED
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
"""Itqan CLI entrypoint implementing `itqan install`, `sync`, `init` and `browse`."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
import sys
|
|
9
|
+
|
|
10
|
+
import click
|
|
11
|
+
|
|
12
|
+
from itqan_cli import browse, credentials
|
|
13
|
+
from itqan_cli.client import DEFAULT_REGISTRY_URL, CatalogLanguage, CatalogPackage, RegistryClient, ResolvedAssetPayload
|
|
14
|
+
from itqan_cli.downloader import AssetDownloader
|
|
15
|
+
from itqan_cli.exceptions import ItqanCliError, RegistryApiError
|
|
16
|
+
from itqan_cli.init_template import pick_samples, render_fallback, render_from_catalog, render_header
|
|
17
|
+
from itqan_cli.lockfile import (
|
|
18
|
+
AssetLockfile,
|
|
19
|
+
LockfileEntry,
|
|
20
|
+
LockfileState,
|
|
21
|
+
evaluate_lockfile_state,
|
|
22
|
+
serialize_lockfile,
|
|
23
|
+
)
|
|
24
|
+
from itqan_cli.manifest import AssetManifest, load_manifest, parse_manifest_content, validate_assets_dir
|
|
25
|
+
|
|
26
|
+
DEFAULT_ASSETS_DIR = "assets"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@click.group()
|
|
30
|
+
@click.version_option(package_name="itqan-cli", prog_name="itqan")
|
|
31
|
+
def cli() -> None:
|
|
32
|
+
"""Itqan Quranic Asset Manager.
|
|
33
|
+
|
|
34
|
+
Install and pin Itqan Quranic assets in your project.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _run_install(
|
|
39
|
+
manifest_path: str,
|
|
40
|
+
lockfile_path: str,
|
|
41
|
+
assets_dir: str | None,
|
|
42
|
+
registry_url: str,
|
|
43
|
+
api_key: str | None,
|
|
44
|
+
force: bool,
|
|
45
|
+
) -> int:
|
|
46
|
+
m_path = Path(manifest_path)
|
|
47
|
+
l_path = Path(lockfile_path)
|
|
48
|
+
|
|
49
|
+
click.echo(f"Evaluating asset declarations in {m_path}...")
|
|
50
|
+
state, manifest, lockfile = evaluate_lockfile_state(m_path, l_path)
|
|
51
|
+
|
|
52
|
+
if state == LockfileState.ABSENT:
|
|
53
|
+
click.echo(f"No manifest found at {m_path}. Nothing to install.", err=True)
|
|
54
|
+
return 1
|
|
55
|
+
|
|
56
|
+
if state == LockfileState.INVALID:
|
|
57
|
+
click.echo("Error: Manifest or lockfile is invalid or malformed.", err=True)
|
|
58
|
+
return 1
|
|
59
|
+
|
|
60
|
+
if state == LockfileState.ORPHAN:
|
|
61
|
+
click.echo(f"Error: Lockfile exists at {l_path} but manifest {m_path} is missing.", err=True)
|
|
62
|
+
return 1
|
|
63
|
+
|
|
64
|
+
# --assets-dir wins; otherwise the manifest's assets_dir, else "assets",
|
|
65
|
+
# both relative to the manifest's folder.
|
|
66
|
+
assert manifest is not None
|
|
67
|
+
a_dir = Path(assets_dir) if assets_dir else m_path.parent / (manifest.assets_dir or DEFAULT_ASSETS_DIR)
|
|
68
|
+
|
|
69
|
+
client = RegistryClient(base_url=registry_url, api_key=api_key)
|
|
70
|
+
downloader = AssetDownloader(assets_dir=a_dir)
|
|
71
|
+
|
|
72
|
+
resolved_payloads: list[ResolvedAssetPayload] = []
|
|
73
|
+
|
|
74
|
+
if state == LockfileState.FRESH and not force:
|
|
75
|
+
assert lockfile is not None
|
|
76
|
+
assert manifest is not None
|
|
77
|
+
click.echo("Lockfile is FRESH. Installing locked versions...")
|
|
78
|
+
# In FRESH mode, we query the registry with the locked exact pins
|
|
79
|
+
locked_requests = {
|
|
80
|
+
slug: manifest.assets[slug].to_request(version=entry.version) for slug, entry in lockfile.assets.items()
|
|
81
|
+
}
|
|
82
|
+
resolved_payloads = client.resolve_manifest(locked_requests)
|
|
83
|
+
else:
|
|
84
|
+
# MISSING or STALE or force: Resolve against declared constraints
|
|
85
|
+
assert manifest is not None
|
|
86
|
+
if state == LockfileState.STALE:
|
|
87
|
+
click.echo("Lockfile is STALE. Re-resolving against manifest...")
|
|
88
|
+
else:
|
|
89
|
+
click.echo("Resolving declared assets against package registry...")
|
|
90
|
+
|
|
91
|
+
resolved_payloads = client.resolve_manifest(manifest.registry_requests)
|
|
92
|
+
|
|
93
|
+
# Build the new lockfile in memory — do NOT write it yet.
|
|
94
|
+
# We only commit the lockfile after every asset has been downloaded
|
|
95
|
+
# successfully. This ensures the lockfile always reflects reality:
|
|
96
|
+
# if a download fails halfway, the old lockfile is preserved.
|
|
97
|
+
new_lock_entries: dict[str, LockfileEntry] = {}
|
|
98
|
+
for item in resolved_payloads:
|
|
99
|
+
declared = manifest.assets[item.entry_name]
|
|
100
|
+
new_lock_entries[item.entry_name] = LockfileEntry(
|
|
101
|
+
slug=item.entry_name,
|
|
102
|
+
constraint=declared.version,
|
|
103
|
+
version=item.resolved_version,
|
|
104
|
+
asset=declared.asset if declared.asset != item.entry_name else None,
|
|
105
|
+
language=declared.language,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
new_lockfile = AssetLockfile(
|
|
109
|
+
lockfile_version=1,
|
|
110
|
+
manifest_schema_version=manifest.schema_version,
|
|
111
|
+
assets=new_lock_entries,
|
|
112
|
+
)
|
|
113
|
+
pending_lockfile_bytes = serialize_lockfile(new_lockfile)
|
|
114
|
+
|
|
115
|
+
# --- Materialize assets BEFORE writing the lockfile ---
|
|
116
|
+
# If any download fails, we raise immediately and the lockfile is untouched.
|
|
117
|
+
click.echo(f"Materializing {len(resolved_payloads)} asset(s) into {a_dir}...")
|
|
118
|
+
download_summary = []
|
|
119
|
+
|
|
120
|
+
for item in resolved_payloads:
|
|
121
|
+
res = downloader.materialize_asset(item, force=force)
|
|
122
|
+
status_str = "downloaded" if res.downloaded else "cached (up-to-date)"
|
|
123
|
+
click.echo(f" * {res.slug} [{res.version}]: {status_str} -> {res.target_path}")
|
|
124
|
+
download_summary.append(res)
|
|
125
|
+
|
|
126
|
+
# --- All downloads succeeded: now atomically write the lockfile ---
|
|
127
|
+
if state != LockfileState.FRESH or force:
|
|
128
|
+
# Write to a sibling temp file then rename — prevents a partial write
|
|
129
|
+
# from leaving a corrupt lockfile if the process is interrupted.
|
|
130
|
+
tmp_fd, tmp_path_str = __import__("tempfile").mkstemp(
|
|
131
|
+
prefix=".itqan_assets_lock_",
|
|
132
|
+
suffix=".tmp",
|
|
133
|
+
dir=l_path.parent,
|
|
134
|
+
)
|
|
135
|
+
tmp_lock = Path(tmp_path_str)
|
|
136
|
+
try:
|
|
137
|
+
with open(tmp_fd, "wb") as f:
|
|
138
|
+
f.write(pending_lockfile_bytes)
|
|
139
|
+
os.replace(tmp_lock, l_path)
|
|
140
|
+
click.echo(f"Updated lockfile at {l_path}")
|
|
141
|
+
except Exception:
|
|
142
|
+
try:
|
|
143
|
+
tmp_lock.unlink()
|
|
144
|
+
except OSError:
|
|
145
|
+
pass
|
|
146
|
+
raise
|
|
147
|
+
|
|
148
|
+
click.echo(f"Success! {len(download_summary)} asset(s) materialized successfully.")
|
|
149
|
+
return 0
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
@cli.command("install")
|
|
153
|
+
@click.option(
|
|
154
|
+
"--manifest",
|
|
155
|
+
"-m",
|
|
156
|
+
"manifest_path",
|
|
157
|
+
default="itqan-assets.yaml",
|
|
158
|
+
show_default=True,
|
|
159
|
+
help="Path to itqan-assets.yaml manifest.",
|
|
160
|
+
)
|
|
161
|
+
@click.option(
|
|
162
|
+
"--lockfile",
|
|
163
|
+
"-l",
|
|
164
|
+
"lockfile_path",
|
|
165
|
+
default="itqan-assets.lock",
|
|
166
|
+
show_default=True,
|
|
167
|
+
help="Path to itqan-assets.lock lockfile.",
|
|
168
|
+
)
|
|
169
|
+
@click.option(
|
|
170
|
+
"--assets-dir",
|
|
171
|
+
"-d",
|
|
172
|
+
"assets_dir",
|
|
173
|
+
default=None,
|
|
174
|
+
help="Target directory for downloaded assets. Overrides the manifest's assets_dir "
|
|
175
|
+
f"(default: '{DEFAULT_ASSETS_DIR}' next to the manifest).",
|
|
176
|
+
)
|
|
177
|
+
@click.option(
|
|
178
|
+
"--registry-url",
|
|
179
|
+
envvar="ITQAN_REGISTRY_URL",
|
|
180
|
+
default=DEFAULT_REGISTRY_URL,
|
|
181
|
+
show_default=True,
|
|
182
|
+
help="URL of the Itqan Package Registry API.",
|
|
183
|
+
)
|
|
184
|
+
@click.option(
|
|
185
|
+
"--api-key",
|
|
186
|
+
envvar="ITQAN_API_KEY",
|
|
187
|
+
default=None,
|
|
188
|
+
help="API key for authentication & metrics attribution.",
|
|
189
|
+
)
|
|
190
|
+
@click.option(
|
|
191
|
+
"--force",
|
|
192
|
+
"-f",
|
|
193
|
+
is_flag=True,
|
|
194
|
+
default=False,
|
|
195
|
+
help="Force re-resolution and re-download of assets.",
|
|
196
|
+
)
|
|
197
|
+
def install_command(
|
|
198
|
+
manifest_path: str,
|
|
199
|
+
lockfile_path: str,
|
|
200
|
+
assets_dir: str | None,
|
|
201
|
+
registry_url: str,
|
|
202
|
+
api_key: str | None,
|
|
203
|
+
force: bool,
|
|
204
|
+
) -> None:
|
|
205
|
+
"""Install assets declared in itqan-assets.yaml."""
|
|
206
|
+
api_key = _api_key(api_key, registry_url)
|
|
207
|
+
try:
|
|
208
|
+
exit_code = _run_install(
|
|
209
|
+
manifest_path=manifest_path,
|
|
210
|
+
lockfile_path=lockfile_path,
|
|
211
|
+
assets_dir=assets_dir,
|
|
212
|
+
registry_url=registry_url,
|
|
213
|
+
api_key=api_key,
|
|
214
|
+
force=force,
|
|
215
|
+
)
|
|
216
|
+
sys.exit(exit_code)
|
|
217
|
+
except ItqanCliError as exc:
|
|
218
|
+
click.echo(f"Error: {exc.message}", err=True)
|
|
219
|
+
if isinstance(exc, RegistryApiError) and exc.status_code in (401, 403):
|
|
220
|
+
_explain_access(Path(manifest_path), registry_url, api_key)
|
|
221
|
+
sys.exit(exc.exit_code)
|
|
222
|
+
except Exception as exc:
|
|
223
|
+
click.echo(f"Unexpected error: {exc}", err=True)
|
|
224
|
+
sys.exit(1)
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _api_key(api_key: str | None, registry_url: str) -> str | None:
|
|
228
|
+
"""--api-key / ITQAN_API_KEY when given, else the key `itqan login` saved for this registry."""
|
|
229
|
+
if api_key:
|
|
230
|
+
return api_key
|
|
231
|
+
try:
|
|
232
|
+
return credentials.load_api_key(registry_url)
|
|
233
|
+
except ItqanCliError as exc:
|
|
234
|
+
click.echo(f"Warning: {exc.message}", err=True)
|
|
235
|
+
return None
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _explain_access(m_path: Path, registry_url: str, api_key: str | None) -> None:
|
|
239
|
+
"""After a 401/403, name the manifest's assets the caller can't install
|
|
240
|
+
and where to request access. Best effort: stays quiet if it can't tell."""
|
|
241
|
+
try:
|
|
242
|
+
manifest = load_manifest(m_path)
|
|
243
|
+
catalog = {p.slug: p for p in RegistryClient(base_url=registry_url, api_key=api_key).list_all_packages()}
|
|
244
|
+
except ItqanCliError:
|
|
245
|
+
return
|
|
246
|
+
declared = {entry.asset or entry.slug for entry in manifest.assets.values()}
|
|
247
|
+
for line in browse.access_footer([catalog[slug] for slug in sorted(declared) if slug in catalog], bool(api_key)):
|
|
248
|
+
click.echo(line, err=True)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
# Alias: `itqan sync` runs `install`
|
|
252
|
+
cli.add_command(install_command, name="sync")
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
@cli.command("init")
|
|
256
|
+
@click.option(
|
|
257
|
+
"--manifest",
|
|
258
|
+
"-m",
|
|
259
|
+
"manifest_path",
|
|
260
|
+
default="itqan-assets.yaml",
|
|
261
|
+
show_default=True,
|
|
262
|
+
help="Path of the manifest to create.",
|
|
263
|
+
)
|
|
264
|
+
@click.option(
|
|
265
|
+
"--assets-dir",
|
|
266
|
+
"-d",
|
|
267
|
+
"assets_dir",
|
|
268
|
+
default=DEFAULT_ASSETS_DIR,
|
|
269
|
+
show_default=True,
|
|
270
|
+
help="Folder, relative to the manifest, that `itqan install` downloads into.",
|
|
271
|
+
)
|
|
272
|
+
@click.option(
|
|
273
|
+
"--registry-url",
|
|
274
|
+
envvar="ITQAN_REGISTRY_URL",
|
|
275
|
+
default=DEFAULT_REGISTRY_URL,
|
|
276
|
+
show_default=True,
|
|
277
|
+
help="URL of the Itqan Package Registry API.",
|
|
278
|
+
)
|
|
279
|
+
@click.option(
|
|
280
|
+
"--api-key",
|
|
281
|
+
envvar="ITQAN_API_KEY",
|
|
282
|
+
default=None,
|
|
283
|
+
help="API key; without one, only open-access assets are suggested.",
|
|
284
|
+
)
|
|
285
|
+
@click.option("--force", "-f", is_flag=True, default=False, help="Overwrite an existing manifest.")
|
|
286
|
+
def init_command(manifest_path: str, assets_dir: str, registry_url: str, api_key: str | None, force: bool) -> None:
|
|
287
|
+
"""Create itqan-assets.yaml with a few real assets from the registry.
|
|
288
|
+
|
|
289
|
+
If the registry can't be reached, writes the same file with commented-out
|
|
290
|
+
example entries instead.
|
|
291
|
+
"""
|
|
292
|
+
m_path = Path(manifest_path)
|
|
293
|
+
if m_path.exists() and not force:
|
|
294
|
+
click.echo(f"Error: {m_path} already exists. Use --force to overwrite it.", err=True)
|
|
295
|
+
sys.exit(1)
|
|
296
|
+
|
|
297
|
+
api_key = _api_key(api_key, registry_url)
|
|
298
|
+
try:
|
|
299
|
+
validate_assets_dir(assets_dir)
|
|
300
|
+
client = RegistryClient(base_url=registry_url, api_key=api_key)
|
|
301
|
+
packages = client.list_packages(open_access=None if api_key else True)
|
|
302
|
+
samples = pick_samples([p for p in packages if browse.can_install(p, has_api_key=bool(api_key))])
|
|
303
|
+
if samples:
|
|
304
|
+
content = render_from_catalog(samples, assets_dir)
|
|
305
|
+
else:
|
|
306
|
+
click.echo("Warning: the registry has no installable assets yet; writing commented examples.", err=True)
|
|
307
|
+
content = render_fallback(assets_dir, reason="The registry had no installable assets,")
|
|
308
|
+
except RegistryApiError as exc:
|
|
309
|
+
click.echo(f"Warning: Could not reach the registry ({exc.message}); writing commented examples.", err=True)
|
|
310
|
+
samples = []
|
|
311
|
+
content = render_fallback(assets_dir, reason="Could not reach the registry,")
|
|
312
|
+
except ItqanCliError as exc:
|
|
313
|
+
click.echo(f"Error: {exc.message}", err=True)
|
|
314
|
+
sys.exit(exc.exit_code)
|
|
315
|
+
|
|
316
|
+
parse_manifest_content(content.encode("utf-8")) # never write a manifest `install` would reject
|
|
317
|
+
m_path.parent.mkdir(parents=True, exist_ok=True)
|
|
318
|
+
m_path.write_text(content, encoding="utf-8")
|
|
319
|
+
click.echo(f"Created {m_path} with {len(samples)} sample asset(s).")
|
|
320
|
+
click.echo("Edit it as needed, then run `itqan install` to download the assets.")
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
@cli.command("browse")
|
|
324
|
+
@click.argument("query", required=False)
|
|
325
|
+
@click.option("--category", "-c", default=None, help="Only assets of this category (mushaf, tafsir, translation, ...).")
|
|
326
|
+
@click.option(
|
|
327
|
+
"--manifest",
|
|
328
|
+
"-m",
|
|
329
|
+
"manifest_path",
|
|
330
|
+
default="itqan-assets.yaml",
|
|
331
|
+
show_default=True,
|
|
332
|
+
help="Manifest to add the picked assets to; created if missing.",
|
|
333
|
+
)
|
|
334
|
+
@click.option(
|
|
335
|
+
"--lockfile",
|
|
336
|
+
"-l",
|
|
337
|
+
"lockfile_path",
|
|
338
|
+
default="itqan-assets.lock",
|
|
339
|
+
show_default=True,
|
|
340
|
+
help="Lockfile used if you choose to install right away.",
|
|
341
|
+
)
|
|
342
|
+
@click.option(
|
|
343
|
+
"--interactive/--no-interactive",
|
|
344
|
+
default=None,
|
|
345
|
+
help="Pick assets to add (default when run in a terminal) or just print the list.",
|
|
346
|
+
)
|
|
347
|
+
@click.option(
|
|
348
|
+
"--json", "as_json", is_flag=True, default=False, help="Print the list as JSON (implies --no-interactive)."
|
|
349
|
+
)
|
|
350
|
+
@click.option(
|
|
351
|
+
"--registry-url",
|
|
352
|
+
envvar="ITQAN_REGISTRY_URL",
|
|
353
|
+
default=DEFAULT_REGISTRY_URL,
|
|
354
|
+
show_default=True,
|
|
355
|
+
help="URL of the Itqan Package Registry API.",
|
|
356
|
+
)
|
|
357
|
+
@click.option(
|
|
358
|
+
"--api-key",
|
|
359
|
+
envvar="ITQAN_API_KEY",
|
|
360
|
+
default=None,
|
|
361
|
+
help="API key; without one, assets that need access approval can't be picked.",
|
|
362
|
+
)
|
|
363
|
+
def browse_command(
|
|
364
|
+
query: str | None,
|
|
365
|
+
category: str | None,
|
|
366
|
+
manifest_path: str,
|
|
367
|
+
lockfile_path: str,
|
|
368
|
+
interactive: bool | None,
|
|
369
|
+
as_json: bool,
|
|
370
|
+
registry_url: str,
|
|
371
|
+
api_key: str | None,
|
|
372
|
+
) -> None:
|
|
373
|
+
"""Browse installable assets and add them to itqan-assets.yaml.
|
|
374
|
+
|
|
375
|
+
QUERY searches names, slugs, descriptions and publishers on the registry;
|
|
376
|
+
without it, every asset is listed and you can filter by typing in the picker.
|
|
377
|
+
"""
|
|
378
|
+
if interactive is None:
|
|
379
|
+
interactive = sys.stdin.isatty() and sys.stdout.isatty()
|
|
380
|
+
if as_json:
|
|
381
|
+
interactive = False
|
|
382
|
+
m_path = Path(manifest_path)
|
|
383
|
+
api_key = _api_key(api_key, registry_url)
|
|
384
|
+
|
|
385
|
+
try:
|
|
386
|
+
manifest = load_manifest(m_path) if m_path.exists() else None
|
|
387
|
+
targets = browse.manifest_targets(manifest)
|
|
388
|
+
client = RegistryClient(base_url=registry_url, api_key=api_key)
|
|
389
|
+
packages = client.list_all_packages(search=query, category=category)
|
|
390
|
+
except ItqanCliError as exc:
|
|
391
|
+
click.echo(f"Error: {exc.message}", err=True)
|
|
392
|
+
sys.exit(exc.exit_code)
|
|
393
|
+
|
|
394
|
+
if as_json:
|
|
395
|
+
click.echo(json.dumps(browse.to_json(packages, targets), ensure_ascii=False, indent=2))
|
|
396
|
+
return
|
|
397
|
+
if not packages:
|
|
398
|
+
click.echo("No assets match." if query or category else "The registry has no installable assets yet.")
|
|
399
|
+
return
|
|
400
|
+
if not interactive:
|
|
401
|
+
click.echo(browse.render_table(packages, targets))
|
|
402
|
+
if targets:
|
|
403
|
+
click.echo(f"\n* already in {m_path}")
|
|
404
|
+
for line in browse.access_footer(packages, has_api_key=bool(api_key)):
|
|
405
|
+
click.echo(line)
|
|
406
|
+
return
|
|
407
|
+
|
|
408
|
+
picked = browse.pick_packages(packages, targets, has_api_key=bool(api_key))
|
|
409
|
+
if not picked:
|
|
410
|
+
click.echo("Nothing added.")
|
|
411
|
+
return
|
|
412
|
+
click.echo(f"Picked: {', '.join(package.slug for package in picked)}")
|
|
413
|
+
picks: list[tuple[CatalogPackage, CatalogLanguage]] = []
|
|
414
|
+
for package in picked:
|
|
415
|
+
languages = browse.pick_languages(package, targets)
|
|
416
|
+
if languages is None: # Ctrl-C
|
|
417
|
+
click.echo("Nothing added.")
|
|
418
|
+
return
|
|
419
|
+
if len(browse.missing_languages(targets, package)) > 1:
|
|
420
|
+
click.echo(f" {package.slug}: {', '.join(lang.language or 'source' for lang in languages) or 'none'}")
|
|
421
|
+
picks += [(package, language) for language in languages]
|
|
422
|
+
|
|
423
|
+
try:
|
|
424
|
+
added = _add_to_manifest(m_path, manifest, picks)
|
|
425
|
+
except ItqanCliError as exc:
|
|
426
|
+
click.echo(f"Error: {exc.message}", err=True)
|
|
427
|
+
sys.exit(exc.exit_code)
|
|
428
|
+
if not added:
|
|
429
|
+
click.echo("Nothing added.")
|
|
430
|
+
return
|
|
431
|
+
click.echo(f"Added {len(added)} asset(s) to {m_path}: {', '.join(added)}")
|
|
432
|
+
|
|
433
|
+
import questionary
|
|
434
|
+
|
|
435
|
+
if questionary.confirm("Run `itqan install` now?", default=True).ask():
|
|
436
|
+
install_command.callback(
|
|
437
|
+
manifest_path=str(m_path),
|
|
438
|
+
lockfile_path=lockfile_path,
|
|
439
|
+
assets_dir=None,
|
|
440
|
+
registry_url=registry_url,
|
|
441
|
+
api_key=api_key,
|
|
442
|
+
force=False,
|
|
443
|
+
)
|
|
444
|
+
else:
|
|
445
|
+
click.echo("Run `itqan install` when you're ready to download them.")
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
def _add_to_manifest(
|
|
449
|
+
m_path: Path, manifest: AssetManifest | None, picks: list[tuple[CatalogPackage, CatalogLanguage]]
|
|
450
|
+
) -> list[str]:
|
|
451
|
+
"""Write the picked assets into the manifest (creating it if needed) and
|
|
452
|
+
return the new entry names."""
|
|
453
|
+
entries, warnings = browse.plan_entries(picks, manifest)
|
|
454
|
+
for warning in warnings:
|
|
455
|
+
click.echo(f"Warning: {warning}", err=True)
|
|
456
|
+
if not entries:
|
|
457
|
+
return []
|
|
458
|
+
content = m_path.read_text(encoding="utf-8") if manifest is not None else render_header(DEFAULT_ASSETS_DIR)
|
|
459
|
+
updated = browse.add_entries(content, entries)
|
|
460
|
+
m_path.parent.mkdir(parents=True, exist_ok=True)
|
|
461
|
+
m_path.write_text(updated, encoding="utf-8")
|
|
462
|
+
return [entry.name for entry in entries]
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
# Where users create API keys in the CMS web app.
|
|
466
|
+
API_KEYS_PAGE = "https://cms.itqan.dev/account/api-keys"
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
@cli.command("login")
|
|
470
|
+
@click.option(
|
|
471
|
+
"--registry-url",
|
|
472
|
+
envvar="ITQAN_REGISTRY_URL",
|
|
473
|
+
default=DEFAULT_REGISTRY_URL,
|
|
474
|
+
show_default=True,
|
|
475
|
+
help="Registry the key is for.",
|
|
476
|
+
)
|
|
477
|
+
@click.option("--api-key", default=None, help="The key; prompted for (hidden) when omitted.")
|
|
478
|
+
def login_command(registry_url: str, api_key: str | None) -> None:
|
|
479
|
+
"""Save your API key so commands can install assets you have approved access to.
|
|
480
|
+
|
|
481
|
+
The key is checked against the registry first, then saved for that registry
|
|
482
|
+
in your user config folder, readable only by you. --api-key or ITQAN_API_KEY
|
|
483
|
+
on another command still take precedence over the saved key.
|
|
484
|
+
"""
|
|
485
|
+
if not api_key:
|
|
486
|
+
click.echo(f"Create an API key at {API_KEYS_PAGE}")
|
|
487
|
+
api_key = click.prompt("API key", hide_input=True).strip()
|
|
488
|
+
try:
|
|
489
|
+
account = RegistryClient(base_url=registry_url, api_key=api_key).whoami()
|
|
490
|
+
except RegistryApiError as exc:
|
|
491
|
+
if exc.status_code == 401:
|
|
492
|
+
click.echo(f"Error: {registry_url} did not accept that API key. Nothing was saved.", err=True)
|
|
493
|
+
sys.exit(1)
|
|
494
|
+
if exc.status_code != 404:
|
|
495
|
+
click.echo(f"Error: {exc.message}", err=True)
|
|
496
|
+
sys.exit(exc.exit_code)
|
|
497
|
+
account = None # a registry without the key check: save the key unverified
|
|
498
|
+
click.echo(f"Warning: {registry_url} can't check API keys; saving it unverified.", err=True)
|
|
499
|
+
except ItqanCliError as exc:
|
|
500
|
+
click.echo(f"Error: {exc.message}", err=True)
|
|
501
|
+
sys.exit(exc.exit_code)
|
|
502
|
+
try:
|
|
503
|
+
path = credentials.save_api_key(registry_url, api_key)
|
|
504
|
+
except ItqanCliError as exc:
|
|
505
|
+
click.echo(f"Error: {exc.message}", err=True)
|
|
506
|
+
sys.exit(exc.exit_code)
|
|
507
|
+
if account is None:
|
|
508
|
+
who = ""
|
|
509
|
+
elif account.name and account.name != account.email:
|
|
510
|
+
who = f" as {account.name} <{account.email}>"
|
|
511
|
+
else:
|
|
512
|
+
who = f" as {account.email}"
|
|
513
|
+
click.echo(f"Logged in to {registry_url}{who}. Key saved in {path}.")
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
@cli.command("logout")
|
|
517
|
+
@click.option(
|
|
518
|
+
"--registry-url",
|
|
519
|
+
envvar="ITQAN_REGISTRY_URL",
|
|
520
|
+
default=DEFAULT_REGISTRY_URL,
|
|
521
|
+
show_default=True,
|
|
522
|
+
help="Registry whose saved key to remove.",
|
|
523
|
+
)
|
|
524
|
+
def logout_command(registry_url: str) -> None:
|
|
525
|
+
"""Remove the API key `itqan login` saved for the registry."""
|
|
526
|
+
try:
|
|
527
|
+
removed = credentials.delete_api_key(registry_url)
|
|
528
|
+
except ItqanCliError as exc:
|
|
529
|
+
click.echo(f"Error: {exc.message}", err=True)
|
|
530
|
+
sys.exit(exc.exit_code)
|
|
531
|
+
click.echo(f"Removed the saved key for {registry_url}." if removed else f"No key saved for {registry_url}.")
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
if __name__ == "__main__":
|
|
535
|
+
cli()
|