Remove unnecessary settings on system-update@.service
[platform/upstream/swup.git] / swup.py
diff --git a/swup.py b/swup.py
index de18de6..a291d9a 100755 (executable)
--- a/swup.py
+++ b/swup.py
@@ -9,16 +9,21 @@ import hashlib
 import os
 import tempfile
 import shutil
+import copy
 import sys
 import zipfile
 import rpm
 import subprocess as sub
 import distutils
 import fileinput
+import shlex
+import re
 
 update_repo="http://www.planux.com/updates"
 update_cache="/var/cache/updatemanager"
-
+zypp_cache="/var/cache/zypp"
+# 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 +131,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 +221,15 @@ 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))
+        args = shlex.split("zypper --quiet --reposd-dir %s ar --no-gpgcheck --no-keep-packages %s %s" %(repodir, repourl, update_id))
+        r = sub.Popen(args).wait()
+        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) )
+        args = shlex.split("zypper --quiet --non-interactive --reposd-dir %s patch -d" % repodir)
+        r = sub.Popen(args).wait()
+        if r not in patch_okay_codes:
+            raise Exception("zypper patch error: %s" % r)
 
 def install_update(update_data):
     u = update_data
@@ -230,13 +242,27 @@ 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))
-    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) )
+        args = shlex.split("zypper --quiet --reposd-dir %s ar --no-gpgcheck --no-keep-packages %s %s" %(repodir, repourl, update_id))
+        r = sub.Popen(args).wait()
+        if r != 0:
+            raise Exception("zypper add repo error: %s" % r)
+    args = shlex.split("plymouth message --text='%s'" % u['title'])
+    sub.Popen(args).wait()
+    args = shlex.split("zypper -n  --reposd-dir %s patch --with-interactive  --repo %s " % (repodir, update_id))
+    p = sub.Popen(args, stderr=sub.PIPE, stdout=sub.PIPE)
+    (pout, perr) = p.communicate()
+    print(pout)
+    print(perr)
+    if re.search('Problem occured', perr):
+        raise Exception("zypper patch error: %s" % perr)
+    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))
+    for id in os.listdir("%s/install" % update_cache):
+        if re.search(update_id, id):
+            os.remove("%s/install/%s" % (update_cache, id))
 
     os_release = get_current_version()
     current_version = os_release['version_id'].strip('"')
@@ -259,6 +285,29 @@ def list_updates():
             print "%s" %u['id']
             print "    %s" %u['title']
 
+def get_uninstalled_updates(updates):
+    uninstalled = copy.deepcopy(updates)
+    to_install = []
+    to_install_fixed = []
+    if os.path.isdir("%s/install/" % update_cache):
+        to_install = os.listdir("%s/install/" % update_cache)
+    for i in to_install:
+        to_install_fixed.append(re.sub('^[0-9]*-', '', i))
+    for i in uninstalled.keys():
+        if i not in to_install_fixed:
+            uninstalled.pop(i)
+
+    if os.path.isdir("%s/installed/" % update_cache):
+        update_ids = sorted(uninstalled.keys(), reverse=True)
+        installed = sorted(os.listdir("%s/installed/" % update_cache), reverse=True)
+        if len(installed) != 0:
+            latest = installed[0]
+            if update_ids.count(latest) != 0:
+                for i in update_ids[update_ids.index(latest):]:
+                    uninstalled.pop(i)
+
+    return uninstalled
+
 def get_options():
     parser = OptionParser()
     parser.add_option("-V", "--os-version", action="store_true", dest="osver", default=False,
@@ -334,9 +383,19 @@ def run_action(options):
         download_update(u)
         install_update(u)
 
+    if options.install_all:
+        updates = parse_updates()
+        uninstalled_updates = get_uninstalled_updates(updates)
+        for u in uninstalled_updates.values():
+            if not os.path.exists("%s/packages/%s/rpms" % (zypp_cache, u['id'])):
+                os.makedirs("%s/packages/%s/rpms" % (zypp_cache, u['id']))
+            download_update(u)
+            install_update(u)
+
 if __name__ == '__main__':
     try:
         options = get_options()
         run_action(options)
-    except:
+    except Exception as e:
+        print(e)
         sys.exit(-1)