Skip to content

Commit

Permalink
refactor: seperate solving from installing
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra committed Aug 23, 2024
1 parent 0fac969 commit 91ca7d3
Show file tree
Hide file tree
Showing 11 changed files with 528 additions and 246 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ rattler = { version = "0.27.5", default-features = false, features = [
"cli-tools",
"indicatif",
] }
rattler_cache = { version = "0.1.7", default-features = false }
rattler_conda_types = { version = "0.27.2", default-features = false }
rattler_digest = { version = "1.0.1", default-features = false, features = ["serde"] }
rattler_index = { version = "0.19.24", default-features = false }
Expand Down
33 changes: 20 additions & 13 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//! The build module contains the code for running the build process for a given [`Output`]
use rattler_conda_types::{Channel, MatchSpec, ParseStrictness};
use std::path::PathBuf;
use std::vec;
//! The build module contains the code for running the build process for a given
//! [`Output`]
use std::{path::PathBuf, vec};

use miette::IntoDiagnostic;
use rattler_conda_types::{Channel, MatchSpec, ParseStrictness};
use rattler_index::index;
use rattler_solve::{ChannelPriority, SolveStrategy};

use crate::metadata::Output;
use crate::package_test::TestConfiguration;
use crate::recipe::parser::TestType;
use crate::render::solver::load_repodatas;
use crate::{package_test, tool_configuration};
use crate::{
metadata::Output, package_test, package_test::TestConfiguration, recipe::parser::TestType,
render::solver::load_repodatas, tool_configuration,
};

/// Check if the build should be skipped because it already exists in any of the channels
/// Check if the build should be skipped because it already exists in any of the
/// channels
pub async fn skip_existing(
mut outputs: Vec<Output>,
tool_configuration: &tool_configuration::Configuration,
Expand Down Expand Up @@ -93,8 +93,9 @@ pub async fn skip_existing(
Ok(outputs)
}

/// Run the build for the given output. This will fetch the sources, resolve the dependencies,
/// and execute the build script. Returns the path to the resulting package.
/// Run the build for the given output. This will fetch the sources, resolve the
/// dependencies, and execute the build script. Returns the path to the
/// resulting package.
pub async fn run_build(
output: Output,
tool_configuration: &tool_configuration::Configuration,
Expand Down Expand Up @@ -133,6 +134,11 @@ pub async fn run_build(
.await
.into_diagnostic()?;

output
.install_environments(tool_configuration)
.await
.into_diagnostic()?;

output.run_build_script().await.into_diagnostic()?;

// Package all the new files
Expand All @@ -148,7 +154,8 @@ pub async fn run_build(

// We run all the package content tests
for test in output.recipe.tests() {
// TODO we could also run each of the (potentially multiple) test scripts and collect the errors
// TODO we could also run each of the (potentially multiple) test scripts and
// collect the errors
if let TestType::PackageContents { package_contents } = test {
package_contents
.run_test(&paths_json, &output.build_configuration.target_platform)
Expand Down
57 changes: 41 additions & 16 deletions src/console_utils.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//! This module contains utilities for logging and progress bar handling.
use clap_verbosity_flag::{InfoLevel, Verbosity};
use console::style;
use indicatif::{
HumanBytes, HumanDuration, MultiProgress, ProgressBar, ProgressState, ProgressStyle,
};
use std::borrow::Cow;
use std::time::Duration;
use std::{
borrow::Cow,
collections::HashMap,
future::Future,
io,
str::FromStr,
sync::{Arc, Mutex},
time::Instant,
time::{Duration, Instant},
};

use clap_verbosity_flag::{InfoLevel, Verbosity};
use console::style;
use indicatif::{
HumanBytes, HumanDuration, MultiProgress, ProgressBar, ProgressState, ProgressStyle,
};
use tracing::{field, Level};
use tracing_core::{span::Id, Event, Field, Subscriber};
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{
filter::{Directive, ParseError},
fmt::{
Expand All @@ -25,6 +25,7 @@ use tracing_subscriber::{
},
layer::{Context, SubscriberExt},
registry::LookupSpan,
util::SubscriberInitExt,
EnvFilter, Layer,
};

Expand Down Expand Up @@ -297,7 +298,8 @@ impl LoggingOutputHandler {
}
}

/// Return a string with the current indentation level (bars added to the front of the string).
/// Return a string with the current indentation level (bars added to the
/// front of the string).
pub fn with_indent_levels(&self, template: &str) -> String {
let state = self.state.lock().unwrap();
let indent_str = indent_levels(state.indentation_level);
Expand All @@ -309,7 +311,8 @@ impl LoggingOutputHandler {
&self.progress_bars
}

/// Returns the style to use for a progressbar that is currently in progress.
/// Returns the style to use for a progressbar that is currently in
/// progress.
pub fn default_bytes_style(&self) -> indicatif::ProgressStyle {
let template_str = self.with_indent_levels(
"{spinner:.green} {prefix:20!} [{elapsed_precise}] [{bar:40!.bright.yellow/dim.white}] {bytes:>8} @ {smoothed_bytes_per_sec:8}"
Expand Down Expand Up @@ -340,7 +343,8 @@ impl LoggingOutputHandler {
)
}

/// Returns the style to use for a progressbar that is currently in progress.
/// Returns the style to use for a progressbar that is currently in
/// progress.
pub fn default_progress_style(&self) -> indicatif::ProgressStyle {
let template_str = self.with_indent_levels(
"{spinner:.green} {prefix:20!} [{elapsed_precise}] [{bar:40!.bright.yellow/dim.white}] {pos:>7}/{len:7}"
Expand All @@ -351,7 +355,8 @@ impl LoggingOutputHandler {
.progress_chars("━━╾─")
}

/// Returns the style to use for a progressbar that is in Deserializing state.
/// Returns the style to use for a progressbar that is in Deserializing
/// state.
pub fn deserializing_progress_style(&self) -> indicatif::ProgressStyle {
let template_str =
self.with_indent_levels("{spinner:.green} {prefix:20!} [{elapsed_precise}] {wide_msg}");
Expand Down Expand Up @@ -387,7 +392,8 @@ impl LoggingOutputHandler {
.progress_chars("━━╾─")
}

/// Returns the style to use for a progressbar that is indeterminate and simply shows a spinner.
/// Returns the style to use for a progressbar that is indeterminate and
/// simply shows a spinner.
pub fn long_running_progress_style(&self) -> indicatif::ProgressStyle {
let template_str = self.with_indent_levels("{spinner:.green} {msg}");
ProgressStyle::with_template(&template_str).unwrap()
Expand All @@ -407,7 +413,8 @@ impl LoggingOutputHandler {
});
}

/// Displays a spinner with the given message while running the specified function to completion.
/// Displays a spinner with the given message while running the specified
/// function to completion.
pub fn wrap_in_progress<T, F: FnOnce() -> T>(
&self,
msg: impl Into<Cow<'static, str>>,
Expand All @@ -422,6 +429,23 @@ impl LoggingOutputHandler {
pb.finish_and_clear();
result
}

/// Displays a spinner with the given message while running the specified
/// function to completion.
pub async fn wrap_in_progress_async<T, Fut: Future<Output = T>>(
&self,
msg: impl Into<Cow<'static, str>>,
future: Fut,
) -> T {
let pb = self.add_progress_bar(
ProgressBar::new_spinner().with_style(self.long_running_progress_style()),
);
pb.enable_steady_tick(Duration::from_millis(100));
pb.set_message(msg);
let result = future.await;
pb.finish_and_clear();
result
}
}

impl io::Write for LoggingOutputHandler {
Expand Down Expand Up @@ -456,7 +480,8 @@ pub enum LogStyle {
Plain,
}

/// Constructs a default [`EnvFilter`] that is used when the user did not specify a custom RUST_LOG.
/// Constructs a default [`EnvFilter`] that is used when the user did not
/// specify a custom RUST_LOG.
pub fn get_default_env_filter(
verbose: clap_verbosity_flag::LevelFilter,
) -> Result<EnvFilter, ParseError> {
Expand Down
87 changes: 37 additions & 50 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ use hash::HashInfo;
use metadata::{
BuildConfiguration, BuildSummary, Directories, Output, PackageIdentifier, PackagingSettings,
};
use miette::{IntoDiagnostic, WrapErr};
use miette::IntoDiagnostic;
use opt::*;
use package_test::TestConfiguration;
use petgraph::{algo::toposort, graph::DiGraph, visit::DfsPostOrder};
use rattler_conda_types::{package::ArchiveType, Channel, ChannelConfig, Platform};
use rattler_conda_types::{package::ArchiveType, Channel, Platform};
use rattler_solve::{ChannelPriority, SolveStrategy};
use recipe::{
parser::{find_outputs_from_src, Dependency, Recipe},
Expand Down Expand Up @@ -119,17 +119,16 @@ pub fn get_tool_config(
tool_configuration::reqwest_client_from_auth_storage(args.common.auth_file.clone())
.into_diagnostic()?;

Ok(Configuration {
client,
fancy_log_handler: fancy_log_handler.clone(),
no_clean: args.keep_build,
no_test: args.no_test,
use_zstd: args.common.use_zstd,
use_bz2: args.common.use_bz2,
render_only: args.render_only,
skip_existing: args.skip_existing,
..Configuration::default()
})
Ok(Configuration::builder()
.with_logging_output_handler(fancy_log_handler.clone())
.with_keep_build(args.keep_build)
.with_compression_threads(args.compression_threads)
.with_reqwest_client(client)
.with_testing(!args.no_test)
.with_zstd_repodata_enabled(args.common.use_zstd)
.with_bz2_repodata_enabled(args.common.use_zstd)
.with_skip_existing(args.skip_existing)
.finish())
}

/// Returns the output for the build.
Expand Down Expand Up @@ -331,14 +330,6 @@ pub async fn get_build_output(
),
};

if args.render_only && args.with_solve {
let output_with_resolved_dependencies = output
.resolve_dependencies(tool_config)
.await
.into_diagnostic()?;
outputs.push(output_with_resolved_dependencies);
continue;
}
outputs.push(output);
}

Expand Down Expand Up @@ -385,20 +376,24 @@ pub async fn run_test_from_args(
fancy_log_handler: LoggingOutputHandler,
) -> miette::Result<()> {
let package_file = canonicalize(args.package_file).into_diagnostic()?;
let client = tool_configuration::reqwest_client_from_auth_storage(args.common.auth_file)
.into_diagnostic()?;

let channel_config = ChannelConfig::default_with_root_dir(
std::env::current_dir()
.into_diagnostic()
.context("failed to determine the current directory")?,
);
let tool_config = Configuration::builder()
.with_logging_output_handler(fancy_log_handler)
.with_keep_build(true)
.with_compression_threads(args.compression_threads)
.with_reqwest_client(
tool_configuration::reqwest_client_from_auth_storage(args.common.auth_file)
.into_diagnostic()?,
)
.with_zstd_repodata_enabled(args.common.use_zstd)
.with_bz2_repodata_enabled(args.common.use_zstd)
.finish();

let channels = args
.channel
.unwrap_or_else(|| vec!["conda-forge".to_string()])
.into_iter()
.map(|name| Channel::from_str(name, &channel_config).map(|c| c.base_url))
.map(|name| Channel::from_str(name, &tool_config.channel_config).map(|c| c.base_url))
.collect::<Result<Vec<_>, _>>()
.into_diagnostic()?;

Expand All @@ -411,14 +406,7 @@ pub async fn run_test_from_args(
channels,
channel_priority: ChannelPriority::Strict,
solve_strategy: SolveStrategy::Highest,
tool_configuration: Configuration {
client,
fancy_log_handler,
// duplicate from `keep_test_prefix`?
no_clean: false,
compression_threads: args.compression_threads,
..Configuration::default()
},
tool_configuration: tool_config,
};

let package_name = package_file
Expand Down Expand Up @@ -471,19 +459,18 @@ pub async fn rebuild_from_args(
output.build_configuration.directories.output_dir =
canonicalize(output_dir).into_diagnostic()?;

let client = tool_configuration::reqwest_client_from_auth_storage(args.common.auth_file)
.into_diagnostic()?;

let tool_config = tool_configuration::Configuration {
client,
fancy_log_handler,
no_clean: true,
no_test: args.no_test,
use_zstd: args.common.use_zstd,
use_bz2: args.common.use_bz2,
compression_threads: args.compression_threads,
..Configuration::default()
};
let tool_config = Configuration::builder()
.with_logging_output_handler(fancy_log_handler)
.with_keep_build(true)
.with_compression_threads(args.compression_threads)
.with_reqwest_client(
tool_configuration::reqwest_client_from_auth_storage(args.common.auth_file)
.into_diagnostic()?,
)
.with_testing(!args.no_test)
.with_zstd_repodata_enabled(args.common.use_zstd)
.with_bz2_repodata_enabled(args.common.use_zstd)
.finish();

output
.build_configuration
Expand Down
15 changes: 15 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ async fn main() -> miette::Result<()> {
}

if build_args.render_only {
let outputs = if build_args.with_solve {
let mut updated_outputs = Vec::new();
for output in outputs {
updated_outputs.push(
output
.resolve_dependencies(&tool_config)
.await
.into_diagnostic()?,
);
}
updated_outputs
} else {
outputs
};

println!(
"{}",
serde_json::to_string_pretty(&outputs).into_diagnostic()?
Expand Down
Loading

0 comments on commit 91ca7d3

Please sign in to comment.