Add block-recommends option to choose if install recommmended packages
[tools/mic.git] / plugins / backend / zypppkgmgr.py
index 74d364e..b9cba62 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python -tt
+#!/usr/bin/python3 -tt
 #
 # Copyright (c) 2010, 2011 Intel, Inc.
 #
 
 import os
 import shutil
-import urlparse
+import urllib.parse
 import rpm
+import glob
 
-import zypp
+import zypp #pylint: disable=import-error
 if not hasattr(zypp, 'PoolQuery') or \
    not hasattr(zypp.RepoManager, 'loadSolvFile'):
     raise ImportError("python-zypp in host system cannot support PoolQuery or "
@@ -33,7 +34,7 @@ from mic.utils import misc, rpmmisc, runner, fs_related
 from mic.utils.grabber import myurlgrab, TextProgress
 from mic.utils.proxy import get_proxy_for
 from mic.utils.errors import CreatorError, RepoError, RpmError
-from mic.imager.baseimager import BaseImageCreator
+from mic.conf import configmgr
 
 class RepositoryStub:
     def __init__(self):
@@ -54,10 +55,11 @@ from mic.pluginbase import BackendPlugin
 class Zypp(BackendPlugin):
     name = 'zypp'
 
-    def __init__(self, target_arch, instroot, cachedir):
+    def __init__(self, target_arch, instroot, cachedir, strict_mode = False):
         self.cachedir = cachedir
         self.instroot  = instroot
         self.target_arch = target_arch
+        self.strict_mode = strict_mode
 
         self.__pkgs_license = {}
         self.__pkgs_content = {}
@@ -107,7 +109,6 @@ class Zypp(BackendPlugin):
     def _cleanupRpmdbLocks(self, installroot):
         # cleans up temporary files left by bdb so that differing
         # versions of rpm don't cause problems
-        import glob
         for f in glob.glob(installroot + "/var/lib/rpm/__db*"):
             os.unlink(f)
 
@@ -189,8 +190,8 @@ class Zypp(BackendPlugin):
 
             ed1 = p1.edition()
             ed2 = p2.edition()
-            (e1, v1, r1) = map(str, [ed1.epoch(), ed1.version(), ed1.release()])
-            (e2, v2, r2) = map(str, [ed2.epoch(), ed2.version(), ed2.release()])
+            (e1, v1, r1) = list(map(str, [ed1.epoch(), ed1.version(), ed1.release()]))
+            (e2, v2, r2) = list(map(str, [ed2.epoch(), ed2.version(), ed2.release()]))
             return rpm.labelCompare((e1, v1, r1), (e2, v2, r2))
 
         found = False
@@ -225,10 +226,10 @@ class Zypp(BackendPlugin):
                         cmp=lambda x,y: cmpEVR(zypp.asKindPackage(x), zypp.asKindPackage(y)),
                         reverse=True):
             item = zypp.asKindPackage(pitem)
-            if item.name() in self.excpkgs.keys() and \
+            if item.name() in list(self.excpkgs.keys()) and \
                self.excpkgs[item.name()] == item.repoInfo().name():
                 continue
-            if item.name() in self.incpkgs.keys() and \
+            if item.name() in list(self.incpkgs.keys()) and \
                self.incpkgs[item.name()] != item.repoInfo().name():
                 continue
 
@@ -236,7 +237,7 @@ class Zypp(BackendPlugin):
             obspkg = self.whatObsolete(item)
             if arch:
                 if arch == str(item.arch()):
-                    item.status().setToBeInstalled (zypp.ResStatus.USER)
+                    pitem.status().setToBeInstalled (zypp.ResStatus.USER)
             else:
                 markPoolItem(obspkg, pitem)
             if not ispattern:
@@ -253,10 +254,10 @@ class Zypp(BackendPlugin):
                             cmp=lambda x,y: cmpEVR(zypp.asKindPackage(x), zypp.asKindPackage(y)),
                             reverse=True):
                 item = zypp.asKindPackage(pitem)
-                if item.name() in self.excpkgs.keys() and \
+                if item.name() in list(self.excpkgs.keys()) and \
                    self.excpkgs[item.name()] == item.repoInfo().name():
                     continue
-                if item.name() in self.incpkgs.keys() and \
+                if item.name() in list(self.incpkgs.keys()) and \
                    self.incpkgs[item.name()] != item.repoInfo().name():
                     continue
 
@@ -300,12 +301,15 @@ class Zypp(BackendPlugin):
         self.to_deselect.append(pkg)
 
     def selectGroup(self, grp, include = ksparser.GROUP_DEFAULT):
+        def compareGroup(pitem):
+            item = zypp.asKindPattern(pitem)
+            return item.repoInfo().priority()
         if not self.Z:
             self.__initialize_zypp()
         found = False
         q = zypp.PoolQuery()
         q.addKind(zypp.ResKind.pattern)
-        for pitem in q.queryResults(self.Z.pool()):
+        for pitem in sorted(q.queryResults(self.Z.pool()), key=compareGroup):
             item = zypp.asKindPattern(pitem)
             summary = "%s" % item.summary()
             name = "%s" % item.name()
@@ -316,9 +320,7 @@ class Zypp(BackendPlugin):
 
         if found:
             if include == ksparser.GROUP_REQUIRED:
-                map(
-                    lambda p: self.deselectPackage(p),
-                    grp.default_packages.keys())
+                list([self.deselectPackage(p) for p in list(grp.default_packages.keys())])
 
             return None
         else:
@@ -380,7 +382,7 @@ class Zypp(BackendPlugin):
             if not ssl_verify:
                 baseurl.setQueryParam("ssl_verify", "no")
             if proxy:
-                host = urlparse.urlparse(proxy)[1]
+                host = urllib.parse.urlparse(proxy)[1]
                 # scheme, host, path, parm, query, frag = urlparse.urlparse(proxy)
 
                 proxyinfo = host.rsplit(":", 1)
@@ -444,7 +446,7 @@ class Zypp(BackendPlugin):
 
             self.__build_repo_cache(name)
 
-        except RuntimeError, e:
+        except RuntimeError as e:
             raise CreatorError(str(e))
 
         msger.verbose('repo: %s was added' % name)
@@ -459,10 +461,30 @@ class Zypp(BackendPlugin):
     def checkPackage(self, pkg):
         self.check_pkgs.append(pkg)
 
+    def _get_local_packages(self):
+        """Return a list of rpm path to be local installed.
+        This is the hook where subclasses may specify a set of rpms which
+        it requires to be installed locally.
+        This returns an empty list by default.
+        Note, subclasses should usually chain up to the base class
+        implementation of this hook.
+        """
+        cropts = configmgr.create
+        if cropts['local_pkgs_path']:
+            if os.path.isdir(cropts['local_pkgs_path']):
+                return glob.glob(
+                        os.path.join(cropts['local_pkgs_path'], '*.rpm'))
+            elif os.path.splitext(cropts['local_pkgs_path'])[-1] == '.rpm':
+                return [cropts['local_pkgs_path']]
+        return []
+    def __localinst_packages(self):
+        for rpm_path in self._get_local_packages():
+            self.installLocal(rpm_path)
     def runInstall(self, checksize = 0):
         os.environ["HOME"] = "/"
         os.environ["LD_PRELOAD"] = ""
         self.buildTransaction()
+        self.__localinst_packages()
 
         todo = zypp.GetResolvablesToInsDel(self.Z.pool())
         installed_pkgs = todo._toInstall
@@ -492,7 +514,7 @@ class Zypp(BackendPlugin):
             raise CreatorError('Packages absent in image: %s' % ','.join(self.check_pkgs))
 
         # record all pkg and the content
-        localpkgs = self.localpkgs.keys()
+        localpkgs = list(self.localpkgs.keys())
         for pkg in dlpkgs:
             license = ''
             if pkg.name() in localpkgs:
@@ -515,15 +537,15 @@ class Zypp(BackendPlugin):
 
                 license = pkg.license()
 
-            if license in self.__pkgs_license.keys():
+            if license in list(self.__pkgs_license.keys()):
                 self.__pkgs_license[license].append(pkg_long_name)
             else:
                 self.__pkgs_license[license] = [pkg_long_name]
 
         total_count = len(dlpkgs)
         cached_count = 0
-        download_total_size = sum(map(lambda x: int(x.downloadSize()), dlpkgs))
-        localpkgs = self.localpkgs.keys()
+        download_total_size = sum([int(x.downloadSize()) for x in dlpkgs])
+        localpkgs = list(self.localpkgs.keys())
 
         msger.info("Checking packages cached ...")
         for po in dlpkgs:
@@ -550,7 +572,7 @@ class Zypp(BackendPlugin):
             raise CreatorError("No enough space used for downloading.")
 
         # record the total size of installed pkgs
-        install_total_size = sum(map(lambda x: int(x.installSize()), dlpkgs))
+        install_total_size = sum([int(x.installSize()) for x in dlpkgs])
         # check needed size before actually download and install
 
         # FIXME: for multiple partitions for loop type, check fails
@@ -567,14 +589,14 @@ class Zypp(BackendPlugin):
             if download_count > 0:
                 msger.info("Downloading packages ...")
             self.downloadPkgs(dlpkgs, download_count)
-        except CreateError, e:
-            raise CreateError("Package download failed: %s" %(e,))
+        except CreatorError as e:
+            raise CreatorError("Package download failed: %s" %(e,))
 
         try:
             self.installPkgs(dlpkgs)
         except (RepoError, RpmError):
             raise
-        except Exception, e:
+        except Exception as e:
             raise CreatorError("Package installation failed: %s" % (e,))
 
     def getVcsInfo(self):
@@ -697,6 +719,9 @@ class Zypp(BackendPlugin):
             self.repo_manager.loadFromCache(repo)
 
         self.Z = zypp.ZYppFactory_instance().getZYpp()
+        if configmgr.create['block_recommends']:
+            msger.info("zypp not install recommend packages")
+            self.Z.resolver().setOnlyRequires(True)
         self.Z.initializeTarget(zypp.Pathname(self.instroot))
         self.Z.target().load()
 
@@ -758,7 +783,7 @@ class Zypp(BackendPlugin):
                           % (pkg, hdr['arch']))
 
     def downloadPkgs(self, package_objects, count):
-        localpkgs = self.localpkgs.keys()
+        localpkgs = list(self.localpkgs.keys())
         progress_obj = TextProgress(count)
 
         for po in package_objects:
@@ -810,6 +835,26 @@ class Zypp(BackendPlugin):
                 for e in errors:
                     msger.warning(e[0])
                 raise RepoError('Could not run transaction.')
+    def show_unresolved_dependencies_msg(self, unresolved_dependencies):
+        for pkg, need, needflags, sense, key in unresolved_dependencies:
+
+            package = '-'.join(pkg)
+
+            if needflags == rpm.RPMSENSE_LESS:
+                deppkg = ' < '.join(need)
+            elif needflags == rpm.RPMSENSE_EQUAL:
+                deppkg = ' = '.join(need)
+            elif needflags == rpm.RPMSENSE_GREATER:
+                deppkg = ' > '.join(need)
+            else:
+                deppkg = '-'.join(need)
+
+            if sense == rpm.RPMDEP_SENSE_REQUIRES:
+                msger.warning("[%s] Requires [%s], which is not provided" \
+                              % (package, deppkg))
+
+            elif sense == rpm.RPMDEP_SENSE_CONFLICTS:
+                msger.warning("[%s] Conflicts with [%s]" % (package, deppkg))
 
     def installPkgs(self, package_objects):
         if not self.ts:
@@ -825,7 +870,7 @@ class Zypp(BackendPlugin):
         self.ts.setProbFilter(probfilter)
         self.ts_pre.setProbFilter(probfilter)
 
-        localpkgs = self.localpkgs.keys()
+        localpkgs = list(self.localpkgs.keys())
 
         for po in package_objects:
             pkgname = po.name()
@@ -873,32 +918,15 @@ class Zypp(BackendPlugin):
                 if len(errors) == 0:
                     msger.warning('scriptlet or other non-fatal errors occurred '
                                   'during transaction.')
-
+                    if self.strict_mode:
+                        raise CreatorError("mic failes to install some packages")
                 else:
                     for e in errors:
                         msger.warning(e[0])
                     raise RepoError('Could not run transaction.')
 
         else:
-            for pkg, need, needflags, sense, key in unresolved_dependencies:
-                package = '-'.join(pkg)
-
-                if needflags == rpm.RPMSENSE_LESS:
-                    deppkg = ' < '.join(need)
-                elif needflags == rpm.RPMSENSE_EQUAL:
-                    deppkg = ' = '.join(need)
-                elif needflags == rpm.RPMSENSE_GREATER:
-                    deppkg = ' > '.join(need)
-                else:
-                    deppkg = '-'.join(need)
-
-                if sense == rpm.RPMDEP_SENSE_REQUIRES:
-                    msger.warning("[%s] Requires [%s], which is not provided" \
-                                  % (package, deppkg))
-
-                elif sense == rpm.RPMDEP_SENSE_CONFLICTS:
-                    msger.warning("[%s] Conflicts with [%s]" % (package, deppkg))
-
+            self.show_unresolved_dependencies_msg(unresolved_dependencies)
             raise RepoError("Unresolved dependencies, transaction failed.")
 
     def __initialize_transaction(self):
@@ -938,7 +966,7 @@ class Zypp(BackendPlugin):
         proxies = None
         repoinfo = pobj.repoInfo()
         reponame = "%s" % repoinfo.name()
-        repos = filter(lambda r: r.name == reponame, self.repos)
+        repos = [r for r in self.repos if r.name == reponame]
         repourl = str(repoinfo.baseUrls()[0])
 
         if repos:
@@ -972,8 +1000,8 @@ class Zypp(BackendPlugin):
         def cmpEVR(p1, p2):
             ed1 = p1.edition()
             ed2 = p2.edition()
-            (e1, v1, r1) = map(str, [ed1.epoch(), ed1.version(), ed1.release()])
-            (e2, v2, r2) = map(str, [ed2.epoch(), ed2.version(), ed2.release()])
+            (e1, v1, r1) = list(map(str, [ed1.epoch(), ed1.version(), ed1.release()]))
+            (e2, v2, r2) = list(map(str, [ed2.epoch(), ed2.version(), ed2.release()]))
             return rpm.labelCompare((e1, v1, r1), (e2, v2, r2))
 
         if not self.Z: