Repository navigation
refactor(wheel): convert py_wheel_dist build tool to Rust #4511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
| # True when resolved from the `shrubbery` toolchain. | ||
| is_rust = True, | ||
|
Comment on lines
+83
to
+84
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()`. | ||
|
|
@@ -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. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ], | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.