Skip to content

Commit

Permalink
Write relative paths for scripts in data directory
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 10, 2024
1 parent ec83151 commit a81e2d7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/install-wheel-rs/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
/// tqdm/cli.py,sha256=x_c8nmc4Huc-lKEsAXj78ZiyqSJ9hJ71j7vltY67icw,10509
/// tqdm-4.62.3.dist-info/RECORD,,
/// ```
#[derive(Deserialize, Serialize, PartialOrd, PartialEq, Ord, Eq)]
#[derive(Deserialize, Serialize, PartialOrd, PartialEq, Ord, Eq, Debug)]
pub(crate) struct RecordEntry {
pub(crate) path: String,
pub(crate) hash: Option<String>,
Expand Down
23 changes: 18 additions & 5 deletions crates/install-wheel-rs/src/wheel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,17 @@ fn install_script(
)));
}

let target_path = layout.scheme.scripts.join(file.file_name());
let script_absolute = layout.scheme.scripts.join(file.file_name());
let script_relative =
pathdiff::diff_paths(&script_absolute, site_packages).ok_or_else(|| {
Error::Io(io::Error::new(
io::ErrorKind::Other,
format!(
"Could not find relative path for: {}",
script_absolute.simplified_display()
),
))
})?;

let path = file.path();
let mut script = File::open(&path)?;
Expand All @@ -461,24 +471,25 @@ fn install_script(
let start = format_shebang(&layout.sys_executable, &layout.os_name)
.as_bytes()
.to_vec();
let mut target = File::create(&target_path)?;
let mut target = File::create(&script_absolute)?;
let size_and_encoded_hash = copy_and_hash(&mut start.chain(script), &mut target)?;
fs::remove_file(&path)?;
Some(size_and_encoded_hash)
} else {
// reading and writing is slow especially for large binaries, so we move them instead
drop(script);
fs::rename(&path, &target_path)?;
fs::rename(&path, &script_absolute)?;
None
};
#[cfg(unix)]
{
use std::fs::Permissions;
use std::os::unix::fs::PermissionsExt;

fs::set_permissions(&target_path, Permissions::from_mode(0o755))?;
fs::set_permissions(&script_absolute, Permissions::from_mode(0o755))?;
}

// Find the existing entry in the `RECORD`.
let relative_to_site_packages = path
.strip_prefix(site_packages)
.expect("Prefix must no change");
Expand All @@ -493,7 +504,9 @@ fn install_script(
path.simplified_display()
))
})?;
entry.path = target_path.display().to_string();

// Update the entry in the `RECORD`.
entry.path = script_relative.simplified_display().to_string();
if let Some((size, encoded_hash)) = size_and_encoded_hash {
entry.size = Some(size);
entry.hash = Some(encoded_hash);
Expand Down

0 comments on commit a81e2d7

Please sign in to comment.