import rpm
import glob
import functools
+import tempfile
import subprocess
import zypp #pylint: disable=import-error
# chroot into install root and remove packages with rpm cmd
# note: the rpm package should has been installed in install root
- retcode = 0
+ # some rpm packages only include tpk package, them should be uninstalled
+ script = """#!/bin/bash
+pkgs=$1
+if [ -n "$pkgs" ]; then
+ if [ -f /usr/bin/rpm ]; then
+ rpm -e --allmatches $pkgs
+ if [ -f /usr/bin/pkgcmd ]; then
+ for pkg in $pkgs; do
+ info=$(pkgcmd -l --global | grep $pkg)
+ pkgid=$(echo ${info#*pkgid} | awk -F ' ' '{print $1}' | sed 's/.*\\[\\(.*\\)\\].*/\\1/')
+ if [ -n "$pkgid" ]; then
+ pkgtype=$(echo ${info#*pkg_type} | awk -F ' ' '{print $1}' | sed 's/.*\\[\\(.*\\)\\].*/\\1/')
+ echo "Uninstalling preload $pkgtype package $pkgid ..."
+ if [[ $pkgtype == 'tpk' ]]; then
+ cmd='/usr/bin/tpk-backend'
+ elif [[ $pkgtype == 'wgt' ]]; then
+ cmd='/usr/bin/wgt-backend'
+ else
+ cmd='/usr/bin/unified-backend'
+ fi
+ if [ -f $cmd ]; then
+ $cmd -d $pkg --preload --force-remove
+ else
+ echo "$cmd is not installed"
+ exit 1
+ fi
+ fi
+ done
+ else
+ echo 'pkgmgr package is not installed in image, no preload packages'
+ fi
+ else
+ echo 'rpm package is not installed in image'
+ exit 1
+ fi
+fi
+exit 0
+"""
+ if os.path.exists(self.instroot + "/tmp"):
+ shutil.rmtree(self.instroot + "/tmp")
+ os.mkdir(self.instroot + "/tmp", 0o755)
+ (fd, path) = tempfile.mkstemp(prefix="uninstall_preload_pkg-",
+ dir=self.instroot + "/tmp")
+ os.write(fd, script.encode())
+ os.close(fd)
+ os.chmod(path, 0o700)
+
try:
- cmd = 'rpm -e --allmatches %s' % ' '.join(to_delete)
- msger.debug("do chroot and run command [%s] to erase packages" % cmd)
- retcode = subprocess.call(cmd, preexec_fn=mychroot, shell=True)
- except (IOError, OSError) as err:
- raise CreatorError("chroot err: %s" % str(err))
+ script_path = "/tmp/" + os.path.basename(path)
+ pkgs = ' '.join(to_delete)
+ p = subprocess.Popen(['/bin/bash', script_path, pkgs],
+ preexec_fn=mychroot,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+ while p.poll() == None:
+ msger.info(p.stdout.readline().strip().decode())
+ if p.returncode != 0:
+ raise CreatorError("Failed to uninstall preload packages")
+ except OSError as msg:
+ raise CreatorError("Failed to uninstall preload packages : %s" % msg)
+ finally:
+ os.unlink(path)
return to_delete