tempfile.mkdtemp does not like bytes in py3k
[~helmut/debian-dedup.git] / autoimport.py
index a0681b3..76e9962 100755 (executable)
@@ -13,6 +13,10 @@ import sqlite3
 import subprocess
 import tempfile
 import urllib
+try:
+    from urllib.parse import unquote
+except ImportError:
+    from urllib import unquote
 
 import concurrent.futures
 from debian import deb822
@@ -42,7 +46,7 @@ def process_file(pkgs, filename):
     if len(parts) != 3:
         raise ValueError("filename not in form name_version_arch.deb")
     name, version, _ = parts
-    version = urllib.unquote(version)
+    version = unquote(version)
     if name in pkgs and version_compare(pkgs[name]["version"], version) > 0:
         return
     pkgs[name] = dict(version=version, filename=filename)
@@ -60,7 +64,7 @@ def process_pkg(name, pkgdict, outpath):
     importcmd = ["python", "importpkg.py"]
     if "sha256hash" in pkgdict:
         importcmd.extend(["-H", pkgdict["sha256hash"]])
-    if filename.startswith("http://"):
+    if filename.startswith(("http://", "https://", "ftp://", "file://")):
         with open(outpath, "w") as outp:
             dl = subprocess.Popen(["curl", "-s", filename],
                                   stdout=subprocess.PIPE, close_fds=True)
@@ -83,16 +87,19 @@ def main():
                       help="avoid reimporting same versions")
     parser.add_option("-p", "--prune", action="store_true",
                       help="prune packages old packages")
+    parser.add_option("-d", "--database", action="store",
+                      default="test.sqlite3",
+                      help="path to the sqlite3 database file")
     options, args = parser.parse_args()
-    tmpdir = tempfile.mkdtemp(prefix=b"debian-dedup")
-    db = sqlite3.connect("test.sqlite3")
+    tmpdir = tempfile.mkdtemp(prefix="debian-dedup")
+    db = sqlite3.connect(options.database)
     cur = db.cursor()
     cur.execute("PRAGMA foreign_keys = ON;")
     e = concurrent.futures.ThreadPoolExecutor(multiprocessing.cpu_count())
     pkgs = {}
     for d in args:
         print("processing %s" % d)
-        if d.startswith("http://"):
+        if d.startswith(("http://", "https://", "ftp://", "file://")):
             process_http(pkgs, d)
         elif os.path.isdir(d):
             process_dir(pkgs, d)