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
21 changes: 21 additions & 0 deletions crates/py_wheel_dist/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 = "py_wheel_dist_lib",
srcs = ["src/lib.rs"],
edition = "2024",
deps = [
"@crates//:anyhow",
"@crates//:argfile",
"@crates//:lexopt",
],
)

distribution_filegroup(
name = "distribution",
)
154 changes: 154 additions & 0 deletions crates/py_wheel_dist/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
//! Library for generating the output directory for `py_wheel_dist`.
//!
//! Reads the canonical wheel filename from `--name_file` (UTF-8, whitespace-trimmed),
//! creates the `--output` directory, and copies `--wheel` to `<output>/<wheel_name>`.

// 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 anyhow::{Context, Result, bail};
use lexopt::prelude::*;

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

/// Usage text for `--help`. `lexopt` does not generate help, so this is maintained by hand.
const USAGE: &str = "\
Usage: py_wheel_dist --wheel <PATH> --name_file <PATH> --output <PATH>

Copy a wheel into a dist directory under its canonical filename.

Options:
--wheel <PATH> Path to the wheel to copy
--name_file <PATH> File containing the sanitized wheel filename
--output <PATH> Output directory to copy the wheel into
@<FLAGFILE> Read newline-delimited arguments from <FLAGFILE>
-h, --help Print this help";

/// Creates the output directory and copies the wheel file to `<output>/<wheel_name>`, where
/// `wheel_name` is the whitespace-trimmed content of `name_file_path`.
pub fn create_wheel_dist(
wheel_path: &Path,
name_file_path: &Path,
output_dir: &Path,
) -> Result<PathBuf> {
let raw_name = fs::read_to_string(name_file_path)
.with_context(|| format!("reading {}", name_file_path.display()))?;
let wheel_name = raw_name.trim();

fs::create_dir_all(output_dir).with_context(|| format!("creating {}", output_dir.display()))?;

let destination = output_dir.join(wheel_name);
fs::copy(wheel_path, &destination).with_context(|| {
format!(
"copying {} to {}",
wheel_path.display(),
destination.display()
)
})?;
Ok(destination)
}

/// Parsed command-line options for `py_wheel_dist`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Options {
/// Path to the input `.whl` file.
pub wheel: PathBuf,
/// Path to the file containing the canonical wheel filename.
pub name_file: PathBuf,
/// Path to the output directory where the wheel should be copied.
pub output: PathBuf,
}

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

/// Parses command-line arguments (excluding `argv[0]` and subcommand name) into [`Cli`].
pub fn parse_args<I, T>(args: I) -> Result<Cli>
where
I: IntoIterator<Item = T>,
T: Into<OsString>,
{
let expanded = argfile::expand_args_from(
args.into_iter().map(Into::into),
argfile::parse_fromfile,
argfile::PREFIX,
)
.context("reading @flagfile")?;

let mut wheel: Option<PathBuf> = None;
let mut name_file: Option<PathBuf> = None;
let mut output: Option<PathBuf> = None;

let mut parser = lexopt::Parser::from_args(expanded);
while let Some(arg) = parser.next()? {
match arg {
Short(flags::HELP_SHORT) | Long(flags::HELP) => return Ok(Cli::Help),
Long(flags::WHEEL) => {
wheel = Some(PathBuf::from(parser.value()?));
}
Long(flags::NAME_FILE) => {
name_file = Some(PathBuf::from(parser.value()?));
}
Long(flags::OUTPUT) => {
output = Some(PathBuf::from(parser.value()?));
}
_ => bail!("{}", arg.unexpected()),
}
}

let wheel = wheel.with_context(|| format!("missing required argument: --{}", flags::WHEEL))?;
let name_file =
name_file.with_context(|| format!("missing required argument: --{}", flags::NAME_FILE))?;
let output =
output.with_context(|| format!("missing required argument: --{}", flags::OUTPUT))?;

Ok(Cli::Run(Options {
wheel,
name_file,
output,
}))
}

/// Parses CLI arguments (excluding `argv[0]` and subcommand name), populates the wheel dist
/// directory, 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!("py_wheel_dist: error: {err:#}");
return ExitCode::FAILURE;
}
};
if let Err(err) = create_wheel_dist(&options.wheel, &options.name_file, &options.output) {
eprintln!("py_wheel_dist: error: {err:#}");
return ExitCode::FAILURE;
}
ExitCode::SUCCESS
}
1 change: 1 addition & 0 deletions crates/shrubbery/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rust_library(
edition = "2024",
deps = [
"//crates/exe_zip_maker:exe_zip_maker_lib",
"//crates/py_wheel_dist:py_wheel_dist_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 @@ -11,6 +11,7 @@ use std::process::ExitCode;
pub mod subcommands {
// keep-sorted start case=no
pub const EXE_ZIP_MAKER: &str = "exe_zip_maker";
pub const PY_WHEEL_DIST: &str = "py_wheel_dist";
pub const WHEELMAKER: &str = "wheelmaker";
pub const ZIP_MAIN_MAKER: &str = "zip_main_maker";
pub const ZIPPER: &str = "zipper";
Expand All @@ -21,6 +22,7 @@ pub mod subcommands {
pub const SUBCOMMANDS: &[&str] = &[
// keep-sorted start case=no
subcommands::EXE_ZIP_MAKER,
subcommands::PY_WHEEL_DIST,
subcommands::WHEELMAKER,
subcommands::ZIP_MAIN_MAKER,
subcommands::ZIPPER,
Expand Down Expand Up @@ -66,6 +68,7 @@ where
match sub_raw.as_ref() {
// keep-sorted start case=no
subcommands::EXE_ZIP_MAKER => exe_zip_maker_lib::run_cli(iter),
subcommands::PY_WHEEL_DIST => py_wheel_dist_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
2 changes: 2 additions & 0 deletions python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ bzl_library(
"//python/private:bzlmod_enabled",
"//python/private:py_package",
"//python/private:py_wheel",
"//python/private:toolchain_types",
"//python/private:util",
"//python/private/toolchains:shrubbery_toolchain",
"@bazel_skylib//rules:native_binary",
],
)
Expand Down
11 changes: 9 additions & 2 deletions python/packaging.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ load("//python:py_binary.bzl", "py_binary")
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")
load("//python/private:py_package.bzl", "py_package_lib")
load("//python/private:py_wheel.bzl", _PyWheelInfo = "PyWheelInfo", _py_wheel = "py_wheel")
load("//python/private:toolchain_types.bzl", "SHRUBBERY_TOOLCHAIN_TYPE")
load("//python/private:util.bzl", "copy_propagating_kwargs")
load("//python/private/toolchains:shrubbery_toolchain.bzl", "get_shrubbery_tool")

# Re-export as public API
PyWheelInfo = _PyWheelInfo
Expand All @@ -45,12 +47,14 @@ def _py_wheel_dist_impl(ctx):
args.add("--name_file", name_file)
args.add("--output", out.path)

tool = get_shrubbery_tool(ctx, "py_wheel_dist", ctx.attr._copier)
ctx.actions.run(
mnemonic = "PyWheelDistDir",
executable = ctx.executable._copier,
executable = tool.files_to_run,
toolchain = tool.toolchain,
inputs = [wheel, name_file],
outputs = [out],
arguments = [args],
arguments = tool.prefix_args + [args],
)
return [
DefaultInfo(
Expand Down Expand Up @@ -84,6 +88,9 @@ This also has the advantage that stamping information is included in the wheel's
default = Label("//python/private:py_wheel_dist"),
),
},
toolchains = [
config_common.toolchain_type(SHRUBBERY_TOOLCHAIN_TYPE, mandatory = False),
],
)

def py_wheel(
Expand Down
19 changes: 16 additions & 3 deletions python/private/toolchains/shrubbery_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,27 @@ def get_shrubbery_tool(ctx, subcommand, fallback):
Returns:
{type}`struct` with fields:
* `executable`: {type}`Target` to pass to `actions_run()`.
* `prefix_args`: {type}`list[str]` `[subcommand]` when resolved from
the `shrubbery` toolchain, else `[]`.
* `files_to_run`: {type}`FilesToRunProvider` to pass to
`ctx.actions.run(executable = ...)`.
* `is_rust`: {type}`bool` True when resolved from the `shrubbery`
toolchain.
* `prefix_args`: {type}`list[str]` `[subcommand]` when `is_rust` is
True, else `[]`.
* `toolchain`: {type}`Label | None` the toolchain type to pass to
`actions_run()` or `ctx.actions.run()` so the action runs on the exec
platform the tool was built for. `None` when falling back to
`fallback`.
"""
toolchain = ctx.toolchains[SHRUBBERY_TOOLCHAIN_TYPE]
if toolchain and toolchain.shrubbery:
executable = toolchain.shrubbery
return struct(
# Target to pass to `actions_run()`.
executable = toolchain.shrubbery,
executable = executable,
# `FilesToRunProvider` to pass to `ctx.actions.run(executable = ...)`.
files_to_run = executable[DefaultInfo].files_to_run,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have both executable and files_to_run? The two are equivalent. Prefer files_to_run instead of executable, as its more powerful and flexible.

# True when resolved from the `shrubbery` toolchain.
is_rust = True,
Comment on lines +83 to +84

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"is_rust" is slightly misleading: that shrubbery is rust based is an implementation detail. rename this to "from_shrubbery_toolchain" instead

Also: i'm guessing for testing? which is fine, I just want to verify my understanding

# Leading subcommand argument prepended when invoking `shrubbery`.
prefix_args = [subcommand],
# Toolchain type for `actions_run()` / `ctx.actions.run()`.
Expand All @@ -81,6 +90,10 @@ def get_shrubbery_tool(ctx, subcommand, fallback):
return struct(
# Fallback tool target to pass to `actions_run()`.
executable = fallback,
# `FilesToRunProvider` to pass to `ctx.actions.run(executable = ...)`.
files_to_run = fallback[DefaultInfo].files_to_run,
# False when falling back to the built-in Python or script tool.
is_rust = False,
# Empty prefix when invoking the standalone fallback tool directly.
prefix_args = [],
# `None` so fallback tools use the default exec tools or host execution.
Expand Down
15 changes: 15 additions & 0 deletions tests/py_wheel_dist/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@rules_rust//rust:defs.bzl", "rust_test")

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

licenses(["notice"])

rust_test(
name = "py_wheel_dist_test",
size = "small",
srcs = ["py_wheel_dist_test.rs"],
edition = "2024",
deps = [
"//crates/py_wheel_dist:py_wheel_dist_lib",
],
)
Loading
Loading