#!/usr/bin/env python3 """Load packages into a SQLite database with the tables of sql/doc.sql and sql/store.sql. Non-normative tooling, standard library only. Usage: python3 tools/load.py --sqlite lab.db PACKAGE [PACKAGE ...] Files without tables of their own (assets, datasets, derived files) are copied by SHA-256 into an asset store folder beside the database, named .assets/. """ import argparse import os import sqlite3 import sys ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, ROOT) from tools.oracle import db # noqa: E402 def main(): ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("--sqlite", required=True, help="the database file; created with the tables if new") ap.add_argument("packages", nargs="+", help="package folders") a = ap.parse_args() new = not os.path.exists(a.sqlite) conn = sqlite3.connect(a.sqlite) if new: db.create(conn, os.path.join(ROOT, "sql")) for p in a.packages: key = db.load(conn, p, a.sqlite + ".assets") n = conn.execute("SELECT COUNT(*) FROM nodes WHERE doc_key = ?", (key,)).fetchone()[0] print("%s: %d nodes" % (key, n)) conn.commit() if __name__ == "__main__": main()