# Path: shipwright/_lib/msg.py
# class BuildComplete(Message, namedtuple('BuildComplete', ['target'])):
#     """
#     A message to signify that the build has finished for a target.
# 
#     The build may not have completed succesfully
#     """


from . import docker
from .msg import BuildComplete


def do_build(client, build_ref, targets, cache):
    """
    Generic function for building multiple images while
    notifying a callback function with output produced.

    Given a list of targets it builds the target with the given
    build_func while streaming the output through the given
    show_func.

    Returns an iterator of (image, docker_image_id) pairs as
    the final output.

    Building an image can take sometime so  the results are returned as
    an iterator in case the caller wants to use restults in between builds.

    The consequences of this is you must either call it as part of a for loop
    or pass it to a function like list() which can consume an iterator.

    """

    build_index = {t.image.name: t.ref for t in targets}

    for target in targets:
        parent_ref = None
        if target.parent:
            parent_ref = build_index.get(target.parent)
        for evt in build(client, parent_ref, target, cache):
            yield evt

# Path: shipwright/_lib/msg.py
# class BuildComplete(Message, namedtuple('BuildComplete', ['target'])):
#     """
#     A message to signify that the build has finished for a target.
# 
#     The build may not have completed succesfully
#     """


# Path: shipwright/_lib/build.py
from . import docker
from .msg import BuildComplete



def do_build(client, build_ref, targets, cache):
    """
    Generic function for building multiple images while
    notifying a callback function with output produced.

    Given a list of targets it builds the target with the given
    build_func while streaming the output through the given
    show_func.

    Returns an iterator of (image, docker_image_id) pairs as
    the final output.

    Building an image can take sometime so  the results are returned as
    an iterator in case the caller wants to use restults in between builds.

    The consequences of this is you must either call it as part of a for loop
    or pass it to a function like list() which can consume an iterator.

    """

    build_index = {t.image.name: t.ref for t in targets}

    for target in targets:
        parent_ref = None
        if target.parent:
            parent_ref = build_index.get(target.parent)
        for evt in build(client, parent_ref, target, cache):
            yield evt

        yield BuildComplete(target)