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]
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)):
- r = 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:
- r = 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)
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)):
- r = 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)
- print "zypper -n --reposd-dir %s patch --with-interactive --repo %s " % (repodir, update_id)
- os.system("plymouth message --text='%s'" % u['title'])
- r = os.system("zypper -n --reposd-dir %s patch --with-interactive --repo %s " % (repodir, update_id) )
+ 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('"')
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,
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)