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
This commit is contained in:
Pierre Riteau 2024-10-29 13:54:23 +01:00
parent f15c0d3d48
commit 1c7d17d1ee

View File

@ -45,15 +45,20 @@ def _detect_install_prefix(path: os.path) -> str:
return prefix_path 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') direct_url = os.path.join(dist._path, 'direct_url.json')
editable = None
if os.path.isfile(direct_url): if os.path.isfile(direct_url):
with open(direct_url, 'r') as f: with open(direct_url, 'r') as f:
direct_url_content = json.loads(f.readline().strip()) direct_url_content = json.loads(f.readline().strip())
url = direct_url_content['url'] dir_info = direct_url_content.get('dir_info')
prefix = 'file://' if dir_info is not None:
if url.startswith(prefix): editable = dir_info.get('editable')
return url[len(prefix):] if editable:
url = direct_url_content['url']
prefix = 'file://'
if url.startswith(prefix):
return url[len(prefix):]
return None return None
@ -66,7 +71,7 @@ def _get_base_path() -> os.path:
kolla_ansible_dist = list(Distribution.discover(name="kolla_ansible")) kolla_ansible_dist = list(Distribution.discover(name="kolla_ansible"))
if kolla_ansible_dist: 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: if direct_url:
return direct_url return direct_url