Skip to content

Commit

Permalink
Return to baseline with more aggressive can_write_dir check.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Nov 17, 2024
1 parent e95483b commit 31200bb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
17 changes: 6 additions & 11 deletions pex/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 1 addition & 11 deletions pex/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_issue_1598.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 31200bb

Please sign in to comment.