[elbe-devel] [PATCH 6/8] pkgutils: rework copy_kinitrd function

Manuel Traut manut at linutronix.de
Fri Jan 5 22:35:33 CET 2018


copy_kinitrd implemented download and extraction of debian packages.
To improve readability and reusability of the code, split out the
download and extraction of a debian package into seperate functions.

Remove code-duplication by introducing a target_dir and dest variable.

Signed-off-by: Manuel Traut <manut at linutronix.de>
---
 elbepack/pkgutils.py | 149 +++++++++++++++++++++------------------------------
 1 file changed, 62 insertions(+), 87 deletions(-)

diff --git a/elbepack/pkgutils.py b/elbepack/pkgutils.py
index 65ddc4b7..41131432 100644
--- a/elbepack/pkgutils.py
+++ b/elbepack/pkgutils.py
@@ -133,11 +133,6 @@ def get_uri_nonvirtapt(apt_sources, target_pkg, arch):
                 return "", pkg
 
 
-def get_initrd_uri(prj, defs, arch):
-    target_pkg = get_initrd_pkg(prj, defs)
-    return get_uri(prj, defs, arch, target_pkg)
-
-
 def get_uri(prj, defs, arch, target_pkg):
     if arch == "default":
         arch = prj.text("buildimage/arch", default=defs, key="arch")
@@ -194,107 +189,87 @@ def get_dsc_size(fname):
     return sz
 
 
-def copy_kinitrd(prj, target_dir, defs, arch="default"):
+def download_pkg(prj, target_dir, defs, package, arch="default"):
+
+    dest = os.path.join(target_dir, "%s.deb" % package)
+
     try:
-        sha256, uri = get_initrd_uri(prj, defs, arch)
+        sha256, uri = get_uri(prj, defs, arch, package)
     except KeyError:
-        raise NoKinitrdException('no elbe-bootstrap package available')
+        raise NoKinitrdException('no package %s available' % package)
         return
     except SystemError:
         raise NoKinitrdException('a configured mirror is not reachable')
         return
     except CommandError as e:
-        raise NoKinitrdException("couldn't download elbe-bootstrap package")
-        return
+        raise NoKinitrdException("couldn't download package %s" % package)
 
     try:
-        tmpdir = mkdtemp()
+        if uri.startswith("file://"):
+            system('cp "%s" "%s"' % (uri[len("file://"):], dest))
 
-        try:
-            if uri.startswith("file://"):
-                system('cp "%s" "%s"' %
-                       (uri[len("file://"):], os.path.join(tmpdir, "pkg.deb")))
-            elif uri.startswith("http://"):
-                system('wget -O "%s" "%s"' %
-                       (os.path.join(tmpdir, "pkg.deb"), uri))
-            elif uri.startswith("ftp://"):
-                system('wget -O "%s" "%s"' %
-                       (os.path.join(tmpdir, "pkg.deb"), uri))
-            else:
-                raise NoKinitrdException('no elbe-bootstrap package available')
-        except CommandError as e:
-            raise NoKinitrdException(
-                "couldn't download elbe-bootstrap package")
-            return
-
-        if len(sha256) > 0:
-            m = hashlib.sha256()
-            with open(os.path.join(tmpdir, "pkg.deb"), "rb") as f:
+        elif uri.startswith("http://") or uri.startswith("ftp://"):
+            system('wget -O "%s" "%s"' % (dest, uri))
+        else:
+            raise NoKinitrdException('could not retreive %s' % (package, uri))
+    except CommandError as e:
+        raise NoKinitrdException("couldn't download package %s" % package)
+        return
+
+    if len(sha256) > 0:
+        m = hashlib.sha256()
+        with open(dest, "rb") as f:
+            buf = f.read(65536)
+            while len(buf) > 0:
+                m.update(buf)
                 buf = f.read(65536)
-                while len(buf) > 0:
-                    m.update(buf)
-                    buf = f.read(65536)
 
-            if m.hexdigest() != sha256:
-                raise NoKinitrdException('elbe-bootstrap failed to verify !!!')
-        else:
-            print("-----------------------------------------------------")
-            print("WARNING:")
-            print("Using untrusted elbe-bootstrap")
-            print("-----------------------------------------------------")
+        if m.hexdigest() != sha256:
+            raise NoKinitrdException('%s failed to verify !!!' % package)
+    else:
+        print("-----------------------------------------------------")
+        print("WARNING:")
+        print("Using untrusted %s package" % package)
+        print("-----------------------------------------------------")
+    return
+
+
+def extract_pkg(prj, target_dir, defs, package, arch="default"):
+
+    download_pkg(prj, target_dir, defs, package, arch)
 
+    try:
+        system('dpkg -x "%s" "%s"' %
+               (os.path.join(target_dir, "%s.deb" % package), target_dir))
+    except CommandError:
         try:
-            system('dpkg -x "%s" "%s"' %
-                   (os.path.join(tmpdir, "pkg.deb"), tmpdir))
+            # dpkg did not work, try falling back to ar and tar
+            system('ar p "%s" data.tar.gz | tar xz -C "%s"' %
+                   (os.path.join(target_dir, "%s.deb" % package), target_dir))
         except CommandError:
-            try:
-                # dpkg did not work, try falling back to ar and tar
-                system('ar p "%s" data.tar.gz | tar xz -C "%s"' %
-                       (os.path.join(tmpdir, "pkg.deb"), tmpdir))
-            except CommandError:
-                system('ar p "%s" data.tar.xz | tar xJ -C "%s"' %
-                       (os.path.join(tmpdir, "pkg.deb"), tmpdir))
+            system('ar p "%s" data.tar.xz | tar xJ -C "%s"' %
+                   (os.path.join(target_dir, "%s.deb" % package), target_dir))
+
+
+def copy_kinitrd(prj, target_dir, defs, arch="default"):
+
+    target_pkg = get_initrd_pkg(prj, defs)
+
+    try:
+        tmpdir = mkdtemp()
+        extract_pkg(prj, tmpdir, defs, target_pkg, arch)
 
         # copy is done twice, because paths in elbe-bootstarp_1.0 and 0.9
         # differ
+        initrd = os.path.join(tmpdir, 'var', 'lib', 'elbe', 'initrd')
         if prj.has("mirror/cdrom"):
-            system(
-                'cp "%s" "%s"' %
-                (os.path.join(
-                    tmpdir,
-                    'var',
-                    'lib',
-                    'elbe',
-                    'initrd',
-                    'initrd-cdrom.gz'),
-                    os.path.join(
-                    target_dir,
-                    "initrd.gz")))
+            system('cp "%s" "%s"' % (os.path.join(initrd, 'initrd-cdrom.gz'),
+                                     os.path.join(target_dir, "initrd.gz")))
         else:
-            system(
-                'cp "%s" "%s"' %
-                (os.path.join(
-                    tmpdir,
-                    'var',
-                    'lib',
-                    'elbe',
-                    'initrd',
-                    'initrd.gz'),
-                    os.path.join(
-                    target_dir,
-                    "initrd.gz")))
-
-        system(
-            'cp "%s" "%s"' %
-            (os.path.join(
-                tmpdir,
-                'var',
-                'lib',
-                'elbe',
-                'initrd',
-                'vmlinuz'),
-                os.path.join(
-                target_dir,
-                "vmlinuz")))
+            system('cp "%s" "%s"' % (os.path.join(initrd, 'initrd.gz'),
+                                     os.path.join(target_dir, "initrd.gz")))
+
+        system('cp "%s" "%s"' % (os.path.join(initrd, 'vmlinuz'),
+                                 os.path.join(target_dir, "vmlinuz")))
     finally:
         system('rm -rf "%s"' % tmpdir)
-- 
2.15.1




More information about the elbe-devel mailing list