From 59a1fc65461733fd5017f0a944e769506b6dbcc2 Mon Sep 17 00:00:00 2001 From: Yolanda Robla Date: Fri, 5 May 2017 16:50:16 +0200 Subject: [PATCH] Add sort_mount_point method As this method can be introduced without any dependency, provide it on an independent change to simplify reviews. This is a partial refactor based on I592c0b1329409307197460cfa8fd69798013f1f8 Change-Id: Idaf3d2b3b3e23d0b9d6bc071d67b961a829ae422 Co-Authored-By: Andreas Florath --- diskimage_builder/block_device/utils.py | 20 +++++++++++++++++++ .../functional/test_blockdevice_utils.py | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/diskimage_builder/block_device/utils.py b/diskimage_builder/block_device/utils.py index d8d33f921..f93fe67fb 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 43b0befdf..2be1f248b 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)