From: wanchao-xu Date: Tue, 3 Sep 2024 08:34:43 +0000 (+0800) Subject: Optimize RPMCallback to support install and uninstall package. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0309fb8d9cce0c460f410d3ea2519ef49f08ad64;p=tools%2Fmic.git Optimize RPMCallback to support install and uninstall package. Change-Id: I37d77d2a237003231fd8967197627806568d5832 Signed-off-by: wanchao-xu --- diff --git a/mic/bootstrap.py b/mic/bootstrap.py index f5df5f1..c5c4659 100644 --- a/mic/bootstrap.py +++ b/mic/bootstrap.py @@ -27,7 +27,7 @@ import errno from mic import msger from mic.utils import errors, proxy, misc -from mic.utils.rpmmisc import readRpmHeader, RPMInstallCallback +from mic.utils.rpmmisc import readRpmHeader, RPMCallback from mic.chroot import cleanup_mounts, setup_chrootenv, cleanup_chrootenv from mic.conf import configmgr from functools import reduce @@ -156,7 +156,7 @@ class MiniBackend(object): # run transaction self.ts.order() - cb = RPMInstallCallback(self.ts) + cb = RPMCallback(self.ts) errs = self.ts.run(cb.callback, '') # ts.run() exit codes are, hmm, "creative": None means all ok, empty diff --git a/mic/utils/rpmmisc.py b/mic/utils/rpmmisc.py index c5b6320..ecd85dc 100644 --- a/mic/utils/rpmmisc.py +++ b/mic/utils/rpmmisc.py @@ -25,7 +25,7 @@ from mic.utils.errors import CreatorError from mic.utils.proxy import get_proxy_for from mic.utils import runner -class RPMInstallCallback: +class RPMCallback: """ Command line callback class for callbacks from the RPM library. """ @@ -36,8 +36,8 @@ class RPMInstallCallback: self.total_installed = 0 self.total_installing = 0 self.installed_pkg_names = [] - self.total_removed = 0 - self.total_removing = 0 + self.total_uninstalled = 0 + self.total_uninstalling = 0 self.mark = "+" self.marks = 40 self.lastmsg = None @@ -75,10 +75,10 @@ class RPMInstallCallback: fmt_bar = "%-" + width + "s" if progress: bar = fmt_bar % (self.mark * int(marks * (percent / 100.0)), ) - fmt = "%-10.10s: %-50.50s " + bar + " " + done + fmt = "%s: %-50.50s " + bar + " " + done else: bar = fmt_bar % (self.mark * marks, ) - fmt = "%-10.10s: %-50.50s " + bar + " " + done + fmt = "%s: %-50.50s " + bar + " " + done return fmt def _logPkgString(self, hdr): @@ -91,6 +91,16 @@ class RPMInstallCallback: return pkg + def _getPkgName(self, rpmloc): + pkgname = "" + if rpmloc is not None: + m = re.match(r"(.*)-(\d+.*)-(\d+\.\d+)\.(.+)\.rpm", os.path.basename(rpmloc)) + if m: + pkgname = m.group(1) + else: + pkgname = os.path.basename(rpmloc) + return pkgname + def callback(self, what, bytes, total, h, user): if what == rpm.RPMCALLBACK_TRANS_START: if bytes == 6: @@ -112,12 +122,7 @@ class RPMInstallCallback: rpmloc = h hdr = readRpmHeader(self.ts, h) - m = re.match(r"(.*)-(\d+.*)-(\d+\.\d+)\.(.+)\.rpm", os.path.basename(rpmloc)) - if m: - pkgname = m.group(1) - else: - pkgname = os.path.basename(rpmloc) - msger.info("Next install: %s " % pkgname) + msger.info("Next install: %s " % self._getPkgName(rpmloc)) handle = self._makeHandle(hdr) fd = os.open(rpmloc, os.O_RDONLY) @@ -154,61 +159,57 @@ class RPMInstallCallback: elif what == rpm.RPMCALLBACK_INST_PROGRESS: if h is not None: - percent = (self.total_installed*100)/self.total_actions + actions = self.total_installing + self.total_uninstalling + percent = (actions*100)/self.total_actions if total > 0: try: hdr, rpmloc = h except: rpmloc = h - m = re.match(r"(.*)-(\d+.*)-(\d+\.\d+)\.(.+)\.rpm", os.path.basename(rpmloc)) - if m: - pkgname = m.group(1) - else: - pkgname = os.path.basename(rpmloc) + pkgname = self._getPkgName(rpmloc) if self.output: - fmt = self._makefmt(self.total_installing, percent) + fmt = self._makefmt(actions, percent) msg = fmt % ("Installing", pkgname) if msg != self.lastmsg: self.lastmsg = msg msger.info(msg) - if self.total_installed == self.total_actions: + if actions == self.total_actions: msger.raw('') msger.verbose('\n'.join(self.logString)) elif what == rpm.RPMCALLBACK_UNINST_START: - self.total_removing += 1 + self.total_uninstalling += 1 + if h is not None: + try: + hdr, rpmloc = h + except: + rpmloc = h + + msger.info("Next uninstall: %s " % self._getPkgName(rpmloc)) elif what == rpm.RPMCALLBACK_UNINST_PROGRESS: if h is not None: - percent = (self.total_removed*100)/self.total_actions + actions = self.total_installing + self.total_uninstalling + percent = (actions*100)/self.total_actions if total > 0: try: hdr, rpmloc = h except: rpmloc = h - m = re.match(r"(.*)-(\d+.*)-(\d+\.\d+)\.(.+)\.rpm", os.path.basename(rpmloc)) - if m: - pkgname = m.group(1) - else: - pkgname = os.path.basename(rpmloc) + pkgname = self._getPkgName(rpmloc) if self.output: - fmt = self._makefmt(self.total_removing, percent) + fmt = self._makefmt(actions, percent) msg = fmt % ("Uninstalling", pkgname) if msg != self.lastmsg: self.lastmsg = msg - msger.info(msg) - if self.total_installed == self.total_actions: - msger.raw('') - msger.verbose('\n'.join(self.logString)) - elif what == rpm.RPMCALLBACK_UNINST_STOP: - self.total_removed += 1 + pass elif what == rpm.RPMCALLBACK_REPACKAGE_START: pass diff --git a/plugins/backend/yumpkgmgr.py b/plugins/backend/yumpkgmgr.py index fc2340f..9fc2ac4 100644 --- a/plugins/backend/yumpkgmgr.py +++ b/plugins/backend/yumpkgmgr.py @@ -424,7 +424,7 @@ class Yum(BackendPlugin, yum.YumBase): raise CreatorError("ordering packages for installation failed") # FIXME: callback should be refactored a little in yum - cb = rpmmisc.RPMInstallCallback(self.ts) + cb = rpmmisc.RPMCallback(self.ts) cb.tsInfo = self.tsInfo cb.filelog = False diff --git a/plugins/backend/zypppkgmgr.py b/plugins/backend/zypppkgmgr.py index d3543c1..6a1e788 100644 --- a/plugins/backend/zypppkgmgr.py +++ b/plugins/backend/zypppkgmgr.py @@ -885,7 +885,7 @@ class Zypp(BackendPlugin): self.__initialize_transaction() self.ts_pre.order() - cb = rpmmisc.RPMInstallCallback(self.ts_pre) + cb = rpmmisc.RPMCallback(self.ts_pre) cb.headmsg = "Preinstall" installlogfile = "%s/__catched_stderr.buf" % (self.instroot) @@ -978,7 +978,7 @@ class Zypp(BackendPlugin): for pkg_bak in self.pkgs_bak: self.ts.addInstall(pkg_bak["header"], pkg_bak["rpmpath"], 'u') self.ts.order() - cb = rpmmisc.RPMInstallCallback(self.ts) + cb = rpmmisc.RPMCallback(self.ts) installlogfile = "%s/__catched_stderr.buf" % (self.instroot) # start to catch stderr output from librpm @@ -1094,7 +1094,7 @@ exit 0 self.show_unresolved_dependencies_msg(unresolved_dependencies) raise RpmError("Unresolved dependencies, transaction failed.") - cb = rpmmisc.RPMInstallCallback(self.ts) + cb = rpmmisc.RPMCallback(self.ts) logfile = "%s/__catched_stderr.buf" % (self.instroot) # start to catch stderr output from librpm