Add exceptions and error checking 36/5736/2
authorWilliam Douglas <william.douglas@intel.com>
Mon, 8 Jul 2013 20:08:29 +0000 (13:08 -0700)
committerWilliam Douglas <william.douglas@intel.com>
Wed, 17 Jul 2013 16:31:50 +0000 (09:31 -0700)
Check return values for os.system calls and raise exceptions to calls
that fail.

Change-Id: I6165e2f530ad729457a64f793acdb30838d980cb
Signed-off-by: William Douglas <william.douglas@intel.com>
swup.py

diff --git a/swup.py b/swup.py
index de18de6..6073105 100755 (executable)
--- a/swup.py
+++ b/swup.py
@@ -18,7 +18,8 @@ import fileinput
 
 update_repo="http://www.planux.com/updates"
 update_cache="/var/cache/updatemanager"
-
+# zypper return values that are acceptable when running the patch command
+patch_okay_codes = [0, 102, 103]
 
 class FakeSecHead(object):
     def __init__(self, fp):
@@ -126,6 +127,7 @@ def download_update(update_data):
         if downloaded_csum != announced_csum:
             print "Error: Checksum mismatch"
             os.remove("%s/download/%s" % (update_cache,location))
+            raise Exception("Checksum failed")
     else:
         print "%s already downloaded" % location
 
@@ -215,9 +217,13 @@ def prepare_update(update_data, download):
     repodir = "%s/repos.d" %update_cache
     repourl = "file://%s/download/%s/content" % (update_cache, update_id)
     if not os.path.exists("%s/%s.repo" % (repourl, update_id)):
-        os.system("zypper --quiet --reposd-dir %s ar --no-gpgcheck --no-keep-packages %s %s" %(repodir, repourl, update_id))
+        r = os.system("zypper --quiet --reposd-dir %s ar --no-gpgcheck --no-keep-packages %s %s" %(repodir, repourl, update_id))
+        if r != 0:
+            raise Exception("zypper add repo error: %s" % r)
     if not download:
-        os.system("zypper --quiet --non-interactive --reposd-dir %s patch --repo %s -d" % (repodir, update_id) )
+        r = os.system("zypper --quiet --non-interactive --reposd-dir %s patch --repo %s -d" % (repodir, update_id) )
+        if r not in patch_okay_codes:
+            raise Exception("zypper patch error: %s" % r)
 
 def install_update(update_data):
     u = update_data
@@ -230,10 +236,14 @@ def install_update(update_data):
     repodir = "%s/repos.d" %update_cache
     repourl = "file://%s/download/%s/content" % (update_cache, update_id)
     if not os.path.exists("%s/%s.repo" % (repodir, update_id)):
-        os.system("zypper --quiet --reposd-dir %s ar --no-gpgcheck --no-keep-packages %s %s" %(repodir, repourl, update_id))
+        r = os.system("zypper --quiet --reposd-dir %s ar --no-gpgcheck --no-keep-packages %s %s" %(repodir, repourl, update_id))
+        if r != 0:
+            raise Exception("zypper add repo error: %s" % r)
     print "zypper -n  --reposd-dir %s patch --with-interactive  --repo %s " % (repodir, update_id)
     os.system("plymouth message --text='%s'" % u['title'])
-    os.system("zypper -n  --reposd-dir %s patch --with-interactive --repo %s " % (repodir, update_id) )
+    r = os.system("zypper -n  --reposd-dir %s patch --with-interactive --repo %s " % (repodir, update_id) )
+    if r not in patch_okay_codes:
+        raise Exception("zypper patch error: %s" % r)
     if not os.path.exists("%s/installed" % (update_cache)):
         os.mkdir("%s/installed" % (update_cache))
     shutil.copyfile("%s/download/%s/content/%s" %(update_cache, update_id, update_id), "%s/installed/%s" % (update_cache, update_id))