diff --git a/pex/common.py b/pex/common.py index 1135dee9e..c37974de4 100644 --- a/pex/common.py +++ b/pex/common.py @@ -515,21 +515,16 @@ def can_write_dir(path): # type: (str) -> bool """Determines if the directory at path can be written to by the current process. - If the directory doesn't exist, determines if it can be created and thus written to. - - N.B.: This is a best-effort check only that uses permission heuristics and does not actually test - that the directory can be written to with and writes. + Attempts to create the directory if it doesn't already exist. :param path: The directory path to test. :return:`True` if the given path is a directory that can be written to by the current process. """ - while not os.access(path, os.F_OK): - parent_path = os.path.dirname(path) - if not parent_path or (parent_path == path): - # We've recursed up to the root without success, which shouldn't happen, - return False - path = parent_path - return os.path.isdir(path) and os.access(path, os.R_OK | os.W_OK | os.X_OK) + try: + os.unlink(touch(os.path.join(path, ".write-test"))) + return True + except (IOError, OSError): + return False def touch( diff --git a/pex/variables.py b/pex/variables.py index 6849686ca..aca8aec62 100644 --- a/pex/variables.py +++ b/pex/variables.py @@ -171,17 +171,7 @@ def _default_pex_root(): # third party import lifecycle events. from pex.cache import root as cache_root - pex_root = cache_root.path(expand_user=True) - if not can_write_dir(pex_root): - tmp_root = os.path.realpath(safe_mkdtemp()) - pex_warnings.warn( - "The default PEX_ROOT is {pex_root} but that path is un-writeable, falling back to a " - "temporary PEX_ROOT of {tmp_root} which will hurt performance.".format( - pex_root=pex_root, tmp_root=tmp_root - ) - ) - pex_root = tmp_root - return pex_root + return cache_root.path(expand_user=True) class Variables(object): diff --git a/tests/integration/test_issue_1598.py b/tests/integration/test_issue_1598.py index 52a6764ed..f395f2aef 100644 --- a/tests/integration/test_issue_1598.py +++ b/tests/integration/test_issue_1598.py @@ -23,7 +23,7 @@ def test_mount_respects_env( pex_root = os.path.join(home, rel_pex_root) os.makedirs(pex_root) os.chmod(pex_root, 0o555) - unwritable_pex_root_warning = "PEXWarning: The default PEX_ROOT is {}".format(pex_root) + unwritable_pex_root_warning = "PEXWarning: PEX_ROOT is configured as {}".format(pex_root) pex_file = os.path.join(str(tmpdir), "pex.pex")