remove curl dependency
authorHelmut Grohne <helmut@subdivi.de>
Mon, 23 May 2016 19:03:52 +0000 (21:03 +0200)
committerHelmut Grohne <helmut@subdivi.de>
Mon, 23 May 2016 19:03:52 +0000 (21:03 +0200)
Teach importpkg how to download urls using urlopen and thus remove the
need for invoking curl.

README
autoimport.py
importpkg.py

diff --git a/README b/README
index bf4da52..5329bd8 100644 (file)
--- a/README
+++ b/README
@@ -1,7 +1,7 @@
 Required packages
 -----------------
 
-    aptitude install python python-debian python-lzma curl python-jinja2 python-werkzeug sqlite3 python-imaging python-yaml python-concurrent.futures python-pkg-resources
+    aptitude install python python-debian python-lzma python-jinja2 python-werkzeug sqlite3 python-imaging python-yaml python-concurrent.futures python-pkg-resources
 
 Create a database
 -----------------
index 132e671..abd3a5f 100755 (executable)
@@ -70,15 +70,9 @@ def process_pkg(name, pkgdict, outpath):
     if "sha256hash" in pkgdict:
         importcmd.extend(["-H", pkgdict["sha256hash"]])
     if filename.startswith(("http://", "https://", "ftp://", "file://")):
+        importcmd.append(filename)
         with open(outpath, "w") as outp:
-            dl = subprocess.Popen(["curl", "-s", filename],
-                                  stdout=subprocess.PIPE, close_fds=True)
-            imp = subprocess.Popen(importcmd, stdin=dl.stdout, stdout=outp,
-                                   close_fds=True)
-            if imp.wait():
-                raise ValueError("importpkg failed")
-            if dl.wait():
-                raise ValueError("curl failed")
+            subprocess.check_call(importcmd, stdout=outp, close_fds=True)
     else:
         with open(filename) as inp:
             with open(outpath, "w") as outp:
index fedad73..badef15 100755 (executable)
@@ -9,6 +9,10 @@ import argparse
 import hashlib
 import sys
 import zlib
+try:
+    from urllib.request import urlopen
+except ImportError:
+    from urllib import urlopen
 
 import yaml
 
@@ -72,20 +76,22 @@ class ImportpkgExtractor(DebExtractor):
         raise ProcessingFinished()
 
 def main():
-    parser = argparse.ArgumentParser()
-    parser.add_argument("-H", "--hash", action="store",
-                        help="verify that stdin hash given sha256 hash")
-    args = parser.parse_args()
     try:
         stdin = sys.stdin.buffer
     except AttributeError: # python2
         stdin = sys.stdin
+    parser = argparse.ArgumentParser()
+    parser.add_argument("-H", "--hash", action="store",
+                        help="verify that stdin hash given sha256 hash")
+    parser.add_argument("input", nargs='?', default=stdin, type=urlopen,
+                        help="read from this location instead of stdin")
+    args = parser.parse_args()
     dumper = yaml.SafeDumper(sys.stdout)
     dumper.open()
     if args.hash:
         stdin = HashedStream(stdin, hashlib.sha256())
     try:
-        ImportpkgExtractor(dumper.represent).process(stdin)
+        ImportpkgExtractor(dumper.represent).process(args.input)
     except ProcessingFinished:
         pass
     else: