-
Notifications
You must be signed in to change notification settings - Fork 0
Add detection pack registry and publishing workflows #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| from pathlib import Path | ||
| import hashlib | ||
| import json | ||
| import shutil | ||
| import tarfile | ||
|
|
||
| from detlab.packs import load_pack_manifest | ||
|
|
||
| LOCAL_REGISTRY = Path(".detlab/registry") | ||
|
|
||
|
|
||
|
|
||
| def calculate_checksum(path: Path) -> str: | ||
| sha256 = hashlib.sha256() | ||
|
|
||
| with path.open("rb") as f: | ||
| while chunk := f.read(8192): | ||
| sha256.update(chunk) | ||
|
|
||
| return sha256.hexdigest() | ||
|
|
||
|
|
||
|
|
||
| def build_pack(pack_dir: Path, output_dir: Path = Path("dist")) -> Path: | ||
| manifest = load_pack_manifest(pack_dir) | ||
|
|
||
| output_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| archive_name = f"{manifest['name']}-{manifest['version']}.tar.gz" | ||
| archive_path = output_dir / archive_name | ||
|
|
||
| with tarfile.open(archive_path, "w:gz") as tar: | ||
| tar.add(pack_dir, arcname=pack_dir.name) | ||
|
|
||
| checksum = calculate_checksum(archive_path) | ||
|
|
||
| metadata = { | ||
| "name": manifest["name"], | ||
| "version": manifest["version"], | ||
| "checksum": checksum, | ||
| "platforms": manifest.get("platforms", []), | ||
| } | ||
|
|
||
| metadata_path = output_dir / f"{manifest['name']}-{manifest['version']}.json" | ||
| metadata_path.write_text(json.dumps(metadata, indent=2), encoding="utf-8") | ||
|
|
||
| return archive_path | ||
|
|
||
|
|
||
|
|
||
| def install_pack(archive_path: Path, install_dir: Path = LOCAL_REGISTRY) -> Path: | ||
| install_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| with tarfile.open(archive_path, "r:gz") as tar: | ||
| tar.extractall(path=install_dir) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
|
|
||
| return install_dir | ||
|
|
||
|
|
||
|
|
||
| def publish_pack(pack_dir: Path, registry_dir: Path = Path("registry")) -> dict: | ||
| registry_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| archive = build_pack(pack_dir, registry_dir) | ||
|
|
||
| checksum = calculate_checksum(archive) | ||
|
|
||
| manifest = load_pack_manifest(pack_dir) | ||
|
|
||
| published_metadata = { | ||
| "name": manifest["name"], | ||
| "version": manifest["version"], | ||
| "checksum": checksum, | ||
| "archive": archive.name, | ||
| } | ||
|
|
||
| metadata_file = registry_dir / f"{manifest['name']}.json" | ||
| metadata_file.write_text( | ||
| json.dumps(published_metadata, indent=2), | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| return published_metadata | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| from pathlib import Path | ||
|
|
||
| from detlab.registry import calculate_checksum | ||
|
|
||
|
|
||
|
|
||
| def test_calculate_checksum(tmp_path: Path): | ||
| test_file = tmp_path / "test.txt" | ||
| test_file.write_text("detlab", encoding="utf-8") | ||
|
|
||
| checksum = calculate_checksum(test_file) | ||
|
|
||
| assert isinstance(checksum, str) | ||
| assert len(checksum) == 64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These new workflows are documented as
detlab pack ..., but I checked the actual console entry point (pyproject.tomlpointsdetlabatdetlab.main:app) anddetlab/main.pystill only registers the existing top-level commands; there is nopackTyper sub-app or command. As a result, users following this README will get an unknown-command error instead of being able to build, publish, or install packs.Useful? React with 👍 / 👎.