From 106ccbcde53891b5f4cde393c47180e846f37e8f Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Mon, 13 Mar 2023 16:00:23 -0400 Subject: [PATCH] single opt for get, plus simple unit test --- koji/__init__.py | 17 ++++++++++-- tests/test_lib/test_rawheader_fields.py | 37 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/test_lib/test_rawheader_fields.py diff --git a/koji/__init__.py b/koji/__init__.py index 92854c22..7785b7ec 100644 --- a/koji/__init__.py +++ b/koji/__init__.py @@ -920,12 +920,25 @@ class RawHeader(object): else: raise GenericError("Unknown header data type: %x" % dtype) - def get(self, key, default=None, decode=None): + def get(self, key, default=None, decode=None, single=False): + # With decode on, we will _mostly_ return the same value that rpmlib will. + # There are exceptions where rpmlib will automatically translate or update values, e.g. + # * fields that rpm treats as scalars + # * special tags like Headerimmutable + # * i18n string translations + # * the Fileclass extension tag that overlaps a concrete tag + # * auto converting PREINPROG/POSTINPROG/etc to string arrays for older rpms entry = self.index.get(key) if entry is None: return default else: - return self._getitem(*entry[1:], decode=decode) + value = self._getitem(*entry[1:], decode=decode) + if single and isinstance(value, list): + if len(value) == 1: + return value[0] + else: + raise ValueError('single value requested for array at key %s' % key) + return value def rip_rpm_sighdr(src): diff --git a/tests/test_lib/test_rawheader_fields.py b/tests/test_lib/test_rawheader_fields.py new file mode 100644 index 00000000..18e0663c --- /dev/null +++ b/tests/test_lib/test_rawheader_fields.py @@ -0,0 +1,37 @@ +# coding=utf-8 +from __future__ import absolute_import +import os.path +import unittest + +import koji + + +class TestRawHeaderFields(unittest.TestCase): + + RPMFILES = [ + "test-deps-1-1.fc24.x86_64.rpm", + "test-files-1-1.fc27.noarch.rpm", + "test-nosrc-1-1.fc24.nosrc.rpm", + "test-deps-1-1.fc24.x86_64.rpm.signed", + "test-nopatch-1-1.fc24.nosrc.rpm", + "test-src-1-1.fc24.src.rpm", + ] + + def test_header_sizes(self): + for basename in self.RPMFILES: + fn = os.path.join(os.path.dirname(__file__), 'data/rpms', basename) + + rh = koji.RawHeader(koji.rip_rpm_hdr(fn)) + hdr = koji.get_rpm_header(fn) + + for key in rh.index: + if key in (63, 1141): + continue + ours = rh.get(key, decode=True) + theirs = hdr[key] + if type(ours) != type(theirs): + if isinstance(ours, list) and len(ours) == 1 and ours[0] == theirs: + # rpm is presenting as a scalar + continue + # otherwise + self.assertEqual(ours, theirs)