From e65cb4e37b839f61cde5746c80e288473d41b96d Mon Sep 17 00:00:00 2001 From: Michael Hanke Date: Mon, 9 Oct 2023 14:25:51 +0200 Subject: [PATCH] Support `{python}` placeholder This placeholder is expanded on *execution* of a container, rather than on configuration/addition. This helps use the same Python installation that is also executing the datalad-container code. Previously, the docker-support code would expand and then hardcode `sys.executable` on configuring a container. This led to non-portable configuration (e.g., hardcoded `python.exe` on windows), and would fail to pick up the correct python installation in any case where the `python` entrypoint would not point to the correct one. Closes #226 Closes #224 --- datalad_container/containers_add.py | 6 +++++- datalad_container/containers_run.py | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/datalad_container/containers_add.py b/datalad_container/containers_add.py index 2c2b69b9..3fe1900c 100644 --- a/datalad_container/containers_add.py +++ b/datalad_container/containers_add.py @@ -65,7 +65,8 @@ def _guess_call_fmt(ds, name, url): elif url.startswith('shub://') or url.startswith('docker://'): return 'singularity exec {img} {cmd}' elif url.startswith('dhub://'): - return op.basename(sys.executable) + ' -m datalad_container.adapters.docker run {img} {cmd}' + # {python} is replaced with sys.executable on *execute* + return '{python} -m datalad_container.adapters.docker run {img} {cmd}' def _ensure_datalad_remote(repo): @@ -142,6 +143,9 @@ class ContainersAdd(Interface): replaced with the desired command. Additional placeholders: '{img_dspath}' is relative path to the dataset containing the image, '{img_dirpath}' is the directory containing the '{img}'. + '{python}' expands to the path of the Python executable that is + running the respective DataLad session, for example a + 'datalad containers-run' command. """, metavar="FORMAT", constraints=EnsureStr() | EnsureNone(), diff --git a/datalad_container/containers_run.py b/datalad_container/containers_run.py index d99e0d02..d7b4e796 100644 --- a/datalad_container/containers_run.py +++ b/datalad_container/containers_run.py @@ -4,6 +4,7 @@ import logging import os.path as op +import sys from datalad.interface.base import Interface from datalad.interface.base import build_doc @@ -115,6 +116,10 @@ def __call__(cmd, container_name=None, dataset=None, 'Convert it to a plain string.'.format(callspec)) try: cmd_kwargs = dict( + # point to the python installation that runs *this* code + # we know that it would have things like the docker + # adaptor installed with this extension package + python=sys.executable, img=image_path, cmd=cmd, img_dspath=image_dspath,