Skip to content

Commit 3372dd8

Browse files
ravanellijlebon
authored andcommitted
cmd-koji-upload: Refactor get_rpm_list
- Refactor get_rpm_list to allow packages from the new meta.json and extensions.json from the node-image Signed-off-by: Renata Ravanelli <[email protected]>
1 parent 2562626 commit 3372dd8

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

src/cmd-koji-upload

+44-8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ sys.path.insert(0, f"{cosa_dir}/cosalib")
3131
sys.path.insert(0, cosa_dir)
3232

3333
from cosalib.cmdlib import get_basearch, load_json, write_json
34+
from enum import IntEnum
3435

3536
try:
3637
from cosalib.build import _Build
@@ -105,7 +106,8 @@ class Build(_Build):
105106
# new kind of image here, but _Build is geared towards that and has a
106107
# image_name_base() member that wants a platform string to name stuff
107108
self.platform = "koji"
108-
self._tmpdir = tempfile.mkdtemp(prefix="koji-build", dir=kwargs['buildroot'])
109+
self.buildroot = kwargs['buildroot']
110+
self._tmpdir = tempfile.mkdtemp(prefix="koji-build", dir=self.buildroot)
109111
self._state_file_tpl = self._tmpdir + "/cosa-cmd-koji-upload-{name}-{version}-{release}"
110112
kwargs.update({
111113
"require_commit": True,
@@ -116,7 +118,7 @@ class Build(_Build):
116118
self.name = f"{self.build_name}-{self.basearch}"
117119
self.source = self.get_meta_key(
118120
"meta", self.ckey("container-config-git"))
119-
self.host_rpms = self.get_rpm_list('host')
121+
self.host_rpms = self.get_rpm_list(RpmListType.HOST)
120122
def __del__(self):
121123
try:
122124
shutil.rmtree(self._tmpdir)
@@ -195,20 +197,41 @@ class Build(_Build):
195197

196198
return fname, True
197199

198-
def get_rpm_list(self, host=None):
200+
def get_rpm_list(self, kind: RpmListType):
199201
"""
200-
Translate commitmeta.json/HOST OS rpms into a json list
202+
Translate metadata/HOST OS rpms into a json list
201203
Returns the json rpms list
202204
"""
203205
components = []
204-
if host is None:
206+
if kind == RpmListType.META:
207+
file_path = f"{self.buildroot}/meta.json"
208+
with open(file_path) as f:
209+
data = json.load(f)
210+
rpms = data['rpmdb.pkglist']
211+
elif kind == RpmListType.EXTENSIONS:
212+
file_path = f"{self.buildroot}/extensions.json"
213+
with open(file_path) as f:
214+
data = json.load(f)
215+
rpms = []
216+
# Extensions in node image are stored different
217+
for name, full_version in data.items():
218+
version_release, arch = full_version.rsplit('.', 1)
219+
version_str, release = version_release.rsplit('-', 1)
220+
if '.' in version_str:
221+
epoch, version = version_str.split('.', 1)
222+
else:
223+
epoch = '0'
224+
version = version_str
225+
rpm_list = [name, epoch, version, release, arch]
226+
rpms.append(rpm_list)
227+
elif kind == RpmListType.COMMITMETA:
205228
rpms = self.commit["rpmostree.rpmdb.pkglist"]
206-
else:
229+
elif kind == RpmListType.HOST:
207230
host_rpms = subprocess.check_output('rpm -qa --qf="%{NAME}:%{EPOCH}:%{RELEASE}:%{VERSION}:%{ARCH}:%{SIGMD5}:%{SIGPGP} \n"', shell=True).strip()
208231
rpms = (host_rpms.decode('utf-8')).split("\n")
209232

210233
for rpm in rpms:
211-
if host is None:
234+
if kind != RpmListType.HOST:
212235
name, epoch, version, release, arch = rpm
213236
sigmd5, sigpgp, epoch = None, None, None
214237
else:
@@ -365,6 +388,11 @@ def kinit(keytab, principle):
365388
except Exception as err:
366389
raise Exception("failed to auth: ", err)
367390

391+
class RpmListType(IntEnum):
392+
COMMITMETA = 1
393+
EXTENSIONS = 2
394+
HOST = 3
395+
META = 4
368396

369397
class _KojiBase():
370398
"""
@@ -648,6 +676,10 @@ class Upload(_KojiBase):
648676

649677
@property
650678
def image_files(self):
679+
""" Backward-compatible property to maintain existing behavior. """
680+
return self.get_image_files(meta=False)
681+
682+
def get_image_files(self, meta=False):
651683
""" Generate outputs prepares the output listing. """
652684
if self._image_files is not None:
653685
return self._image_files
@@ -657,7 +689,11 @@ class Upload(_KojiBase):
657689
file_output = self.get_file_meta(value)
658690
if file_output is not None:
659691
if "commitmeta.json" in value['upload_path']:
660-
file_output["components"] = self.build.get_rpm_list()
692+
file_output["components"] = self.build.get_rpm_list(RpmListType.COMMITMETA)
693+
elif "meta.json" in value['upload_path'] and meta is not False:
694+
file_output["components"] = self.build.get_rpm_list(RpmListType.META)
695+
elif "extensions.json" in value['upload_path'] and meta is not False:
696+
file_output["components"] = self.build.get_rpm_list(RpmListType.EXTENSIONS)
661697
outputs.append(file_output)
662698
self._image_files = outputs
663699
return self._image_files

0 commit comments

Comments
 (0)