Optimize RPMCallback to support install and uninstall package.
authorwanchao-xu <wanchao.xu@samsung.com>
Tue, 3 Sep 2024 08:34:43 +0000 (16:34 +0800)
committerwanchao-xu <wanchao.xu@samsung.com>
Thu, 5 Sep 2024 01:41:36 +0000 (09:41 +0800)
Change-Id: I37d77d2a237003231fd8967197627806568d5832
Signed-off-by: wanchao-xu <wanchao.xu@samsung.com>
mic/bootstrap.py
mic/utils/rpmmisc.py
plugins/backend/yumpkgmgr.py
plugins/backend/zypppkgmgr.py

index f5df5f14fbc3217ec0dff67106fd66d944da175d..c5c4659a6d245ed83b5f7007c7feec5282a7e457 100644 (file)
@@ -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 
index c5b6320e52ac3356303172cd7e00f702421a59ac..ecd85dcebf562037be7212ffdb20c1132f385279 100644 (file)
@@ -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
index fc2340fc797a376205e8f6b90c6216dec7e7793b..9fc2ac4eb17969662c1534e60a59e95e16451d3f 100644 (file)
@@ -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
 
index d3543c10ff5fe8c015541adaca733c68e269292b..6a1e788248388a908e6dea154c0b0f9cf5f9f32b 100644 (file)
@@ -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