-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson2db.py
More file actions
executable file
·66 lines (53 loc) · 1.76 KB
/
json2db.py
File metadata and controls
executable file
·66 lines (53 loc) · 1.76 KB
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
64
65
66
#!/usr/bin/python3
import contextlib
import gzip
import json
import lzma
import sqlite3
import subprocess
import sys
import tempfile
from pathlib import Path
def parse_lines(fobj):
for line in fobj:
platform, status, test, subtest, files, note = json.loads(line)
files = json.dumps(files) if files else None # also does [] -> None
yield (platform, status, test, subtest, files, note)
if len(sys.argv) != 3:
print(f"usage: {sys.argv[0]} results.json{{.gz,.xz}} results.sqlite.gz")
sys.exit(1)
_, results_json, results_sqlite = sys.argv
if Path(results_sqlite).exists():
raise RuntimeError(f"{results_sqlite} already exists")
with tempfile.NamedTemporaryFile(dir="/var/tmp") as tmpf:
db_conn = sqlite3.connect(tmpf.name)
db_cur = db_conn.cursor()
db_cur.execute("""
CREATE TABLE results (
platform TEXT NOT NULL,
status TEXT NOT NULL,
test TEXT NOT NULL,
subtest TEXT,
files JSONB,
note TEXT
)
""")
with contextlib.ExitStack() as stack:
if results_json.endswith(".gz"):
res_in = stack.enter_context(gzip.open(results_json, mode="rb"))
elif results_json.endswith(".xz"):
res_in = stack.enter_context(lzma.open(results_json, mode="rb"))
else:
res_in = stack.enter_context(open(results_json, mode="rb"))
db_cur.executemany(
"INSERT INTO results VALUES(?,?,?,?,?,?)",
parse_lines(res_in),
)
db_conn.commit()
db_conn.close()
with open(results_sqlite, "wb") as gz_out:
subprocess.run(
("gzip", "-9"),
stdin=tmpf.fileno(),
stdout=gz_out.fileno(),
)