def generate_script()

in scripts/local_cloudbuild.py [0:0]


def generate_script(cloudbuild):
    """Generate the contents of a shell script

    Args:
        cloudbuild (CloudBuild): Valid cloudbuild configuration

    Returns:
        (str): Contents of shell script
    """
    # This deletes everything in /workspace including hidden files,
    # but not /workspace itself
    cleanup_step = Step(
        args=['rm', '-rf', '/workspace'],
        dir_='',
        env=[],
        name=DEBIAN_IMAGE,
    )
    cleanup_command = generate_command(cleanup_step, {}, set())
    subs_used = set()
    docker_commands = [
        generate_command(step, cloudbuild.substitutions, subs_used)
        for step in cloudbuild.steps]

    # Check that all user variables were referenced at least once
    user_subs_unused = [name for name in cloudbuild.substitutions.keys()
                        if name not in subs_used and name[0] == '_']
    if user_subs_unused:
        nice_list = '"' + '", "'.join(sorted(user_subs_unused)) + '"'
        raise ValueError(
            'User substitution variables {} were defined in the '
            '--substitution flag but never used in the cloudbuild file.'.
            format(nice_list))

    cleanup_str = ' '.join(cleanup_command)
    docker_lines = []
    for docker_command in docker_commands:
        line = ' '.join(docker_command) + '\n\n'
        docker_lines.append(line)
    docker_str = ''.join(docker_lines)

    s = BUILD_SCRIPT_TEMPLATE.format(cleanup_str=cleanup_str,
                                     docker_str=docker_str)
    return s