Skip to content

Clean garbage tmpdirs with tempfile.TemporaryDirectory #7941

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

graingert
Copy link
Member

@graingert graingert commented Oct 25, 2020

Closes #7940

see also #7933 for my first attempt

@graingert graingert force-pushed the clean-garbage-tmpdirs-with-tempfile-temporary-directory branch from a53c978 to 09b0e20 Compare October 25, 2020 12:01
garbage = parent.joinpath(f"garbage-{uuid.uuid4()}")
path.rename(garbage)
rm_rf(garbage)
with tempfile.TemporaryDirectory(
Copy link
Member

Choose a reason for hiding this comment

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

This should spec a root directory and a prefix

Copy link
Member Author

Choose a reason for hiding this comment

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

dir is the root directory

@bluetech
Copy link
Member

IIUC, this is basically a hack to coerce TemporaryDirectory.cleanup() to do the dirty work for us, right? I like it :) Although I think a comment is in order to explain what it's doing, it seems odd if you don't get it.

@RonnyPfannschmidt
Copy link
Member

Since the rmtree helper is simple, how about replicating

@graingert
Copy link
Member Author

so it turns out this PR does fix my original issue, but my test case is hitting #7947

@graingert graingert force-pushed the clean-garbage-tmpdirs-with-tempfile-temporary-directory branch from 09b0e20 to 216c7af Compare October 26, 2020 12:51
@graingert
Copy link
Member Author

IIUC, this is basically a hack to coerce TemporaryDirectory.cleanup() to do the dirty work for us, right? I like it :) Although I think a comment is in order to explain what it's doing, it seems odd if you don't get it.

I'm just composing a comment for this now - and I'd like to comment the use of the rename to "garbage-", but I'm not really clear on why this rename was done originally: https://github.com/pytest-dev/pytest/pull/3988/files#diff-b2559484fd51ba4a312585ab8375eda68c43b0b66bb329ec036106255928a2b9R169

@graingert graingert force-pushed the clean-garbage-tmpdirs-with-tempfile-temporary-directory branch from 216c7af to 5ba2ca9 Compare October 26, 2020 12:57
Python has a very nice rm_rf hidden in
tempfile.TemporaryDirectory._rmtree we can use it to cleanup our
"garbage-" dir
https://github.com/python/cpython/blob/3d43f1dce3e9aaded38f9a2c73e3c66acf85505c/Lib/tempfile.py#L784-L812
@graingert graingert force-pushed the clean-garbage-tmpdirs-with-tempfile-temporary-directory branch from 5ba2ca9 to ceea364 Compare October 26, 2020 13:08
@graingert
Copy link
Member Author

this breaks on windows and I don't know why

Base automatically changed from master to main March 9, 2021 20:40
@RonnyPfannschmidt
Copy link
Member

@graingert the name garbage was replicated from pylib which uses it as indicator of folder names that can be removed even if things fail from the processs that initially tries to take them out

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.

Pytest cannot delete tmp_path directories without stat.S_IXUSR mode bit set
3 participants