Skip to content

Commit

Permalink
feat: drop rattler_installs_packages for uv crates
Browse files Browse the repository at this point in the history
  • Loading branch information
mattem committed Oct 16, 2024
1 parent 781650c commit 5c1b6d1
Show file tree
Hide file tree
Showing 21 changed files with 13,511 additions and 7,772 deletions.
17,895 changes: 11,379 additions & 6,516 deletions Cargo.Bazel.lock

Large diffs are not rendered by default.

3,089 changes: 1,986 additions & 1,103 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@ repository = "https://github.com/aspect-build/rules_py"
license = "Apache 2"
edition = "2021"
readme = "README.md"
rust-version = "1.74.1"
rust-version = "1.81.0"

[workspace.dependencies]
clap = { version = "4.5.20", features = ["derive"] }
itertools = "0.13.0"
miette = { version = "7.2", features = ["fancy"] }
tempfile = "3.13.0"
thiserror = "1.0.64"
uv-cache = { git = "https://github.com/astral-sh/uv", rev = "855c1917e1e0e2b48c38de71bebc845af016afae" }
uv-distribution-filename = { git = "https://github.com/astral-sh/uv.git", rev = "855c1917e1e0e2b48c38de71bebc845af016afae" }
uv-extract = { git = "https://github.com/astral-sh/uv.git", rev = "855c1917e1e0e2b48c38de71bebc845af016afae" }
uv-install-wheel = { git = "https://github.com/astral-sh/uv", rev = "855c1917e1e0e2b48c38de71bebc845af016afae" }
uv-pypi-types = { git = "https://github.com/astral-sh/uv.git", rev = "855c1917e1e0e2b48c38de71bebc845af016afae" }
uv-python = { git = "https://github.com/astral-sh/uv", rev = "855c1917e1e0e2b48c38de71bebc845af016afae" }
uv-virtualenv = { git = "https://github.com/astral-sh/uv", rev = "855c1917e1e0e2b48c38de71bebc845af016afae" }

[patch.crates-io]
reqwest-middleware = { git = "https://github.com/astral-sh/reqwest-middleware", rev = "5e3eaf254b5bd481c75d2710eed055f95b756913", features = ["multipart"] }
reqwest-retry = { git = "https://github.com/astral-sh/reqwest-middleware", rev = "5e3eaf254b5bd481c75d2710eed055f95b756913" }

[profile.release]
strip = true
11 changes: 9 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ python_register_toolchains(
name = "python_toolchain_3_8",
python_version = "3.8.12",
# Setting `set_python_version_constraint` will set special constraints on the registered toolchain.
# This means that this toolchain registration will only be selected for `py_binary` / `py_test` targets
# This means that this toolchain registration will only be selected for `py_binary` / `py_test` targets
# that have the `python_version = "3.8.12"` attribute set. Targets that have no `python_attribute` will use
# the default toolchain resolved which can be seen below.
set_python_version_constraint = True,
Expand Down Expand Up @@ -154,7 +154,7 @@ rules_rust_dependencies()

RUST_EDITION = "2021"

RUST_VERSION = "1.77.2"
RUST_VERSION = "1.81.0"

rust_register_toolchains(
edition = RUST_EDITION,
Expand All @@ -173,6 +173,13 @@ rust_repository_set(
extra_target_triples = [
"aarch64-apple-darwin",
],
sha256s = {
"rustc-1.81.0-aarch64-apple-darwin.tar.xz": "bed00f549a08030b232ad811728e3a5d7239e2e53b667df9cfb11eabf87f2cf3",
"clippy-1.81.0-aarch64-apple-darwin.tar.xz": "fcab64f49cd2fb47f3c9ee96cf31ce178b05be66b7dbc0543c3ea217bd4786bf",
"cargo-1.81.0-aarch64-apple-darwin.tar.xz": "cc826e6592016db7a5750a97051b71b48aca2d79f146daf08e953d56000ae43d",
"llvm-tools-1.81.0-aarch64-apple-darwin.tar.xz": "907aaf74df0dd97a23da03fde3a5162535498e5ab67c0adb6fee22999ca461fb",
"rust-std-1.81.0-aarch64-apple-darwin.tar.xz": "2dba5210a79617a9240570c1f7fcc24912a2c96689a3159324727e5a516c6326",
},
versions = [RUST_VERSION],
)

Expand Down
5 changes: 0 additions & 5 deletions py/private/py_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ def _py_binary_rule_impl(ctx):
"{{ARG_COLLISION_STRATEGY}}": ctx.attr.package_collisions,
"{{ARG_PYTHON}}": to_rlocation_path(ctx, py_toolchain.python) if py_toolchain.runfiles_interpreter else py_toolchain.python.path,
"{{ARG_VENV_NAME}}": ".{}.venv".format(ctx.attr.name),
"{{ARG_VENV_PYTHON_VERSION}}": "{}.{}.{}".format(
py_toolchain.interpreter_version_info.major,
py_toolchain.interpreter_version_info.minor,
py_toolchain.interpreter_version_info.micro,
),
"{{ARG_PTH_FILE}}": to_rlocation_path(ctx, site_packages_pth_file),
"{{ENTRYPOINT}}": to_rlocation_path(ctx, ctx.file.main),
"{{PYTHON_ENV}}": "\n".join(_dict_to_exports(env)).strip(),
Expand Down
7 changes: 0 additions & 7 deletions py/private/py_unpacked_wheel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ def _py_unpacked_wheel_impl(ctx):
unpack_directory.path,
"--wheel",
ctx.file.src.path,
"--python",
py_toolchain.python,
"--python-version",
"{}.{}.{}".format(
py_toolchain.interpreter_version_info.major,
py_toolchain.interpreter_version_info.minor,
py_toolchain.interpreter_version_info.micro,
),
"--package-name",
ctx.attr.py_package_name,
])

ctx.actions.run(
Expand Down Expand Up @@ -72,9 +68,6 @@ _attrs = {
allow_single_file = [".whl"],
mandatory = True,
),
"py_package_name": attr.string(
mandatory = True,
),
# NB: this is read by _resolve_toolchain in py_semantics.
"_interpreter_version_flag": attr.label(
default = "//py:interpreter_version",
Expand Down
6 changes: 1 addition & 5 deletions py/private/py_venv.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ def _py_venv_rule_imp(ctx):
"{{ARG_PYTHON}}": to_rlocation_path(ctx, py_toolchain.python) if py_toolchain.runfiles_interpreter else py_toolchain.python.path,
"{{ARG_COLLISION_STRATEGY}}": ctx.attr.package_collisions,
"{{ARG_VENV_LOCATION}}": paths.join(ctx.attr.location, ctx.attr.venv_name),
"{{ARG_VENV_PYTHON_VERSION}}": "{}.{}.{}".format(
py_toolchain.interpreter_version_info.major,
py_toolchain.interpreter_version_info.minor,
py_toolchain.interpreter_version_info.micro,
),
"{{ARG_VENV_NAME}}": ".{}.venv".format(ctx.attr.name),
"{{ARG_PTH_FILE}}": to_rlocation_path(ctx, site_packages_pth_file),
"{{EXEC_PYTHON_BIN}}": "python{}".format(
py_toolchain.interpreter_version_info.major,
Expand Down
4 changes: 2 additions & 2 deletions py/private/run.tmpl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export VIRTUAL_ENV
"${VENV_TOOL}" \
--location "${VIRTUAL_ENV}" \
--python "$(python_location)" \
--python-version "{{ARG_VENV_PYTHON_VERSION}}" \
--pth-file "$(rlocation {{ARG_PTH_FILE}})" \
--collision-strategy "{{ARG_COLLISION_STRATEGY}}"
--collision-strategy "{{ARG_COLLISION_STRATEGY}}" \
--venv-name "{{ARG_VENV_NAME}}"

PATH="${VIRTUAL_ENV}/bin:${PATH}"
export PATH
Expand Down
4 changes: 2 additions & 2 deletions py/private/venv.tmpl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ VIRTUAL_ENV="$(alocation "${VENV_ROOT}/{{ARG_VENV_LOCATION}}")"
"${VENV_TOOL}" \
--location "${VIRTUAL_ENV}" \
--python "$(alocation $(rlocation {{ARG_PYTHON}}))" \
--python-version "{{ARG_VENV_PYTHON_VERSION}}" \
--pth-file "$(rlocation {{ARG_PTH_FILE}})" \
--pth-entry-prefix "${RUNFILES_DIR}" \
--collision-strategy "{{ARG_COLLISION_STRATEGY}}"
--collision-strategy "{{ARG_COLLISION_STRATEGY}}" \
--venv-name "{{ARG_VENV_NAME}}"

echo "Created virtualenv in ${VIRTUAL_ENV}"
1 change: 0 additions & 1 deletion py/tests/virtual/django/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ django_resolutions = resolutions.from_requirements(all_whl_requirements_by_packa
py_unpacked_wheel(
name = "django_wheel",
src = "@django_4_2_4//file",
py_package_name = "Django",
)

compile_pip_requirements(
Expand Down
5 changes: 2 additions & 3 deletions py/tools/pex/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ load("//py:defs.bzl", "py_binary", "py_unpacked_wheel")
py_unpacked_wheel(
name = "pex_unpacked",
src = "@rules_py_pex_2_3_1//file",
py_package_name = "pex"
)

py_binary(
name = "pex",
srcs = ["main.py"],
main = "main.py",
visibility = ["//visibility:public"],
deps = [":pex_unpacked"],
visibility = ["//visibility:public"]
)
)
13 changes: 11 additions & 2 deletions py/tools/py/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("@rules_rust//rust:defs.bzl", "rust_library")
rust_library(
name = "py",
srcs = [
"src/interpreter.rs",
# "src/interpreter.rs",
"src/lib.rs",
"src/pth.rs",
"src/unpack.rs",
Expand All @@ -14,7 +14,16 @@ rust_library(
"//py/tools/venv_bin:__pkg__",
],
deps = [
"@crate_index//:itertools",
"@crate_index//:miette",
"@crate_index//:rattler_installs_packages",
"@crate_index//:tempfile",
"@crate_index//:thiserror",
"@crate_index//:uv-cache",
"@crate_index//:uv-distribution-filename",
"@crate_index//:uv-extract",
"@crate_index//:uv-install-wheel",
"@crate_index//:uv-pypi-types",
"@crate_index//:uv-python",
"@crate_index//:uv-virtualenv",
],
)
11 changes: 10 additions & 1 deletion py/tools/py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@ rust-version.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
itertools = { workspace = true }
miette = { workspace = true }
rattler_installs_packages = { git = "https://github.com/prefix-dev/rip", rev = "b047c9ec0b42125a67d35346f08b7e7848ac24f4", default-features = false, features = ["rustls-tls"] }
tempfile = { workspace = true }
thiserror = { workspace = true }
uv-cache = { workspace = true }
uv-distribution-filename = { workspace = true }
uv-extract = { workspace = true }
uv-install-wheel = { workspace = true }
uv-pypi-types = { workspace = true }
uv-python = { workspace = true }
uv-virtualenv = { workspace = true }
43 changes: 0 additions & 43 deletions py/tools/py/src/interpreter.rs

This file was deleted.

2 changes: 0 additions & 2 deletions py/tools/py/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod interpreter;
mod pth;
mod unpack;
mod venv;

pub use unpack::unpack_wheel;
pub use venv::create_venv;

pub(crate) use interpreter::Interpreter;
pub use pth::{PthFile, SymlinkCollisionResolutionStrategy};
94 changes: 61 additions & 33 deletions py/tools/py/src/unpack.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,69 @@
use std::path::Path;
use std::{
fs,
path::{Path, PathBuf},
str::FromStr,
};

use miette::{miette, IntoDiagnostic, Result};
use rattler_installs_packages::{
artifacts::wheel::UnpackWheelOptions, artifacts::Wheel, types::PackageName,
};

use crate::Interpreter;

pub fn unpack_wheel(
python: &Path,
version: &str,
location: &Path,
pkg_name: &str,
wheel: &Path,
) -> Result<()> {
let interpreter = Interpreter::new(python, version)?;
let python_executable = interpreter.executable()?;
let install_paths = interpreter.install_paths(false);

let unpack_options = UnpackWheelOptions {
installer: Some("Aspect Build rules_py".to_string()),
// launcher_arch:
..UnpackWheelOptions::default()
pub fn unpack_wheel(version: &str, location: &Path, wheel: &Path) -> Result<()> {
let python_version: uv_python::PythonVersion = version
.parse()
.map_err(|_| miette!("Failed to parse Python version"))?;

let wheel_file_reader = fs::File::open(wheel).into_diagnostic()?;

let temp = tempfile::tempdir().into_diagnostic()?;

let _ = uv_extract::unzip(wheel_file_reader, temp.path()).into_diagnostic()?;

let site_packages_dir = location
.join("lib")
.join(format!(
"python{}.{}",
python_version.major(),
python_version.minor()
))
.join("site-packages");

let scheme = uv_pypi_types::Scheme {
purelib: site_packages_dir.to_path_buf(),
platlib: site_packages_dir.to_path_buf(),
// No windows support.
scripts: location.join("bin"),
data: site_packages_dir.to_path_buf(),
include: location.join("lib").join("include"),
};

let package_name: PackageName = pkg_name.parse().unwrap();
let wheel = Wheel::from_path(wheel, &package_name.into())
.map_err(|_| miette!("Failed to create wheel from path"))?;

wheel
.unpack(
&location,
&install_paths,
&python_executable,
&unpack_options,
)
.into_diagnostic()?;
let layout = uv_install_wheel::Layout {
sys_executable: PathBuf::new(),
python_version: (python_version.major(), python_version.minor()),
// Don't stamp in the path to the interpreter into the generated bins
// as we don't want an absolute path here.
// Perhaps this should be set to just "python" so it picks up the one in the venv path?
os_name: "/bin/false".to_string(),
scheme,
};

let filename = wheel
.file_name()
.and_then(|f| f.to_str())
.expect("Exepected to get filename from wheel path");
let wheel_file_name =
uv_distribution_filename::WheelFilename::from_str(filename).into_diagnostic()?;

uv_install_wheel::linker::install_wheel(
&layout,
false,
temp.path(),
&wheel_file_name,
None,
None,
Some("aspect_rule_py"),
uv_install_wheel::linker::LinkMode::Copy,
&uv_install_wheel::linker::Locks::default(),
)
.into_diagnostic()?;

Ok(())
}
Loading

0 comments on commit 5c1b6d1

Please sign in to comment.