rename test.py to importpkg.py
[~helmut/debian-dedup.git] / autoimport.py
1 #!/usr/bin/python
2
3 import gzip
4 import io
5 import sqlite3
6 import subprocess
7 import sys
8 import urllib
9
10 from debian import deb822
11
12 def main():
13     urlbase = sys.argv[1]
14     db = sqlite3.connect("test.sqlite3")
15     cur = db.cursor()
16     cur.execute("SELECT package, version FROM package;")
17     knownpkgs = dict((row[0], row[1]) for row in cur.fetchall())
18
19     pkglist = urllib.urlopen(urlbase + "/dists/sid/main/binary-amd64/Packages.gz").read()
20     pkglist = gzip.GzipFile(fileobj=io.BytesIO(pkglist)).read()
21     distpkgs = set()
22     for pkg in deb822.Packages.iter_paragraphs(io.BytesIO(pkglist)):
23         name = pkg["Package"]
24         distpkgs.add(name)
25         if pkg["Version"] == knownpkgs.get(name, ()):
26             continue
27         pkgurl = "%s/%s" % (urlbase, pkg["Filename"])
28         print("importing %s" % name)
29         dl = subprocess.Popen(["curl", "-s", pkgurl], stdout=subprocess.PIPE)
30         imp = subprocess.Popen("./importpkg.py", stdin=dl.stdout)
31         if dl.wait():
32             print("curl failed")
33         if imp.wait():
34             print("import failed")
35     
36     cur.execute("PRAGMA foreign_keys=1;")
37     cur.executemany("DELETE FROM package WHERE package = ?;",
38                     ((pkg,) for pkg in set(knownpkgs) - distpkgs))
39     db.commit()
40
41 if __name__ == "__main__":
42     main()