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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#!/usr/bin/python
"""This tool reads a yaml file as generated by importpkg.py on stdin and
updates the database with the contents."""
import binascii
import optparse
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 dependency WHERE pid = ?;", (pid,))
cur.execute("UPDATE package SET version = ?, architecture = ?, source = ? WHERE id = ?;",
(metadata["version"], metadata["architecture"], metadata["source"], pid))
cur.execute("DELETE FROM control WHERE pid = ?;", (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 name, content in metadata["data"].items():
content = sqlite3.Binary(binascii.a2b_base64(content))
cur.execute("INSERT INTO controlcontent (content) VALUES (?);",
(content,))
docid = cur.lastrowid
cur.execute("INSERT INTO control (pid, name, cid) VALUES (?, ?, ?);",
(pid, name, docid))
commit = next(gen)
if commit != "commit":
raise ValueError("missing commit block")
def main():
parser = optparse.OptionParser()
parser.add_option("-d", "--database", action="store",
default="test.sqlite3",
help="path to the sqlite3 database file")
options, args = parser.parse_args()
db = sqlite3.connect(options.database)
readyaml(db, sys.stdin)
if __name__ == "__main__":
main()
|