Skip to content

Commit e8bad29

Browse files
authored
Merge pull request galaxyproject#21276 from nilchia/fix_container
[25.0] Update the mulled.py script to check json output
2 parents 238e0b6 + 2438c60 commit e8bad29

3 files changed

Lines changed: 37 additions & 13 deletions

File tree

lib/galaxy/tool_util/deps/container_classes.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,28 @@
5252
from __future__ import print_function
5353
5454
import json
55-
import re
5655
import subprocess
5756
import tarfile
5857
5958
t = tarfile.TarFile("${cached_image_file}")
6059
meta_str = t.extractfile('repositories').read()
6160
meta = json.loads(meta_str)
62-
tag, tag_value = next(iter(meta.items()))
63-
rev, rev_value = next(iter(tag_value.items()))
61+
repository, tag_value = next(iter(meta.items()))
62+
tag, id = next(iter(tag_value.items()))
6463
cmd = "${images_cmd}"
65-
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
64+
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, text=True)
6665
stdo, stde = proc.communicate()
6766
found = False
6867
for line in stdo.split("\n"):
69-
tmp = re.split(r'\s+', line)
70-
if tmp[0] == tag and tmp[1] == rev and tmp[2] == rev_value:
71-
found = True
68+
line = line.strip()
69+
if line:
70+
try:
71+
img = json.loads(line)
72+
if img.get("Repository") == repository and img.get("Tag") == tag and img.get("ID") == id:
73+
found = True
74+
break
75+
except (json.JSONDecodeError, ValueError):
76+
pass
7277
if not found:
7378
print("Loading image")
7479
cmd = "cat ${cached_image_file} | ${load_cmd}"
@@ -502,7 +507,7 @@ def containerize_command(self, command: str) -> str:
502507
{run_command}"""
503508

504509
def __cache_from_file_command(self, cached_image_file: str, docker_host_props: Dict[str, Any]) -> str:
505-
images_cmd = docker_util.build_docker_images_command(truncate=False, **docker_host_props)
510+
images_cmd = docker_util.build_docker_images_command(truncate=False, format="json", **docker_host_props)
506511
load_cmd = docker_util.build_docker_load_command(**docker_host_props)
507512

508513
return string.Template(LOAD_CACHED_IMAGE_COMMAND_TEMPLATE).safe_substitute(

lib/galaxy/tool_util/deps/container_resolvers/mulled.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""This module describes the :class:`MulledContainerResolver` ContainerResolver plugin."""
22

3+
import json
34
import logging
45
import os
56
import subprocess
@@ -188,13 +189,27 @@ def list_docker_cached_mulled_images(
188189
if resolution_cache is not None and cache_key in resolution_cache:
189190
images_and_versions = resolution_cache.get(cache_key)
190191
else:
191-
command = build_docker_images_command(truncate=True, sudo=False, to_str=False)
192+
command = build_docker_images_command(truncate=True, format="json", sudo=False, to_str=False)
192193
try:
193-
images_and_versions = unicodify(subprocess.check_output(command)).strip().splitlines()
194+
output = unicodify(subprocess.check_output(command)).strip()
194195
except subprocess.CalledProcessError:
195196
log.info("Call to `docker images` failed, configured container resolution may be broken")
196197
return []
197-
images_and_versions = [":".join(line.split()[0:2]) for line in images_and_versions[1:]]
198+
199+
# Parse JSON output from docker images
200+
images_and_versions = []
201+
for line in output.splitlines():
202+
if line.strip():
203+
try:
204+
image_info = json.loads(line)
205+
repository = image_info.get("Repository", "")
206+
tag = image_info.get("Tag", "")
207+
if repository and tag:
208+
images_and_versions.append(f"{repository}:{tag}")
209+
except json.JSONDecodeError:
210+
log.warning(f"Failed to parse docker image JSON: {line}")
211+
continue
212+
198213
if resolution_cache is not None:
199214
resolution_cache[cache_key] = images_and_versions
200215

lib/galaxy/tool_util/deps/docker_util.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ def build_docker_cache_command(image: str, **kwds) -> str:
6363
return cache_command
6464

6565

66-
def build_docker_images_command(truncate=True, **kwds) -> Union[str, List[str]]:
67-
args = ["--no-trunc"] if not truncate else []
66+
def build_docker_images_command(truncate=True, format: Optional[str] = None, **kwds) -> Union[str, List[str]]:
67+
args = []
68+
if not truncate:
69+
args.append("--no-trunc")
70+
if format:
71+
args.extend(["--format", format])
6872
return command_shell("images", args, **kwds)
6973

7074

0 commit comments

Comments
 (0)