implement autoimport
authorHelmut Grohne <helmut@subdivi.de>
Wed, 20 Feb 2013 18:04:18 +0000 (19:04 +0100)
committerHelmut Grohne <helmut@subdivi.de>
Wed, 20 Feb 2013 18:04:18 +0000 (19:04 +0100)
autoimport.py [new file with mode: 0755]
test.py

diff --git a/autoimport.py b/autoimport.py
new file mode 100755 (executable)
index 0000000..23092d7
--- /dev/null
@@ -0,0 +1,44 @@
+#!/usr/bin/python
+
+import gzip
+import io
+import sqlite3
+import subprocess
+import sys
+import urllib
+
+from debian import deb822
+
+def main():
+    urlbase = sys.argv[1]
+    db = sqlite3.connect("test.sqlite3")
+    cur = db.cursor()
+
+    cur.execute("SELECT package, version, architecture FROM package;")
+    knownpkgs = dict((row[0], dict(package=row[0], version=row[1],
+                                   architecture=row[2]))
+                     for row in cur.fetchall())
+    pkglist = urllib.urlopen(urlbase + "/dists/sid/main/binary-amd64/Packages.gz").read()
+    pkglist = gzip.GzipFile(fileobj=io.BytesIO(pkglist)).read()
+    distpkgs = dict()
+    for pkg in deb822.Packages.iter_paragraphs(io.BytesIO(pkglist)):
+        name = pkg["Package"]
+        distpkgs[name] = pkg
+        if pkg["Version"] == knownpkgs.get(name, dict(version=()))["version"]:
+            continue
+        pkgurl = "%s/%s" % (urlbase, pkg["Filename"])
+        print("importing %s" % name)
+        dl = subprocess.Popen(["curl", "-s", pkgurl], stdout=subprocess.PIPE)
+        imp = subprocess.Popen("./test.py", stdin=dl.stdout)
+        if dl.wait():
+            print("curl failed")
+        if imp.wait():
+            print("import failed")
+    
+    cur.execute("PRAGMA foreign_keys=1;")
+    cur.executemany("DELETE FROM package WHERE package = ?;",
+                    ((pkg,) for pkg in set(knownpkgs) - set(distpkgs)))
+    db.commit()
+
+if __name__ == "__main__":
+    main()
diff --git a/test.py b/test.py
index a9761a9..a45720a 100755 (executable)
--- a/test.py
+++ b/test.py
@@ -313,9 +313,7 @@ def process_package(db, filelike):
 
 def main():
     db = sqlite3.connect("test.sqlite3")
-
-    with open(sys.argv[1]) as pkg:
-        process_package(db, pkg)
+    process_package(db, sys.stdin)
 
 if __name__ == "__main__":
     main()