diff options
author | Helmut Grohne <helmut@subdivi.de> | 2013-08-03 22:14:40 +0200 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2013-08-03 22:14:40 +0200 |
commit | 751f19ec1107c9059ae4834e4b757741ebee6cbd (patch) | |
tree | a409cfc9528f7e71e4e8718fb854422d564cb164 /autoimport.py | |
parent | cb3708825bf7ea32314040575cef35980dad0cd8 (diff) | |
download | debian-dedup-751f19ec1107c9059ae4834e4b757741ebee6cbd.tar.gz |
convert remaining code to sqlalchemy
No explicit "import sqlite3" left. It's still a bit rough around the
corners, particularly since sqlalchemy's support for executemany is
totally broken.
Diffstat (limited to 'autoimport.py')
-rwxr-xr-x | autoimport.py | 30 |
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() |