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>
Tue, 3 Sep 2024 08:55:05 +0000 (16:55 +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 e2ea8f1a03e27cd60503c77ea9e98099b859985c..a0833811c377e23e3d4293b966f4910c4f4d1bbd 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
 
@@ -155,7 +155,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 a5fca96be8d3ee3bf77407cb4c8ba6c41125380c..74989c8452bdbd2688ade9f191cf1357959e3aa7 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
@@ -73,10 +73,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):
@@ -89,6 +89,16 @@ class RPMInstallCallback:
 
         return pkg
 
+    def _getPkgName(self, rpmloc):
+        pkgname = ""
+        if rpmloc is not None:
+            m = re.match("(.*)-(\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:
@@ -110,12 +120,7 @@ class RPMInstallCallback:
                     rpmloc = h
                     hdr = readRpmHeader(self.ts, h)
 
-                m = re.match("(.*)-(\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)
@@ -152,61 +157,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("(.*)-(\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 dbf220835e228c7bcb4d676fa27f1c3a9c8fbb4f..83dc86ccf7f54dba341222b3e1ede334b370cbb5 100644 (file)
@@ -432,7 +432,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 79beca0f43cd28a820b049144c053481eab94929..5c5c22a7d2cb06ddcdddd7ca55fc998de4fa2e60 100644 (file)
@@ -889,7 +889,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)
 
@@ -982,7 +982,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
@@ -1098,7 +1098,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