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ář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2017-11-02 14:51:00 +01:00
parent cb740f063e
commit 388be481ea
2 changed files with 41 additions and 33 deletions

View File

@ -15,22 +15,14 @@
import os import os
import sys
from fnmatch import fnmatch from fnmatch import fnmatch
import contextlib import contextlib
from functools import cmp_to_key
from six.moves import shlex_quote from six.moves import shlex_quote
from kobo.shortcuts import force_list, relative_path, run from kobo.shortcuts import force_list, relative_path, run
from pungi import util 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): def get_boot_options(arch, createfrom, efi=True):
"""Checks to see what we need as the -b option for mkisofs""" """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) seen_dirs.add(dn)
f = open(file_name, "w") 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, # make sure all files required for boot come first,
# otherwise there may be problems with booting (large LBA address, etc.) # otherwise there may be problems with booting (large LBA address, etc.)
found = False found = False
@ -357,9 +349,7 @@ def write_graft_points(file_name, h, exclude=None):
def _is_rpm(path): def _is_rpm(path):
if path.endswith(".rpm"): return path.endswith(".rpm")
return True
return False
def _is_image(path): def _is_image(path):
@ -380,27 +370,12 @@ def _is_image(path):
return False return False
def cmp_graft_points(x, y): def graft_point_sort_key(x):
x_is_rpm = _is_rpm(x) """
y_is_rpm = _is_rpm(y) Images are sorted first, followed by other files. RPMs always come last.
x_is_image = _is_image(x) In the same group paths are sorted alphabetically.
y_is_image = _is_image(y) """
return (0 if _is_image(x) else 2 if _is_rpm(x) else 1, x)
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)
@contextlib.contextmanager @contextlib.contextmanager

View File

@ -4,6 +4,7 @@ import mock
import os import os
import sys import sys
import unittest import unittest
import itertools
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) 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_run.call_args_list), 1)
self.assertEqual(len(mock_unmount.call_args_list), 0) self.assertEqual(len(mock_unmount.call_args_list), 0)
self.assertEqual(len(log.mock_calls), 1) 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')