def BuildSelection(buildSelection, selectionToBuild):
    cwd = BuildTools.TryChangeToDirectoryAndGetCwd(buildSelection)
    oldEnvironmentVariable = BuildTools.AddEnvironmentVariablesFromSelection(buildSelection)
    BuildTools.HandleTerminalCommandsSelection(buildSelection)
    TerminalTools.LoadDefaultEnvironmentVariablesFile()
    if (BuildTools.FILES_KEY in buildSelection):
        composeFiles = buildSelection[BuildTools.FILES_KEY]
        buildComposeFile = BuildTools.GetAvailableComposeFilename('build', selectionToBuild)
        DockerComposeTools.MergeComposeFiles(composeFiles, buildComposeFile)
        try:
            DockerComposeTools.DockerComposeBuild([buildComposeFile])
        except:
            BuildTools.RemoveComposeFileIfNotPreserved(buildComposeFile, buildSelection)
            raise
        if (BuildTools.ADDITIONAL_TAG_KEY in buildSelection):
            DockerComposeTools.TagImages(buildComposeFile, buildSelection[BuildTools.ADDITIONAL_TAG_KEY])
        if (BuildTools.ADDITIONAL_TAGS_KEY in buildSelection):
            for tag in buildSelection[BuildTools.ADDITIONAL_TAGS_KEY]:
                DockerComposeTools.TagImages(buildComposeFile, tag)
        if (SAVE_IMAGES_KEY in buildSelection):
            outputFolder = buildSelection[SAVE_IMAGES_KEY]
            DockerComposeTools.SaveImages(buildComposeFile, outputFolder)
        if (BuildTools.COMPOSE_FILE_WITH_DIGESTS_KEY in buildSelection):
            composeFileWithDigests = buildSelection[BuildTools.COMPOSE_FILE_WITH_DIGESTS_KEY]
            BuildTools.GenerateComposeFileWithDigests(composeFiles, composeFileWithDigests)
        BuildTools.RemoveComposeFileIfNotPreserved(buildComposeFile, buildSelection)
    BuildTools.RemoveEnvironmentVariables(oldEnvironmentVariable)
    os.chdir(cwd)
def BuildSelection(buildSelection, selectionToBuild):
    cwd = BuildTools.TryChangeToDirectoryAndGetCwd(buildSelection)
    oldEnvironmentVariable = BuildTools.AddEnvironmentVariablesFromSelection(buildSelection)
    BuildTools.HandleTerminalCommandsSelection(buildSelection)
    TerminalTools.LoadDefaultEnvironmentVariablesFile()
    if (BuildTools.FILES_KEY in buildSelection):
        composeFiles = buildSelection[BuildTools.FILES_KEY]
        buildComposeFile = BuildTools.GetAvailableComposeFilename('build', selectionToBuild)
        DockerComposeTools.MergeComposeFiles(composeFiles, buildComposeFile)
        try:
            DockerComposeTools.DockerComposeBuild([buildComposeFile])
        except:
            BuildTools.RemoveComposeFileIfNotPreserved(buildComposeFile, buildSelection)
            raise
        if (BuildTools.ADDITIONAL_TAG_KEY in buildSelection):
            DockerComposeTools.TagImages(buildComposeFile, buildSelection[BuildTools.ADDITIONAL_TAG_KEY])
        if (BuildTools.ADDITIONAL_TAGS_KEY in buildSelection):
            for tag in buildSelection[BuildTools.ADDITIONAL_TAGS_KEY]:
                DockerComposeTools.TagImages(buildComposeFile, tag)
        if (SAVE_IMAGES_KEY in buildSelection):
            outputFolder = buildSelection[SAVE_IMAGES_KEY]
            DockerComposeTools.SaveImages(buildComposeFile, outputFolder)
        if (BuildTools.COMPOSE_FILE_WITH_DIGESTS_KEY in buildSelection):
            composeFileWithDigests = buildSelection[BuildTools.COMPOSE_FILE_WITH_DIGESTS_KEY]
            BuildTools.GenerateComposeFileWithDigests(composeFiles, composeFileWithDigests)
        BuildTools.RemoveComposeFileIfNotPreserved(buildComposeFile, buildSelection)
    BuildTools.RemoveEnvironmentVariables(oldEnvironmentVariable)
    os.chdir(cwd)