summaryrefslogtreecommitdiff
path: root/readyaml.py
blob: b66c7f30e64ecb42aaee2a52441c39628059b055 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/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 main():
    db = sqlite3.connect("test.sqlite3")
    cur = db.cursor()
    cur.execute("PRAGMA foreign_keys = ON;")
    gen = yaml.safe_load_all(sys.stdin)
    metadata = next(gen)
    package = metadata["package"]
    cur.execute("SELECT version FROM package WHERE package = ?;",
                    (package,))
    row = cur.fetchone()
    if row and version_compare(row[0], metadata["version"]) > 0:
        return

    cur.execute("BEGIN;")
    cur.execute("DELETE FROM content WHERE package = ?;",
                (package,))
    cur.execute("INSERT OR REPLACE INTO package (package, version, architecture, source) VALUES (?, ?, ?, ?);",
                (package, metadata["version"], metadata["architecture"],
                 metadata["source"]))
    cur.execute("DELETE FROM dependency WHERE package = ?;",
                (package,))
    cur.executemany("INSERT INTO dependency (package, required) VALUES (?, ?);",
                    ((package, dep) for dep in metadata["depends"]))
    for entry in gen:
        if entry == "commit":
            db.commit()
            return

        cur.execute("INSERT INTO content (package, filename, size) VALUES (?, ?, ?);",
                    (package, entry["name"], entry["size"]))
        cid = cur.lastrowid
        cur.executemany("INSERT INTO hash (cid, function, hash) VALUES (?, ?, ?);",
                        ((cid, func, hexhash)
                         for func, hexhash in entry["hashes"].items()))
    raise ValueError("missing commit block")

if __name__ == "__main__":
    main()