<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def generate_build(self, image, targetname, rebuilds=None, cache_repo='', cache_tag='', buildargs=None, **kwargs):
 """ Separate the build into a series of one or more intermediate steps. Each specified build directory gets its own step Args: image (str):
 name of the image as defined in the dockermake.py file targetname (str):
 name to tag the final built image with rebuilds (List[str]):
 list of image layers to rebuild (i.e., without docker's cache) cache_repo (str):
 repository to get images for caches in builds cache_tag (str):
 tags to use from repository for caches in builds buildargs (dict):
 build-time dockerfile arugments **kwargs (dict):
 extra keyword arguments for the BuildTarget object """

        from_image = self.get_external_base_image(image)
        if cache_repo or cache_tag:
            cache_from = utils.generate_name(image, cache_repo, cache_tag)
        else:
            cache_from = None
        if from_image is None:
            raise errors.NoBaseError("No base image found in %s's dependencies" % image)
        if isinstance(from_image, ExternalDockerfile):
            build_first = from_image
            base_image = from_image.tag
        else:
            base_image = from_image
            build_first = None
        build_steps = []
        istep = 0
        sourceimages = set()
        if rebuilds is None:
            rebuilds = []
        else:
            rebuilds = set(rebuilds)

        for base_name in self.sort_dependencies(image):
            istep += 1
            buildname = 'dmkbuild_%s_%d' % (image, istep)
            secret_files = self.ymldefs[base_name].get('secret_files', None)
            squash = self.ymldefs[base_name].get('squash', bool(secret_files))
            build_steps.append(
                    dockermake.step.BuildStep(
                            base_name,
                            base_image,
                            self.ymldefs[base_name],
                            buildname,
                            bust_cache=base_name in rebuilds,
                            build_first=build_first, cache_from=cache_from,
                            buildargs=buildargs,
                            squash=squash,
                            secret_files=secret_files))

            base_image = buildname
            build_first = None

            for sourceimage, files in iteritems(self.ymldefs[base_name].get('copy_from', {})):
                sourceimages.add(sourceimage)
                for sourcepath, destpath in iteritems(files):
                    istep += 1
                    buildname = 'dmkbuild_%s_%d' % (image, istep)
                    build_steps.append(
                            dockermake.step.FileCopyStep(
                                    sourceimage, sourcepath, destpath,
                                    base_name, base_image, self.ymldefs[base_name],
                                    buildname, bust_cache=base_name in rebuilds,
                                    build_first=build_first, cache_from=cache_from))
                    base_image = buildname

        sourcebuilds = [self.generate_build(img,
                                            img,
                                            cache_repo=cache_repo,
                                            cache_tag=cache_tag,
                                            **kwargs)
                        for img in sourceimages]

        return builds.BuildTarget(imagename=image,
                                  targetname=targetname,
                                  steps=build_steps,
                                  sourcebuilds=sourcebuilds,
                                  from_image=from_image,
                                  **kwargs)