#!/usr/bin/python """This tool reads a yaml file as generated by importpkg.py on stdin and updates the database with the contents.""" import sqlite3 import sys from debian.debian_support import version_compare import yaml def readyaml(db, stream): cur = db.cursor() cur.execute("PRAGMA foreign_keys = ON;") gen = yaml.safe_load_all(stream) metadata = next(gen) package = metadata["package"] cur.execute("SELECT id, version FROM package WHERE name = ?;", (package,)) row = cur.fetchone() if row: pid, version = row if version_compare(version, metadata["version"]) > 0: return else: pid = None cur.execute("BEGIN;") if pid is not None: cur.execute("DELETE FROM content WHERE pid = ?;", (pid,)) cur.execute("DELETE FROM dependency WHERE pid = ?;", (pid,)) cur.execute("UPDATE package SET version = ?, architecture = ?, source = ? WHERE id = ?;", (metadata["version"], metadata["architecture"], metadata["source"], pid)) else: cur.execute("INSERT INTO package (name, version, architecture, source) VALUES (?, ?, ?, ?);", (package, metadata["version"], metadata["architecture"], metadata["source"])) pid = cur.lastrowid cur.executemany("INSERT INTO dependency (pid, required) VALUES (?, ?);", ((pid, dep) for dep in metadata["depends"])) for entry in gen: if entry == "commit": db.commit() return cur.execute("INSERT INTO content (pid, filename, size) VALUES (?, ?, ?);", (pid, entry["name"], entry["size"])) cid = cur.lastrowid for func, hexhash in entry["hashes"].items(): cur.execute("SELECT id FROM hashvalue WHERE hash = ?;", (hexhash,)) row = cur.fetchone() if row: hid = row[0] else: cur.execute("INSERT INTO hashvalue (hash) VALUES (?);", (hexhash,)) hid = cur.lastrowid cur.execute("INSERT INTO hash (cid, function, hid) VALUES (?, ?, ?);", (cid, func, hid)) raise ValueError("missing commit block") def main(): db = sqlite3.connect("test.sqlite3") readyaml(db, sys.stdin) if __name__ == "__main__": main()