From d4c5d95968323fabc7e5499d376c5d63b690d9f1 Mon Sep 17 00:00:00 2001 From: Jana Cupova Date: Feb 06 2023 12:17:33 +0000 Subject: Move class out of function and create to_hexdigest function --- diff --git a/koji/__init__.py b/koji/__init__.py index 66b31b6..a6208d0 100644 --- a/koji/__init__.py +++ b/koji/__init__.py @@ -944,49 +944,51 @@ def get_sighdr_key(sighdr): return get_sigpacket_key_id(sig) -def spliced_sig_reader(path, sighdr, bufsize=8192): - """A generator that yields the contents of an rpm with signature spliced in""" - class Stream(io.RawIOBase): - def __init__(self, path, sighdr): - self.path = path - self.sighdr = sighdr - self.buf = None - self.gen = self.generator() - - def generator(self): - (start, size) = find_rpm_sighdr(self.path) - with open(path, 'rb') as fo: - # the part before the signature - yield fo.read(start) - - # the spliced signature - yield sighdr - - # skip original signature - fo.seek(size, 1) - - # the part after the signature - while True: - buf = fo.read(bufsize) - if not buf: - break - yield buf +class SplicedSigStreamReader(io.RawIOBase): + def __init__(self, path, sighdr, bufsize): + self.path = path + self.sighdr = sighdr + self.buf = None + self.gen = self.generator() + self.bufsize = bufsize + + def generator(self): + (start, size) = find_rpm_sighdr(self.path) + with open(self.path, 'rb') as fo: + # the part before the signature + yield fo.read(start) + + # the spliced signature + yield self.sighdr + + # skip original signature + fo.seek(size, 1) + + # the part after the signature + while True: + buf = fo.read(self.bufsize) + if not buf: + break + yield buf + + def readable(self): + return True - def readable(self): - return True + def readinto(self, b): + try: + expected_buf_size = len(b) + data = self.buf or next(self.gen) + output = data[:expected_buf_size] + self.buf = data[expected_buf_size:] + b[:len(output)] = output + return len(output) + except StopIteration: + return 0 # indicate EOF - def readinto(self, b): - try: - expected_buf_size = len(b) - data = self.buf or next(self.gen) - output = data[:expected_buf_size] - self.buf = data[expected_buf_size:] - b[:len(output)] = output - return len(output) - except StopIteration: - return 0 # indicate EOF - - return io.BufferedReader(Stream(path, sighdr), buffer_size=bufsize) + +def spliced_sig_reader(path, sighdr, bufsize=8192): + """Returns a file-like object whose contents have the new signature spliced in""" + return io.BufferedReader(SplicedSigStreamReader(path, sighdr, bufsize), buffer_size=bufsize) def splice_rpm_sighdr(sighdr, src, dst=None, bufsize=8192, callback=None): diff --git a/kojihub/kojihub.py b/kojihub/kojihub.py index 7294dbc..469b839 100644 --- a/kojihub/kojihub.py +++ b/kojihub/kojihub.py @@ -7980,6 +7980,12 @@ class MultiSum(object): for name, checksum in self.checksums.items(): checksum.update(buf) + def to_hexdigest(self): + checksums_hex = {} + for name, checksum in self.checksums.items(): + checksums_hex[name] = checksum.hexdigest() + return checksums_hex + def calculate_chsum(path, checksum_types): """Calculate checksum for specific checksum_types @@ -8002,7 +8008,7 @@ def calculate_chsum(path, checksum_types): break msum.update(chunk) f.close() - return msum.checksums + return msum.to_hexdigest() def write_signed_rpm(an_rpm, sigkey, force=False, checksum_types=None): @@ -8052,7 +8058,7 @@ def write_signed_rpm(an_rpm, sigkey, force=False, checksum_types=None): koji.ensuredir(os.path.dirname(signedpath)) msum = MultiSum(checksum_types) koji.splice_rpm_sighdr(sighdr, rpm_path, dst=signedpath, callback=msum.update) - create_rpm_checksum(rpm_id, sigkey, msum.checksums) + create_rpm_checksum(rpm_id, sigkey, msum.to_hexdigest()) def query_history(tables=None, **kwargs): @@ -12376,14 +12382,14 @@ class RootExports(object): if missing_chsum_sigkeys: binfo = get_build(rpm_info['build_id']) builddir = koji.pathinfo.build(binfo) - rpm_path = koji.joinpath(builddir, koji.pathinfo.rpm(rpm_info)) + rpm_path = joinpath(builddir, koji.pathinfo.rpm(rpm_info)) for sigkey, chsums in missing_chsum_sigkeys.items(): - signedpath = koji.joinpath(builddir, koji.pathinfo.signed(rpm_info, sigkey)) + signedpath = joinpath(builddir, koji.pathinfo.signed(rpm_info, sigkey)) if os.path.exists(signedpath): with open(signedpath, 'rb') as fo: chsums_dict = calculate_chsum(fo, chsums) else: - sig_path = koji.joinpath(builddir, koji.pathinfo.sighdr(rpm_info, sigkey)) + sig_path = joinpath(builddir, koji.pathinfo.sighdr(rpm_info, sigkey)) with open(sig_path, 'rb') as fo: sighdr = fo.read() with koji.spliced_sig_reader(rpm_path, sighdr) as fo: @@ -15596,6 +15602,6 @@ def create_rpm_checksum(rpm_id, sigkey, chsum_dict): if chsum_dict: insert = BulkInsertProcessor(table='rpm_checksum') for func, chsum in chsum_dict.items(): - insert.add_record(rpm_id=rpm_id, sigkey=sigkey, checksum=chsum.hexdigest(), + insert.add_record(rpm_id=rpm_id, sigkey=sigkey, checksum=chsum, checksum_type=koji.CHECKSUM_TYPES[func]) insert.execute() diff --git a/tests/test_hub/test_get_rpm_checksums.py b/tests/test_hub/test_get_rpm_checksums.py index 592bf35..3fec3d3 100644 --- a/tests/test_hub/test_get_rpm_checksums.py +++ b/tests/test_hub/test_get_rpm_checksums.py @@ -17,13 +17,15 @@ class TestGetRpmChecksums(unittest.TestCase): self.create_rpm_checksum = mock.patch('kojihub.kojihub.create_rpm_checksum').start() self.calculate_chsum = mock.patch('kojihub.kojihub.calculate_chsum').start() self.get_rpm = mock.patch('kojihub.kojihub.get_rpm').start() + self.get_build = mock.patch('kojihub.kojihub.get_build').start() self.QueryProcessor = mock.patch('kojihub.kojihub.QueryProcessor', side_effect=self.getQuery).start() self.queries = [] self.query_execute = mock.MagicMock() self.rpm_info = {'id': 123, 'name': 'test-name', 'version': '1.1', 'release': '123', - 'arch': 'arch'} + 'arch': 'arch', 'build_id': 3} self.nvra = "%(name)s-%(version)s-%(release)s.%(arch)s" % self.rpm_info + self.build_info = {'build_id': 3, 'name': 'test-name', 'version': '1.1', 'release': '123'} def tearDown(self): mock.patch.stopall() @@ -125,6 +127,7 @@ class TestGetRpmChecksums(unittest.TestCase): rpm_id = 123 checksum_types = ['md5'] self.get_rpm.return_value = self.rpm_info + self.get_build.return_value = self.build_info expected_result = {'sigkey-1': {'md5': 'checksum-md5'}} calculate_chsum_res = {'sigkey-1': {'md5': 'checksum-md5'}} self.query_execute.side_effect = [ @@ -155,33 +158,12 @@ class TestGetRpmChecksums(unittest.TestCase): [{'checksum': 'checksum-md5', 'checksum_type': 0}], {'sigkey-1': {'md5'}} ) - def test_missing_valid_checksum_generated_with_strict(self): - self.os_path.return_value = False - rpm_id = 123 - checksum_types = ['md5'] - self.get_rpm.return_value = self.rpm_info - self.query_execute.return_value = [{'sigkey': 'sigkey-1'}] - with self.assertRaises(koji.GenericError) as ex: - self.exports.getRPMChecksums(rpm_id, checksum_types=checksum_types, strict=True) - self.assertEqual(f"Rpm {self.nvra} doesn't have cached signed copies.", - str(ex.exception)) - - self.assertEqual(len(self.queries), 1) - query = self.queries[0] - self.assertEqual(query.tables, ['rpmsigs']) - self.assertEqual(query.joins, None) - self.assertEqual(query.clauses, ['rpm_id=%(rpm_id)i']) - - self.get_rpm.assert_called_once_with(rpm_id, strict=True) - self.calculate_chsum.assert_not_called() - self.create_rpm_checksum.assert_not_called() - self.create_rpm_checksum_output.assert_not_called() - @mock.patch('kojihub.kojihub.open') def test_missing_valid_more_checksum_generated_and_exists(self, open): self.os_path.return_value = True rpm_id = 123 self.get_rpm.return_value = self.rpm_info + self.get_build.return_value = self.build_info checksum_types = ['md5', 'sha256'] expected_result = {'sigkey-1': {'md5': 'checksum-md5', 'sha256': 'checksum-sha256'}} calculate_chsum_res = {'sigkey-1': {'sha256': 'checksum-sha256'}} @@ -222,6 +204,7 @@ class TestGetRpmChecksums(unittest.TestCase): self.os_path.return_value = True rpm_id = 123 self.get_rpm.return_value = self.rpm_info + self.get_build.return_value = self.build_info checksum_types = ['md5', 'sha256'] expected_result = {'sigkey1': {'md5': 'checksum-md5', 'sha256': 'checksum-sha256'}, 'sigkey2': {'md5': 'checksum-md5', 'sha256': 'checksum-sha256'}}