mvd001/cyberset / scripts /code /load_cvefixes_sqlite.py
mvd001's picture
download
raw
2.24 kB
#!/usr/bin/env python3
"""Stream CVEfixes_v1.0.8.sql.gz (inside the Zenodo zip) into a slim SQLite DB without unpacking the zip:
file_change keeps only metadata columns (code_before/code_after/diff blobs are discarded via an INSTEAD OF trigger),
every other table is loaded verbatim. Usage: load_cvefixes_sqlite.py <zip> <member> <out.db>"""
import sys, zipfile, gzip, io, re, sqlite3, time
zp, member, out = sys.argv[1:4]
con = sqlite3.connect(out); con.execute("PRAGMA journal_mode=OFF"); con.execute("PRAGMA synchronous=OFF")
FC_KEEP = ["file_change_id", "hash", "filename", "old_path", "new_path", "change_type", "num_lines_added", "num_lines_deleted",
"programming_language", "nloc", "complexity", "token_count", "before_change"]
f = io.TextIOWrapper(gzip.GzipFile(fileobj=zipfile.ZipFile(zp).open(member)), encoding="utf-8", errors="replace")
buf = []; n = 0; t0 = time.time(); cur = con.cursor()
for line in f:
buf.append(line)
if not line.rstrip().endswith(";"): continue
stmt = "".join(buf)
if not sqlite3.complete_statement(stmt): continue
buf = []
s = stmt.lstrip()
if s.startswith(("BEGIN", "COMMIT", "PRAGMA")): continue
if s.startswith('CREATE TABLE IF NOT EXISTS "file_change"') or s.startswith("CREATE TABLE file_change"):
cols = re.findall(r'"(\w+)"\s+\w+', s.split("(", 1)[1])
keep = [c for c in cols if c in FC_KEEP]
cur.execute(f"CREATE TABLE file_change_slim ({', '.join(keep)})")
cur.execute(f"CREATE VIEW file_change AS SELECT {', '.join('NULL AS ' + c for c in cols)}")
cur.execute(f"CREATE TRIGGER fc_ins INSTEAD OF INSERT ON file_change BEGIN INSERT INTO file_change_slim VALUES ({', '.join('NEW.' + c for c in keep)}); END")
print("file_change columns:", cols, flush=True); continue
try: cur.execute(stmt)
except sqlite3.Error as e:
if not s.startswith("CREATE INDEX"): print("ERR", e, s[:200], flush=True)
n += 1
if n % 200000 == 0:
con.commit(); print(f"{n} stmts {time.time()-t0:.0f}s", flush=True)
con.commit()
for t in [r[0] for r in con.execute("select name from sqlite_master where type='table'")]:
print(t, con.execute(f"select count(*) from {t}").fetchone()[0], flush=True)

Xet Storage Details

Size:
2.24 kB
·
Xet hash:
7ddddc08040ea0e9eaa8347cc41b0329c747f4d4869373f6e9226b8460c7bc53

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.