Skip to content
Draft
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
1 change: 1 addition & 0 deletions crates/shrubbery/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rust_library(
"//crates/extract_wheel_files:extract_wheel_files_lib",
"//crates/py_console_script_gen:py_console_script_gen_lib",
"//crates/py_wheel_dist:py_wheel_dist_lib",
"//crates/venv_shebang_rewriter:venv_shebang_rewriter_lib",
"//crates/wheelmaker:wheelmaker_lib",
"//crates/zip_main_maker:zip_main_maker_lib",
"//crates/zipper:zipper_lib",
Expand Down
3 changes: 3 additions & 0 deletions crates/shrubbery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod subcommands {
pub const EXTRACT_WHEEL_FILES: &str = "extract_wheel_files";
pub const PY_CONSOLE_SCRIPT_GEN: &str = "py_console_script_gen";
pub const PY_WHEEL_DIST: &str = "py_wheel_dist";
pub const VENV_SHEBANG_REWRITER: &str = "venv_shebang_rewriter";
pub const WHEELMAKER: &str = "wheelmaker";
pub const ZIP_MAIN_MAKER: &str = "zip_main_maker";
pub const ZIPPER: &str = "zipper";
Expand All @@ -27,6 +28,7 @@ pub const SUBCOMMANDS: &[&str] = &[
subcommands::EXTRACT_WHEEL_FILES,
subcommands::PY_CONSOLE_SCRIPT_GEN,
subcommands::PY_WHEEL_DIST,
subcommands::VENV_SHEBANG_REWRITER,
subcommands::WHEELMAKER,
subcommands::ZIP_MAIN_MAKER,
subcommands::ZIPPER,
Expand Down Expand Up @@ -75,6 +77,7 @@ where
subcommands::EXTRACT_WHEEL_FILES => extract_wheel_files_lib::run_cli(iter),
subcommands::PY_CONSOLE_SCRIPT_GEN => py_console_script_gen_lib::run_cli(iter),
subcommands::PY_WHEEL_DIST => py_wheel_dist_lib::run_cli(iter),
subcommands::VENV_SHEBANG_REWRITER => venv_shebang_rewriter_lib::run_cli(iter),
subcommands::WHEELMAKER => wheelmaker_lib::run_cli(iter),
subcommands::ZIP_MAIN_MAKER => zip_main_maker_lib::run_cli(iter),
subcommands::ZIPPER => zipper_lib::run_cli(iter),
Expand Down
20 changes: 20 additions & 0 deletions crates/venv_shebang_rewriter/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@rules_rust//rust:defs.bzl", "rust_library")
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup") # buildifier: disable=bzl-visibility

package(default_visibility = ["//:__subpackages__"])

licenses(["notice"])

rust_library(
name = "venv_shebang_rewriter_lib",
srcs = ["src/lib.rs"],
edition = "2024",
deps = [
"@crates//:anyhow",
"@crates//:lexopt",
],
)

distribution_filegroup(
name = "distribution",
)
209 changes: 209 additions & 0 deletions crates/venv_shebang_rewriter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
//! Rewrites a `console_script` wrapper's shebang into a batch/shell-Python polyglot.
//!
//! Reads `<in_path>`, replaces its first line with a platform-specific polyglot header for
//! `<target_os>`, writes the result to `<out_path>`, and marks the output executable on Unix.

// Errors must be returned, not panicked; see the `rust-lint` config in .bazelrc.
#![deny(clippy::expect_used, clippy::unwrap_used)]

use std::ffi::OsString;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::str::FromStr;

use anyhow::{Context, Result, bail};
use lexopt::prelude::*;

mod flags {
// keep-sorted start
pub const HELP: &str = "help";
pub const HELP_SHORT: char = 'h';
// keep-sorted end
}

/// Usage text for `--help`. `lexopt` does not generate help, so this is maintained by hand.
const USAGE: &str = "\
Usage: venv_shebang_rewriter <IN_PATH> <OUT_PATH> <TARGET_OS>

Rewrite a console_script wrapper's shebang into a batch/shell-Python polyglot.

Arguments:
<IN_PATH> Path to the input script
<OUT_PATH> Path to write the rewritten script to
<TARGET_OS> Target OS family: 'unix' or 'windows'

Options:
-h, --help Print this help";

/// Shell-Python polyglot header for Unix-like platforms.
pub const UNIX_HEADER: &[u8] =
b"#!/bin/sh\n'''exec' \"$(dirname \"$0\")/python3\" \"$0\" \"$@\"\n' '''\n";

/// Batch-Python polyglot header for Windows (`python.exe`).
pub const WINDOWS_PYTHON_HEADER: &[u8] =
b"@setlocal enabledelayedexpansion & \"%~dp0python.exe\" -x \"%~f0\" %* & exit /b !ERRORLEVEL!\r\n";

/// Batch-Python polyglot header for Windows GUI scripts (`pythonw.exe`).
pub const WINDOWS_PYTHONW_HEADER: &[u8] =
b"@setlocal enabledelayedexpansion & \"%~dp0pythonw.exe\" -x \"%~f0\" %* & exit /b !ERRORLEVEL!\r\n";

/// Splits `input` at the first `b'\n'` byte into `(first_line, rest)`.
///
/// `first_line` includes the trailing `b'\n'` if one is present, or the entire slice
/// if `input` contains no newline. `rest` contains all bytes after the first `b'\n'`.
pub fn split_first_line(input: &[u8]) -> (&[u8], &[u8]) {
if let Some(pos) = input.iter().position(|&b| b == b'\n') {
(&input[..=pos], &input[pos + 1..])
} else {
(input, b"")
}
}

/// Rewrites the first line of `input` into a batch or shell-Python polyglot header,
/// preserving the remaining bytes verbatim.
pub fn rewrite_shebang_bytes(input: &[u8], target_os: TargetOs) -> Vec<u8> {
let (first_line, rest) = split_first_line(input);
let header = match target_os {
TargetOs::Windows if first_line.starts_with(b"#!pythonw") => WINDOWS_PYTHONW_HEADER,
TargetOs::Windows => WINDOWS_PYTHON_HEADER,
TargetOs::Unix => UNIX_HEADER,
};

let mut output = Vec::with_capacity(header.len() + rest.len());
output.extend_from_slice(header);
output.extend_from_slice(rest);
output
}

/// Reads `in_path`, rewrites the shebang for `target_os`, writes the result to `out_path`,
/// and sets executable permissions (`mode | 0o111`) on Unix.
///
/// # Errors
///
/// Returns an error if `in_path` cannot be read, or `out_path` cannot be written or
/// have its permissions updated.
pub fn rewrite_shebang(in_path: &Path, out_path: &Path, target_os: TargetOs) -> Result<()> {
let input = fs::read(in_path).with_context(|| format!("reading {}", in_path.display()))?;
let output = rewrite_shebang_bytes(&input, target_os);

fs::write(out_path, &output).with_context(|| format!("writing {}", out_path.display()))?;

#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mut perms = fs::metadata(out_path)
.with_context(|| format!("reading metadata of {}", out_path.display()))?
.permissions();
perms.set_mode(perms.mode() | 0o111);
fs::set_permissions(out_path, perms)
.with_context(|| format!("setting permissions on {}", out_path.display()))?;
}

Ok(())
}

/// Target operating system family for generated shebang headers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TargetOs {
Unix,
Windows,
}

impl FromStr for TargetOs {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self> {
match s {
"unix" => Ok(Self::Unix),
"windows" => Ok(Self::Windows),
other => bail!("unsupported target_os '{other}': expected 'unix' or 'windows'"),
}
}
}

/// Parsed command-line options for `venv_shebang_rewriter`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Options {
/// Path to the input script.
pub in_path: PathBuf,
/// Path to write the rewritten script to.
pub out_path: PathBuf,
/// Target OS family selecting the polyglot header.
pub target_os: TargetOs,
}

/// Result of parsing the command line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Cli {
/// `--help` was requested.
Help,
/// Rewrite the shebang with the given options.
Run(Options),
}

/// Parses command-line arguments (excluding `argv[0]` and subcommand name) into [`Cli`].
///
/// Expects exactly three positionals: `<in_path> <out_path> <target_os>`.
pub fn parse_args<I, T>(args: I) -> Result<Cli>
where
I: IntoIterator<Item = T>,
T: Into<OsString>,
{
let mut positional: Vec<OsString> = Vec::new();

let mut parser = lexopt::Parser::from_args(args);
while let Some(arg) = parser.next()? {
match arg {
Short(flags::HELP_SHORT) | Long(flags::HELP) => return Ok(Cli::Help),
Value(val) => positional.push(val),
_ => bail!("{}", arg.unexpected()),
}
}

if positional.len() != 3 {
bail!(
"expected 3 positional arguments, got {}\n{USAGE}",
positional.len()
);
}
let mut positional = positional.into_iter();
let in_path = PathBuf::from(positional.next().context("missing <IN_PATH>")?);
let out_path = PathBuf::from(positional.next().context("missing <OUT_PATH>")?);
let target_os = positional.next().context("missing <TARGET_OS>")?;
let target_os = target_os
.to_str()
.with_context(|| format!("target_os is not valid UTF-8: {target_os:?}"))?
.parse::<TargetOs>()?;

Ok(Cli::Run(Options {
in_path,
out_path,
target_os,
}))
}

/// Parses CLI arguments (excluding `argv[0]` and subcommand name), rewrites the shebang, and
/// returns the process exit code.
pub fn run_cli<I, T>(args: I) -> ExitCode
where
I: IntoIterator<Item = T>,
T: Into<OsString>,
{
let options = match parse_args(args) {
Ok(Cli::Help) => {
println!("{USAGE}");
return ExitCode::SUCCESS;
}
Ok(Cli::Run(opts)) => opts,
Err(err) => {
eprintln!("venv_shebang_rewriter: error: {err:#}");
return ExitCode::FAILURE;
}
};
if let Err(err) = rewrite_shebang(&options.in_path, &options.out_path, options.target_os) {
eprintln!("venv_shebang_rewriter: error: {err:#}");
return ExitCode::FAILURE;
}
ExitCode::SUCCESS
}
2 changes: 2 additions & 0 deletions python/private/pypi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ bzl_library(
"//python/private:common",
"//python/private:py_info",
"//python/private:rule_builders",
"//python/private:toolchain_types",
"//python/private/toolchains:shrubbery_toolchain",
],
)

Expand Down
44 changes: 30 additions & 14 deletions python/private/pypi/venv_rewrite_shebang.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ load("//python/private:attributes.bzl", "WINDOWS_CONSTRAINTS_ATTRS")
load("//python/private:common.bzl", "is_windows_platform", "runfiles_root_path")
load("//python/private:py_info.bzl", "PyInfoBuilder", "VenvSymlinkEntry", "VenvSymlinkKind")
load("//python/private:rule_builders.bzl", "ruleb")
load("//python/private:toolchain_types.bzl", "SHRUBBERY_TOOLCHAIN_TYPE")
load("//python/private/toolchains:shrubbery_toolchain.bzl", "get_shrubbery_tool")

def _venv_rewrite_shebang_impl(ctx):
is_windows = is_windows_platform(ctx)
Expand All @@ -16,20 +18,31 @@ def _venv_rewrite_shebang_impl(ctx):
in_file = ctx.file.src

action_args = ctx.actions.args()
rewriter_file = ctx.files._venv_shebang_rewriter[0]
inputs = depset([in_file, rewriter_file])

if rewriter_file.path.endswith(".ps1"):
action_exe = "powershell.exe"
action_args.add_all([
"-ExecutionPolicy",
"Bypass",
"-NoProfile",
"-File",
rewriter_file,
])
tool = get_shrubbery_tool(
ctx,
"venv_shebang_rewriter",
ctx.attr._venv_shebang_rewriter,
)
if tool.is_rust:
inputs = depset([in_file])
action_exe = tool.files_to_run
action_toolchain = tool.toolchain
action_args.add_all(tool.prefix_args)
else:
action_exe = ctx.attr._venv_shebang_rewriter[DefaultInfo].files_to_run
rewriter_file = ctx.files._venv_shebang_rewriter[0]
inputs = depset([in_file, rewriter_file])
action_toolchain = None
if rewriter_file.path.endswith(".ps1"):
action_exe = "powershell.exe"
action_args.add_all([
"-ExecutionPolicy",
"Bypass",
"-NoProfile",
"-File",
rewriter_file,
])
else:
action_exe = tool.files_to_run

action_args.add(in_file)
action_args.add(out_file)
Expand All @@ -42,7 +55,7 @@ def _venv_rewrite_shebang_impl(ctx):
arguments = [action_args],
mnemonic = "PyVenvRewriteBin",
progress_message = "Rewriting venv bin script %{input}",
toolchain = None,
toolchain = action_toolchain,
)

symlink = VenvSymlinkEntry(
Expand All @@ -66,6 +79,9 @@ def _venv_rewrite_shebang_impl(ctx):
_builder = ruleb.Rule(
implementation = _venv_rewrite_shebang_impl,
executable = True,
toolchains = [
ruleb.ToolchainType(SHRUBBERY_TOOLCHAIN_TYPE, mandatory = False),
],
)
_builder.attrs.update({
"package": attr.string(),
Expand Down
2 changes: 1 addition & 1 deletion tests/shrubbery/shrubbery_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn temp_test_dir(prefix: &str) -> PathBuf {

#[test]
fn test_subcommands_registry_sorted_and_complete() {
assert_eq!(SUBCOMMANDS.len(), 7);
assert_eq!(SUBCOMMANDS.len(), 8);
let mut sorted = SUBCOMMANDS.to_vec();
sorted.sort_unstable();
assert_eq!(SUBCOMMANDS, sorted.as_slice());
Expand Down
21 changes: 21 additions & 0 deletions tests/venv_shebang_rewriter/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@rules_rust//rust:defs.bzl", "rust_test")

package(default_visibility = ["//:__subpackages__"])

licenses(["notice"])

rust_test(
name = "venv_shebang_rewriter_test",
size = "small",
srcs = ["venv_shebang_rewriter_test.rs"],
data = [
"//crates/shrubbery",
],
edition = "2024",
env = {
"SHRUBBERY_BIN": "$(rootpath //crates/shrubbery)",
},
deps = [
"//crates/venv_shebang_rewriter:venv_shebang_rewriter_lib",
],
)
Loading
Loading