import yaml
import re


def replace_override_vars(full_text, env, override_vars):
    """
    Given the full text of a yaml spec, return the full
    text with user variable overrides in the 'env' block.

    The env yaml block looks like:

        env:
            variables:
                .......
                .......

    The regex will find and match to the above yaml block.
    """
    updated_env = dict(env)
    if override_vars is not None:
        for key, val in env.items():
            updated_env[key].update(override_vars)
    updated_env = {"env": updated_env}
    dump = yaml.dump(updated_env, default_flow_style=False, sort_keys=False)
    updated_env_text = f"\n{dump}\n"

    env_block_pattern = r"\nenv\s*:\s*(\n+( |\t)+.*)+\n*"
    regex = re.compile(env_block_pattern)
    updated_full_text = re.sub(regex, updated_env_text, full_text)
    return updated_full_text