Repository: pypa/pip

Issue:
In Windows, pip wouldn't delete read-only file(when install via git)
I put a package in github, and install by command `pip install git+https://github.com/RobberPhex/mysql-connector-python`
I got a error:

```
Cleaning up...
  Exception:
Traceback (most recent call last):
  File "C:\Python34\lib\shutil.py", line 370, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 5] Access is denied: 'C:\\Users\\ROBBER~1\\AppData\\Local\\Temp\\pip-9hyythjm-build\\.git\\objects\\pack\\pack-cfbcf285d8e9cfd47348c04e497acb0e554e55e3.idx'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\pip\commands\install.py", line 283, in run
    requirement_set.install(install_options, global_options, root=options.root_path)
  File "C:\Python34\lib\site-packages\pip\req.py", line 1444, in install
    requirement.remove_temporary_source()
  File "C:\Python34\lib\site-packages\pip\req.py", line 761, in remove_temporary_source
    rmtree(self._temp_build_dir)
  File "C:\Python34\lib\site-packages\pip\util.py", line 43, in rmtree
    onerror=rmtree_errorhandler)
  File "C:\Python34\lib\shutil.py", line 477, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Python34\lib\shutil.py", line 372, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\Python34\lib\site-packages\pip\util.py", line 53, in rmtree_errorhandler
    (exctype is PermissionError and value.args[3] == 5) #python3.3
IndexError: tuple index out of range
```

It is because pip wouldn't delete read-only file in Windows.



Discussion:

Resolving patch:
diff --git a/pip/util.py b/pip/util.py
--- a/pip/util.py
+++ b/pip/util.py
@@ -11,8 +11,7 @@
 
 from pip.exceptions import InstallationError, BadCommand
 from pip.backwardcompat import(
-    WindowsError, string_types, raw_input, console_to_str,
-    PermissionError, stdlib_pkgs
+    string_types, raw_input, console_to_str, stdlib_pkgs
 )
 from pip.locations import (
     site_packages, user_site, running_under_virtualenv, virtualenv_no_global,
@@ -52,19 +51,15 @@ def rmtree_errorhandler(func, path, exc_info):
     """On Windows, the files in .svn are read-only, so when rmtree() tries to
     remove them, an exception is thrown.  We catch that here, remove the
     read-only attribute, and hopefully continue without problems."""
-    exctype, value = exc_info[:2]
-    if not ((exctype is WindowsError and value.args[0] == 5) or  # others
-            (exctype is OSError and value.args[0] == 13) or  # python2.4
-            (exctype is PermissionError and value.args[3] == 5)  # python3.3
-            ):
-        raise
-    # file type should currently be read only
-    if ((os.stat(path).st_mode & stat.S_IREAD) != stat.S_IREAD):
+    # if file type currently read only
+    if os.stat(path).st_mode & stat.S_IREAD:
+        # convert to read/write
+        os.chmod(path, stat.S_IWRITE)
+        # use the original function to repeat the operation
+        func(path)
+        return
+    else:
         raise
-    # convert to read/write
-    os.chmod(path, stat.S_IWRITE)
-    # use the original function to repeat the operation
-    func(path)
 
 
 def display_path(path):


Test patch: