Merge branch 'crosshash'
[~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 from debian.debian_support import version_compare
12
13 def main():
14     urlbase = sys.argv[1]
15     db = sqlite3.connect("test.sqlite3")
16     cur = db.cursor()
17     cur.execute("SELECT package, version FROM package;")
18     knownpkgs = dict((row[0], row[1]) for row in cur.fetchall())
19
20     pkglist = urllib.urlopen(urlbase + "/dists/sid/main/binary-amd64/Packages.gz").read()
21     pkglist = gzip.GzipFile(fileobj=io.BytesIO(pkglist)).read()
22     distpkgs = set()
23     for pkg in deb822.Packages.iter_paragraphs(io.BytesIO(pkglist)):
24         name = pkg["Package"]
25         distpkgs.add(name)
26         if name in knownpkgs and \
27                 version_compare(pkg["Version"], knownpkgs[name]) <= 0:
28             continue
29         pkgurl = "%s/%s" % (urlbase, pkg["Filename"])
30         print("importing %s" % name)
31         dl = subprocess.Popen(["curl", "-s", pkgurl], stdout=subprocess.PIPE)
32         imp = subprocess.Popen("./importpkg.py", stdin=dl.stdout)
33         if imp.wait():
34             print("import failed")
35         if dl.wait():
36             print("curl failed")
37     
38     delpkgs = set(knownpkgs) - distpkgs
39     print("clearing packages %s" % " ".join(delpkgs))
40     cur.execute("PRAGMA foreign_keys=1;")
41     cur.executemany("DELETE FROM content WHERE package = ?;",
42                     ((pkg,) for pkg in delpkgs))
43     cur.executemany("DELETE FROM dependency WHERE package = ?;",
44                     ((pkg,) for pkg in delpkgs))
45     cur.executemany("DELETE FROM package WHERE package = ?;",
46                     ((pkg,) for pkg in delpkgs))
47     db.commit()
48
49 if __name__ == "__main__":
50     main()