diff --git a/diskimage_builder/block_device/utils.py b/diskimage_builder/block_device/utils.py index d8d33f92..f93fe67f 100644 --- a/diskimage_builder/block_device/utils.py +++ b/diskimage_builder/block_device/utils.py @@ -97,3 +97,23 @@ def exec_sudo(cmd): logger.error("Calling [%s] failed with [%s]" % (" ".join(sudo_cmd), rval)) return rval + + +def sort_mount_points(mount_points): + logger.debug("sort_mount_points called [%s]" % mount_points) + + def insert_sorted(mp, sorted_mount_points): + if len(sorted_mount_points) == 0: + sorted_mount_points.append(mp) + return + for idx in range(0, len(sorted_mount_points)): + if sorted_mount_points[idx].startswith(mp): + sorted_mount_points.insert(idx, mp) + return + sorted_mount_points.append(mp) + + sorted_mount_points = [] + for mp in mount_points: + insert_sorted(mp, sorted_mount_points) + logger.debug("sort_mount_points result [%s]" % sorted_mount_points) + return sorted_mount_points diff --git a/diskimage_builder/tests/functional/test_blockdevice_utils.py b/diskimage_builder/tests/functional/test_blockdevice_utils.py index 43b0befd..2be1f248 100644 --- a/diskimage_builder/tests/functional/test_blockdevice_utils.py +++ b/diskimage_builder/tests/functional/test_blockdevice_utils.py @@ -15,6 +15,7 @@ from diskimage_builder.block_device.utils import parse_abs_size_spec from diskimage_builder.block_device.utils import parse_rel_size_spec +from diskimage_builder.block_device.utils import sort_mount_points import testtools @@ -47,3 +48,9 @@ class TestBlockDeviceUtils(testtools.TestCase): """Call parse_abs_size_spec with a completely broken unit spec""" self.assertRaises(RuntimeError, parse_abs_size_spec, "_+!HuHi+-=") + + def test_sort_mount_points(self): + """Run sort_mount_points with a set of paths""" + + smp = sort_mount_points(["/boot", "/", "/var/tmp", "/var"]) + self.assertEqual(['/', '/boot', '/var', '/var/tmp'], smp)