Skip to content

Commit

Permalink
fix: Make sure to delete local temporary clones
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Sep 24, 2022
1 parent bd766ba commit ce65dc7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
24 changes: 19 additions & 5 deletions copier/main.py
Expand Up @@ -28,7 +28,7 @@
from .errors import CopierAnswersInterrupt, ExtensionNotFoundError, UserMessageError
from .subproject import Subproject
from .template import Task, Template
from .tools import Style, TemporaryDirectory, printf
from .tools import Style, TemporaryDirectory, printf, handle_remove_readonly
from .types import (
AnyByStrDict,
JSONSerializable,
Expand Down Expand Up @@ -147,6 +147,20 @@ class Worker:
pretend: bool = False
quiet: bool = False

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
self._cleanup()

def _cleanup(self) -> None:
if self.template.is_temp_clone:
rmtree(
self.template.local_abspath,
ignore_errors=False,
onerror=handle_remove_readonly,
)

def _answers_to_remember(self) -> Mapping:
"""Get only answers that will be remembered in the copier answers file."""
# All internal values must appear first
Expand Down Expand Up @@ -754,8 +768,8 @@ def run_copy(
"""
if data is not None:
kwargs["data"] = data
worker = Worker(src_path=src_path, dst_path=Path(dst_path), **kwargs)
worker.run_copy()
with Worker(src_path=src_path, dst_path=Path(dst_path), **kwargs) as worker:
worker.run_copy()
return worker


Expand All @@ -772,8 +786,8 @@ def run_update(
"""
if data is not None:
kwargs["data"] = data
worker = Worker(dst_path=Path(dst_path), **kwargs)
worker.run_update()
with Worker(dst_path=Path(dst_path), **kwargs) as worker:
worker.run_update()
return worker


Expand Down
4 changes: 4 additions & 0 deletions copier/template.py
Expand Up @@ -461,6 +461,10 @@ def local_abspath(self) -> Path:
raise ValueError("Local template must be a directory.")
return result.absolute()

@cached_property
def is_temp_clone(self) -> bool:
return self.local_abspath != Path(self.url).absolute()

@cached_property
def url_expanded(self) -> str:
"""Get usable URL.
Expand Down

0 comments on commit ce65dc7

Please sign in to comment.