Repository: ray-project/ray

Issue:
PermissionError not defined in Python 2.7
### System information
- **OS Platform and Distribution (e.g., Linux Ubuntu 16.04)**: Ubuntu 16
- **Ray installed from (source or binary)**: binary
- **Ray version**: 0.6.1
- **Python version**: 2.7
- **Exact command to reproduce**:

I don't have access to `/tmp`, and I get this following error:

```
cluster_tests.py:55: in _start_new_cluster
    "num_heartbeats_timeout": 10
/data/rliaw/miniconda3/envs/py2/lib/python2.7/site-packages/ray/test/cluster_utils.py:43: in __init__
    self.add_node(**head_node_args)
/data/rliaw/miniconda3/envs/py2/lib/python2.7/site-packages/ray/test/cluster_utils.py:86: in add_node
    **node_kwargs)
/data/rliaw/miniconda3/envs/py2/lib/python2.7/site-packages/ray/services.py:1777: in start_ray_head
    _internal_config=_internal_config)
/data/rliaw/miniconda3/envs/py2/lib/python2.7/site-packages/ray/services.py:1436: in start_ray_processes
    redis_max_memory=redis_max_memory)
/data/rliaw/miniconda3/envs/py2/lib/python2.7/site-packages/ray/services.py:458: in start_redis
    redis_stdout_file, redis_stderr_file = new_redis_log_file(redirect_output)
/data/rliaw/miniconda3/envs/py2/lib/python2.7/site-packages/ray/tempfile_services.py:182: in new_redis_log_file
    "redis", redirect_output)
/data/rliaw/miniconda3/envs/py2/lib/python2.7/site-packages/ray/tempfile_services.py:166: in new_log_files
    try_to_create_directory("/tmp/ray")
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

directory_path = '/tmp/ray'

    def try_to_create_directory(directory_path):
        """Attempt to create a directory that is globally readable/writable.

        Args:
            directory_path: The path of the directory to create.
        """
        directory_path = os.path.expanduser(directory_path)
        if not os.path.exists(directory_path):
            try:
                os.makedirs(directory_path)
            except OSError as e:
                if e.errno != os.errno.EEXIST:
                    raise e
                logger.warning(
                    "Attempted to create '{}', but the directory already "
                    "exists.".format(directory_path))
            # Change the log directory permissions so others can use it. This is
            # important when multiple people are using the same machine.
        try:
            os.chmod(directory_path, 0o0777)
>       except PermissionError:
E       NameError: global name 'PermissionError' is not defined

/data/rliaw/miniconda3/envs/py2/lib/python2.7/site-packages/ray/tempfile_services.py:69: NameError
```


Discussion:
@devin-petersohn looks like this was introduced in https://github.com/ray-project/ray/pull/3591.
Looks like an easy fix. `PermissionError` was only available as of Python 3.3. 

Resolving patch:
diff --git a/python/ray/tempfile_services.py b/python/ray/tempfile_services.py
--- a/python/ray/tempfile_services.py
+++ b/python/ray/tempfile_services.py
@@ -66,8 +66,16 @@ def try_to_create_directory(directory_path):
         # important when multiple people are using the same machine.
     try:
         os.chmod(directory_path, 0o0777)
-    except PermissionError:
-        pass
+    except OSError as e:
+        # Silently suppress the PermissionError that is thrown by the chmod.
+        # This is done because the user attempting to change the permissions
+        # on a directory may not own it. The chmod is attempted whether the
+        # directory is new or not to avoid race conditions.
+        # ray-project/ray/#3591
+        if e.errno in [errno.EACCES, errno.EPERM]:
+            pass
+        else:
+            raise
 
 
 def get_temp_root():


Test patch: