Skip to content

Commit

Permalink
feat: adds the ability to specify the python interpeter for wheel bui…
Browse files Browse the repository at this point in the history
…lding (#127)
  • Loading branch information
tdejager authored Dec 12, 2023
1 parent caa0175 commit b950ac8
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 8 deletions.
5 changes: 4 additions & 1 deletion crates/rattler_installs_packages/src/artifacts/sdist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn generic_archive_reader(
#[cfg(test)]
mod tests {
use crate::artifacts::SDist;
use crate::python_env::Pep508EnvMakers;
use crate::python_env::{Pep508EnvMakers, PythonLocation};
use crate::wheel_builder::WheelBuilder;
use crate::{index::PackageDb, resolve::ResolveOptions};
use insta::{assert_debug_snapshot, assert_ron_snapshot};
Expand Down Expand Up @@ -261,6 +261,7 @@ mod tests {
None,
&resolve_options,
package_db.1.path(),
PythonLocation::System,
);

let result = wheel_builder.get_sdist_metadata(&sdist).await.unwrap();
Expand All @@ -284,6 +285,7 @@ mod tests {
None,
&resolve_options,
package_db.1.path(),
PythonLocation::System,
);

// Build the wheel
Expand All @@ -309,6 +311,7 @@ mod tests {
None,
&resolve_options,
package_db.1.path(),
PythonLocation::System,
);

// Build the wheel
Expand Down
2 changes: 1 addition & 1 deletion crates/rattler_installs_packages/src/python_env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ pub(crate) use system_python::{
PythonInterpreterVersion,
};
pub use uninstall::{uninstall_distribution, UninstallDistributionError};
pub use venv::{PythonLocation, VEnv};
pub use venv::{PythonLocation, VEnv, VEnvError};
1 change: 1 addition & 0 deletions crates/rattler_installs_packages/src/python_env/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl PythonLocation {
}
}

#[allow(missing_docs)]
#[derive(Error, Debug)]
pub enum VEnvError {
#[error(transparent)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::SDistResolution;
use crate::artifacts::SDist;
use crate::artifacts::Wheel;
use crate::index::PackageDb;
use crate::python_env::WheelTags;
use crate::python_env::{PythonLocation, WheelTags};
use crate::resolve::{PinnedPackage, ResolveOptions};
use crate::types::{
Artifact, ArtifactInfo, ArtifactName, Extra, NormalizedPackageName, PackageName,
Expand Down Expand Up @@ -140,13 +140,15 @@ impl<'db, 'i> PypiDependencyProvider<'db, 'i> {
locked_packages: HashMap<NormalizedPackageName, PinnedPackage<'db>>,
favored_packages: HashMap<NormalizedPackageName, PinnedPackage<'db>>,
options: &'i ResolveOptions,
python_location: PythonLocation,
) -> miette::Result<Self> {
let wheel_builder = WheelBuilder::new(
package_db,
markers,
compatible_tags,
options,
package_db.cache_dir(),
python_location,
);

Ok(Self {
Expand Down
8 changes: 6 additions & 2 deletions crates/rattler_installs_packages/src/resolve/solve.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::dependency_provider::PypiPackageName;
use crate::index::PackageDb;
use crate::python_env::WheelTags;
use crate::python_env::{PythonLocation, WheelTags};
use crate::resolve::dependency_provider::{PypiDependencyProvider, PypiVersion};
use crate::types::PackageName;
use crate::{types::ArtifactInfo, types::Extra, types::NormalizedPackageName, types::Version};
Expand Down Expand Up @@ -157,7 +157,9 @@ pub struct ResolveOptions {
///
/// If `compatible_tags` is defined then the available artifacts of a distribution are filtered to
/// include only artifacts that are compatible with the specified tags. If `None` is passed, the
/// artifacts are not filtered at all.
/// artifacts are not filtered at all
// TODO: refactor this into an input type of sorts later
#[allow(clippy::too_many_arguments)]
pub async fn resolve<'db>(
package_db: &'db PackageDb,
requirements: impl IntoIterator<Item = &Requirement>,
Expand All @@ -166,6 +168,7 @@ pub async fn resolve<'db>(
locked_packages: HashMap<NormalizedPackageName, PinnedPackage<'db>>,
favored_packages: HashMap<NormalizedPackageName, PinnedPackage<'db>>,
options: &ResolveOptions,
python_location: PythonLocation,
) -> miette::Result<Vec<PinnedPackage<'db>>> {
// Construct a provider
let provider = PypiDependencyProvider::new(
Expand All @@ -175,6 +178,7 @@ pub async fn resolve<'db>(
locked_packages,
favored_packages,
options,
python_location,
)?;
let pool = &provider.pool;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub(crate) struct BuildEnvironment<'db> {
build_requirements: Vec<Requirement>,
resolved_wheels: Vec<PinnedPackage<'db>>,
venv: VEnv,
python_location: PythonLocation,
}

impl<'db> BuildEnvironment<'db> {
Expand Down Expand Up @@ -105,6 +106,7 @@ impl<'db> BuildEnvironment<'db> {
locked_packages,
favored_packages,
resolve_options,
self.python_location.clone(),
)
.await
.map_err(|_| WheelBuildError::CouldNotResolveEnvironment(all_requirements))?;
Expand Down Expand Up @@ -152,10 +154,11 @@ impl<'db> BuildEnvironment<'db> {
env_markers: &MarkerEnvironment,
wheel_tags: Option<&WheelTags>,
resolve_options: &ResolveOptions,
python_location: PythonLocation,
) -> Result<BuildEnvironment<'db>, WheelBuildError> {
// Setup a work directory and a new env dir
let work_dir = tempfile::tempdir().unwrap();
let venv = VEnv::create(&work_dir.path().join("venv"), PythonLocation::System).unwrap();
let venv = VEnv::create(&work_dir.path().join("venv"), python_location.clone())?;

// Find the build system
let build_system =
Expand Down Expand Up @@ -184,6 +187,7 @@ impl<'db> BuildEnvironment<'db> {
HashMap::default(),
HashMap::default(),
resolve_options,
python_location.clone(),
)
.await
.map_err(|_| WheelBuildError::CouldNotResolveEnvironment(build_requirements.to_vec()))?;
Expand Down Expand Up @@ -228,6 +232,7 @@ impl<'db> BuildEnvironment<'db> {
entry_point,
resolved_wheels,
venv,
python_location,
})
}
}
14 changes: 12 additions & 2 deletions crates/rattler_installs_packages/src/wheel_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{collections::HashMap, path::PathBuf};
use parking_lot::Mutex;
use pep508_rs::{MarkerEnvironment, Requirement};

use crate::python_env::{PythonLocation, VEnvError};
use crate::resolve::{ResolveOptions, SDistResolution};
use crate::types::{NormalizedPackageName, ParseArtifactNameError, WheelFilename};
use crate::wheel_builder::build_environment::BuildEnvironment;
Expand Down Expand Up @@ -49,6 +50,9 @@ pub struct WheelBuilder<'db, 'i> {

/// Cache of locally built wheels on the system
locally_built_wheels: WheelCache,

/// Location of the python interpreter to use
python_location: PythonLocation,
}

/// An error that can occur while building a wheel
Expand Down Expand Up @@ -84,6 +88,9 @@ pub enum WheelBuildError {

#[error("error parsing artifact name: {0}")]
ArtifactError(#[from] ParseArtifactNameError),

#[error("error creating venv: {0}")]
VEnvError(#[from] VEnvError),
}

impl TryFrom<&SDist> for WheelKey {
Expand Down Expand Up @@ -125,6 +132,7 @@ impl<'db, 'i> WheelBuilder<'db, 'i> {
wheel_tags: Option<&'i WheelTags>,
resolve_options: &'i ResolveOptions,
wheel_cache_dir: &Path,
python_location: PythonLocation,
) -> Self {
// We are running into a chicken & egg problem if we want to build wheels for packages that
// require their build system as sdist as well. For example, `hatchling` requires `hatchling` as
Expand All @@ -146,6 +154,7 @@ impl<'db, 'i> WheelBuilder<'db, 'i> {
wheel_tags,
resolve_options,
locally_built_wheels: WheelCache::new(wheel_cache_dir.to_path_buf()),
python_location,
}
}

Expand All @@ -163,7 +172,6 @@ impl<'db, 'i> WheelBuilder<'db, 'i> {
return Ok(venv.clone());
}

// If not in cache, create a new one
tracing::debug!(
"creating virtual env for: {:?}",
sdist.name().distribution.as_source_str()
Expand All @@ -175,6 +183,7 @@ impl<'db, 'i> WheelBuilder<'db, 'i> {
self.env_markers,
self.wheel_tags,
&self.resolve_options,
self.python_location.clone(),
)
.await?;

Expand Down Expand Up @@ -312,7 +321,7 @@ impl<'db, 'i> WheelBuilder<'db, 'i> {
mod tests {
use crate::artifacts::SDist;
use crate::index::PackageDb;
use crate::python_env::Pep508EnvMakers;
use crate::python_env::{Pep508EnvMakers, PythonLocation};
use crate::resolve::ResolveOptions;
use crate::wheel_builder::wheel_cache::WheelKey;
use crate::wheel_builder::WheelBuilder;
Expand Down Expand Up @@ -348,6 +357,7 @@ mod tests {
None,
&resolve_options,
package_db.1.path(),
PythonLocation::System,
);

// Build the wheel
Expand Down
2 changes: 2 additions & 0 deletions crates/rip_bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ async fn actual_main() -> miette::Result<()> {
HashMap::default(),
HashMap::default(),
&resolve_opts,
PythonLocation::System,
)
.await
{
Expand Down Expand Up @@ -200,6 +201,7 @@ async fn actual_main() -> miette::Result<()> {
Some(&compatible_tags),
&resolve_opts,
package_db.cache_dir(),
PythonLocation::System,
);

for pinned_package in blueprint.into_iter().sorted_by(|a, b| a.name.cmp(&b.name)) {
Expand Down

0 comments on commit b950ac8

Please sign in to comment.