Skip to content

temp_template_repo

This module provides the TempTemplateRepo context manager, which can be used to clone a Git repository to a temporary directory and checkout a specific tag.

TempTemplateRepo

A context manager that clones a Git repository to a temporary directory and checks out a specific tag.

Source code in ssb_project_cli/ssb_project/build/temp_template_repo.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class TempTemplateRepo:
    """A context manager that clones a Git repository to a temporary directory and checks out a specific tag."""

    def __init__(self, template_repo_url: str, checkout: str | None) -> None:
        """Initializes a new TemplateRepo object with the specified template_repo_url and checkout attributes."""
        self.template_repo_url = template_repo_url
        self.checkout = checkout

    def __enter__(self) -> "TempTemplateRepo":
        """Clones the template repository specified by template_repo_url to a temporary directory and checks out the tag specified by checkout."""
        self.temp_dir = TemporaryDirectory()

        # clone the repository
        self.repo = Repo.clone_from(self.template_repo_url, self.temp_dir.name)

        if self.checkout:
            # checkout the specific tag you're interested in
            self.repo.git.checkout(self.checkout)

        self.subdir = "{{cookiecutter.project_name}}"

        return self

    def __exit__(
        self,
        exc_type: Optional[Type[BaseException]],
        exc_val: Optional[BaseException],
        exc_tb: Optional[TracebackType],
    ) -> None:
        """Cleans up the temporary directory created containing the template repository."""
        self.temp_dir.cleanup()

__enter__()

Clones the template repository specified by template_repo_url to a temporary directory and checks out the tag specified by checkout.

Source code in ssb_project_cli/ssb_project/build/temp_template_repo.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def __enter__(self) -> "TempTemplateRepo":
    """Clones the template repository specified by template_repo_url to a temporary directory and checks out the tag specified by checkout."""
    self.temp_dir = TemporaryDirectory()

    # clone the repository
    self.repo = Repo.clone_from(self.template_repo_url, self.temp_dir.name)

    if self.checkout:
        # checkout the specific tag you're interested in
        self.repo.git.checkout(self.checkout)

    self.subdir = "{{cookiecutter.project_name}}"

    return self

__exit__(exc_type, exc_val, exc_tb)

Cleans up the temporary directory created containing the template repository.

Source code in ssb_project_cli/ssb_project/build/temp_template_repo.py
34
35
36
37
38
39
40
41
def __exit__(
    self,
    exc_type: Optional[Type[BaseException]],
    exc_val: Optional[BaseException],
    exc_tb: Optional[TracebackType],
) -> None:
    """Cleans up the temporary directory created containing the template repository."""
    self.temp_dir.cleanup()

__init__(template_repo_url, checkout)

Initializes a new TemplateRepo object with the specified template_repo_url and checkout attributes.

Source code in ssb_project_cli/ssb_project/build/temp_template_repo.py
14
15
16
17
def __init__(self, template_repo_url: str, checkout: str | None) -> None:
    """Initializes a new TemplateRepo object with the specified template_repo_url and checkout attributes."""
    self.template_repo_url = template_repo_url
    self.checkout = checkout