Repository: python-poetry/poetry

Issue:
Poetry fails to install p4python due to read-only files
<!--
  Hi there! Thank you for discovering and submitting an issue.

  Before you submit this; let's make sure of a few things.
  Please make sure the following boxes are ticked if they are correct.
  If not, please try and fulfill these first.
-->

<!-- Checked checkbox should look like this: [x] -->
- [x] I am on the [latest](https://github.com/sdispater/poetry/releases/latest) Poetry version.
- [x] I have searched the [issues](https://github.com/sdispater/poetry/issues) of this repo and believe that this is not a duplicate.
- [x] If an exception occurs when executing a command, I executed it again in debug mode (`-vvv` option).

<!--
  Once those are done, if you're able to fill in the following list with your information,
  it'd be very helpful to whoever handles the issue.
-->

- **OS version and name**: Windows 10
- **Poetry version**: poetry 0.12.2
- **Link of a [pyproject.toml Gist](https://gist.github.com/epage/5f28e3b1e5eeb9a30697363e369a5fde)
- **Link of a [backtrace Gist](https://gist.github.com/epage/2584ad981ff5d9f175d55212b0192987)

## Issue

In digging into the problem, it seems that p4python's files are all marked read-only, causing windows to error when trying to delete them via `shutil.rmtree` which is invoked by poetry's custom temp directory handling.


Discussion:
I face the same issue. 
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32

`pip install p4python` works:
```
pip install p4python
Collecting p4python
  Using cached https://files.pythonhosted.org/packages/e2/eb/9f948e36dc489299a918e8de305fa598a2759798a6e72ca3d5cbe2dbe862/p4python-2017.2.1615960-cp36-cp36m-win_amd64.whl
Installing collected packages: p4python
Successfully installed p4python-2017.2.1615960

```

Adding p4python via pipenv works as well.

I'm not sure why adding it via poetry retrieves  p4python-2017.2.1615960.tar.gz and then throws the permissioning error:

[https://gist.github.com/nagulv/9be7c38d8796b1b1609d45041eaf5f30](p4python backtrace Gist)


I decided to dig a little more into why this happens.

So it looks like poetry went to pypi to get the `info.required_dists` but p4python doesn't have that information in pypi's database

- p4python example: https://pypi.org/pypi/p4python/2018.2.1743033/json
- example of `info.required_dists`: https://pypi.org/pypi/black/json

So poetry looks at all of the wheels.  It will only look at the wheels if
- there is a universal wheel
- there is a platform wheel and no sdist

Because p4python is not universal and has an sdist, the sdist is favored.

So poetry downloads and tries to use the sdist.  The problem is the sdist is probably built from source within perforce where all of the files are marked read-only unless someone is editing them.  This then causes problems with the use of `rmtree`.
A quick workaround would be to have `rmtree` provide its own `onerror`
Except we're using `tempfile`

https://github.com/python/cpython/blob/master/Lib/tempfile.py#L794

Resolving patch:
diff --git a/poetry/utils/helpers.py b/poetry/utils/helpers.py
--- a/poetry/utils/helpers.py
+++ b/poetry/utils/helpers.py
@@ -34,19 +34,18 @@ def normalize_version(version):  # type: (str) -> str
     return str(Version(version))
 
 
+def _del_ro(action, name, exc):
+    os.chmod(name, stat.S_IWRITE)
+    os.remove(name)
+
+
 @contextmanager
 def temporary_directory(*args, **kwargs):
-    try:
-        from tempfile import TemporaryDirectory
-
-        with TemporaryDirectory(*args, **kwargs) as name:
-            yield name
-    except ImportError:
-        name = tempfile.mkdtemp(*args, **kwargs)
+    name = tempfile.mkdtemp(*args, **kwargs)
 
-        yield name
+    yield name
 
-        shutil.rmtree(name)
+    shutil.rmtree(name, onerror=_del_ro)
 
 
 def parse_requires(requires):  # type: (str) -> List[str]


Test patch: