Repository: conda/conda

Issue:
cached repodata.json PermissionsError
```
error: PermissionError(13, 'Permission denied')
command: /Users/apple/.conda/envs/cafe/bin/conda uninstall protobuf
user_agent: conda/4.4.3 requests/2.18.4 CPython/3.6.1 Darwin/16.1.0 OSX/10.12.1
_messageid: -9223372036845375609
_messagetime: 1514148478933 / 2017-12-24 14:47:58
_receipttime: 1514148478933 / 2017-12-24 14:47:58

Traceback (most recent call last):
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/exceptions.py", line 722, in __call__
    return func(*args, **kwargs)
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/cli/main.py", line 78, in _main
    exit_code = do_call(args, p)
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/cli/conda_argparse.py", line 75, in do_call
    exit_code = getattr(module, func_name)(args, parser)
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/cli/main_remove.py", line 82, in execute
    txn = solver.solve_for_transaction(force_remove=args.force)
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/solve.py", line 501, in solve_for_transaction
    force_remove, force_reinstall)
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/solve.py", line 434, in solve_for_diff
    final_precs = self.solve_final_state(deps_modifier, prune, ignore_pinned, force_remove)
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/solve.py", line 178, in solve_final_state
    index, r = self._prepare(prepared_specs)
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/solve.py", line 557, in _prepare
    self.subdirs, prepared_specs)
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/index.py", line 214, in get_reduced_index
    new_records = query_all(spec)
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/index.py", line 187, in query_all
    return tuple(concat(future.result() for future in as_completed(futures)))
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/repodata.py", line 99, in query
    self.load()
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/repodata.py", line 143, in load
    _internal_state = self._load()
  File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/repodata.py", line 222, in _load
    with open(self.cache_path_json, 'w') as fh:
PermissionError: [Errno 13] Permission denied: '/Users/apple/miniconda3/pkgs/cache/809318c1.json'
```


Discussion:

Resolving patch:
diff --git a/conda/core/repodata.py b/conda/core/repodata.py
--- a/conda/core/repodata.py
+++ b/conda/core/repodata.py
@@ -4,7 +4,7 @@
 import bz2
 from collections import defaultdict
 from contextlib import closing
-from errno import ENODEV
+from errno import EACCES, ENODEV, EPERM
 from genericpath import getmtime, isfile
 import hashlib
 import json
@@ -26,7 +26,7 @@
                              with_metaclass)
 from ..common.url import join_url, maybe_unquote
 from ..core.package_cache import PackageCache
-from ..exceptions import CondaDependencyError, CondaHTTPError, CondaIndexError
+from ..exceptions import CondaDependencyError, CondaHTTPError, CondaIndexError, NotWritableError
 from ..gateways.connection import (ConnectionError, HTTPError, InsecureRequestWarning,
                                    InvalidSchema, SSLError)
 from ..gateways.connection.session import CondaSession
@@ -219,8 +219,14 @@ def _load(self):
         else:
             if not isdir(dirname(self.cache_path_json)):
                 mkdir_p(dirname(self.cache_path_json))
-            with open(self.cache_path_json, 'w') as fh:
-                fh.write(raw_repodata_str or '{}')
+            try:
+                with open(self.cache_path_json, 'w') as fh:
+                    fh.write(raw_repodata_str or '{}')
+            except (IOError, OSError) as e:
+                if e.errno in (EACCES, EPERM):
+                    raise NotWritableError(self.cache_path_json, e.errno, caused_by=e)
+                else:
+                    raise
             _internal_state = self._process_raw_repodata_str(raw_repodata_str)
             self._internal_state = _internal_state
             self._pickle_me()


Test patch: