Skip to content

post_gen_project

Post Cookie Hook: Templated File with jinja2 syntax

Cookiecutter post generation hook script that handles operations after the template project is used to generate a target project.

TO_DELETE_TEXT: str = 'TO_DELETE' module-attribute

Signature text to identify folders that must be deleted after generation.

get_context()

Get the Context, that was used by the Templating Engine at render time

Source code in src/cookiecutter_python/hooks/post_gen_project.py
42
43
44
45
46
47
def get_context() -> OrderedDict:
    """Get the Context, that was used by the Templating Engine at render time"""
    # variable with an object of the same type that will be set in the next line
    COOKIECUTTER: OrderedDict = OrderedDict()
    COOKIECUTTER = {{cookiecutter}}  # type: ignore    # pylint: disable=undefined-variable  # noqa: F821
    return COOKIECUTTER

git_commit(request)

Commit the staged changes in the generated project.

Source code in src/cookiecutter_python/hooks/post_gen_project.py
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
def git_commit(request):
    """Commit the staged changes in the generated project."""
    cookiecutter_config_str = (
        '\n'.join((f"  {key}: {val}" for key, val in request.vars.items())) + '\n'
    )
    commit_message = (
        "Template applied from"
        " https://github.com/boromir674/cookiecutter-python-"
        "package\n\n"
        "Template configuration:\n"
        f"{cookiecutter_config_str}"
    )

    request.repo.index.add(
        list(iter((path.relpath(x, start=request.project_dir) for x in iter_files(request))))
    )
    author = Actor(request.vars['author'], request.vars['author_email'])

    request.repo.index.commit(commit_message, author=author, committer=copy(author))

main()

Delete irrelevant to Project Type files and optionally do git commit.

Source code in src/cookiecutter_python/hooks/post_gen_project.py
340
341
342
def main():
    """Delete irrelevant to Project Type files and optionally do git commit."""
    sys.exit(post_hook())

post_file_removal(request)

Preserve only files relevant to Project Type requested to Generate.

Source code in src/cookiecutter_python/hooks/post_gen_project.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def post_file_removal(request):
    """Preserve only files relevant to Project Type requested to Generate."""
    # Remove files that are not relevant to the selected project type {module, cli+comule, pytest-plugin}
    files_to_remove = [
        os.path.join(request.project_dir, *x)
        for x in delete_files[request.vars['project_type']](request)
    ]
    _delete_files(files_to_remove)
    # Remove files that are not relevant to the selected CI/CD Design option
    irrelevant_ci_cd_files = [
        os.path.join(request.project_dir, *path_components)
        for path_components in CICD_DELETE[request.vars['cicd']]
    ]
    _delete_files(irrelevant_ci_cd_files)

    # Remove generated docs folders, but the input builder (ie mkdocs/sphinx) selected
    # remove Grafana stuff if opted-out of Observability
    _remove_irrelevant_rendered_folders(request.project_dir)
    # Remove some top-level files depending on input (ie mkdocs.yml)
    _remove_irrelevant_top_level_files(request)

post_hook()

Delete irrelevant to Project Type files and optionally do git commit.

Source code in src/cookiecutter_python/hooks/post_gen_project.py
261
262
263
264
265
266
267
268
269
270
271
272
273
def post_hook():
    """Delete irrelevant to Project Type files and optionally do git commit."""
    request = get_request()

    # Step 1: Remove irrelevant files
    post_file_removal(request)

    # Step 2: Handle accidental log files
    _handle_logs(request)

    # Step 3: Initialize Git repository and commit changes
    _initialize_and_commit_git_repo(request)
    return 0