| #!/usr/bin/env python3 | |
| """Stream the MoreFixes PostgreSQL plain dump (dump-*.sql.gz) into a slim SQLite DB with the CVEfixes table layout, without a | |
| Postgres server: parses `COPY <table> (<cols>) FROM stdin;` blocks (pg text format, \\N = NULL). Only the tables/columns | |
| the code tier needs are kept: fixes, commits(meta), file_change(meta), cwe_classification, cve(id, dates); method_change is streamed separately. | |
| Usage: load_morefixes_pg.py <dump.sql.gz> <out.db>""" | |
| import sys, gzip, re, sqlite3, time | |
| src, out = sys.argv[1:3] | |
| KEEP = {"fixes": None, "cwe_classification": None, # method_change is streamed by ingest_fixdb_stream.py | |
| "commits": {"hash", "repo_url", "author_date", "committer_date", "merge"}, | |
| "file_change": {"file_change_id", "hash", "filename", "old_path", "new_path", "change_type", "num_lines_added", | |
| "num_lines_deleted", "programming_language", "nloc", "complexity", "token_count"}, | |
| "cve": {"cve_id", "published_date", "last_modified_date"}} | |
| ESC = re.compile(r"\\(?:([btnrfv\\])|([0-7]{1,3})|x([0-9a-fA-F]{1,2}))") | |
| MAP = {"b": "\b", "t": "\t", "n": "\n", "r": "\r", "f": "\f", "v": "\v", "\\": "\\"} | |
| def unesc(s): | |
| if s == r"\N": return None | |
| if "\\" not in s: return s | |
| return ESC.sub(lambda m: MAP[m.group(1)] if m.group(1) else chr(int(m.group(2), 8)) if m.group(2) else chr(int(m.group(3), 16)), s) | |
| con = sqlite3.connect(out); con.execute("PRAGMA journal_mode=OFF"); con.execute("PRAGMA synchronous=OFF") | |
| cur = con.cursor(); t0 = time.time(); tbl = None; n = 0; seen = {} | |
| COPY = re.compile(r"^COPY (?:public\.)?\"?(\w+)\"? \((.*)\) FROM stdin;") | |
| with gzip.open(src, "rt", encoding="utf-8", errors="replace", newline="\n") as f: | |
| for line in f: | |
| if tbl is None: | |
| m = COPY.match(line) | |
| if not m: continue | |
| name = m.group(1); allcols = [c.strip().strip('"') for c in m.group(2).split(",")] | |
| seen[name] = allcols | |
| if name not in KEEP: tbl = ("__skip__", None, None); continue | |
| keep = KEEP[name]; idx = [i for i, c in enumerate(allcols) if keep is None or c in keep] | |
| cols = [allcols[i] for i in idx] | |
| tname = "file_change_slim" if name == "file_change" else name | |
| cur.execute(f"CREATE TABLE IF NOT EXISTS {tname} ({', '.join(cols)})") | |
| tbl = (tname, idx, f"INSERT INTO {tname} VALUES ({', '.join('?' * len(idx))})"); batch = [] | |
| print("COPY", name, "->", tname, cols, flush=True); continue | |
| if line.startswith("\\.") and line.rstrip("\r\n") == "\\.": | |
| if tbl[0] != "__skip__" and batch: cur.executemany(tbl[2], batch) | |
| con.commit(); print("done", tbl[0], f"{time.time()-t0:.0f}s", flush=True); tbl = None; continue | |
| if tbl[0] == "__skip__": continue | |
| parts = line.rstrip("\r\n").split("\t") # dump uses CRLF line ends | |
| batch.append([unesc(parts[i]) if i < len(parts) else None for i in tbl[1]]); n += 1 | |
| if len(batch) >= 5000: cur.executemany(tbl[2], batch); batch = [] | |
| if n % 500000 == 0: print(n, "rows", f"{time.time()-t0:.0f}s", flush=True) | |
| con.commit() | |
| print("tables in dump:", {k: len(v) for k, v in seen.items()}, flush=True) | |
| 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:
- 3.38 kB
- Xet hash:
- 80e799583b87d7998eed04d5e1bb4686d43e4d75c0b3862b89b43101b75cf1d0
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.