diff --git a/kayobe/tests/unit/test_utils.py b/kayobe/tests/unit/test_utils.py index 85e983374..e1b4e2b02 100644 --- a/kayobe/tests/unit/test_utils.py +++ b/kayobe/tests/unit/test_utils.py @@ -179,6 +179,13 @@ key2: value2 result = utils._detect_install_prefix(path) self.assertEqual(expected, os.path.normpath(result)) + def test_detect_install_prefix_multiple_lib_components(self): + tmp_path = os.path.realpath('/tmp') + path = "%s/lib/test/local/lib/python3.6/dist-packages" % tmp_path + expected = os.path.normpath("%s/lib/test/local/" % tmp_path) + result = utils._detect_install_prefix(path) + self.assertEqual(expected, os.path.normpath(result)) + def test_intersect_limits_no_arg_no_cli(self): result = utils.intersect_limits(None, None) self.assertEqual("", result) diff --git a/kayobe/utils.py b/kayobe/utils.py index 2ded367c4..727fd783a 100644 --- a/kayobe/utils.py +++ b/kayobe/utils.py @@ -14,7 +14,6 @@ import base64 import glob -import itertools import logging import os import shutil @@ -40,13 +39,11 @@ def _detect_install_prefix(path): script_path = os.path.realpath(path) script_path = os.path.normpath(script_path) components = script_path.split(os.sep) - # use heuristic: anything before 'lib' in path is the prefix + # use heuristic: anything before the last 'lib' in path is the prefix if 'lib' not in components: return None - prefix = itertools.takewhile( - lambda x: x != "lib", - components - ) + last_lib = len(components) - 1 - components[::-1].index('lib') + prefix = components[:last_lib] prefix_path = os.sep.join(prefix) return prefix_path diff --git a/releasenotes/notes/install-prefix-multiple-lib-9288e8c11da3c0bc.yaml b/releasenotes/notes/install-prefix-multiple-lib-9288e8c11da3c0bc.yaml new file mode 100644 index 000000000..4043b6dea --- /dev/null +++ b/releasenotes/notes/install-prefix-multiple-lib-9288e8c11da3c0bc.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fixes a failure to detect the Kayobe installation prefix when ``lib`` is + present multiple times in the installation path. See `story 2009721 + `__ for details.