From 388be481eaf9d072c17537bf9f1270a866d6408a Mon Sep 17 00:00:00 2001 From: Lubomír Sedlář Date: Nov 08 2017 14:35:29 +0000 Subject: iso-wrapper: Remove hacks for sorting We can use a key function instead of relying to the deprecated cmp. This makes the code work on Python 2.6 and on recent versions it makes it faster. Signed-off-by: Lubomír Sedlář --- diff --git a/pungi/wrappers/iso.py b/pungi/wrappers/iso.py index 6e4fec4..57de093 100644 --- a/pungi/wrappers/iso.py +++ b/pungi/wrappers/iso.py @@ -15,22 +15,14 @@ import os -import sys from fnmatch import fnmatch import contextlib -from functools import cmp_to_key from six.moves import shlex_quote from kobo.shortcuts import force_list, relative_path, run from pungi import util -# HACK: define cmp in python3 -if sys.version_info[0] == 3: - def cmp(a, b): - return (a > b) - (a < b) - - def get_boot_options(arch, createfrom, efi=True): """Checks to see what we need as the -b option for mkisofs""" @@ -342,7 +334,7 @@ def write_graft_points(file_name, h, exclude=None): seen_dirs.add(dn) f = open(file_name, "w") - for i in sorted(result, key=cmp_to_key(cmp_graft_points)): + for i in sorted(result, key=graft_point_sort_key): # make sure all files required for boot come first, # otherwise there may be problems with booting (large LBA address, etc.) found = False @@ -357,9 +349,7 @@ def write_graft_points(file_name, h, exclude=None): def _is_rpm(path): - if path.endswith(".rpm"): - return True - return False + return path.endswith(".rpm") def _is_image(path): @@ -380,27 +370,12 @@ def _is_image(path): return False -def cmp_graft_points(x, y): - x_is_rpm = _is_rpm(x) - y_is_rpm = _is_rpm(y) - x_is_image = _is_image(x) - y_is_image = _is_image(y) - - if x_is_rpm and y_is_rpm: - return cmp(x, y) - if x_is_rpm: - return 1 - if y_is_rpm: - return -1 - - if x_is_image and y_is_image: - return cmp(x, y) - if x_is_image: - return -1 - if y_is_image: - return 1 - - return cmp(x, y) +def graft_point_sort_key(x): + """ + Images are sorted first, followed by other files. RPMs always come last. + In the same group paths are sorted alphabetically. + """ + return (0 if _is_image(x) else 2 if _is_rpm(x) else 1, x) @contextlib.contextmanager diff --git a/tests/test_iso_wrapper.py b/tests/test_iso_wrapper.py index ff97b0b..a4a00a1 100644 --- a/tests/test_iso_wrapper.py +++ b/tests/test_iso_wrapper.py @@ -4,6 +4,7 @@ import mock import os import sys import unittest +import itertools sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) @@ -75,3 +76,35 @@ class TestIsoUtils(unittest.TestCase): self.assertEqual(len(mock_run.call_args_list), 1) self.assertEqual(len(mock_unmount.call_args_list), 0) self.assertEqual(len(log.mock_calls), 1) + + +class TestCmpGraftPoints(unittest.TestCase): + def assertSorted(self, *args): + """Tests that all permutations of arguments yield the same sorted results.""" + for perm in itertools.permutations(args): + self.assertEqual(sorted(perm, key=iso.graft_point_sort_key), + list(args)) + + def test_eq(self): + self.assertSorted('pkgs/foo.rpm', 'pkgs/foo.rpm') + + def test_rpms_sorted_alphabetically(self): + self.assertSorted('pkgs/bar.rpm', 'pkgs/foo.rpm') + + def test_images_sorted_alphabetically(self): + self.assertSorted('aaa.img', 'images/foo', 'isolinux/foo') + + def test_other_files_sorted_alphabetically(self): + self.assertSorted('bar.txt', 'foo.txt') + + def test_rpms_after_images(self): + self.assertSorted('foo.ins', 'bar.rpm') + + def test_other_after_images(self): + self.assertSorted('EFI/anything', 'zzz.txt') + + def test_rpm_after_other(self): + self.assertSorted('bbb.txt', 'aaa.rpm') + + def test_all_kinds(self): + self.assertSorted('etc/file', 'ppc/file', 'c.txt', 'd.txt', 'a.rpm', 'b.rpm')