Skip to content

Commit

Permalink
Ensure a writeable PEX_ROOT under Variables access too.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Nov 17, 2024
1 parent b96cb89 commit e95483b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 29 deletions.
15 changes: 1 addition & 14 deletions pex/dist_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright 2020 Pex project contributors.
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import absolute_import, print_function
from __future__ import absolute_import

import errno
import functools
Expand Down Expand Up @@ -376,12 +376,9 @@ def iter_metadata_files(
):
# type: (...) -> Iterator[MetadataFiles]

debug = "_PEX_DEBUG" in os.environ
files = []
for metadata_type in restrict_types_to or MetadataType.values():
key = MetadataKey(metadata_type=metadata_type, location=location)
if debug:
print(">>> looking for metadata", key, "rescan?:", rescan, file=sys.stderr)
if rescan:
_METADATA_FILES.pop(key, None)
if key not in _METADATA_FILES:
Expand All @@ -397,16 +394,6 @@ def iter_metadata_files(
metadata_files = find_wheel_metadata(location)
if metadata_files:
listing.append(metadata_files)
elif debug:
print(">>> location has unexpected file type", file=sys.stderr)
print("... isfile", os.path.isfile(location), file=sys.stderr)
print("... isdir", os.path.isdir(location), file=sys.stderr)
print("... islink", os.path.islink(location), file=sys.stderr)
if os.path.islink(location):
link = os.readlink(location)
print(" ->", link, file=sys.stderr)
print(" exists?:", os.path.exists(link), file=sys.stderr)
print(" RP:", os.path.realpath(location), file=sys.stderr)
elif MetadataType.EGG_INFO is metadata_type and os.path.isdir(location):
listing.extend(
_find_installed_metadata_files(
Expand Down
12 changes: 2 additions & 10 deletions pex/pex_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

import json
import os
import tempfile
import zipfile

from pex import layout, pex_warnings, variables
from pex.cache import root as cache_root
from pex.common import can_write_dir, open_zip, safe_mkdtemp, touch
from pex.common import can_write_dir, open_zip, safe_mkdtemp
from pex.compatibility import PY2, WINDOWS
from pex.compatibility import string as compatibility_string
from pex.inherit_path import InheritPath
Expand Down Expand Up @@ -511,14 +510,7 @@ def pex_root(self):
# type: () -> str
pex_root = os.path.realpath(os.path.expanduser(self.raw_pex_root))
if not can_write_dir(pex_root):
pex_hash = self.pex_hash
if pex_hash:
tmp_root = os.path.realpath(
os.path.join(tempfile.gettempdir(), "pex_root_unwriteable", pex_hash)
)
touch(os.path.join(tmp_root, ".write-test"))
else:
tmp_root = os.path.realpath(safe_mkdtemp())
tmp_root = os.path.realpath(safe_mkdtemp())
pex_warnings.warn(
"PEX_ROOT is configured as {pex_root} but that path is un-writeable, "
"falling back to a temporary PEX_ROOT of {tmp_root} which will hurt "
Expand Down
12 changes: 11 additions & 1 deletion pex/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,17 @@ def _default_pex_root():
# third party import lifecycle events.
from pex.cache import root as cache_root

return cache_root.path(expand_user=True)
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


class Variables(object):
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_issue_1598.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ 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: PEX_ROOT is configured as {}".format(pex_root)
unwritable_pex_root_warning = "PEXWarning: The default PEX_ROOT is {}".format(pex_root)

pex_file = os.path.join(str(tmpdir), "pex.pex")

result = run_pex_command(
args=[pex_project_dir, "-o", pex_file], env=make_env(HOME=home, _PEX_DEBUG=1), quiet=True
args=[pex_project_dir, "-o", pex_file], env=make_env(HOME=home), quiet=True
)
result.assert_success()

Expand Down
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ passenv =
_PEX_PEXPECT_TIMEOUT
# This allows experimenting with Requires-Python metadata adjustment.
_PEX_REQUIRES_PYTHON
# Allow debugging individual tests.
_PEX_DEBUG
# This allows tests to detect they are running in CI.
CI
# These are to support directing test environments to the correct headers on OSX.
Expand Down

0 comments on commit e95483b

Please sign in to comment.