Repository: astral-sh/uv
File: crates/uv/src/commands/project/run.rs (Rust)

Code before:
        .strip_prefix(
            r#"#!/bin/sh
'''exec' "$(dirname -- "$(realpath -- "$0")")"/'python' "$0" "$@"
' '''
"#,
        )
        // Or an absolute path shebang
        .or_else(|| contents.strip_prefix(&format!("#!{}", previous_executable.display())))
    else {
        // If it's not a Python shebang, we'll skip it
        trace!(
            "Skipping copy of entrypoint `{}`: does not start with expected shebang",
            source.user_display()
        );
        return Ok(());
    };

    let contents = format!("#!{}\n{}", python_executable.display(), contents);
    let mut file = fs_err::OpenOptions::new()
        .create_new(true)
        .write(true)
        .open(target)?;
    file.write_all(contents.as_bytes())?;

    let mut perms = fs_err::metadata(target)?.permissions();
    perms.set_mode(0o755);
    fs_err::set_permissions(target, perms)?;

    trace!("Updated entrypoint at {}", target.user_display());

    Ok(())
}

/// Create a copy of the entrypoint at `source` at `target`, if it's a Python script launcher,
/// replacing the target Python executable with a new one.
#[cfg(windows)]
fn copy_entrypoint(
    source: &Path,
    target: &Path,
    _previous_executable: &Path,
    python_executable: &Path,
) -> Result<(), CopyEntrypointError> {
    use uv_trampoline_builder::Launcher;

    let Some(launcher) = Launcher::try_from_path(source)? else {
        return Ok(());
    };

    let launcher = launcher.with_python_path(python_executable.to_path_buf());
    let mut file = fs_err::OpenOptions::new()
        .create_new(true)

Review comment (security):
I can't remember why we have this in the wheel installer, I need to look:
```rust
let permissions = fs::metadata(&script_absolute)?.permissions();
if permissions.mode() & 0o111 != 0o111 {
    fs::set_permissions(
        script_absolute,
        Permissions::from_mode(permissions.mode() | 0o111),
    )?;
}
```

Code after:
    let Some(contents) = contents
        // Check for a relative path or relocatable shebang
        .strip_prefix(
            r#"#!/bin/sh
'''exec' "$(dirname -- "$(realpath -- "$0")")"/'python' "$0" "$@"
' '''
"#,
        )
        // Or an absolute path shebang
        .or_else(|| contents.strip_prefix(&format!("#!{}\n", previous_executable.display())))
    else {
        // If it's not a Python shebang, we'll skip it
        trace!(
            "Skipping copy of entrypoint `{}`: does not start with expected shebang",
            source.user_display()
        );
        return Ok(());
    };

    let contents = format!("#!{}\n{}", python_executable.display(), contents);
    let mode = fs_err::metadata(source)?.permissions().mode();
    let mut file = fs_err::OpenOptions::new()
        .create_new(true)
        .write(true)
        .mode(mode)
        .open(target)?;
    file.write_all(contents.as_bytes())?;

    trace!("Updated entrypoint at {}", target.user_display());

    Ok(())
}

/// Create a copy of the entrypoint at `source` at `target`, if it's a Python script launcher,
/// replacing the target Python executable with a new one.
#[cfg(windows)]
fn copy_entrypoint(
    source: &Path,
    target: &Path,
    _previous_executable: &Path,
    python_executable: &Path,
) -> Result<(), CopyEntrypointError> {
    use uv_trampoline_builder::Launcher;

    let Some(launcher) = Launcher::try_from_path(source)? else {
        return Ok(());
    };

    let launcher = launcher.with_python_path(python_executable.to_path_buf());
    let mut file = fs_err::OpenOptions::new()
        .create_new(true)