Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ log = "0.4"
logging_timer = "1.1"
nix = { version = "0.31", features = ["fs"] }
owo-colors = "4.3"
pathdiff = "0.2"
pep440_rs = "0.7"
pep508_rs = "0.9"
python-pkginfo = "0.6"
Expand Down
1 change: 1 addition & 0 deletions crates/pexrs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ itertools = { workspace = true }
log = { workspace = true }
logging_timer = { workspace = true }
pex = { path = "../pex" }
platform = { path = "../platform" }
regex = { workspace = true }
venv = { path = "../venv" }
3 changes: 2 additions & 1 deletion crates/pexrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ fn prepare_venv<'a>(
#[cfg(unix)]
if let Some(sh_boot_seed_dir) = sh_boot_seed_dir {
fs::create_dir_all(&sh_boot_seed_dir)?;
std::os::unix::fs::symlink(
platform::unix::symlink(
venv_dir.join("pex"),
sh_boot_seed_dir.join(venv_interpreter.most_specific_exe_name()),
true,
)?;
}
Virtualenv::enclosing(venv_interpreter)
Expand Down
1 change: 1 addition & 0 deletions crates/platform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2024"
[dependencies]
anyhow = { workspace = true }
fs-err = { workspace = true }
pathdiff = { workspace = true }

# N.B.: Although is_executable is cross-platform, we only use it for Windows. The nix unistd access
# function is more robust that the check is_executable does on unix.
Expand Down
2 changes: 1 addition & 1 deletion crates/platform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#[cfg(unix)]
mod unix;
pub mod unix;

#[cfg(windows)]
mod windows;
Expand Down
23 changes: 20 additions & 3 deletions crates/platform/src/unix.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
// Copyright 2026 Pex project contributors.
// SPDX-License-Identifier: Apache-2.0

use std::borrow::Cow;
use std::fs::File;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::{PermissionsExt, symlink};
use std::os::unix::fs::PermissionsExt;
use std::path::Path;

use anyhow::{anyhow, bail};
use nix::errno::Errno;
use nix::unistd;
use nix::unistd::AccessFlags;

pub fn link_or_copy(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> anyhow::Result<()> {
symlink(src, dst).map_err(anyhow::Error::new)
pub fn link_or_copy(
src: impl AsRef<Path>,
dst: impl AsRef<Path>,
relative: bool,
) -> anyhow::Result<()> {
symlink(src, dst, relative)
}

pub fn symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>, relative: bool) -> anyhow::Result<()> {
let src = if relative
&& let Some(rel_base) = dst.as_ref().parent()
&& let Some(rel_path) = pathdiff::diff_paths(src.as_ref(), rel_base)
{
Cow::Owned(rel_path)
} else {
Cow::Borrowed(src.as_ref())
};
std::os::unix::fs::symlink(src, dst).map_err(anyhow::Error::new)
}

pub fn is_executable(path: impl AsRef<Path>) -> anyhow::Result<bool> {
Expand Down
6 changes: 5 additions & 1 deletion crates/platform/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ use std::path::Path;
use fs_err as fs;
use is_executable::IsExecutable;

pub fn link_or_copy(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> anyhow::Result<()> {
pub fn link_or_copy(
src: impl AsRef<Path>,
dst: impl AsRef<Path>,
_relative: bool,
) -> anyhow::Result<()> {
fs::hard_link(&src, &dst)
.or_else(|_| fs::copy(src, dst).map(|_| ()))
.map_err(anyhow::Error::new)
Expand Down
2 changes: 1 addition & 1 deletion crates/venv/src/venv_pex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ if __name__ == "__main__":
)
)?;
mark_executable(main_py_fp.file_mut())?;
link_or_copy(&main_py, venv.prefix().join("pex"))
link_or_copy(&main_py, venv.prefix().join("pex"), true)
}

fn write_repl<'a>(
Expand Down
6 changes: 5 additions & 1 deletion crates/venv/src/virtualenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ fn create_pep_405_venv<'a>(
)?;
let scripts_dir = path.join(SCRIPTS_DIR);
fs::create_dir_all(&scripts_dir)?;
link_or_copy(&base_interpreter.realpath, scripts_dir.join(PYTHON_EXE))?;
link_or_copy(
&base_interpreter.realpath,
scripts_dir.join(PYTHON_EXE),
false,
)?;
let site_packages_relpath = site_packages_relpath(&base_interpreter);
fs::create_dir_all(path.join(site_packages_relpath.as_ref()))?;
Ok(site_packages_relpath)
Expand Down