def _apply_template(template, target, *, checkout, extra_context):
with tempfile.TemporaryDirectory() as tempdir:<EOL><INDENT>repo_dir = cc_main.cookiecutter(<EOL>template,<EOL>checkout=checkout,<EOL>no_input=True,<EOL>output_dir=tempdir,<EOL>extra_context=extra_context)<EOL>for item in os.listdir(repo_dir):<EOL><INDENT>src = os.path.join(repo_dir, item)<EOL>dst = os.path.join(target, item)<EOL>if os.path.isdir(src):<EOL><INDENT>if os.path.exists(dst):<EOL><INDENT>shutil.rmtree(dst)<EOL><DEDENT>shutil.copytree(src, dst)<EOL><DEDENT>else:<EOL><INDENT>if os.path.exists(dst):<EOL><INDENT>os.remove(dst)<EOL><DEDENT>shutil.copy2(src, dst)<EOL><DEDENT><DEDENT><DEDENT>
Apply a template to a temporary directory and then copy results to target.