itqan-cli 0.3.0__tar.gz → 0.5.0__tar.gz
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-0.3.0 → itqan_cli-0.5.0}/CHANGELOG.md +21 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/PKG-INFO +9 -5
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/README.md +8 -4
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/pyproject.toml +1 -1
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/client.py +2 -9
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/downloader.py +30 -1
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/main.py +51 -7
- itqan_cli-0.5.0/src/itqan_cli/progress.py +134 -0
- itqan_cli-0.5.0/src/itqan_cli/usage.py +97 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/conftest.py +3 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_browse.py +59 -1
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_downloader.py +95 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_init.py +75 -0
- itqan_cli-0.5.0/tests/test_install_progress.py +124 -0
- itqan_cli-0.5.0/tests/test_progress.py +187 -0
- itqan_cli-0.5.0/tests/test_usage.py +287 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/.gitignore +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/LICENSE +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/RELEASING.md +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/dart/.gitignore +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/dart/README.md +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/dart/analysis_options.yaml +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/dart/bin/itqan.dart +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/dart/lib/itqan_cli.dart +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/dart/lib/src/launcher.dart +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/dart/lib/src/version.dart +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/dart/pubspec.yaml +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/dart/test/launcher_test.dart +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/npm/README.md +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/npm/bin/itqan.js +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/scripts/dart_package.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/scripts/homebrew_formula.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/scripts/npm_packages.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/__init__.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/__main__.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/add.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/browse.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/credentials.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/exceptions.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/flutter.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/init_template.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/lockfile.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/manifest.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/semver.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/src/itqan_cli/updates.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/__init__.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_access.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_add.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_assets_dir.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_cli_integration.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_client.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_dart_package.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_entry_points.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_flutter.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_homebrew_formula.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_languages.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_lockfile.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_login.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_manifest.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_npm_packages.py +0 -0
- {itqan_cli-0.3.0 → itqan_cli-0.5.0}/tests/test_updates.py +0 -0
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
## v0.5.0 (2026-10-21)
|
|
2
|
+
|
|
3
|
+
### Feat
|
|
4
|
+
|
|
5
|
+
- every request, including asset downloads, sends `User-Agent: itqan-cli/<version>`, a random
|
|
6
|
+
install ID (`X-Itqan-Install-Id`, kept in `<config dir>/install-id`) and the command's name
|
|
7
|
+
(`X-Itqan-Command`), so the registry can count CLI installs and usage. `DO_NOT_TRACK=1` or
|
|
8
|
+
`ITQAN_NO_TELEMETRY=1` leaves out the ID and command and creates no `install-id` file.
|
|
9
|
+
|
|
10
|
+
## v0.4.0 (2026-10-18)
|
|
11
|
+
|
|
12
|
+
### Feat
|
|
13
|
+
|
|
14
|
+
- **install**: `itqan install` shows each download's progress in a terminal: bytes done and total,
|
|
15
|
+
percent, rate and time left, or a running byte count when the size isn't known. Piped and CI
|
|
16
|
+
output stays the same; `--no-progress` or `TERM=dumb` turns it off.
|
|
17
|
+
- **init**: `itqan init` asks whether to run `itqan browse` once it has written the manifest, so
|
|
18
|
+
you can pick assets straight away.
|
|
19
|
+
- **browse**: in a Flutter project, `itqan browse` offers to run `itqan install --flutter`, so the
|
|
20
|
+
installed asset folders get listed in `pubspec.yaml`.
|
|
21
|
+
|
|
1
22
|
## v0.3.0 (2026-10-18)
|
|
2
23
|
|
|
3
24
|
### Feat
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: itqan-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: Itqan Quranic Asset Manager: install and pin Itqan Quranic assets in your project.
|
|
5
5
|
Project-URL: Homepage, https://github.com/Itqan-community/itqan-cli
|
|
6
6
|
Project-URL: Repository, https://github.com/Itqan-community/itqan-cli
|
|
@@ -159,7 +159,7 @@ Run these in your project's folder:
|
|
|
159
159
|
|
|
160
160
|
```bash
|
|
161
161
|
itqan init # 1. create itqan-assets.yaml, with commented-out example assets
|
|
162
|
-
itqan browse # 2. search the catalog and add the assets you want (
|
|
162
|
+
itqan browse # 2. search the catalog and add the assets you want (init offers to run it for you)
|
|
163
163
|
itqan install # 3. download them and write itqan-assets.lock
|
|
164
164
|
```
|
|
165
165
|
|
|
@@ -427,10 +427,10 @@ example, `npm ci` for a project with `itqan-cli` in its `devDependencies`, or
|
|
|
427
427
|
|
|
428
428
|
| Command | What it does |
|
|
429
429
|
|---|---|
|
|
430
|
-
| `itqan init` | Create `itqan-assets.yaml` with a few assets from the catalog as commented-out examples (placeholder examples when offline). `--assets-dir` sets where assets go; `--force` overwrites an existing manifest. |
|
|
430
|
+
| `itqan init` | Create `itqan-assets.yaml` with a few assets from the catalog as commented-out examples (placeholder examples when offline), then offers to run `itqan browse` when run in a terminal. `--assets-dir` sets where assets go; `--force` overwrites an existing manifest. |
|
|
431
431
|
| `itqan browse [SEARCH]` | Search the catalog, pick assets, add them to `itqan-assets.yaml` (created if missing), and optionally install them. `--category` limits it to one kind of asset; `--no-interactive` prints a table (the default when not in a terminal); `--json` prints JSON. |
|
|
432
432
|
| `itqan add ASSET[@RANGE]...` | Add assets to `itqan-assets.yaml` (created if missing) and install them. Without `@RANGE` an entry accepts `^` the newest version; an asset already in the manifest gets the new range. `--language` picks a language other than the source; `--no-install` only edits the manifest. |
|
|
433
|
-
| `itqan install` | Download the assets and write `itqan-assets.lock`. Uses the lockfile's exact versions while it matches the manifest. `--force` picks the newest allowed versions and downloads again. `--flutter` keeps `pubspec.yaml` up to date (see [Flutter](#flutter)). `--assets-dir` overrides the manifest's folder for one run. |
|
|
433
|
+
| `itqan install` | Download the assets and write `itqan-assets.lock`. Uses the lockfile's exact versions while it matches the manifest. `--force` picks the newest allowed versions and downloads again. `--flutter` keeps `pubspec.yaml` up to date (see [Flutter](#flutter)). `--assets-dir` overrides the manifest's folder for one run. In a terminal it shows each download's progress; `--no-progress` turns that off. |
|
|
434
434
|
| `itqan sync` | Same as `itqan install`. |
|
|
435
435
|
| `itqan outdated` | List the locked assets that have newer versions: the newest your range allows and the newest overall. `--json` prints JSON. |
|
|
436
436
|
| `itqan login` | Save your API key for the registry, after checking it. `--api-key` passes the key without the prompt. |
|
|
@@ -447,13 +447,17 @@ Add `--help` to any command for all its options. `install` and `browse` read the
|
|
|
447
447
|
| `--api-key` | `ITQAN_API_KEY` | The key saved by `itqan login`, if any. |
|
|
448
448
|
| `--registry-url` | `ITQAN_REGISTRY_URL` | `https://api.cms.itqan.dev`, the public registry. |
|
|
449
449
|
| | `ITQAN_NO_UPDATE_NOTICE` | Set to `1` to stop `itqan install` saying that newer versions exist. |
|
|
450
|
-
| | `
|
|
450
|
+
| | `DO_NOT_TRACK`, `ITQAN_NO_TELEMETRY` | Set to `1` to send no install ID or command name with requests (see below). |
|
|
451
|
+
| | `ITQAN_CONFIG_DIR` | Where `itqan login` saves keys and the install ID: `~/.config/itqan` (`%APPDATA%\itqan` on Windows). |
|
|
451
452
|
| | `ITQAN_CACHE_DIR` | Dart package only. Where the downloaded program is kept: `~/Library/Caches/itqan` (macOS), `~/.cache/itqan` (Linux), `%LOCALAPPDATA%\itqan` (Windows). |
|
|
452
453
|
| | `ITQAN_CLI_DOWNLOAD_URL` | Dart package only. A folder (`https://` or `file://`) with a release's programs and `SHA256SUMS`, such as a mirror, used instead of the GitHub Release. |
|
|
453
454
|
|
|
454
455
|
A key passed with `--api-key` or `ITQAN_API_KEY` is used instead of the saved one. The saved key
|
|
455
456
|
file is readable only by you.
|
|
456
457
|
|
|
458
|
+
**Usage counts.** Every request sends the CLI version (`User-Agent`), a random install ID kept in the config folder's `install-id` file, and the running command's name, so Itqan can count installs; nothing else about you or your project is sent.
|
|
459
|
+
Set `DO_NOT_TRACK=1` or `ITQAN_NO_TELEMETRY=1` to send only the version: no install ID or command, and no `install-id` file is created.
|
|
460
|
+
|
|
457
461
|
## Troubleshooting
|
|
458
462
|
|
|
459
463
|
**"command not found: itqan".** The folder `itqan` was installed into isn't on your `PATH`. For
|
|
@@ -128,7 +128,7 @@ Run these in your project's folder:
|
|
|
128
128
|
|
|
129
129
|
```bash
|
|
130
130
|
itqan init # 1. create itqan-assets.yaml, with commented-out example assets
|
|
131
|
-
itqan browse # 2. search the catalog and add the assets you want (
|
|
131
|
+
itqan browse # 2. search the catalog and add the assets you want (init offers to run it for you)
|
|
132
132
|
itqan install # 3. download them and write itqan-assets.lock
|
|
133
133
|
```
|
|
134
134
|
|
|
@@ -396,10 +396,10 @@ example, `npm ci` for a project with `itqan-cli` in its `devDependencies`, or
|
|
|
396
396
|
|
|
397
397
|
| Command | What it does |
|
|
398
398
|
|---|---|
|
|
399
|
-
| `itqan init` | Create `itqan-assets.yaml` with a few assets from the catalog as commented-out examples (placeholder examples when offline). `--assets-dir` sets where assets go; `--force` overwrites an existing manifest. |
|
|
399
|
+
| `itqan init` | Create `itqan-assets.yaml` with a few assets from the catalog as commented-out examples (placeholder examples when offline), then offers to run `itqan browse` when run in a terminal. `--assets-dir` sets where assets go; `--force` overwrites an existing manifest. |
|
|
400
400
|
| `itqan browse [SEARCH]` | Search the catalog, pick assets, add them to `itqan-assets.yaml` (created if missing), and optionally install them. `--category` limits it to one kind of asset; `--no-interactive` prints a table (the default when not in a terminal); `--json` prints JSON. |
|
|
401
401
|
| `itqan add ASSET[@RANGE]...` | Add assets to `itqan-assets.yaml` (created if missing) and install them. Without `@RANGE` an entry accepts `^` the newest version; an asset already in the manifest gets the new range. `--language` picks a language other than the source; `--no-install` only edits the manifest. |
|
|
402
|
-
| `itqan install` | Download the assets and write `itqan-assets.lock`. Uses the lockfile's exact versions while it matches the manifest. `--force` picks the newest allowed versions and downloads again. `--flutter` keeps `pubspec.yaml` up to date (see [Flutter](#flutter)). `--assets-dir` overrides the manifest's folder for one run. |
|
|
402
|
+
| `itqan install` | Download the assets and write `itqan-assets.lock`. Uses the lockfile's exact versions while it matches the manifest. `--force` picks the newest allowed versions and downloads again. `--flutter` keeps `pubspec.yaml` up to date (see [Flutter](#flutter)). `--assets-dir` overrides the manifest's folder for one run. In a terminal it shows each download's progress; `--no-progress` turns that off. |
|
|
403
403
|
| `itqan sync` | Same as `itqan install`. |
|
|
404
404
|
| `itqan outdated` | List the locked assets that have newer versions: the newest your range allows and the newest overall. `--json` prints JSON. |
|
|
405
405
|
| `itqan login` | Save your API key for the registry, after checking it. `--api-key` passes the key without the prompt. |
|
|
@@ -416,13 +416,17 @@ Add `--help` to any command for all its options. `install` and `browse` read the
|
|
|
416
416
|
| `--api-key` | `ITQAN_API_KEY` | The key saved by `itqan login`, if any. |
|
|
417
417
|
| `--registry-url` | `ITQAN_REGISTRY_URL` | `https://api.cms.itqan.dev`, the public registry. |
|
|
418
418
|
| | `ITQAN_NO_UPDATE_NOTICE` | Set to `1` to stop `itqan install` saying that newer versions exist. |
|
|
419
|
-
| | `
|
|
419
|
+
| | `DO_NOT_TRACK`, `ITQAN_NO_TELEMETRY` | Set to `1` to send no install ID or command name with requests (see below). |
|
|
420
|
+
| | `ITQAN_CONFIG_DIR` | Where `itqan login` saves keys and the install ID: `~/.config/itqan` (`%APPDATA%\itqan` on Windows). |
|
|
420
421
|
| | `ITQAN_CACHE_DIR` | Dart package only. Where the downloaded program is kept: `~/Library/Caches/itqan` (macOS), `~/.cache/itqan` (Linux), `%LOCALAPPDATA%\itqan` (Windows). |
|
|
421
422
|
| | `ITQAN_CLI_DOWNLOAD_URL` | Dart package only. A folder (`https://` or `file://`) with a release's programs and `SHA256SUMS`, such as a mirror, used instead of the GitHub Release. |
|
|
422
423
|
|
|
423
424
|
A key passed with `--api-key` or `ITQAN_API_KEY` is used instead of the saved one. The saved key
|
|
424
425
|
file is readable only by you.
|
|
425
426
|
|
|
427
|
+
**Usage counts.** Every request sends the CLI version (`User-Agent`), a random install ID kept in the config folder's `install-id` file, and the running command's name, so Itqan can count installs; nothing else about you or your project is sent.
|
|
428
|
+
Set `DO_NOT_TRACK=1` or `ITQAN_NO_TELEMETRY=1` to send only the version: no install ID or command, and no `install-id` file is created.
|
|
429
|
+
|
|
426
430
|
## Troubleshooting
|
|
427
431
|
|
|
428
432
|
**"command not found: itqan".** The folder `itqan` was installed into isn't on your `PATH`. For
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from dataclasses import dataclass
|
|
6
|
-
from importlib.metadata import PackageNotFoundError, version
|
|
7
6
|
|
|
8
7
|
import requests
|
|
9
8
|
|
|
10
9
|
from itqan_cli.exceptions import RegistryApiError
|
|
10
|
+
from itqan_cli.usage import usage_headers
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
@dataclass(frozen=True, slots=True)
|
|
@@ -71,13 +71,6 @@ class Account:
|
|
|
71
71
|
DEFAULT_REGISTRY_URL = "https://api.cms.itqan.dev"
|
|
72
72
|
|
|
73
73
|
|
|
74
|
-
def _cli_version() -> str:
|
|
75
|
-
try:
|
|
76
|
-
return version("itqan-cli")
|
|
77
|
-
except PackageNotFoundError: # running from a source checkout that isn't installed
|
|
78
|
-
return "unknown"
|
|
79
|
-
|
|
80
|
-
|
|
81
74
|
def _error_detail(response: requests.Response) -> str:
|
|
82
75
|
"""`: <reason>` from an error body like a 400 validation error, or ""."""
|
|
83
76
|
try:
|
|
@@ -119,7 +112,7 @@ class RegistryClient:
|
|
|
119
112
|
headers = {
|
|
120
113
|
"Accept": "application/json",
|
|
121
114
|
"Content-Type": "application/json",
|
|
122
|
-
|
|
115
|
+
**usage_headers(),
|
|
123
116
|
}
|
|
124
117
|
if self.api_key:
|
|
125
118
|
headers["X-API-Key"] = self.api_key
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
from collections.abc import Callable
|
|
5
6
|
from dataclasses import dataclass
|
|
6
7
|
import os
|
|
7
8
|
from pathlib import Path
|
|
@@ -12,6 +13,7 @@ import requests
|
|
|
12
13
|
|
|
13
14
|
from itqan_cli.client import ResolvedAssetPayload
|
|
14
15
|
from itqan_cli.exceptions import DownloadError
|
|
16
|
+
from itqan_cli.usage import usage_headers
|
|
15
17
|
|
|
16
18
|
# Version markers live in <assets_dir>/.itqan/<entry name>, outside the asset folders, so
|
|
17
19
|
# bundlers that ship a whole folder (Flutter) don't ship them. The folder name is reserved.
|
|
@@ -20,6 +22,9 @@ _MARKER_DIR = ".itqan"
|
|
|
20
22
|
# A download URL must never produce a basename matching it, or it would clobber the marker.
|
|
21
23
|
_RESERVED_FILENAME = ".itqan_version"
|
|
22
24
|
|
|
25
|
+
# on_progress(downloaded_bytes, total_bytes): total is None when the server doesn't say.
|
|
26
|
+
ProgressCallback = Callable[[int, int | None], None]
|
|
27
|
+
|
|
23
28
|
|
|
24
29
|
def _validate_slug(slug: str) -> None:
|
|
25
30
|
"""Reject slugs that could escape the assets directory via path traversal.
|
|
@@ -113,6 +118,7 @@ class AssetDownloader:
|
|
|
113
118
|
asset: ResolvedAssetPayload,
|
|
114
119
|
*,
|
|
115
120
|
force: bool = False,
|
|
121
|
+
on_progress: ProgressCallback | None = None,
|
|
116
122
|
) -> DownloadResult:
|
|
117
123
|
"""Materialize a single resolved asset into the assets/<entry name>/ folder.
|
|
118
124
|
|
|
@@ -122,6 +128,8 @@ class AssetDownloader:
|
|
|
122
128
|
Ensures:
|
|
123
129
|
- Safety: slug is validated against path traversal before any I/O.
|
|
124
130
|
- Idempotency: skip if already present and non-empty.
|
|
131
|
+
- Progress: `on_progress` is called once the server answers and after every
|
|
132
|
+
chunk, never for a cached asset.
|
|
125
133
|
- Atomicity: stream to temp file and rename upon success.
|
|
126
134
|
- Marker atomicity: version marker is staged as a temp file then
|
|
127
135
|
renamed — both the artifact and its marker are committed together
|
|
@@ -193,14 +201,23 @@ class AssetDownloader:
|
|
|
193
201
|
|
|
194
202
|
try:
|
|
195
203
|
with open(temp_fd, "wb") as f_out:
|
|
196
|
-
with self.session.get(
|
|
204
|
+
with self.session.get(
|
|
205
|
+
asset.download_url, stream=True, timeout=self.timeout, headers=usage_headers()
|
|
206
|
+
) as resp:
|
|
197
207
|
if resp.status_code != 200:
|
|
198
208
|
raise DownloadError(
|
|
199
209
|
f"Failed to download '{asset.slug}' from {asset.download_url}: HTTP {resp.status_code}"
|
|
200
210
|
)
|
|
211
|
+
total = _content_length(resp)
|
|
212
|
+
done = 0
|
|
213
|
+
if on_progress:
|
|
214
|
+
on_progress(done, total)
|
|
201
215
|
for chunk in resp.iter_content(chunk_size=65536):
|
|
202
216
|
if chunk:
|
|
203
217
|
f_out.write(chunk)
|
|
218
|
+
done += len(chunk)
|
|
219
|
+
if on_progress:
|
|
220
|
+
on_progress(done, total)
|
|
204
221
|
|
|
205
222
|
# Write version string to the staged marker tmp file
|
|
206
223
|
marker_tmp.write_text(asset.resolved_version, encoding="utf-8")
|
|
@@ -235,5 +252,17 @@ class AssetDownloader:
|
|
|
235
252
|
raise DownloadError(f"Error downloading asset '{asset.slug}': {exc}") from exc
|
|
236
253
|
|
|
237
254
|
|
|
255
|
+
def _content_length(resp: requests.Response) -> int | None:
|
|
256
|
+
"""The body size in bytes, or None when unknown. A compressed response's Content-Length
|
|
257
|
+
counts compressed bytes, but iter_content yields them decompressed, so it's unknown too."""
|
|
258
|
+
if resp.headers.get("Content-Encoding", "identity").lower() != "identity":
|
|
259
|
+
return None
|
|
260
|
+
try:
|
|
261
|
+
length = int(resp.headers["Content-Length"])
|
|
262
|
+
except (KeyError, ValueError):
|
|
263
|
+
return None
|
|
264
|
+
return length if length >= 0 else None
|
|
265
|
+
|
|
266
|
+
|
|
238
267
|
def _read_marker(marker: Path) -> str | None:
|
|
239
268
|
return marker.read_text(encoding="utf-8").strip() if marker.is_file() else None
|
|
@@ -11,7 +11,7 @@ import sys
|
|
|
11
11
|
|
|
12
12
|
import click
|
|
13
13
|
|
|
14
|
-
from itqan_cli import add, browse, credentials, flutter as flutter_pubspec, updates
|
|
14
|
+
from itqan_cli import add, browse, credentials, flutter as flutter_pubspec, progress, updates
|
|
15
15
|
from itqan_cli.client import DEFAULT_REGISTRY_URL, CatalogLanguage, CatalogPackage, RegistryClient, ResolvedAssetPayload
|
|
16
16
|
from itqan_cli.downloader import AssetDownloader
|
|
17
17
|
from itqan_cli.exceptions import ItqanCliError, RegistryApiError
|
|
@@ -45,6 +45,7 @@ def _run_install(
|
|
|
45
45
|
api_key: str | None,
|
|
46
46
|
force: bool,
|
|
47
47
|
flutter: bool | None = None,
|
|
48
|
+
no_progress: bool = False,
|
|
48
49
|
) -> int:
|
|
49
50
|
m_path = Path(manifest_path)
|
|
50
51
|
l_path = Path(lockfile_path)
|
|
@@ -126,9 +127,19 @@ def _run_install(
|
|
|
126
127
|
# If any download fails, we raise immediately and the lockfile is untouched.
|
|
127
128
|
click.echo(f"Materializing {len(resolved_payloads)} asset(s) into {a_dir}...")
|
|
128
129
|
download_summary = []
|
|
130
|
+
show_progress = progress.enabled(no_progress)
|
|
129
131
|
|
|
130
|
-
for item in resolved_payloads:
|
|
131
|
-
|
|
132
|
+
for index, item in enumerate(resolved_payloads, start=1):
|
|
133
|
+
bar = None
|
|
134
|
+
if show_progress:
|
|
135
|
+
bar = progress.DownloadProgress(
|
|
136
|
+
f"({index}/{len(resolved_payloads)}) {item.entry_name} [{item.resolved_version}]"
|
|
137
|
+
)
|
|
138
|
+
try:
|
|
139
|
+
res = downloader.materialize_asset(item, force=force, on_progress=bar)
|
|
140
|
+
finally:
|
|
141
|
+
if bar:
|
|
142
|
+
bar.clear()
|
|
132
143
|
status_str = "downloaded" if res.downloaded else "cached (up-to-date)"
|
|
133
144
|
click.echo(f" * {res.slug} [{res.version}]: {status_str} -> {res.target_path}")
|
|
134
145
|
download_summary.append(res)
|
|
@@ -236,6 +247,12 @@ def _notify_updates(resolved_payloads: list[ResolvedAssetPayload], manifest: Ass
|
|
|
236
247
|
help="List the installed asset folders under `flutter: assets:` in the pubspec.yaml next to the manifest. "
|
|
237
248
|
"Once used, later installs keep that list up to date without the flag.",
|
|
238
249
|
)
|
|
250
|
+
@click.option(
|
|
251
|
+
"--no-progress",
|
|
252
|
+
is_flag=True,
|
|
253
|
+
default=False,
|
|
254
|
+
help="Don't draw download progress. It's only drawn in a terminal, and not when TERM=dumb.",
|
|
255
|
+
)
|
|
239
256
|
def install_command(
|
|
240
257
|
manifest_path: str,
|
|
241
258
|
lockfile_path: str,
|
|
@@ -244,6 +261,7 @@ def install_command(
|
|
|
244
261
|
api_key: str | None,
|
|
245
262
|
force: bool,
|
|
246
263
|
flutter: bool | None = None,
|
|
264
|
+
no_progress: bool = False,
|
|
247
265
|
) -> None:
|
|
248
266
|
"""Install assets declared in itqan-assets.yaml."""
|
|
249
267
|
api_key = _api_key(api_key, registry_url)
|
|
@@ -256,6 +274,7 @@ def install_command(
|
|
|
256
274
|
api_key=api_key,
|
|
257
275
|
force=force,
|
|
258
276
|
flutter=flutter,
|
|
277
|
+
no_progress=no_progress,
|
|
259
278
|
)
|
|
260
279
|
sys.exit(exit_code)
|
|
261
280
|
except ItqanCliError as exc:
|
|
@@ -284,6 +303,11 @@ def _warn_withdrawn(resolved_payloads: list[ResolvedAssetPayload]) -> None:
|
|
|
284
303
|
)
|
|
285
304
|
|
|
286
305
|
|
|
306
|
+
def _interactive_terminal() -> bool:
|
|
307
|
+
"""Whether a person is at the keyboard to answer prompts."""
|
|
308
|
+
return sys.stdin.isatty() and sys.stdout.isatty()
|
|
309
|
+
|
|
310
|
+
|
|
287
311
|
def _api_key(api_key: str | None, registry_url: str) -> str | None:
|
|
288
312
|
"""--api-key / ITQAN_API_KEY when given, else the key `itqan login` saved for this registry."""
|
|
289
313
|
if api_key:
|
|
@@ -437,7 +461,7 @@ def init_command(manifest_path: str, assets_dir: str, registry_url: str, api_key
|
|
|
437
461
|
|
|
438
462
|
The examples are real assets from the registry, or placeholders when it
|
|
439
463
|
can't be reached. Uncomment the ones you want, or add assets with
|
|
440
|
-
`itqan browse
|
|
464
|
+
`itqan browse`, which init offers to run when used in a terminal.
|
|
441
465
|
"""
|
|
442
466
|
m_path = Path(manifest_path)
|
|
443
467
|
if m_path.exists() and not force:
|
|
@@ -468,6 +492,21 @@ def init_command(manifest_path: str, assets_dir: str, registry_url: str, api_key
|
|
|
468
492
|
m_path.write_text(content, encoding="utf-8")
|
|
469
493
|
examples = f" with {len(samples)} commented-out example asset(s)" if samples else " with commented-out examples"
|
|
470
494
|
click.echo(f"Created {m_path}{examples}.")
|
|
495
|
+
if _interactive_terminal():
|
|
496
|
+
import questionary
|
|
497
|
+
|
|
498
|
+
if questionary.confirm("Run `itqan browse` now to pick assets to add?", default=True).ask():
|
|
499
|
+
browse_command.callback(
|
|
500
|
+
query=None,
|
|
501
|
+
category=None,
|
|
502
|
+
manifest_path=str(m_path),
|
|
503
|
+
lockfile_path="itqan-assets.lock",
|
|
504
|
+
interactive=True,
|
|
505
|
+
as_json=False,
|
|
506
|
+
registry_url=registry_url,
|
|
507
|
+
api_key=api_key,
|
|
508
|
+
)
|
|
509
|
+
return
|
|
471
510
|
click.echo("Next, run `itqan browse` to find assets and add them to it, or uncomment the examples.")
|
|
472
511
|
if flutter_pubspec.is_flutter_project(m_path.parent / flutter_pubspec.PUBSPEC):
|
|
473
512
|
click.echo(f"Then run `itqan install --flutter` to download them and list them in {flutter_pubspec.PUBSPEC}.")
|
|
@@ -531,7 +570,7 @@ def browse_command(
|
|
|
531
570
|
without it, every asset is listed and you can filter by typing in the picker.
|
|
532
571
|
"""
|
|
533
572
|
if interactive is None:
|
|
534
|
-
interactive =
|
|
573
|
+
interactive = _interactive_terminal()
|
|
535
574
|
if as_json:
|
|
536
575
|
interactive = False
|
|
537
576
|
m_path = Path(manifest_path)
|
|
@@ -587,7 +626,11 @@ def browse_command(
|
|
|
587
626
|
|
|
588
627
|
import questionary
|
|
589
628
|
|
|
590
|
-
|
|
629
|
+
# In a Flutter project, offer the install that also lists the folders in pubspec.yaml;
|
|
630
|
+
# the question names --flutter, so saying yes is the opt-in.
|
|
631
|
+
flutter = flutter_pubspec.is_flutter_project(m_path.parent / flutter_pubspec.PUBSPEC)
|
|
632
|
+
command = "itqan install --flutter" if flutter else "itqan install"
|
|
633
|
+
if questionary.confirm(f"Run `{command}` now?", default=True).ask():
|
|
591
634
|
install_command.callback(
|
|
592
635
|
manifest_path=str(m_path),
|
|
593
636
|
lockfile_path=lockfile_path,
|
|
@@ -595,9 +638,10 @@ def browse_command(
|
|
|
595
638
|
registry_url=registry_url,
|
|
596
639
|
api_key=api_key,
|
|
597
640
|
force=False,
|
|
641
|
+
flutter=True if flutter else None,
|
|
598
642
|
)
|
|
599
643
|
else:
|
|
600
|
-
click.echo("Run `
|
|
644
|
+
click.echo(f"Run `{command}` when you're ready to download them.")
|
|
601
645
|
|
|
602
646
|
|
|
603
647
|
def _add_to_manifest(
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""Progress line drawn while `itqan install` downloads an asset.
|
|
2
|
+
|
|
3
|
+
Only drawn in a terminal: piped, redirected or CI output stays plain lines, with no `\\r` frames.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Callable
|
|
9
|
+
import os
|
|
10
|
+
import shutil
|
|
11
|
+
import sys
|
|
12
|
+
import time
|
|
13
|
+
|
|
14
|
+
import click
|
|
15
|
+
|
|
16
|
+
# The bar gets the room the numbers leave, between these widths, or is left out.
|
|
17
|
+
_BAR_MAX_WIDTH = 30
|
|
18
|
+
_BAR_MIN_WIDTH = 8
|
|
19
|
+
# Room kept for the rate ("999.9 KB/s") and ETA ("0:00:00"), so the bar keeps one width all along.
|
|
20
|
+
_RATE_WIDTH = 10
|
|
21
|
+
_ETA_WIDTH = 7
|
|
22
|
+
_SPINNER = "|/-\\"
|
|
23
|
+
# Redraw at most this often (seconds); downloads report every 64 KiB chunk.
|
|
24
|
+
_REDRAW_INTERVAL = 0.1
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def enabled(no_progress: bool = False) -> bool:
|
|
28
|
+
"""Whether to draw progress: not with --no-progress, TERM=dumb, or when stdout isn't a terminal."""
|
|
29
|
+
return not no_progress and os.environ.get("TERM") != "dumb" and _stdout_is_terminal()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _stdout_is_terminal() -> bool:
|
|
33
|
+
return sys.stdout.isatty()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class DownloadProgress:
|
|
37
|
+
"""A one-line progress display, redrawn in place, for one download.
|
|
38
|
+
|
|
39
|
+
Pass it as `materialize_asset(on_progress=...)`. Call `clear()` once the download
|
|
40
|
+
ends or fails, so the next line prints where the progress line was.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(
|
|
44
|
+
self,
|
|
45
|
+
label: str,
|
|
46
|
+
*,
|
|
47
|
+
write: Callable[[str], None] | None = None,
|
|
48
|
+
clock: Callable[[], float] = time.monotonic,
|
|
49
|
+
columns: Callable[[], int] | None = None,
|
|
50
|
+
) -> None:
|
|
51
|
+
self.label = label
|
|
52
|
+
self._write = write or (lambda text: click.echo(text, nl=False))
|
|
53
|
+
self._clock = clock
|
|
54
|
+
self._columns = columns or (lambda: shutil.get_terminal_size().columns)
|
|
55
|
+
self._started: float | None = None
|
|
56
|
+
self._last_draw: float | None = None
|
|
57
|
+
self._drawn_width = 0
|
|
58
|
+
self._frame = 0
|
|
59
|
+
|
|
60
|
+
def __call__(self, done: int, total: int | None) -> None:
|
|
61
|
+
now = self._clock()
|
|
62
|
+
if self._started is None:
|
|
63
|
+
self._started = now
|
|
64
|
+
finished = total is not None and done >= total
|
|
65
|
+
if self._last_draw is not None and now - self._last_draw < _REDRAW_INTERVAL and not finished:
|
|
66
|
+
return
|
|
67
|
+
self._last_draw = now
|
|
68
|
+
self._draw(self.render(done, total, now - self._started))
|
|
69
|
+
|
|
70
|
+
def render(self, done: int, total: int | None, elapsed: float) -> str:
|
|
71
|
+
rate = done / elapsed if elapsed > 0 else 0.0
|
|
72
|
+
speed = f"{format_bytes(rate)}/s" if rate else ""
|
|
73
|
+
if total is None:
|
|
74
|
+
self._frame += 1
|
|
75
|
+
return _join(self.label, _SPINNER[self._frame % len(_SPINNER)], format_bytes(done), speed)
|
|
76
|
+
fraction = min(done / total, 1.0) if total else 1.0
|
|
77
|
+
eta = _format_eta((total - done) / rate) if rate and done < total else ""
|
|
78
|
+
numbers = _join(_format_ratio(done, total), f"{int(fraction * 100):3d}%", speed, eta)
|
|
79
|
+
widest = _join(_format_ratio(total, total), "100%", "x" * _RATE_WIDTH, "x" * _ETA_WIDTH)
|
|
80
|
+
# Leave the last column free: some terminals wrap a line that fills the row.
|
|
81
|
+
width = min(self._columns() - 1 - len(_join(self.label, "[]", widest)), _BAR_MAX_WIDTH)
|
|
82
|
+
if width < _BAR_MIN_WIDTH:
|
|
83
|
+
return _join(self.label, numbers)
|
|
84
|
+
filled = int(fraction * width)
|
|
85
|
+
bar = "=" * filled + (">" if filled < width else "") + " " * (width - filled - 1)
|
|
86
|
+
return _join(self.label, f"[{bar}]", numbers)
|
|
87
|
+
|
|
88
|
+
def clear(self) -> None:
|
|
89
|
+
if self._drawn_width:
|
|
90
|
+
self._write("\r" + " " * self._drawn_width + "\r")
|
|
91
|
+
self._drawn_width = 0
|
|
92
|
+
|
|
93
|
+
def _draw(self, line: str) -> None:
|
|
94
|
+
# A line wider than the terminal wraps, and `\r` then only rewinds its last row.
|
|
95
|
+
line = line[: max(self._columns() - 1, 0)]
|
|
96
|
+
self._write("\r" + line.ljust(self._drawn_width))
|
|
97
|
+
self._drawn_width = len(line)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _join(*parts: str) -> str:
|
|
101
|
+
return " ".join(part for part in parts if part)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
_UNITS = ("B", "KB", "MB", "GB", "TB")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _unit(size: float) -> int:
|
|
108
|
+
index = 0
|
|
109
|
+
while size >= 1000 and index < len(_UNITS) - 1:
|
|
110
|
+
size /= 1000
|
|
111
|
+
index += 1
|
|
112
|
+
return index
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def format_bytes(size: float) -> str:
|
|
116
|
+
"""1234567 -> '1.2 MB' (decimal units, like download managers)."""
|
|
117
|
+
index = _unit(size)
|
|
118
|
+
if index == 0:
|
|
119
|
+
return f"{int(size)} B"
|
|
120
|
+
return f"{size / 1000**index:.1f} {_UNITS[index]}"
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _format_ratio(done: int, total: int) -> str:
|
|
124
|
+
"""Both sides in the total's unit: '45.3/120.0 MB'."""
|
|
125
|
+
index = _unit(total)
|
|
126
|
+
if index == 0:
|
|
127
|
+
return f"{done}/{total} B"
|
|
128
|
+
scale = 1000**index
|
|
129
|
+
return f"{done / scale:.1f}/{total / scale:.1f} {_UNITS[index]}"
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _format_eta(seconds: float) -> str:
|
|
133
|
+
seconds = int(seconds)
|
|
134
|
+
return f"{seconds // 3600}:{seconds // 60 % 60:02d}:{seconds % 60:02d}"
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""Headers that let the registry count CLI usage, sent with every request.
|
|
2
|
+
|
|
3
|
+
- ``User-Agent: itqan-cli/<version>``: always.
|
|
4
|
+
- ``X-Itqan-Install-Id: <uuid4>``: a random ID made on first use and kept in
|
|
5
|
+
``<config dir>/install-id``, so the registry can count installs of the CLI
|
|
6
|
+
without knowing who runs it. Left out when it can't be stored.
|
|
7
|
+
- ``X-Itqan-Command: <command>``: the command that made the request, as
|
|
8
|
+
``itqan --help`` lists it (``sync`` is sent as ``install``). The registry
|
|
9
|
+
doesn't count asset installs for ``outdated``, the one command that only
|
|
10
|
+
checks versions.
|
|
11
|
+
|
|
12
|
+
``DO_NOT_TRACK`` or ``ITQAN_NO_TELEMETRY`` set to anything but ``0`` or
|
|
13
|
+
``false`` leaves out the last two, and no install-id file is made.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
19
|
+
import os
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
import tempfile
|
|
22
|
+
import uuid
|
|
23
|
+
|
|
24
|
+
import click
|
|
25
|
+
|
|
26
|
+
from itqan_cli.credentials import config_dir
|
|
27
|
+
|
|
28
|
+
_OPT_OUT_VARS = ("DO_NOT_TRACK", "ITQAN_NO_TELEMETRY")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def cli_version() -> str:
|
|
32
|
+
try:
|
|
33
|
+
return version("itqan-cli")
|
|
34
|
+
except PackageNotFoundError: # running from a source checkout that isn't installed
|
|
35
|
+
return "unknown"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def user_agent() -> str:
|
|
39
|
+
return f"itqan-cli/{cli_version()}"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def telemetry_disabled() -> bool:
|
|
43
|
+
"""True when an opt-out variable is set to anything but empty, ``0`` or ``false``."""
|
|
44
|
+
return any(os.environ.get(var, "").strip().lower() not in ("", "0", "false") for var in _OPT_OUT_VARS)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def install_id_path() -> Path:
|
|
48
|
+
return config_dir() / "install-id"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def install_id() -> str | None:
|
|
52
|
+
"""This installation's ID, made and stored on first use. A missing or corrupt file
|
|
53
|
+
gets a new ID; None when it can't be stored, so the CLI carries on without one."""
|
|
54
|
+
try:
|
|
55
|
+
path = install_id_path()
|
|
56
|
+
except RuntimeError: # no home directory to put the config dir in
|
|
57
|
+
return None
|
|
58
|
+
try:
|
|
59
|
+
return str(uuid.UUID(path.read_text(encoding="utf-8").strip()))
|
|
60
|
+
except (OSError, ValueError): # missing, unreadable, not text, or not a UUID
|
|
61
|
+
pass
|
|
62
|
+
new_id = str(uuid.uuid4())
|
|
63
|
+
try:
|
|
64
|
+
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
65
|
+
fd, tmp = tempfile.mkstemp(prefix=".install-id_", dir=path.parent)
|
|
66
|
+
try:
|
|
67
|
+
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
|
68
|
+
f.write(new_id + "\n")
|
|
69
|
+
os.replace(tmp, path)
|
|
70
|
+
except BaseException:
|
|
71
|
+
Path(tmp).unlink(missing_ok=True)
|
|
72
|
+
raise
|
|
73
|
+
except OSError:
|
|
74
|
+
return None
|
|
75
|
+
return new_id
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def current_command() -> str | None:
|
|
79
|
+
"""The `itqan` subcommand running now, or None outside one."""
|
|
80
|
+
ctx = click.get_current_context(silent=True)
|
|
81
|
+
if ctx is None or ctx.parent is None:
|
|
82
|
+
return None
|
|
83
|
+
while ctx.parent.parent is not None:
|
|
84
|
+
ctx = ctx.parent
|
|
85
|
+
return ctx.command.name
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def usage_headers() -> dict[str, str]:
|
|
89
|
+
"""The headers to send with a request: see the module docstring."""
|
|
90
|
+
headers = {"User-Agent": user_agent()}
|
|
91
|
+
if telemetry_disabled():
|
|
92
|
+
return headers
|
|
93
|
+
if install := install_id():
|
|
94
|
+
headers["X-Itqan-Install-Id"] = install
|
|
95
|
+
if command := current_command():
|
|
96
|
+
headers["X-Itqan-Command"] = command
|
|
97
|
+
return headers
|
|
@@ -8,3 +8,6 @@ def isolated_config_dir(tmp_path_factory, monkeypatch):
|
|
|
8
8
|
monkeypatch.setenv("ITQAN_CONFIG_DIR", str(tmp_path_factory.mktemp("itqan-config")))
|
|
9
9
|
monkeypatch.delenv("ITQAN_API_KEY", raising=False)
|
|
10
10
|
monkeypatch.delenv("ITQAN_REGISTRY_URL", raising=False)
|
|
11
|
+
# The opt-out variables, so the usage-header tests see the default.
|
|
12
|
+
monkeypatch.delenv("DO_NOT_TRACK", raising=False)
|
|
13
|
+
monkeypatch.delenv("ITQAN_NO_TELEMETRY", raising=False)
|