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

Temporary repo context directory cannot be cleaned up on Windows #156

Closed
faph opened this issue Feb 17, 2021 · 6 comments
Closed

Temporary repo context directory cannot be cleaned up on Windows #156

faph opened this issue Feb 17, 2021 · 6 comments

Comments

@faph
Copy link

faph commented Feb 17, 2021

On Windows all files in .git/ dir are marked as read-only. On Windows, that means that they cannot be deleted by the tempfile.TemporaryDirectory context manager ("Access is denied").

One explicit way to fix this is to do something like this on every file in .git/ prior to exiting the temp dir context manager:

os.chmod(path, stat.S_IWUSR)

See also https://gitpython.readthedocs.io/en/stable/reference.html?highlight=rmtree#git.util.rmtree for implementation.

@asottile
Copy link
Owner

Seems to work fine for me on windows -- do you have a specific reproduction or error (your issue does not contain one)?

C:\Users\asott>python
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> import subprocess
>>> with tempfile.TemporaryDirectory() as tmpdir:
...     subprocess.check_call(('git', 'clone', 'https://github.com/asottile/astpretty', tmpdir))
...
Cloning into 'C:\Users\asott\AppData\Local\Temp\tmp1laa2gyv'...
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 297 (delta 6), reused 0 (delta 0), pack-reused 281 eceiving objects:  75% (22
Receiving objects: 100% (297/297), 59.51 KiB | 1.98 MiB/s, done.
Resolving deltas: 100% (165/165), done.
0
>>>

@faph
Copy link
Author

faph commented Feb 17, 2021

If I execute your example, I get

PermissionError: [WinError 5] Access is denied: 'C:\\Users\\x\\AppData\\Local\\Temp\\tmpho2fabiy\\.git\\objects\\pack\\pack-7bd0124ffc5964ab1db540ea14b92b2a1749b9db.idx'

Only differences: I'm on Python 3.6.8 and cloning using SSH.

@faph
Copy link
Author

faph commented Feb 17, 2021

https://bugs.python.org/issue26660 and python/cpython#10320: Seems this was fixed in Python 3.8?

@asottile
Copy link
Owner

3.8b1 it looks like, yes -- I recommend upgrading!

@CAM-Gerlach
Copy link

Also, it may not help right away but FWIW, things should improve further in Python 3.10 for the remaining cases of OS-level removal problems that python/cpython#10320 cannot avoid (in-use files, transient errors, I/O failures, etc). Assuming python/cpython#24793 lands, you'll be able to explicitly pass ignore_cleanup_errors=True (or whatever we end up calling it) to TemporaryDirectory as well as manually re-run cleanup() multiple times for best-effort deletion of all possible files, as @asottile expressed interest in on bpo-25024 .

@asottile
Copy link
Owner

does the issue persist if you use chdir in a contextmanager-like way?

@contextlib.contextmanager
def cwd(path: str) -> Generator[None, None, None]:
pwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(pwd)

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

No branches or pull requests

3 participants