autoimport.py: convert to use pathlib
authorHelmut Grohne <helmut@subdivi.de>
Wed, 29 Dec 2021 17:05:36 +0000 (18:05 +0100)
committerHelmut Grohne <helmut@subdivi.de>
Wed, 29 Dec 2021 17:18:22 +0000 (18:18 +0100)
autoimport.py

index ec47db0..d9ee0a3 100755 (executable)
@@ -7,7 +7,7 @@ import argparse
 import contextlib
 import errno
 import multiprocessing
-import os
+import pathlib
 import sqlite3
 import subprocess
 import sys
@@ -36,22 +36,21 @@ def process_http(pkgs, url, addhash=True):
             pkgs[name] = inst
 
 def process_file(pkgs, filename):
-    base = os.path.basename(filename)
-    if not base.endswith(".deb"):
+    if filename.suffix != ".deb":
         raise ValueError("filename does not end in .deb")
-    parts = base.split("_")
+    parts = filename.name.split("_")
     if len(parts) != 3:
         raise ValueError("filename not in form name_version_arch.deb")
     name, version, _ = parts
     version = urllib.parse.unquote(version)
     if name in pkgs and version_compare(pkgs[name]["version"], version) > 0:
         return
-    pkgs[name] = dict(version=version, filename=filename)
+    pkgs[name] = dict(version=version, filename=str(filename))
 
 def process_dir(pkgs, d):
-    for entry in os.listdir(d):
+    for entry in d.iterdir():
         try:
-            process_file(pkgs, os.path.join(d, entry))
+            process_file(pkgs, entry)
         except ValueError:
             pass
 
@@ -63,11 +62,11 @@ def process_pkg(name, pkgdict, outpath):
         importcmd.extend(["-H", pkgdict["sha256hash"]])
     if filename.startswith(("http://", "https://", "ftp://", "file://")):
         importcmd.append(filename)
-        with open(outpath, "w") as outp:
+        with outpath.open("w") as outp:
             subprocess.check_call(importcmd, stdout=outp, close_fds=True)
     else:
         with open(filename) as inp:
-            with open(outpath, "w") as outp:
+            with outpath.open("w") as outp:
                 subprocess.check_call(importcmd, stdin=inp, stdout=outp,
                                       close_fds=True)
     print("preprocessed %s" % name)
@@ -86,7 +85,7 @@ def main():
     parser.add_argument("files", nargs='+',
                         help="files or directories or repository urls")
     args = parser.parse_args()
-    tmpdir = tempfile.mkdtemp(prefix="debian-dedup")
+    tmpdir = pathlib.Path(tempfile.mkdtemp(prefix="debian-dedup"))
     db = sqlite3.connect(args.database)
     cur = db.cursor()
     cur.execute("PRAGMA foreign_keys = ON;")
@@ -96,10 +95,12 @@ def main():
         print("processing %s" % d)
         if d.startswith(("http://", "https://", "ftp://", "file://")):
             process_http(pkgs, d, not args.noverify)
-        elif os.path.isdir(d):
-            process_dir(pkgs, d)
         else:
-            process_file(pkgs, d)
+            dp = pathlib.Path(d)
+            if dp.is_dir():
+                process_dir(pkgs, dp)
+            else:
+                process_file(pkgs, dp)
 
     print("reading database")
     cur.execute("SELECT name, version FROM package;")
@@ -115,23 +116,22 @@ def main():
     with e:
         fs = {}
         for name, pkg in pkgs.items():
-            outpath = os.path.join(tmpdir, name)
-            fs[e.submit(process_pkg, name, pkg, outpath)] = name
+            fs[e.submit(process_pkg, name, pkg, tmpdir / name)] = name
 
         for f in concurrent.futures.as_completed(fs.keys()):
             name = fs[f]
             if f.exception():
                 print("%s failed to import: %r" % (name, f.exception()))
                 continue
-            inf = os.path.join(tmpdir, name)
+            inf = tmpdir / name
             print("sqlimporting %s" % name)
-            with open(inf) as inp:
+            with inf.open() as inp:
                 try:
                     readyaml(db, inp)
                 except Exception as exc:
                     print("%s failed sql with exception %r" % (name, exc))
                 else:
-                    os.unlink(inf)
+                    inf.unlink()
 
     if args.prune:
         delpkgs = knownpkgs - distpkgs
@@ -142,12 +142,12 @@ def main():
         # due to ON DELETE CASCADE clauses.
         db.commit()
     try:
-        os.rmdir(tmpdir)
+        tmpdir.rmdir()
     except OSError as err:
         if err.errno != errno.ENOTEMPTY:
             raise
         print("keeping temporary directory %s due to failed packages %s" %
-              (tmpdir, " ".join(os.listdir(tmpdir))))
+              (tmpdir, " ".join(map(str, tmpdir.iterdir()))))
 
 if __name__ == "__main__":
     main()