diff --git a/biobox_cli/biobox_type/short_read_assembler.py b/biobox_cli/biobox_type/short_read_assembler.py index 0bac5f9..0b88ee3 100644 --- a/biobox_cli/biobox_type/short_read_assembler.py +++ b/biobox_cli/biobox_type/short_read_assembler.py @@ -31,8 +31,7 @@ def run(argv): contig_file = opts['--output'] task = opts['--task'] - if not ctn.image_available(image): - util.err_exit('unknown_image', {'image': image}) + ctn.exit_if_no_image_available(image) cntr_fastq_file = "/fastq/input.fq.gz" fastq_values = [(cntr_fastq_file, "paired")] diff --git a/biobox_cli/command/verify.py b/biobox_cli/command/verify.py index 88e39b2..a67a418 100644 --- a/biobox_cli/command/verify.py +++ b/biobox_cli/command/verify.py @@ -23,8 +23,7 @@ def run(argv): image = opts[''] task = opts['--task'] - if not ctn.image_available(image): - util.err_exit('unknown_image', {'image': image}) + ctn.exit_if_no_image_available(image) results = behave.run(biobox, image, task) diff --git a/biobox_cli/container.py b/biobox_cli/container.py index 40b82ff..95a9422 100644 --- a/biobox_cli/container.py +++ b/biobox_cli/container.py @@ -1,10 +1,11 @@ -import logging -import os +import logging, os logging.getLogger("requests").setLevel(logging.WARNING) import docker import docker.utils +import biobox_cli.util as util + def client(): client = docker.Client(**docker.utils.kwargs_from_env(assert_hostname = False)) return client @@ -13,19 +14,22 @@ def get_image_tags(docker_dict): return reduce(lambda acc, x: acc + [x, x.split(":")[0]], docker_dict['RepoTags'], []) -def image_available_locally(image): +def is_image_available_locally(image): image_tags = map(get_image_tags, client().images()) images = set(reduce(lambda acc, x: acc + x, image_tags, [])) return image in images -def image_available(image): - from docker.errors import APIError - if not image_available_locally(image): +def is_image_available(image): + if not is_image_available_locally(image): output = client().pull(image) if "error" in output: return False return True +def exit_if_no_image_available(image): + if not is_image_available(image): + util.err_exit('unknown_image', {'image': image}) + def mount_string(host_dir, container_dir, read_only = True): access = "ro" if read_only else "rw" return ":".join([os.path.abspath(host_dir), container_dir, access]) diff --git a/test/test_container.py b/test/test_container.py index e254815..28e54a0 100644 --- a/test/test_container.py +++ b/test/test_container.py @@ -7,19 +7,19 @@ def test_get_client(): ctn.client() def test_checking_a_local_available_image(): - nose.assert_equal(ctn.image_available_locally("python"), True) + nose.assert_equal(ctn.is_image_available_locally("python"), True) def test_checking_an_local_available_image_with_tag(): - nose.assert_equal(ctn.image_available_locally("python:2.7"), True) + nose.assert_equal(ctn.is_image_available_locally("python:2.7"), True) def test_checking_a_locally_non_existent_image(): - nose.assert_equal(ctn.image_available_locally("biobox/unknown"), False) + nose.assert_equal(ctn.is_image_available_locally("biobox/unknown"), False) def test_checking_an_available_image(): - nose.assert_equal(ctn.image_available("python"), True) + nose.assert_equal(ctn.is_image_available("python"), True) def test_checking_an_available_image_with_tag(): - nose.assert_equal(ctn.image_available("python:2.7"), True) + nose.assert_equal(ctn.is_image_available("python:2.7"), True) def test_create_mount_string_with_relative_dir(): expected = "{}/tmp:/tmp:ro".format(hlpr.project_root())