Thanks to visit codestin.com
Credit goes to github.com

Skip to content

PermissionError on update on Windows #345

@pawamoy

Description

@pawamoy

Related to #233.

So we get this traceback when trying to update a project:

Traceback:
Traceback (most recent call last):
  File "c:\users\user\appdata\local\continuum\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\user\appdata\local\continuum\anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\.local\bin\copier.exe\__main__.py", line 7, in 
  File "c:\users\user\.local\pipx\venvs\copier\lib\site-packages\plumbum\cli\application.py", line 577, in run
    inst, retcode = subapp.run(argv, exit=False)
  File "c:\users\user\.local\pipx\venvs\copier\lib\site-packages\plumbum\cli\application.py", line 572, in run
    retcode = inst.main(*tailargs)
  File "c:\users\user\.local\pipx\venvs\copier\lib\site-packages\copier\cli.py", line 38, in _wrapper
    return method(*args, **kwargs)
  File "c:\users\user\.local\pipx\venvs\copier\lib\site-packages\copier\cli.py", line 297, in main
    dst_path=destination_path, only_diff=self.only_diff,
  File "c:\users\user\.local\pipx\venvs\copier\lib\site-packages\copier\cli.py", line 187, in _copy
    **kwargs,
  File "c:\users\user\.local\pipx\venvs\copier\lib\site-packages\copier\main.py", line 145, in copy
    update_diff(conf=conf)
  File "c:\users\user\.local\pipx\venvs\copier\lib\site-packages\copier\main.py", line 278, in update_diff
    diff = diff_cmd("--inter-hunk-context=0")
  File "c:\users\user\appdata\local\continuum\anaconda3\lib\tempfile.py", line 807, in __exit__
    self.cleanup()
  File "c:\users\user\appdata\local\continuum\anaconda3\lib\tempfile.py", line 811, in cleanup
    _shutil.rmtree(self.name)
  File "c:\users\user\appdata\local\continuum\anaconda3\lib\shutil.py", line 516, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "c:\users\user\appdata\local\continuum\anaconda3\lib\shutil.py", line 395, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "c:\users\user\appdata\local\continuum\anaconda3\lib\shutil.py", line 395, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "c:\users\user\appdata\local\continuum\anaconda3\lib\shutil.py", line 395, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "c:\users\user\appdata\local\continuum\anaconda3\lib\shutil.py", line 400, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "c:\users\user\appdata\local\continuum\anaconda3\lib\shutil.py", line 398, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 5] Accès refusé: 'C:\\Users\\user\\AppData\\Local\\Temp\\1\\copier.main.update_diff.xa4s9r_o\\.git\\objects\\05\\468ab504dde8222f35265177b3f9d4108058f0'

Looking at the code, it simply comes from the use of a temporary directory, and from cloning a repo with git inside it:

with tempfile.TemporaryDirectory(prefix=f"{__name__}.update_diff.") as dst_temp:

When cloning, git sets some files as read-only, and then Python chokes on it when trying to delete the directory in the exit clause of the context manager.

I've found this post on StackOverflow, but using the given solution would require to either subclass TemporaryDirectory to customize its use of shutil.rmtree, or stop using it as a context manager, and manually create and delete the directory.

def handle_remove_readonly(func, path, exc):
  excvalue = exc[1]
  if func in (os.rmdir, os.remove, os.unlink) and excvalue.errno == errno.EACCES:
      os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
      func(path)
  else:
      raise

# use shhutil.rmtree like this:
shutil.rmtree(path, ignore_errors=False, onerror=handle_remove_readonly)

Would you be open to a PR implementing one of these two ways of fixing this issue? If yes, which one?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions