Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make sure to delete local temporary clones #802

Merged
merged 4 commits into from Sep 27, 2022

Conversation

pawamoy
Copy link
Contributor

@pawamoy pawamoy commented Sep 14, 2022

Closes #709

I implemented that on GitPod.io but have a lot of test failures due to pexpect timeouts, and pkg_resources module not being found (it seems the env on gitpod is kinda broken). Opening to see how CI behaves.

@codecov-commenter
Copy link

codecov-commenter commented Sep 14, 2022

Codecov Report

Merging #802 (629a93d) into master (bd766ba) will decrease coverage by 0.20%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #802      +/-   ##
==========================================
- Coverage   96.68%   96.48%   -0.21%     
==========================================
  Files          41       41              
  Lines        2959     2986      +27     
==========================================
+ Hits         2861     2881      +20     
- Misses         98      105       +7     
Flag Coverage Δ
unittests 96.48% <100.00%> (-0.21%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
copier/main.py 94.54% <100.00%> (+0.09%) ⬆️
copier/template.py 98.14% <100.00%> (+0.08%) ⬆️
tests/test_vcs.py 100.00% <100.00%> (ø)
tests/test_tools.py 68.00% <0.00%> (-24.00%) ⬇️
copier/cli.py 96.47% <0.00%> (-1.18%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@pawamoy
Copy link
Contributor Author

pawamoy commented Sep 14, 2022

OK, I was able to fix the env on gitpod by upgrading pip and setuptools in both the pyenv version and the poetry venv.

@pawamoy
Copy link
Contributor Author

pawamoy commented Sep 14, 2022

Network failure in job ubuntu 3.8, re-running it should fix it.
I'll add a test as soon as possible and then mark the PR as ready.

@yajo
Copy link
Member

yajo commented Sep 15, 2022

Don't you think we need to fix this too?

location = tempfile.mkdtemp(prefix=f"{__name__}.clone.")

@pawamoy
Copy link
Contributor Author

pawamoy commented Sep 15, 2022

Oh, that's exactly what gets deleted by the added code. Sorry for not making it clear. I'll update the PR description accordingly.

@yajo
Copy link
Member

yajo commented Sep 15, 2022

Maybe a cleaner solution would be to add a destination argument to the clone() function. When calling it, we already provisioned the temporary directory. This way, the ownership of that variable belongs to the worker, and it would clean it up automatically, as I feel the cleanup should be in the temporary directory class better than in the worker.

@pawamoy
Copy link
Contributor Author

pawamoy commented Sep 17, 2022

I'm not sure to follow.

When calling it, we already provisioned the temporary directory.

Where would it be? I can't seem to find an attribute or property pointing to a temporary directory in the Template class.
If you meant to create the temporary directory above clone, in Template.local_abspath, then the temporary directory is still bound to the template and not the worker. If you meant to do this higher up in the chain again, for example in a method of the worker, then we should also refactor local_abspath as a regular method to be able to pass a destination argument. I'm OK to do this but am not sure of how this would impact the rest of the code flow.

the cleanup should be in the temporary directory class

That would be best indeed, but I don't see how we could use it in methods/properties as it's a context manager, and the temporary clone needs to exist for a longer time than the scope of the Template class' methods/properties.

Copy link
Member

@yajo yajo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reviewed this deeper and now I understand better what you said and why this is a good design. ❤️

I think it's good for a class to provision the paths it needs and clean them up, so let's move that logic to Template itself. It should be:

  1. Define Template._cleanup().
  2. Call it in Worker.__exit__().
  3. Define Template._temp_clone as explained below.

This way, the template creates the paths and provides the way to remove it.

Adding context manager support to Worker also seems to be the way to go. TBH I was thinking that implementing __del__() would be better, but I've now learned that can lead to other problems.

Please don't forget to add a couple of tests:

  1. The temporary clone is removed.
  2. Copying from a local repo is not removed.

Also the API docs to use Worker as a context manager.

Thanks!

copier/main.py Outdated Show resolved Hide resolved
copier/template.py Outdated Show resolved Hide resolved
@pawamoy pawamoy marked this pull request as ready for review September 24, 2022 17:50
copier/template.py Outdated Show resolved Hide resolved
@yajo yajo enabled auto-merge (squash) September 27, 2022 15:15
@yajo yajo merged commit 9b536a2 into copier-org:master Sep 27, 2022
@pawamoy pawamoy deleted the delete-temp-clone branch April 2, 2024 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Temp files being left over
3 participants