From 12a106183be7e78bd5f899dbbf8b887a0d7b0436 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Jan 12 2022 09:53:39 +0000
Subject: [PATCH 1/10] update kiwi deps


---

diff --git a/docs/source/plugins.rst b/docs/source/plugins.rst
index 9019382..79d3fe0 100644
--- a/docs/source/plugins.rst
+++ b/docs/source/plugins.rst
@@ -259,7 +259,7 @@ most simple configuration will look like:
 .. code-block:: shell
 
    $ koji add-group kiwi-build-tag kiwi
-   $ koji add-group-pkg kiwi-build-tag kiwi kiwi-cli
+   $ koji add-group-pkg kiwi-build-tag kiwi kiwi-cli kiwi-systemdeps
 
 Another thing we need to ensure is that we're building in chroot and not in
 container.

From be3e1ba3cd78e3032cfb25e75364d528f9e3bac7 Mon Sep 17 00:00:00 2001
From: Igor Raits <igor.raits@gmail.com>
Date: Jan 12 2022 09:53:39 +0000
Subject: [PATCH 2/10] Add support for kiwi.result.json


Signed-off-by: Igor Raits <igor.raits@gmail.com>

---

diff --git a/plugins/builder/kiwi.py b/plugins/builder/kiwi.py
index 83cffc1..5a48575 100644
--- a/plugins/builder/kiwi.py
+++ b/plugins/builder/kiwi.py
@@ -1,5 +1,6 @@
 import glob
-# import json
+import json
+from json.decoder import JSONDecodeError
 import os
 import xml.dom.minidom
 from fnmatch import fnmatch
@@ -368,11 +369,16 @@ class KiwiCreateImageTask(BaseBuildTask):
         if rv:
             raise koji.GenericError("Kiwi failed")
 
-        # result = json.load(open(joinpath(broot.rootdir(), target_dir[1:], 'kiwi.result'), 'rb'))
-        # nosec comment - we will replace it with json ASAP
-        import pickle
-        result = pickle.load(open(joinpath(broot.rootdir(), target_dir[1:],  # nosec
-                                           'kiwi.result'), 'rb'))
+        resultdir = joinpath(broot.rootdir(), target_dir[1:])
+        try:
+            # new version has json format, older pickle (needs python3-kiwi installed)
+            result_files = json.load(open(joinpath(resultdir, 'kiwi.result.json')))
+        except (FileNotFoundError, JSONDecodeError):
+            # try old variant
+            import pickle
+            result = pickle.load(open(joinpath(resultdir, 'kiwi.result'), 'rb')) # nosec
+            # convert from namedtuple's to normal dict
+            result_files = {k: v._asdict() for k, v in result.result_files.items()}
 
         imgdata = {
             'arch': arch,
@@ -398,10 +404,10 @@ class KiwiCreateImageTask(BaseBuildTask):
         #     self.uploadFile(os.path.join(broot.rootdir()), remoteName=img_file)
         #     imgdata['files'].append(img_file)
         for ftype in ('disk_format_image', 'installation_image'):
-            fdata = result.result_files.get(ftype)
+            fdata = result_files.get(ftype)
             if not fdata:
                 continue
-            fpath = os.path.join(broot.rootdir(), fdata.filename[1:])
+            fpath = os.path.join(broot.rootdir(), fdata['filename'][1:])
             img_file = os.path.basename(fpath)
             self.uploadFile(fpath, remoteName=os.path.basename(img_file))
             imgdata['files'].append(img_file)
@@ -410,7 +416,7 @@ class KiwiCreateImageTask(BaseBuildTask):
             if False:
                 # should be used after kiwi update
                 fpath = os.path.join(broot.rootdir(),
-                                     result['result_files']['image_packages'].filename[1:])
+                                     result_files['image_packages'].filename[1:])
                 hdrlist = self.getImagePackages(fpath)
             else:
                 cachepath = os.path.join(broot.rootdir(), 'var/cache/kiwi/dnf')

From 288f385fec9baf4eb8beb4f73667683de1ec7abe Mon Sep 17 00:00:00 2001
From: Igor Raits <igor.raits@gmail.com>
Date: Jan 12 2022 09:53:39 +0000
Subject: [PATCH 3/10] Store disk_image type of kiwi files


For example, it is used by .raw (OEM) images.

Signed-off-by: Igor Raits <igor.raits@gmail.com>

---

diff --git a/plugins/builder/kiwi.py b/plugins/builder/kiwi.py
index 5a48575..eb61bf1 100644
--- a/plugins/builder/kiwi.py
+++ b/plugins/builder/kiwi.py
@@ -403,7 +403,7 @@ class KiwiCreateImageTask(BaseBuildTask):
         #     img_file = '%s.%s-%s.%s' % (name, version, arch, type)
         #     self.uploadFile(os.path.join(broot.rootdir()), remoteName=img_file)
         #     imgdata['files'].append(img_file)
-        for ftype in ('disk_format_image', 'installation_image'):
+        for ftype in ('disk_image', 'disk_format_image', 'installation_image'):
             fdata = result_files.get(ftype)
             if not fdata:
                 continue

From 7d9a8024dec9883d09a6fc22a8f9df3490f5ee0a Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Jan 12 2022 09:53:39 +0000
Subject: [PATCH 4/10] kiwi: Implant releasever into the kiwi description


Related: https://pagure.io/koji/issue/3194

---

diff --git a/plugins/builder/kiwi.py b/plugins/builder/kiwi.py
index eb61bf1..a818315 100644
--- a/plugins/builder/kiwi.py
+++ b/plugins/builder/kiwi.py
@@ -191,7 +191,7 @@ class KiwiCreateImageTask(BaseBuildTask):
     Methods = ['createKiwiImage']
     _taskWeight = 2.0
 
-    def prepareDescription(self, desc_path, name, release, repos):
+    def prepareDescription(self, desc_path, name, version, release, repos):
         # TODO: update release in desc
         kiwi_files = glob.glob('%s/*.kiwi' % desc_path)
         if len(kiwi_files) != 1:
@@ -227,13 +227,22 @@ class KiwiCreateImageTask(BaseBuildTask):
             image.appendChild(repo_node)
 
         image.setAttribute('name', name)
+        preferences = image.getElementsByTagName('preferences')[0]
+        try:
+            preferences.getElementsByTagName('release-version')[0].childNodes[0].data = version
+        except IndexError:
+            releasever_node = newxml.createElement('release-version')
+            text = newxml.createTextNode(version)
+            releasever_node.appendChild(text)
+            preferences.appendChild(releasever_node)
+
         # TODO: release is part of version (major.minor.release)
-        # preferences = image.getElementsByTagName('preferences')[0]
         # try:
         #    preferences.getElementsByTagName('release')[0].childNodes[0].data = release
         # except Exception:
         #    rel_node = newxml.createElement('release')
-        #    rel_node.data = release
+        #    text = newxml.createTextNode(release)
+        #    rel_node.appendChild(rel_node)
         #    preferences.appendChild(rel_node)
 
         types = []
@@ -353,7 +362,7 @@ class KiwiCreateImageTask(BaseBuildTask):
         repos.append(baseurl)
 
         path = os.path.join(scmsrcdir, desc_path)
-        desc, types = self.prepareDescription(path, name, release, repos)
+        desc, types = self.prepareDescription(path, name, version, release, repos)
         self.uploadFile(desc)
 
         cmd = ['kiwi-ng']
diff --git a/plugins/cli/kiwi.py b/plugins/cli/kiwi.py
index c2fb6af..9ae9046 100644
--- a/plugins/cli/kiwi.py
+++ b/plugins/cli/kiwi.py
@@ -61,6 +61,7 @@ def handle_kiwi_build(goptions, session, args):
         arches=arches,
         desc_url=scm,
         desc_path=path,
+        repos=options.repo,
         **kwargs)
 
     if not goptions.quiet:
diff --git a/plugins/hub/kiwi.py b/plugins/hub/kiwi.py
index be51bb0..2ab0cdd 100644
--- a/plugins/hub/kiwi.py
+++ b/plugins/hub/kiwi.py
@@ -16,7 +16,7 @@ koji.tasks.LEGACY_SIGNATURES['createKiwiImage'] = [
 
 @export
 def kiwiBuild(target, arches, desc_url, desc_path, optional_arches=None, profile=None,
-              scratch=False, priority=None):
+              scratch=False, priority=None, repos=None):
     context.session.assertPerm('image')
     taskOpts = {
         'channel': 'image',
@@ -32,6 +32,7 @@ def kiwiBuild(target, arches, desc_url, desc_path, optional_arches=None, profile
         'optional_arches': optional_arches,
         'profile': profile,
         'scratch': scratch,
+        'repos': repos or [],
     }
     return kojihub.make_task('kiwiBuild',
                              [target, arches, desc_url, desc_path, opts],

From e31b9654d40f67cb3af8df053124071bd8558d72 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Jan 12 2022 09:53:39 +0000
Subject: [PATCH 5/10] kiwi: use separate --release


---

diff --git a/plugins/builder/kiwi.py b/plugins/builder/kiwi.py
index a818315..277f08a 100644
--- a/plugins/builder/kiwi.py
+++ b/plugins/builder/kiwi.py
@@ -16,7 +16,6 @@ class KiwiBuildTask(BuildImageTask):
     _taskWeight = 4.0
 
     def get_nvrp(self, desc_path):
-        # TODO: update release in desc
         kiwi_files = glob.glob('%s/*.kiwi' % desc_path)
         if len(kiwi_files) != 1:
             raise koji.GenericError("Repo must contain only one .kiwi file.")
@@ -28,16 +27,11 @@ class KiwiBuildTask(BuildImageTask):
 
         name = image.getAttribute('name')
         version = None
-        release = None
         for preferences in image.getElementsByTagName('preferences'):
             try:
                 version = preferences.getElementsByTagName('version')[0].childNodes[0].data
             except Exception:
                 pass
-            try:
-                release = preferences.getElementsByTagName('release')[0].childNodes[0].data
-            except Exception:
-                release = None
         profile = None
         try:
             for p in image.getElementsByTagName('profiles')[0].getElementsByTagName('profile'):
@@ -48,7 +42,7 @@ class KiwiBuildTask(BuildImageTask):
             pass
         if not version:
             raise koji.BuildError("Description file doesn't contain preferences/version")
-        return name, version, release, profile
+        return name, version, profile
 
     def handler(self, target, arches, desc_url, desc_path, opts=None):
         target_info = self.session.getBuildTarget(target, strict=True)
@@ -101,18 +95,20 @@ class KiwiBuildTask(BuildImageTask):
 
         path = os.path.join(scmsrcdir, desc_path)
 
-        name, version, release, default_profile = self.get_nvrp(path)
+        name, version, default_profile = self.get_nvrp(path)
         if opts.get('profile') or default_profile:
             # package name is a combination of name + profile
             # in case profiles are not used, let's use the standalone name
             name = "%s-%s" % (name, opts.get('profile', default_profile))
 
         bld_info = {}
+        if opts.get('release'):
+            release = opts['release']
+        else:
+            release = self.session.getNextRelease({'name': name, 'version': version})
         if not opts['scratch']:
             bld_info = self.initImageBuild(name, version, release, target_info, opts)
             release = bld_info['release']
-        elif not release:
-            release = self.session.getNextRelease({'name': name, 'version': version})
 
         try:
             subtasks = {}
@@ -191,8 +187,7 @@ class KiwiCreateImageTask(BaseBuildTask):
     Methods = ['createKiwiImage']
     _taskWeight = 2.0
 
-    def prepareDescription(self, desc_path, name, version, release, repos):
-        # TODO: update release in desc
+    def prepareDescription(self, desc_path, name, version, repos):
         kiwi_files = glob.glob('%s/*.kiwi' % desc_path)
         if len(kiwi_files) != 1:
             raise koji.GenericError("Repo must contain only one .kiwi file.")
@@ -236,15 +231,6 @@ class KiwiCreateImageTask(BaseBuildTask):
             releasever_node.appendChild(text)
             preferences.appendChild(releasever_node)
 
-        # TODO: release is part of version (major.minor.release)
-        # try:
-        #    preferences.getElementsByTagName('release')[0].childNodes[0].data = release
-        # except Exception:
-        #    rel_node = newxml.createElement('release')
-        #    text = newxml.createTextNode(release)
-        #    rel_node.appendChild(rel_node)
-        #    preferences.appendChild(rel_node)
-
         types = []
         for pref in image.getElementsByTagName('preferences'):
             for type in pref.getElementsByTagName('type'):
@@ -362,7 +348,7 @@ class KiwiCreateImageTask(BaseBuildTask):
         repos.append(baseurl)
 
         path = os.path.join(scmsrcdir, desc_path)
-        desc, types = self.prepareDescription(path, name, version, release, repos)
+        desc, types = self.prepareDescription(path, name, version, repos)
         self.uploadFile(desc)
 
         cmd = ['kiwi-ng']
@@ -378,6 +364,16 @@ class KiwiCreateImageTask(BaseBuildTask):
         if rv:
             raise koji.GenericError("Kiwi failed")
 
+        # rename artifacts accordingly to release
+        bundle_dir = '/builddir/result/bundle'
+        cmd = ['kiwi-ng', 'result', 'bundle',
+               '--target-dir', target_dir,
+               '--bundle-dir', bundle_dir,
+               '--id', release]
+        rv = broot.mock(['--cwd', broot.tmpdir(within=True), '--chroot', '--'] + cmd)
+        if rv:
+            raise koji.GenericError("Kiwi failed")
+
         resultdir = joinpath(broot.rootdir(), target_dir[1:])
         try:
             # new version has json format, older pickle (needs python3-kiwi installed)
@@ -385,7 +381,7 @@ class KiwiCreateImageTask(BaseBuildTask):
         except (FileNotFoundError, JSONDecodeError):
             # try old variant
             import pickle
-            result = pickle.load(open(joinpath(resultdir, 'kiwi.result'), 'rb')) # nosec
+            result = pickle.load(open(joinpath(resultdir, 'kiwi.result'), 'rb'))  # nosec
             # convert from namedtuple's to normal dict
             result_files = {k: v._asdict() for k, v in result.result_files.items()}
 
@@ -408,18 +404,22 @@ class KiwiCreateImageTask(BaseBuildTask):
         if os.path.exists(root_log_path):
             self.uploadFile(root_log_path, remoteName="image-root.log")
 
-        # for type in types:
-        #     img_file = '%s.%s-%s.%s' % (name, version, arch, type)
-        #     self.uploadFile(os.path.join(broot.rootdir()), remoteName=img_file)
-        #     imgdata['files'].append(img_file)
         for ftype in ('disk_image', 'disk_format_image', 'installation_image'):
             fdata = result_files.get(ftype)
             if not fdata:
                 continue
-            fpath = os.path.join(broot.rootdir(), fdata['filename'][1:])
+            # hack to use correct paths derived from results
+            filename = os.path.basename(fdata['filename'])
+            (name, ext) = os.path.splitext(filename)
+            filename = f'{name}-{release}{ext}'
+            fpath = os.path.dirname(fdata['filename'])[len(target_dir) + 1:]
+            fpath = os.path.join(broot.rootdir(), bundle_dir[1:], fpath, filename)
             img_file = os.path.basename(fpath)
-            self.uploadFile(fpath, remoteName=os.path.basename(img_file))
-            imgdata['files'].append(img_file)
+            if os.path.exists(fpath):
+                self.uploadFile(fpath, remoteName=os.path.basename(img_file))
+                imgdata['files'].append(img_file)
+            else:
+                self.logger.debug(f'File {img_file} is not present in bundle but is in results')
 
         if not self.opts.get('scratch'):
             if False:
diff --git a/plugins/cli/kiwi.py b/plugins/cli/kiwi.py
index 9ae9046..7e7af2c 100644
--- a/plugins/cli/kiwi.py
+++ b/plugins/cli/kiwi.py
@@ -18,6 +18,7 @@ def handle_kiwi_build(goptions, session, args):
     parser = OptionParser(usage=usage)
     parser.add_option("--scratch", action="store_true", default=False,
                       help="Perform a scratch build")
+    parser.add_option("--release", help="Release of the output image")
     parser.add_option("--repo", action="append",
                       help="Specify a repo that will override the repo used to install "
                            "RPMs in the image. May be used multiple times. The "
@@ -50,6 +51,7 @@ def handle_kiwi_build(goptions, session, args):
                             for arch in options.optional_arches.split(',')
                             if arch],
         'profile': options.kiwi_profile,
+        'release': options.release,
     }
 
     arches = []
diff --git a/plugins/hub/kiwi.py b/plugins/hub/kiwi.py
index 2ab0cdd..3adf6e9 100644
--- a/plugins/hub/kiwi.py
+++ b/plugins/hub/kiwi.py
@@ -16,7 +16,7 @@ koji.tasks.LEGACY_SIGNATURES['createKiwiImage'] = [
 
 @export
 def kiwiBuild(target, arches, desc_url, desc_path, optional_arches=None, profile=None,
-              scratch=False, priority=None, repos=None):
+              scratch=False, priority=None, repos=None, release=None):
     context.session.assertPerm('image')
     taskOpts = {
         'channel': 'image',
@@ -32,6 +32,7 @@ def kiwiBuild(target, arches, desc_url, desc_path, optional_arches=None, profile
         'optional_arches': optional_arches,
         'profile': profile,
         'scratch': scratch,
+        'release': release,
         'repos': repos or [],
     }
     return kojihub.make_task('kiwiBuild',

From 450ad4fa15a84e3906dd3f32848b75e68753a3ba Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Jan 12 2022 12:07:05 +0000
Subject: [PATCH 6/10] fix packaging


---

diff --git a/docs/source/plugins.rst b/docs/source/plugins.rst
index 79d3fe0..839678b 100644
--- a/docs/source/plugins.rst
+++ b/docs/source/plugins.rst
@@ -258,8 +258,8 @@ most simple configuration will look like:
 
 .. code-block:: shell
 
-   $ koji add-group kiwi-build-tag kiwi
-   $ koji add-group-pkg kiwi-build-tag kiwi kiwi-cli kiwi-systemdeps
+   $ koji add-group kiwi-build-tag kiwi-build
+   $ koji add-group-pkg kiwi-build-tag kiwi-build kiwi-cli kiwi-systemdeps
 
 Another thing we need to ensure is that we're building in chroot and not in
 container.
diff --git a/plugins/Makefile b/plugins/Makefile
index 6ea1d2a..caf0706 100644
--- a/plugins/Makefile
+++ b/plugins/Makefile
@@ -1,11 +1,12 @@
+PYVER_MAJOR := $(shell $(PYTHON) -c 'import sys; print(".".join(sys.version.split(".")[:1]))')
 PKGDIR = $(shell $(PYTHON) -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")/$(PACKAGE)
 
 CLIPLUGINDIR = $(PKGDIR)/koji_cli_plugins
 HUBPLUGINDIR = /usr/lib/koji-hub-plugins
 BUILDERPLUGINDIR = /usr/lib/koji-builder-plugins
-CLIFILES = $(wildcard cli/*.py)
-HUBFILES = $(wildcard hub/*.py)
-BUILDERFILES = $(wildcard builder/*.py)
+CLIFILES_ALL = $(wildcard cli/*.py)
+HUBFILES_ALL = $(wildcard hub/*.py)
+BUILDERFILES_ALL = $(wildcard builder/*.py)
 CLICONFDIR = /etc/koji/plugins
 HUBCONFDIR = /etc/koji-hub/plugins
 BUILDERCONFDIR = /etc/kojid/plugins
@@ -13,6 +14,16 @@ CLICONFFILES = $(wildcard cli/*.conf)
 HUBCONFFILES = $(wildcard hub/*.conf)
 BUILDERCONFFILES = $(wildcard builder/*.conf)
 
+ifeq ($(PYVER_MAJOR),2)
+	HUBFILES=$(filter-out hub/kiwi.py,$(HUBFILES_ALL))
+	BUILDERFILES=$(filter-out builder/kiwi.py,$(BUILDERFILES_ALL))
+	CLIFILES=$(filter-out cli/kiwi.py,$(CLIFILES_ALL))
+else
+	HUBFILES=$(HUBFILES_ALL)
+	BUILDERFILES=$(BUILDERFILES_ALL)
+	CLIFILES=$(CLIFILES_ALL)
+endif
+	
 _default:
 	@echo "nothing to make.  try make install"
 
diff --git a/plugins/builder/kiwi.py b/plugins/builder/kiwi.py
index 277f08a..cac54ee 100644
--- a/plugins/builder/kiwi.py
+++ b/plugins/builder/kiwi.py
@@ -303,7 +303,7 @@ class KiwiCreateImageTask(BaseBuildTask):
                           arch=arch,
                           task_id=self.id,
                           repo_id=repo_info['id'],
-                          install_group='kiwi',
+                          install_group='kiwi-build',
                           setup_dns=True,
                           bind_opts={'dirs': {'/dev': '/dev', }})
         broot.workdir = self.workdir
@@ -404,10 +404,7 @@ class KiwiCreateImageTask(BaseBuildTask):
         if os.path.exists(root_log_path):
             self.uploadFile(root_log_path, remoteName="image-root.log")
 
-        for ftype in ('disk_image', 'disk_format_image', 'installation_image'):
-            fdata = result_files.get(ftype)
-            if not fdata:
-                continue
+        for ftype, fdata in result_files.items():
             # hack to use correct paths derived from results
             filename = os.path.basename(fdata['filename'])
             (name, ext) = os.path.splitext(filename)

From 16ce5c162adf4ae0c228ac5f0cda9305c9444ff6 Mon Sep 17 00:00:00 2001
From: Igor Raits <igor.raits@gmail.com>
Date: Jan 16 2022 12:22:17 +0000
Subject: [PATCH 7/10] kiwi: Collect all files from bundle directory


Bundle directory has slightly different files (compressed, etc.) and
does not contain any JSON files so let's just collect all files from
there.

Signed-off-by: Igor Raits <igor.raits@gmail.com>

---

diff --git a/plugins/builder/kiwi.py b/plugins/builder/kiwi.py
index cac54ee..e2b2e83 100644
--- a/plugins/builder/kiwi.py
+++ b/plugins/builder/kiwi.py
@@ -1,6 +1,4 @@
 import glob
-import json
-from json.decoder import JSONDecodeError
 import os
 import xml.dom.minidom
 from fnmatch import fnmatch
@@ -374,17 +372,6 @@ class KiwiCreateImageTask(BaseBuildTask):
         if rv:
             raise koji.GenericError("Kiwi failed")
 
-        resultdir = joinpath(broot.rootdir(), target_dir[1:])
-        try:
-            # new version has json format, older pickle (needs python3-kiwi installed)
-            result_files = json.load(open(joinpath(resultdir, 'kiwi.result.json')))
-        except (FileNotFoundError, JSONDecodeError):
-            # try old variant
-            import pickle
-            result = pickle.load(open(joinpath(resultdir, 'kiwi.result'), 'rb'))  # nosec
-            # convert from namedtuple's to normal dict
-            result_files = {k: v._asdict() for k, v in result.result_files.items()}
-
         imgdata = {
             'arch': arch,
             'task_id': self.id,
@@ -404,25 +391,18 @@ class KiwiCreateImageTask(BaseBuildTask):
         if os.path.exists(root_log_path):
             self.uploadFile(root_log_path, remoteName="image-root.log")
 
-        for ftype, fdata in result_files.items():
-            # hack to use correct paths derived from results
-            filename = os.path.basename(fdata['filename'])
-            (name, ext) = os.path.splitext(filename)
-            filename = f'{name}-{release}{ext}'
-            fpath = os.path.dirname(fdata['filename'])[len(target_dir) + 1:]
-            fpath = os.path.join(broot.rootdir(), bundle_dir[1:], fpath, filename)
-            img_file = os.path.basename(fpath)
-            if os.path.exists(fpath):
-                self.uploadFile(fpath, remoteName=os.path.basename(img_file))
-                imgdata['files'].append(img_file)
-            else:
-                self.logger.debug(f'File {img_file} is not present in bundle but is in results')
+        bundle_path = os.path.join(broot.rootdir(), bundle_dir[1:])
+        for fname in os.listdir(bundle_path):
+            self.uploadFile(os.path.join(bundle_path, fname), remoteName=fname)
+            imgdata['files'].append(fname)
 
         if not self.opts.get('scratch'):
             if False:
                 # should be used after kiwi update
-                fpath = os.path.join(broot.rootdir(),
-                                     result_files['image_packages'].filename[1:])
+                fpath = os.path.join(
+                    bundle_path,
+                    next(f for f in imgdata['files'] if f.endswith('.packages')),
+                )
                 hdrlist = self.getImagePackages(fpath)
             else:
                 cachepath = os.path.join(broot.rootdir(), 'var/cache/kiwi/dnf')

From d74d94614e14eff5ea18289e75e2ed1d10fb13c7 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Jan 17 2022 12:25:12 +0000
Subject: [PATCH 8/10] remove unused code


---

diff --git a/plugins/builder/kiwi.py b/plugins/builder/kiwi.py
index e2b2e83..d1bcc2f 100644
--- a/plugins/builder/kiwi.py
+++ b/plugins/builder/kiwi.py
@@ -4,7 +4,6 @@ import xml.dom.minidom
 from fnmatch import fnmatch
 
 import koji
-from koji.util import joinpath, to_list
 from koji.tasks import ServerExit
 from __main__ import BaseBuildTask, BuildImageTask, BuildRoot, SCM
 
@@ -123,7 +122,7 @@ class KiwiBuildTask(BuildImageTask):
                     canfail.append(subtasks[arch])
             self.logger.debug("Got image subtasks: %r" % (subtasks))
             self.logger.debug("Waiting on image subtasks (%s can fail)..." % canfail)
-            results = self.wait(to_list(subtasks.values()), all=True,
+            results = self.wait(list(subtasks.values()), all=True,
                                 failany=True, canfail=canfail)
 
             # if everything failed, fail even if all subtasks are in canfail

From c1e36926b0c21077c930c7b297fd4d2ad9a6242e Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Jan 17 2022 14:44:54 +0000
Subject: [PATCH 9/10] optional "make prep"


---

diff --git a/plugins/builder/kiwi.py b/plugins/builder/kiwi.py
index d1bcc2f..1d9f52a 100644
--- a/plugins/builder/kiwi.py
+++ b/plugins/builder/kiwi.py
@@ -344,6 +344,12 @@ class KiwiCreateImageTask(BaseBuildTask):
         self.logger.debug('BASEURL: %s' % baseurl)
         repos.append(baseurl)
 
+        if opts.get('make_prep'):
+            cmd = ['make', 'prep']
+            rv = broot.mock(['--cwd', broot.tmpdir(within=True), '--chroot', '--'] + cmd)
+            if rv:
+                raise koji.GenericError("Preparation step failed")
+
         path = os.path.join(scmsrcdir, desc_path)
         desc, types = self.prepareDescription(path, name, version, repos)
         self.uploadFile(desc)
diff --git a/plugins/cli/kiwi.py b/plugins/cli/kiwi.py
index 7e7af2c..8786b13 100644
--- a/plugins/cli/kiwi.py
+++ b/plugins/cli/kiwi.py
@@ -27,6 +27,8 @@ def handle_kiwi_build(goptions, session, args):
                       help="Do not display progress of the upload")
     parser.add_option("--kiwi-profile", action="store", default=None,
                       help="Select profile from description file")
+    parser.add_option("--make-prep", action="store_true", default=False,
+                      help="Run 'make prep' in checkout before starting the build")
     parser.add_option("--can-fail", action="store", dest="optional_arches",
                       metavar="ARCH1,ARCH2,...", default="",
                       help="List of archs which are not blocking for build "
@@ -52,6 +54,7 @@ def handle_kiwi_build(goptions, session, args):
                             if arch],
         'profile': options.kiwi_profile,
         'release': options.release,
+        'make_prep': options.make_prep,
     }
 
     arches = []
diff --git a/plugins/hub/kiwi.py b/plugins/hub/kiwi.py
index 3adf6e9..fd4877c 100644
--- a/plugins/hub/kiwi.py
+++ b/plugins/hub/kiwi.py
@@ -16,7 +16,7 @@ koji.tasks.LEGACY_SIGNATURES['createKiwiImage'] = [
 
 @export
 def kiwiBuild(target, arches, desc_url, desc_path, optional_arches=None, profile=None,
-              scratch=False, priority=None, repos=None, release=None):
+              scratch=False, priority=None, make_prep=False, repos=None, release=None):
     context.session.assertPerm('image')
     taskOpts = {
         'channel': 'image',
@@ -34,6 +34,7 @@ def kiwiBuild(target, arches, desc_url, desc_path, optional_arches=None, profile
         'scratch': scratch,
         'release': release,
         'repos': repos or [],
+        'make_prep': make_prep,
     }
     return kojihub.make_task('kiwiBuild',
                              [target, arches, desc_url, desc_path, opts],

From 79a81930ec702114fd7f6855353d0ba704870d68 Mon Sep 17 00:00:00 2001
From: Tomas Kopecek <tkopecek@redhat.com>
Date: Jan 19 2022 14:26:14 +0000
Subject: [PATCH 10/10] fix working dir path


---

diff --git a/plugins/builder/kiwi.py b/plugins/builder/kiwi.py
index 1d9f52a..6a08e82 100644
--- a/plugins/builder/kiwi.py
+++ b/plugins/builder/kiwi.py
@@ -346,7 +346,7 @@ class KiwiCreateImageTask(BaseBuildTask):
 
         if opts.get('make_prep'):
             cmd = ['make', 'prep']
-            rv = broot.mock(['--cwd', broot.tmpdir(within=True), '--chroot', '--'] + cmd)
+            rv = broot.mock(['--cwd', os.path.join(broot.tmpdir(within=True), os.path.basename(scmsrcdir), desc_path), '--chroot', '--'] + cmd)
             if rv:
                 raise koji.GenericError("Preparation step failed")
 

