Fix installation prefix detection

When multiple 'lib' components are present in the installation path,
Kayobe fails to detect the right installation prefix which causes
various commands to raise an error.

Rewrite logic to detect the last 'lib' component in path instead. This
should work as long as this function is not stored under a lib directory
inside the Kayobe source tree.

Change-Id: Ie9b15db6563546ede9ce9735292ec566d540432a
Story: 2009721
Task: 44109
This commit is contained in:
Pierre Riteau 2021-12-02 22:41:53 +01:00
parent 8897ea6d35
commit e438189a19
3 changed files with 16 additions and 6 deletions

View File

@ -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)

View File

@ -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

View File

@ -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
<https://storyboard.openstack.org/#!/story/2009721>`__ for details.