Use dummy reporter instead of option reporter

I'm experimenting with getting with of the `if let Some(reporter) = self.reporter {...}` and `with_reporter()` methods. Added bonus is that we see where we're currently loosing information, as we currently do in `BuildDispatch::install`.
This commit is contained in:
konstin 2023-12-15 10:32:35 +01:00
parent a361ccfbb3
commit 095f033b61
5 changed files with 21 additions and 22 deletions

View file

@ -395,9 +395,9 @@ async fn install(
let wheels = wheels.into_iter().chain(local).collect::<Vec<_>>();
if !wheels.is_empty() {
let start = std::time::Instant::now();
puffin_installer::Installer::new(venv)
let reporter = InstallReporter::from(printer).with_length(wheels.len() as u64);
puffin_installer::Installer::new(venv, reporter)
.with_link_mode(link_mode)
.with_reporter(InstallReporter::from(printer).with_length(wheels.len() as u64))
.install(&wheels)?;
let s = if wheels.len() == 1 { "" } else { "s" };

View file

@ -245,9 +245,9 @@ pub(crate) async fn sync_requirements(
let wheels = wheels.into_iter().chain(local).collect::<Vec<_>>();
if !wheels.is_empty() {
let start = std::time::Instant::now();
puffin_installer::Installer::new(&venv)
let reporter = InstallReporter::from(printer).with_length(wheels.len() as u64);
puffin_installer::Installer::new(&venv, reporter)
.with_link_mode(link_mode)
.with_reporter(InstallReporter::from(printer).with_length(wheels.len() as u64))
.install(&wheels)?;
let s = if wheels.len() == 1 { "" } else { "s" };

View file

@ -16,6 +16,7 @@ use platform_tags::Tags;
use puffin_build::{BuildKind, SourceBuild, SourceBuildContext};
use puffin_cache::Cache;
use puffin_client::RegistryClient;
use puffin_installer::InstallDummyReporter;
use puffin_installer::{Downloader, InstallPlan, Installer, Reinstall};
use puffin_interpreter::{Interpreter, Virtualenv};
use puffin_resolver::{DistFinder, Manifest, ResolutionOptions, Resolver};
@ -205,7 +206,7 @@ impl BuildContext for BuildDispatch {
if wheels.len() == 1 { "" } else { "s" },
wheels.iter().map(ToString::to_string).join(", ")
);
Installer::new(venv)
Installer::new(venv, InstallDummyReporter)
.install(&wheels)
.context("Failed to install build dependencies")?;
}
@ -214,7 +215,7 @@ impl BuildContext for BuildDispatch {
})
}
#[instrument(skip_all, fields(source_dist = source_dist, subdirectory = ?subdirectory))]
#[instrument(skip_all, fields(source_dist = source_dist, subdirectory = ? subdirectory))]
fn setup_build<'a>(
&'a self,
source: &'a Path,

View file

@ -7,16 +7,16 @@ use puffin_interpreter::Virtualenv;
pub struct Installer<'a> {
venv: &'a Virtualenv,
link_mode: install_wheel_rs::linker::LinkMode,
reporter: Option<Box<dyn Reporter>>,
reporter: Box<dyn Reporter>,
}
impl<'a> Installer<'a> {
/// Initialize a new installer.
pub fn new(venv: &'a Virtualenv) -> Self {
pub fn new(venv: &'a Virtualenv, reporter: impl Reporter + 'static) -> Self {
Self {
venv,
link_mode: install_wheel_rs::linker::LinkMode::default(),
reporter: None,
reporter: Box::new(reporter),
}
}
@ -26,15 +26,6 @@ impl<'a> Installer<'a> {
Self { link_mode, ..self }
}
/// Set the [`Reporter`] to use for this installer.
#[must_use]
pub fn with_reporter(self, reporter: impl Reporter + 'static) -> Self {
Self {
reporter: Some(Box::new(reporter)),
..self
}
}
/// Install a set of wheels into a Python virtual environment.
pub fn install(self, wheels: &[CachedDist]) -> Result<()> {
tokio::task::block_in_place(|| {
@ -57,9 +48,7 @@ impl<'a> Installer<'a> {
)
.with_context(|| format!("Failed to install: {wheel}"))?;
if let Some(reporter) = self.reporter.as_ref() {
reporter.on_install_progress(wheel);
}
self.reporter.on_install_progress(wheel);
Ok::<(), Error>(())
})
@ -74,3 +63,10 @@ pub trait Reporter: Send + Sync {
/// Callback to invoke when the resolution is complete.
fn on_install_complete(&self);
}
pub struct DummyReporter;
impl Reporter for DummyReporter {
fn on_install_progress(&self, _wheel: &CachedDist) {}
fn on_install_complete(&self) {}
}

View file

@ -1,5 +1,7 @@
pub use downloader::{Downloader, Reporter as DownloadReporter};
pub use installer::{Installer, Reporter as InstallReporter};
pub use installer::{
DummyReporter as InstallDummyReporter, Installer, Reporter as InstallReporter,
};
pub use plan::{InstallPlan, Reinstall};
pub use site_packages::SitePackages;
pub use uninstall::uninstall;