# Path: harpoon/executor.py
# def docker_context():
#     """Make a docker context"""
#     try:
#         client = docker.from_env(
#             version="auto",
#             timeout=int(os.environ.get("DOCKER_CLIENT_TIMEOUT", 180)),
#             assert_hostname=False,
#         )
# 
#         info = client.info()
#         log.info(
#             "Connected to docker daemon\tdriver=%s\tkernel=%s",
#             info["Driver"],
#             info["KernelVersion"],
#         )
#     except (DockerException, APIError) as error:
#         raise BadDockerConnection(error=error)
# 
#     return client
# 
# Path: harpoon/ship/builder.py
# class Builder(BuilderBase):
#     """Build an image from Image configuration"""
# 
#     def make_image(
#         self,
#         conf,
#         images,
#         chain=None,
#         parent_chain=None,
#         made=None,
#         ignore_deps=False,
#         ignore_parent=False,
#         pushing=False,
#     ):
#         """Make us an image"""
#         made = {} if made is None else made
#         chain = [] if chain is None else chain
#         parent_chain = [] if parent_chain is None else parent_chain
# 
#         if conf.name in made:
#             return
# 
#         if conf.name in chain and not ignore_deps:
#             raise BadCommand("Recursive dependency images", chain=chain + [conf.name])
# 
#         if conf.name in parent_chain and not ignore_parent:
#             raise BadCommand("Recursive FROM statements", chain=parent_chain + [conf.name])
# 
#         if conf.name not in images:
#             raise NoSuchImage(looking_for=conf.name, available=images.keys())
# 
#         if not ignore_deps:
#             for dependency, image in conf.dependency_images():
#                 self.make_image(
#                     images[dependency],
#                     images,
#                     chain=chain + [conf.name],
#                     made=made,
#                     pushing=pushing,
#                 )
# 
#         if not ignore_parent:
#             for dep in conf.commands.dependent_images:
#                 if not isinstance(dep, str):
#                     self.make_image(
#                         dep, images, chain, parent_chain + [conf.name], made=made, pushing=pushing
#                     )
# 
#         # Should have all our dependencies now
#         log.info("Making image for '%s' (%s)", conf.name, conf.image_name)
#         cached = self.build_image(conf, pushing=pushing)
#         made[conf.name] = True
#         return cached
# 
#     def build_image(self, conf, pushing=False):
#         """Build this image"""
#         with conf.make_context() as context:
#             try:
#                 stream = BuildProgressStream(conf.harpoon.silent_build)
#                 with self.remove_replaced_images(conf) as info:
#                     cached = NormalBuilder().build(conf, context, stream)
#                     info["cached"] = cached
#             except (KeyboardInterrupt, Exception) as error:
#                 exc_info = sys.exc_info()
#                 if stream.current_container:
#                     Runner().stage_build_intervention(conf, stream.current_container)
# 
#                 if isinstance(error, KeyboardInterrupt):
#                     raise UserQuit()
#                 else:
#                     exc_info[1].__traceback__ = exc_info[2]
#                     raise exc_info[1]
#             finally:
#                 if stream and stream.intermediate_images and conf.cleanup_intermediate_images:
#                     for image in stream.intermediate_images:
#                         log.info("Deleting intermediate image\timage=%s", image)
#                         try:
#                             conf.harpoon.docker_api.remove_image(image)
#                         except Exception as error:
#                             log.error(
#                                 "Failed to remove intermediate image\timage=%s\terror=%s",
#                                 image,
#                                 error,
#                             )
# 
#         return cached
# 
#     def layered(self, images, only_pushable=False):
#         """Yield layers of images"""
#         if only_pushable:
#             operate_on = dict(
#                 (image, instance) for image, instance in images.items() if instance.image_index
#             )
#         else:
#             operate_on = images
# 
#         layers = Layers(operate_on, all_deps=images)
#         layers.add_all_to_layers()
#         for layer in layers.layered:
#             buf = []
#             for image_name, image in layer:
#                 if image.image_index:
#                     buf.append((image_name, image))
#             if buf:
#                 yield buf


from harpoon.executor import docker_context
from harpoon.ship.builder import Builder
from contextlib import contextmanager
import docker.errors
import uuid
        if "docker_client" not in info:
            info["docker_client"] = docker_context()
        return info["docker_client"]

    @property
    def docker_api(self):
        return self.docker_client.api

    def refresh_docker_client(self):
        if "docker_client" in info:
            del info["docker_client"]

    def new_docker_client(self):
        self.refresh_docker_client()
        return self.docker_client

    @contextmanager
    def a_built_image(self, options, harpoon_options=None, images=None, **kwargs):
        ident = str(uuid.uuid1())
        ident_tag = "{0}:latest".format(ident)

        conf = self.make_image(options, harpoon_options, **kwargs)
        if images:
            images[conf.image_name] = conf
        conf.image_name = ident

        if images:
            images[conf.image_name] = conf
            cached = Builder().make_image(conf, images)
        else:

# Path: harpoon/executor.py
# def docker_context():
#     """Make a docker context"""
#     try:
#         client = docker.from_env(
#             version="auto",
#             timeout=int(os.environ.get("DOCKER_CLIENT_TIMEOUT", 180)),
#             assert_hostname=False,
#         )
# 
#         info = client.info()
#         log.info(
#             "Connected to docker daemon\tdriver=%s\tkernel=%s",
#             info["Driver"],
#             info["KernelVersion"],
#         )
#     except (DockerException, APIError) as error:
#         raise BadDockerConnection(error=error)
# 
#     return client
# 
# Path: harpoon/ship/builder.py
# class Builder(BuilderBase):
#     """Build an image from Image configuration"""
# 
#     def make_image(
#         self,
#         conf,
#         images,
#         chain=None,
#         parent_chain=None,
#         made=None,
#         ignore_deps=False,
#         ignore_parent=False,
#         pushing=False,
#     ):
#         """Make us an image"""
#         made = {} if made is None else made
#         chain = [] if chain is None else chain
#         parent_chain = [] if parent_chain is None else parent_chain
# 
#         if conf.name in made:
#             return
# 
#         if conf.name in chain and not ignore_deps:
#             raise BadCommand("Recursive dependency images", chain=chain + [conf.name])
# 
#         if conf.name in parent_chain and not ignore_parent:
#             raise BadCommand("Recursive FROM statements", chain=parent_chain + [conf.name])
# 
#         if conf.name not in images:
#             raise NoSuchImage(looking_for=conf.name, available=images.keys())
# 
#         if not ignore_deps:
#             for dependency, image in conf.dependency_images():
#                 self.make_image(
#                     images[dependency],
#                     images,
#                     chain=chain + [conf.name],
#                     made=made,
#                     pushing=pushing,
#                 )
# 
#         if not ignore_parent:
#             for dep in conf.commands.dependent_images:
#                 if not isinstance(dep, str):
#                     self.make_image(
#                         dep, images, chain, parent_chain + [conf.name], made=made, pushing=pushing
#                     )
# 
#         # Should have all our dependencies now
#         log.info("Making image for '%s' (%s)", conf.name, conf.image_name)
#         cached = self.build_image(conf, pushing=pushing)
#         made[conf.name] = True
#         return cached
# 
#     def build_image(self, conf, pushing=False):
#         """Build this image"""
#         with conf.make_context() as context:
#             try:
#                 stream = BuildProgressStream(conf.harpoon.silent_build)
#                 with self.remove_replaced_images(conf) as info:
#                     cached = NormalBuilder().build(conf, context, stream)
#                     info["cached"] = cached
#             except (KeyboardInterrupt, Exception) as error:
#                 exc_info = sys.exc_info()
#                 if stream.current_container:
#                     Runner().stage_build_intervention(conf, stream.current_container)
# 
#                 if isinstance(error, KeyboardInterrupt):
#                     raise UserQuit()
#                 else:
#                     exc_info[1].__traceback__ = exc_info[2]
#                     raise exc_info[1]
#             finally:
#                 if stream and stream.intermediate_images and conf.cleanup_intermediate_images:
#                     for image in stream.intermediate_images:
#                         log.info("Deleting intermediate image\timage=%s", image)
#                         try:
#                             conf.harpoon.docker_api.remove_image(image)
#                         except Exception as error:
#                             log.error(
#                                 "Failed to remove intermediate image\timage=%s\terror=%s",
#                                 image,
#                                 error,
#                             )
# 
#         return cached
# 
#     def layered(self, images, only_pushable=False):
#         """Yield layers of images"""
#         if only_pushable:
#             operate_on = dict(
#                 (image, instance) for image, instance in images.items() if instance.image_index
#             )
#         else:
#             operate_on = images
# 
#         layers = Layers(operate_on, all_deps=images)
#         layers.add_all_to_layers()
#         for layer in layers.layered:
#             buf = []
#             for image_name, image in layer:
#                 if image.image_index:
#                     buf.append((image_name, image))
#             if buf:
#                 yield buf


# Path: tests/helpers/mixins/dockers.py
from harpoon.executor import docker_context
from harpoon.ship.builder import Builder
from contextlib import contextmanager
import docker.errors
import uuid

        if "docker_client" not in info:
            info["docker_client"] = docker_context()
        return info["docker_client"]

    @property
    def docker_api(self):
        return self.docker_client.api

    def refresh_docker_client(self):
        if "docker_client" in info:
            del info["docker_client"]

    def new_docker_client(self):
        self.refresh_docker_client()
        return self.docker_client

    @contextmanager
    def a_built_image(self, options, harpoon_options=None, images=None, **kwargs):
        ident = str(uuid.uuid1())
        ident_tag = "{0}:latest".format(ident)

        conf = self.make_image(options, harpoon_options, **kwargs)
        if images:
            images[conf.image_name] = conf
        conf.image_name = ident

        if images:
            images[conf.image_name] = conf
            cached = Builder().make_image(conf, images)
        else:

            cached = Builder().build_image(conf)