summaryrefslogtreecommitdiff
path: root/autoimport.py
diff options
context:
space:
mode:
Diffstat (limited to 'autoimport.py')
-rwxr-xr-xautoimport.py30
1 files changed, 17 insertions, 13 deletions
diff --git a/autoimport.py b/autoimport.py
index 481a3f8..c1e0da5 100755
--- a/autoimport.py
+++ b/autoimport.py
@@ -8,13 +8,15 @@ import io
import multiprocessing
import optparse
import os
-import sqlite3
import subprocess
import urllib
import concurrent.futures
from debian import deb822
from debian.debian_support import version_compare
+import sqlalchemy
+
+from dedup.utils import enable_sqlite_foreign_keys
from readyaml import readyaml
@@ -83,9 +85,8 @@ def main():
help="prune packages old packages")
options, args = parser.parse_args()
subprocess.check_call(["mkdir", "-p", "tmp"])
- db = sqlite3.connect("test.sqlite3")
- cur = db.cursor()
- cur.execute("PRAGMA foreign_keys = ON;")
+ db = sqlalchemy.create_engine("sqlite:///test.sqlite3")
+ enable_sqlite_foreign_keys(db)
e = concurrent.futures.ThreadPoolExecutor(multiprocessing.cpu_count())
pkgs = {}
for d in args:
@@ -98,8 +99,9 @@ def main():
process_file(pkgs, d)
print("reading database")
- cur.execute("SELECT name, version FROM package;")
- knownpkgs = dict((row[0], row[1]) for row in cur.fetchall())
+ with db.begin() as conn:
+ cur = conn.execute("SELECT name, version FROM package;")
+ knownpkgs = dict((row[0], row[1]) for row in cur.fetchall())
distpkgs = set(pkgs.keys())
if options.new:
for name in distpkgs:
@@ -122,7 +124,8 @@ def main():
print("sqlimporting %s" % name)
with open(inf) as inp:
try:
- readyaml(db, inp)
+ with db.begin() as conn:
+ readyaml(conn, inp)
except Exception as exc:
print("%s failed sql with exception %r" % (name, exc))
else:
@@ -130,12 +133,13 @@ def main():
if options.prune:
delpkgs = knownpkgs - distpkgs
- print("clearing packages %s" % " ".join(delpkgs))
- cur.executemany("DELETE FROM package WHERE name = ?;",
- ((pkg,) for pkg in delpkgs))
- # Tables content, dependency and sharing will also be pruned
- # due to ON DELETE CASCADE clauses.
- db.commit()
+ if delpkgs:
+ print("clearing packages %s" % " ".join(delpkgs))
+ with db.begin() as conn:
+ conn.execute(sqlalchemy.text("DELETE FROM package WHERE name = :name;"),
+ [dict(name=pkg) for pkg in delpkgs])
+ # Tables content, dependency and sharing will also be pruned
+ # due to ON DELETE CASCADE clauses.
if __name__ == "__main__":
main()