Skip to content

Commit

Permalink
Consolidate relative symlink creation. (#2667)
Browse files Browse the repository at this point in the history
This is work towards #2658 which will need to tackle falling back from
symlinks when there are insufficient permissions to create them.
  • Loading branch information
jsirois authored Feb 8, 2025
1 parent bc291ca commit a16c3de
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
7 changes: 4 additions & 3 deletions pex/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ def symlink(
self._ensure_parent(dst)
abs_src = os.path.realpath(src)
abs_dst = os.path.realpath(os.path.join(self.chroot, dst))
safe_symlink(os.path.relpath(abs_src, os.path.dirname(abs_dst)), abs_dst)
safe_relative_symlink(abs_src, abs_dst)

def write(
self,
Expand Down Expand Up @@ -768,7 +768,7 @@ def iter_files():
write_entry(filename, arcname)


def relative_symlink(
def safe_relative_symlink(
src, # type: Text
dst, # type: Text
):
Expand All @@ -779,6 +779,7 @@ def relative_symlink(
:param dst: The path to create the symlink at.
"""
dst_parent = os.path.dirname(dst)
safe_mkdir(dst_parent)
rel_src = os.path.relpath(src, dst_parent)
safe_symlink(rel_src, dst)

Expand Down Expand Up @@ -831,7 +832,7 @@ def iter_copytree(
yield src_entry, dst_entry
try:
if copy_mode is CopyMode.SYMLINK:
relative_symlink(src_entry, dst_entry)
safe_relative_symlink(src_entry, dst_entry)
elif is_dir:
os.mkdir(dst_entry)
else:
Expand Down
16 changes: 10 additions & 6 deletions pex/pep_376.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
from fileinput import FileInput

from pex import hashing
from pex.common import CopyMode, is_pyc_dir, is_pyc_file, safe_mkdir, safe_open
from pex.fs import safe_link, safe_symlink
from pex.common import (
CopyMode,
is_pyc_dir,
is_pyc_file,
safe_mkdir,
safe_open,
safe_relative_symlink,
)
from pex.fs import safe_link
from pex.interpreter import PythonInterpreter
from pex.typing import TYPE_CHECKING, cast
from pex.util import CacheHelper
Expand Down Expand Up @@ -413,10 +420,7 @@ def _reinstall_site_packages(
if copy_mode is CopyMode.SYMLINK and not (
src_entry.endswith(".dist-info") and os.path.isdir(src_entry)
):
dst_parent = os.path.dirname(dst_entry)
safe_mkdir(dst_parent)
rel_src = os.path.relpath(src_entry, dst_parent)
safe_symlink(rel_src, dst_entry)
safe_relative_symlink(src_entry, dst_entry)
traverse.discard(path)
elif is_dir:
safe_mkdir(dst_entry)
Expand Down
10 changes: 4 additions & 6 deletions pex/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
from pex.atomic_directory import AtomicDirectory, atomic_directory
from pex.auth import PasswordEntry
from pex.cache.dirs import BuiltWheelDir, CacheDir
from pex.common import pluralize, safe_mkdir, safe_mkdtemp, safe_open
from pex.common import pluralize, safe_mkdir, safe_mkdtemp, safe_open, safe_relative_symlink
from pex.compatibility import url_unquote, urlparse
from pex.dependency_configuration import DependencyConfiguration
from pex.dist_metadata import DistMetadata, Distribution, Requirement, is_wheel
from pex.fingerprinted_distribution import FingerprintedDistribution
from pex.fs import safe_symlink
from pex.jobs import Raise, SpawnedJob, execute_parallel, iter_map_parallel
from pex.network_configuration import NetworkConfiguration
from pex.orderedset import OrderedSet
Expand Down Expand Up @@ -572,10 +571,9 @@ def finalize_install(self, install_requests):
# Note: Create a relative path symlink between the two directories so that the
# PEX_ROOT can be used within a chroot environment where the prefix of the path may
# change between programs running inside and outside of the chroot.
source_path = os.path.join(atomic_dir.work_dir, self.request.wheel_file)
start_dir = os.path.dirname(source_path)
relative_target_path = os.path.relpath(self.install_chroot, start_dir)
safe_symlink(relative_target_path, source_path)
safe_relative_symlink(
self.install_chroot, os.path.join(atomic_dir.work_dir, self.request.wheel_file)
)

return self._iter_resolved_distributions(install_requests, fingerprint=wheel_dir_hash)

Expand Down

0 comments on commit a16c3de

Please sign in to comment.