From 1c7d17d1ee972fdfae47258b5a28230b5ee1787c Mon Sep 17 00:00:00 2001 From: Pierre Riteau Date: Tue, 29 Oct 2024 13:54:23 +0100 Subject: [PATCH] Fix detection of editable installation When installing kolla-ansible with `pip install ./kolla-ansible`, pip always creates a direct_url.json file, even when not using an editable installation. We see this behaviour with Python 3.12, while direct_url.json is only created for editable installations on Python 3.9, which was used when this code was initially developed for Kayobe. When using a regular (non-editable) installation, this would make kolla-ansible invoke site.yml from the source directory instead of the virtualenv installation, causing a failure to load Ansible collections: Invalid plugin FQCN (ansible.utils.ipaddr): unable to locate collection ansible.utils Fix by returning the source URL only if dir_info.editable is True. Change-Id: Icdc2cedaa6a6e3a6b4351b1f4369e2e8b3a2dc97 --- kolla_ansible/utils.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/kolla_ansible/utils.py b/kolla_ansible/utils.py index 0f1728ec35..3f16490483 100644 --- a/kolla_ansible/utils.py +++ b/kolla_ansible/utils.py @@ -45,15 +45,20 @@ def _detect_install_prefix(path: os.path) -> str: return prefix_path -def _get_direct_url(dist: Distribution) -> str: +def _get_direct_url_if_editable(dist: Distribution) -> str: direct_url = os.path.join(dist._path, 'direct_url.json') + editable = None if os.path.isfile(direct_url): with open(direct_url, 'r') as f: direct_url_content = json.loads(f.readline().strip()) - url = direct_url_content['url'] - prefix = 'file://' - if url.startswith(prefix): - return url[len(prefix):] + dir_info = direct_url_content.get('dir_info') + if dir_info is not None: + editable = dir_info.get('editable') + if editable: + url = direct_url_content['url'] + prefix = 'file://' + if url.startswith(prefix): + return url[len(prefix):] return None @@ -66,7 +71,7 @@ def _get_base_path() -> os.path: kolla_ansible_dist = list(Distribution.discover(name="kolla_ansible")) if kolla_ansible_dist: - direct_url = _get_direct_url(kolla_ansible_dist[0]) + direct_url = _get_direct_url_if_editable(kolla_ansible_dist[0]) if direct_url: return direct_url