Files
beeos/tools/import_hivestore_repo.py

74 lines
2.4 KiB
Python

from __future__ import annotations
"""Import a HiveStore repo into BeeOS.
Supports:
- local JSON file path
- raw GitHub JSON URL
- normal GitHub repo URL, normalized to /raw/main/hivestore_index.json
"""
import argparse
import json
import shutil
import sys
import urllib.request
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parents[1]
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
from beeos.core.hivestore_sources import HiveStoreSourceManager, normalize_github_url, source_id_from_url
def fetch_or_copy_index(url_or_path: str, cache_path: Path) -> dict:
cache_path.parent.mkdir(parents=True, exist_ok=True)
local = Path(url_or_path)
if local.exists():
shutil.copyfile(local, cache_path)
return json.loads(cache_path.read_text(encoding="utf-8"))
normalized = normalize_github_url(url_or_path)
with urllib.request.urlopen(normalized, timeout=20) as response:
data = response.read()
cache_path.write_bytes(data)
return json.loads(data.decode("utf-8"))
def main() -> None:
parser = argparse.ArgumentParser(description="Import a BeeOS HiveStore repo source.")
parser.add_argument("source", help="Local hivestore_index.json, raw URL, or GitHub repo URL")
parser.add_argument("--project-root", default=".", help="BeeOS project root")
parser.add_argument("--name", default="", help="Display name override")
parser.add_argument("--no-fetch", action="store_true", help="Add source record without fetching/cache validation")
args = parser.parse_args()
project_root = Path(args.project_root).resolve()
data_dir = project_root / "beeos" / "data"
manager = HiveStoreSourceManager(data_dir)
normalized = normalize_github_url(args.source)
source_id = source_id_from_url(normalized)
cache_path = data_dir / "hivestore_cache" / f"{source_id}.json"
name = args.name or "Imported HiveStore Repo"
if not args.no_fetch:
data = fetch_or_copy_index(args.source, cache_path)
name = args.name or data.get("source_name", name)
index = str(cache_path.relative_to(data_dir))
else:
index = normalized
source = manager.add_source(name=name, url=args.source, index=index, enabled=True)
print("Imported HiveStore source:")
print(f" id: {source.source_id}")
print(f" name: {source.name}")
print(f" index: {source.index}")
if __name__ == "__main__":
main()