Rename Distribution to Dist in all structs and traits (#384)

We tend to avoid abbreviations, but this one is just so long and
absolutely ubiquitous.
This commit is contained in:
Charlie Marsh 2023-11-10 06:55:11 -08:00 committed by GitHub
parent 5cef40d87a
commit 6a15950cb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
42 changed files with 471 additions and 548 deletions

View file

@ -1,7 +1,5 @@
pub use source_distribution::{
SourceDistributionExtension, SourceDistributionFilename, SourceDistributionFilenameError,
};
pub use source_dist::{SourceDistExtension, SourceDistFilename, SourceDistFilenameError};
pub use wheel::{WheelFilename, WheelFilenameError};
mod source_distribution;
mod source_dist;
mod wheel;

View file

@ -4,7 +4,7 @@ expression: "WheelFilename::from_str(\"foo-1.2.3-build-python-abi-platform.whl\"
---
Ok(
WheelFilename {
distribution: PackageName(
name: PackageName(
"foo",
),
version: Version {

View file

@ -4,7 +4,7 @@ expression: "WheelFilename::from_str(\"foo-1.2.3-ab.cd.ef-gh-ij.kl.mn.op.qr.st.w
---
Ok(
WheelFilename {
distribution: PackageName(
name: PackageName(
"foo",
),
version: Version {

View file

@ -4,7 +4,7 @@ expression: "WheelFilename::from_str(\"foo-1.2.3-foo-bar-baz.whl\")"
---
Ok(
WheelFilename {
distribution: PackageName(
name: PackageName(
"foo",
),
version: Version {

View file

@ -7,37 +7,33 @@ use pep440_rs::Version;
use puffin_normalize::{InvalidNameError, PackageName};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SourceDistributionExtension {
pub enum SourceDistExtension {
Zip,
TarGz,
}
impl FromStr for SourceDistributionExtension {
type Err = SourceDistributionFilenameError;
impl FromStr for SourceDistExtension {
type Err = SourceDistFilenameError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"zip" => Self::Zip,
"tar.gz" => Self::TarGz,
other => {
return Err(SourceDistributionFilenameError::InvalidExtension(
other.to_string(),
))
}
other => return Err(SourceDistFilenameError::InvalidExtension(other.to_string())),
})
}
}
impl Display for SourceDistributionExtension {
impl Display for SourceDistExtension {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
SourceDistributionExtension::Zip => f.write_str("zip"),
SourceDistributionExtension::TarGz => f.write_str("tar.gz"),
SourceDistExtension::Zip => f.write_str("zip"),
SourceDistExtension::TarGz => f.write_str("tar.gz"),
}
}
}
impl SourceDistributionExtension {
impl SourceDistExtension {
pub fn from_filename(filename: &str) -> Option<(&str, Self)> {
if let Some(stem) = filename.strip_suffix(".zip") {
return Some((stem, Self::Zip));
@ -52,37 +48,37 @@ impl SourceDistributionExtension {
/// Note that this is a normalized and not an exact representation, keep the original string if you
/// need the latter.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SourceDistributionFilename {
pub struct SourceDistFilename {
pub name: PackageName,
pub version: Version,
pub extension: SourceDistributionExtension,
pub extension: SourceDistExtension,
}
impl SourceDistributionFilename {
impl SourceDistFilename {
/// No `FromStr` impl since we need to know the package name to be able to reasonable parse
/// these (consider e.g. `a-1-1.zip`)
pub fn parse(
filename: &str,
package_name: &PackageName,
) -> Result<Self, SourceDistributionFilenameError> {
let Some((stem, extension)) = SourceDistributionExtension::from_filename(filename) else {
return Err(SourceDistributionFilenameError::InvalidExtension(
) -> Result<Self, SourceDistFilenameError> {
let Some((stem, extension)) = SourceDistExtension::from_filename(filename) else {
return Err(SourceDistFilenameError::InvalidExtension(
filename.to_string(),
));
};
if stem.len() <= package_name.as_ref().len() + "-".len() {
return Err(SourceDistributionFilenameError::InvalidFilename {
return Err(SourceDistFilenameError::InvalidFilename {
filename: filename.to_string(),
package_name: package_name.to_string(),
});
}
let actual_package_name = PackageName::from_str(&stem[..package_name.as_ref().len()])
.map_err(|err| {
SourceDistributionFilenameError::InvalidPackageName(filename.to_string(), err)
SourceDistFilenameError::InvalidPackageName(filename.to_string(), err)
})?;
if &actual_package_name != package_name {
return Err(SourceDistributionFilenameError::InvalidFilename {
return Err(SourceDistFilenameError::InvalidFilename {
filename: filename.to_string(),
package_name: package_name.to_string(),
});
@ -90,7 +86,7 @@ impl SourceDistributionFilename {
// We checked the length above
let version = Version::from_str(&stem[package_name.as_ref().len() + "-".len()..])
.map_err(SourceDistributionFilenameError::InvalidVersion)?;
.map_err(SourceDistFilenameError::InvalidVersion)?;
Ok(Self {
name: package_name.clone(),
@ -100,14 +96,14 @@ impl SourceDistributionFilename {
}
}
impl Display for SourceDistributionFilename {
impl Display for SourceDistFilename {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}-{}.{}", self.name, self.version, self.extension)
}
}
#[derive(Error, Debug, Clone)]
pub enum SourceDistributionFilenameError {
pub enum SourceDistFilenameError {
#[error("Source distribution name {filename} doesn't start with package name {package_name}")]
InvalidFilename {
filename: String,
@ -126,7 +122,7 @@ mod tests {
use puffin_normalize::PackageName;
use std::str::FromStr;
use crate::SourceDistributionFilename;
use crate::SourceDistFilename;
/// Only test already normalized names since the parsing is lossy
#[test]
@ -137,12 +133,9 @@ mod tests {
"foo-lib-1.2.3.tar.gz",
] {
assert_eq!(
SourceDistributionFilename::parse(
normalized,
&PackageName::from_str("foo_lib").unwrap()
)
.unwrap()
.to_string(),
SourceDistFilename::parse(normalized, &PackageName::from_str("foo_lib").unwrap())
.unwrap()
.to_string(),
normalized
);
}
@ -151,20 +144,17 @@ mod tests {
#[test]
fn errors() {
for invalid in ["b-1.2.3.zip", "a-1.2.3-gamma.3.zip", "a-1.2.3.tar.zstd"] {
assert!(SourceDistributionFilename::parse(
invalid,
&PackageName::from_str("a").unwrap()
)
.is_err());
assert!(
SourceDistFilename::parse(invalid, &PackageName::from_str("a").unwrap()).is_err()
);
}
}
#[test]
fn name_to_long() {
assert!(SourceDistributionFilename::parse(
"foo.zip",
&PackageName::from_str("foo-lib").unwrap()
)
.is_err());
assert!(
SourceDistFilename::parse("foo.zip", &PackageName::from_str("foo-lib").unwrap())
.is_err()
);
}
}

View file

@ -10,7 +10,7 @@ use puffin_normalize::{InvalidNameError, PackageName};
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct WheelFilename {
pub distribution: PackageName,
pub name: PackageName,
pub version: Version,
pub python_tag: Vec<String>,
pub abi_tag: Vec<String>,
@ -39,7 +39,7 @@ impl FromStr for WheelFilename {
// if this is a problem in practice.
let mut parts = basename.split('-');
let distribution = parts
let name = parts
.next()
.expect("split always yields 1 or more elements");
@ -71,7 +71,7 @@ impl FromStr for WheelFilename {
));
};
let (distribution, version, python_tag, abi_tag, platform_tag) =
let (name, version, python_tag, abi_tag, platform_tag) =
if let Some(platform_tag) = parts.next() {
if parts.next().is_some() {
return Err(WheelFilenameError::InvalidWheelFileName(
@ -80,7 +80,7 @@ impl FromStr for WheelFilename {
));
}
(
distribution,
name,
version,
python_tag_or_abi_tag,
abi_tag_or_platform_tag,
@ -88,7 +88,7 @@ impl FromStr for WheelFilename {
)
} else {
(
distribution,
name,
version,
build_tag_or_python_tag,
python_tag_or_abi_tag,
@ -96,12 +96,12 @@ impl FromStr for WheelFilename {
)
};
let distribution = PackageName::from_str(distribution)
let name = PackageName::from_str(name)
.map_err(|err| WheelFilenameError::InvalidPackageName(filename.to_string(), err))?;
let version = Version::from_str(version)
.map_err(|err| WheelFilenameError::InvalidVersion(filename.to_string(), err))?;
Ok(WheelFilename {
distribution,
name,
version,
python_tag: python_tag.split('.').map(String::from).collect(),
abi_tag: abi_tag.split('.').map(String::from).collect(),
@ -112,13 +112,7 @@ impl FromStr for WheelFilename {
impl Display for WheelFilename {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}-{}-{}.whl",
self.distribution,
self.version,
self.get_tag()
)
write!(f, "{}-{}-{}.whl", self.name, self.version, self.get_tag())
}
}
@ -140,7 +134,7 @@ impl WheelFilename {
/// The wheel filename without the extension
pub fn stem(&self) -> String {
format!("{}-{}-{}", self.distribution, self.version, self.get_tag())
format!("{}-{}-{}", self.name, self.version, self.get_tag())
}
}

View file

@ -90,7 +90,7 @@ pub fn find_dist_info<'a, T: Copy>(
let (dist_info_dir, file) = path.split_once('/')?;
let dir_stem = dist_info_dir.strip_suffix(".dist-info")?;
let (name, version) = dir_stem.rsplit_once('-')?;
if PackageName::from_str(name).ok()? == filename.distribution
if PackageName::from_str(name).ok()? == filename.name
&& Version::from_str(version).ok()? == filename.version
&& file == "METADATA"
{

View file

@ -904,7 +904,7 @@ pub fn install_wheel(
_extras: &[String],
sys_executable: impl AsRef<Path>,
) -> Result<String, Error> {
let name = &filename.distribution;
let name = &filename.name;
let _my_span = span!(Level::DEBUG, "install_wheel", name = name.as_ref());
let base_location = location.venv_root();

View file

@ -47,7 +47,7 @@ pub enum Error {
#[error("Unsupported archive format (extension not recognized): {0}")]
UnsupportedArchiveType(String),
#[error("Invalid source distribution: {0}")]
InvalidSourceDistribution(String),
InvalidSourceDist(String),
#[error("Invalid pyproject.toml")]
InvalidPyprojectToml(#[from] toml::de::Error),
#[error("Failed to install requirements from {0}")]
@ -331,7 +331,7 @@ impl SourceBuild {
.await?;
} else {
if !source_tree.join("setup.py").is_file() {
return Err(Error::InvalidSourceDistribution(
return Err(Error::InvalidSourceDist(
"The archive contains neither a pyproject.toml or a setup.py at the top level"
.to_string(),
));
@ -517,6 +517,11 @@ async fn create_pep517_build_environment(
"Calling `{}.get_requires_for_build_wheel()`",
pep517_backend.backend
);
if pep517_backend.backend_path.is_some() {
return Err(Error::InvalidSourceDist(
"backend-path is not supported yet".to_string(),
));
}
let script = formatdoc! {
r#"
{}
@ -621,7 +626,7 @@ fn extract_archive(sdist: &Path, extracted: &PathBuf) -> Result<PathBuf, Error>
// TODO(konstin): Verify the name of the directory
let top_level = fs::read_dir(extracted)?.collect::<io::Result<Vec<DirEntry>>>()?;
let [root] = top_level.as_slice() else {
return Err(Error::InvalidSourceDistribution(format!(
return Err(Error::InvalidSourceDist(format!(
"The top level of the archive must only contain a list directory, but it contains {top_level:?}"
)));
};

View file

@ -22,10 +22,10 @@ pub(crate) fn freeze(cache: &Path, _printer: Printer) -> Result<ExitStatus> {
// Build the installed index.
let site_packages = SitePackages::try_from_executable(&python)?;
for distribution in site_packages.distributions() {
for dist in site_packages.distributions() {
#[allow(clippy::print_stdout)]
{
println!("{distribution}");
println!("{dist}");
}
}

View file

@ -13,7 +13,7 @@ use platform_host::Platform;
use platform_tags::Tags;
use puffin_client::RegistryClientBuilder;
use puffin_dispatch::BuildDispatch;
use puffin_distribution::{AnyDistribution, BaseDistribution};
use puffin_distribution::{AnyDist, Metadata};
use puffin_installer::{Builder, InstallPlan};
use puffin_interpreter::Virtualenv;
@ -128,7 +128,7 @@ pub(crate) async fn sync_requirements(
} else {
let start = std::time::Instant::now();
let wheel_finder = puffin_resolver::DistributionFinder::new(&tags, &client)
let wheel_finder = puffin_resolver::DistFinder::new(&tags, &client)
.with_reporter(FinderReporter::from(printer).with_length(remote.len() as u64));
let resolution = wheel_finder.resolve(&remote).await?;
@ -182,7 +182,7 @@ pub(crate) async fn sync_requirements(
.into_iter()
.partition_map(|download| match download {
puffin_installer::Download::Wheel(wheel) => Either::Left(wheel),
puffin_installer::Download::SourceDistribution(sdist) => Either::Right(sdist),
puffin_installer::Download::SourceDist(sdist) => Either::Right(sdist),
});
// Build any missing source distributions.
@ -307,14 +307,14 @@ pub(crate) async fn sync_requirements(
for event in extraneous
.into_iter()
.map(|distribution| ChangeEvent {
distribution: AnyDistribution::from(distribution),
dist: AnyDist::from(distribution),
kind: ChangeEventKind::Remove,
})
.chain(wheels.into_iter().map(|distribution| ChangeEvent {
distribution: AnyDistribution::from(distribution),
dist: AnyDist::from(distribution),
kind: ChangeEventKind::Add,
}))
.sorted_unstable_by_key(|event| event.distribution.name().clone())
.sorted_unstable_by_key(|event| event.dist.name().clone())
{
match event.kind {
ChangeEventKind::Add => {
@ -322,8 +322,8 @@ pub(crate) async fn sync_requirements(
printer,
" {} {}{}",
"+".green(),
event.distribution.name().as_ref().white().bold(),
event.distribution.version_or_url().to_string().dimmed()
event.dist.name().as_ref().white().bold(),
event.dist.version_or_url().to_string().dimmed()
)?;
}
ChangeEventKind::Remove => {
@ -331,8 +331,8 @@ pub(crate) async fn sync_requirements(
printer,
" {} {}{}",
"-".red(),
event.distribution.name().as_ref().white().bold(),
event.distribution.version_or_url().to_string().dimmed()
event.dist.name().as_ref().white().bold(),
event.dist.version_or_url().to_string().dimmed()
)?;
}
}
@ -351,6 +351,6 @@ enum ChangeEventKind {
#[derive(Debug)]
struct ChangeEvent {
distribution: AnyDistribution,
dist: AnyDist,
kind: ChangeEventKind,
}

View file

@ -6,7 +6,7 @@ use colored::Colorize;
use tracing::debug;
use platform_host::Platform;
use puffin_distribution::BaseDistribution;
use puffin_distribution::Metadata;
use puffin_interpreter::Virtualenv;
use crate::commands::{elapsed, ExitStatus};

View file

@ -5,9 +5,7 @@ use std::time::Duration;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use url::Url;
use puffin_distribution::{
BaseDistribution, CachedDistribution, Distribution, SourceDistribution, VersionOrUrl,
};
use puffin_distribution::{CachedDist, Dist, Metadata, SourceDist, VersionOrUrl};
use puffin_installer::Download;
use puffin_normalize::ExtraName;
use puffin_normalize::PackageName;
@ -39,7 +37,7 @@ impl FinderReporter {
}
impl puffin_resolver::FinderReporter for FinderReporter {
fn on_progress(&self, wheel: &Distribution) {
fn on_progress(&self, wheel: &Dist) {
self.progress.set_message(format!("{wheel}"));
self.progress.inc(1);
}
@ -74,7 +72,7 @@ impl UnzipReporter {
}
impl puffin_installer::UnzipReporter for UnzipReporter {
fn on_unzip_progress(&self, wheel: &Distribution) {
fn on_unzip_progress(&self, wheel: &Dist) {
self.progress.set_message(format!("{wheel}"));
self.progress.inc(1);
}
@ -144,7 +142,7 @@ impl InstallReporter {
}
impl puffin_installer::InstallReporter for InstallReporter {
fn on_install_progress(&self, wheel: &CachedDistribution) {
fn on_install_progress(&self, wheel: &CachedDist) {
self.progress.set_message(format!("{wheel}"));
self.progress.inc(1);
}
@ -179,7 +177,7 @@ impl BuildReporter {
}
impl puffin_installer::BuildReporter for BuildReporter {
fn on_progress(&self, wheel: &Distribution) {
fn on_progress(&self, wheel: &Dist) {
self.progress.set_message(format!("{wheel}"));
self.progress.inc(1);
}
@ -248,7 +246,7 @@ impl puffin_resolver::ResolverReporter for ResolverReporter {
self.progress.finish_and_clear();
}
fn on_build_start(&self, distribution: &SourceDistribution) -> usize {
fn on_build_start(&self, dist: &SourceDist) -> usize {
let progress = self.multi_progress.insert_before(
&self.progress,
ProgressBar::with_draw_target(None, self.printer.target()),
@ -258,7 +256,7 @@ impl puffin_resolver::ResolverReporter for ResolverReporter {
progress.set_message(format!(
"{} {}",
"Building".bold().green(),
distribution.to_color_string()
dist.to_color_string()
));
let mut bars = self.bars.lock().unwrap();
@ -266,13 +264,13 @@ impl puffin_resolver::ResolverReporter for ResolverReporter {
bars.len() - 1
}
fn on_build_complete(&self, distribution: &SourceDistribution, index: usize) {
fn on_build_complete(&self, dist: &SourceDist, index: usize) {
let bars = self.bars.lock().unwrap();
let progress = &bars[index];
progress.finish_with_message(format!(
"{} {}",
"Built".bold().green(),
distribution.to_color_string()
dist.to_color_string()
));
}
@ -313,7 +311,7 @@ trait ColorDisplay {
fn to_color_string(&self) -> String;
}
impl ColorDisplay for &SourceDistribution {
impl ColorDisplay for &SourceDist {
fn to_color_string(&self) -> String {
let name = self.name();
let version_or_url = self.version_or_url();

View file

@ -15,10 +15,10 @@ use pep508_rs::Requirement;
use platform_tags::Tags;
use puffin_build::{SourceBuild, SourceBuildContext};
use puffin_client::RegistryClient;
use puffin_distribution::BaseDistribution;
use puffin_distribution::Metadata;
use puffin_installer::{Builder, Downloader, InstallPlan, Installer, Unzipper};
use puffin_interpreter::{InterpreterInfo, Virtualenv};
use puffin_resolver::{DistributionFinder, Manifest, PreReleaseMode, ResolutionMode, Resolver};
use puffin_resolver::{DistFinder, Manifest, PreReleaseMode, ResolutionMode, Resolver};
use puffin_traits::BuildContext;
/// The main implementation of [`BuildContext`], used by the CLI, see [`BuildContext`]
@ -135,7 +135,7 @@ impl BuildContext for BuildDispatch {
if remote.len() == 1 { "" } else { "s" },
remote.iter().map(ToString::to_string).join(", ")
);
let resolution = DistributionFinder::new(&tags, &self.client)
let resolution = DistFinder::new(&tags, &self.client)
.resolve(&remote)
.await
.context("Failed to resolve build dependencies")?;
@ -163,9 +163,7 @@ impl BuildContext for BuildDispatch {
.into_iter()
.partition_map(|download| match download {
puffin_installer::Download::Wheel(wheel) => Either::Left(wheel),
puffin_installer::Download::SourceDistribution(sdist) => {
Either::Right(sdist)
}
puffin_installer::Download::SourceDist(sdist) => Either::Right(sdist),
});
// Build any missing source distributions.

View file

@ -1,19 +1,19 @@
use puffin_normalize::PackageName;
use crate::cached::CachedDistribution;
use crate::installed::InstalledDistribution;
use crate::traits::BaseDistribution;
use crate::{Distribution, VersionOrUrl};
use crate::cached::CachedDist;
use crate::installed::InstalledDist;
use crate::traits::Metadata;
use crate::{Dist, VersionOrUrl};
/// A distribution which either exists remotely or locally.
#[derive(Debug, Clone)]
pub enum AnyDistribution {
Remote(Distribution),
Cached(CachedDistribution),
Installed(InstalledDistribution),
pub enum AnyDist {
Remote(Dist),
Cached(CachedDist),
Installed(InstalledDist),
}
impl BaseDistribution for AnyDistribution {
impl Metadata for AnyDist {
fn name(&self) -> &PackageName {
match self {
Self::Remote(dist) => dist.name(),
@ -31,20 +31,20 @@ impl BaseDistribution for AnyDistribution {
}
}
impl From<Distribution> for AnyDistribution {
fn from(dist: Distribution) -> Self {
impl From<Dist> for AnyDist {
fn from(dist: Dist) -> Self {
Self::Remote(dist)
}
}
impl From<CachedDistribution> for AnyDistribution {
fn from(dist: CachedDistribution) -> Self {
impl From<CachedDist> for AnyDist {
fn from(dist: CachedDist) -> Self {
Self::Cached(dist)
}
}
impl From<InstalledDistribution> for AnyDistribution {
fn from(dist: InstalledDistribution) -> Self {
impl From<InstalledDist> for AnyDist {
fn from(dist: InstalledDist) -> Self {
Self::Installed(dist)
}
}

View file

@ -4,8 +4,8 @@ use std::str::FromStr;
use anyhow::{anyhow, Result};
use url::Url;
use crate::traits::BaseDistribution;
use crate::{BuiltDistribution, Distribution, SourceDistribution, VersionOrUrl};
use crate::traits::Metadata;
use crate::{BuiltDist, Dist, SourceDist, VersionOrUrl};
use pep440_rs::Version;
use puffin_normalize::PackageName;
@ -13,28 +13,28 @@ use crate::direct_url::DirectUrl;
/// A built distribution (wheel) that exists in a local cache.
#[derive(Debug, Clone)]
pub enum CachedDistribution {
pub enum CachedDist {
/// The distribution exists in a registry, like `PyPI`.
Registry(CachedRegistryDistribution),
Registry(CachedRegistryDist),
/// The distribution exists at an arbitrary URL.
Url(CachedDirectUrlDistribution),
Url(CachedDirectUrlDist),
}
#[derive(Debug, Clone)]
pub struct CachedRegistryDistribution {
pub struct CachedRegistryDist {
pub name: PackageName,
pub version: Version,
pub path: PathBuf,
}
#[derive(Debug, Clone)]
pub struct CachedDirectUrlDistribution {
pub struct CachedDirectUrlDist {
pub name: PackageName,
pub url: Url,
pub path: PathBuf,
}
impl BaseDistribution for CachedRegistryDistribution {
impl Metadata for CachedRegistryDist {
fn name(&self) -> &PackageName {
&self.name
}
@ -44,7 +44,7 @@ impl BaseDistribution for CachedRegistryDistribution {
}
}
impl BaseDistribution for CachedDirectUrlDistribution {
impl Metadata for CachedDirectUrlDist {
fn name(&self) -> &PackageName {
&self.name
}
@ -54,7 +54,7 @@ impl BaseDistribution for CachedDirectUrlDistribution {
}
}
impl BaseDistribution for CachedDistribution {
impl Metadata for CachedDist {
fn name(&self) -> &PackageName {
match self {
Self::Registry(dist) => dist.name(),
@ -70,45 +70,35 @@ impl BaseDistribution for CachedDistribution {
}
}
impl CachedDistribution {
/// Initialize a [`CachedDistribution`] from a [`Distribution`].
pub fn from_remote(remote: Distribution, path: PathBuf) -> Self {
impl CachedDist {
/// Initialize a [`CachedDist`] from a [`Dist`].
pub fn from_remote(remote: Dist, path: PathBuf) -> Self {
match remote {
Distribution::Built(BuiltDistribution::Registry(dist)) => {
Self::Registry(CachedRegistryDistribution {
name: dist.name,
version: dist.version,
path,
})
}
Distribution::Built(BuiltDistribution::DirectUrl(dist)) => {
Self::Url(CachedDirectUrlDistribution {
name: dist.name,
url: dist.url,
path,
})
}
Distribution::Source(SourceDistribution::Registry(dist)) => {
Self::Registry(CachedRegistryDistribution {
name: dist.name,
version: dist.version,
path,
})
}
Distribution::Source(SourceDistribution::DirectUrl(dist)) => {
Self::Url(CachedDirectUrlDistribution {
name: dist.name,
url: dist.url,
path,
})
}
Distribution::Source(SourceDistribution::Git(dist)) => {
Self::Url(CachedDirectUrlDistribution {
name: dist.name,
url: dist.url,
path,
})
}
Dist::Built(BuiltDist::Registry(dist)) => Self::Registry(CachedRegistryDist {
name: dist.name,
version: dist.version,
path,
}),
Dist::Built(BuiltDist::DirectUrl(dist)) => Self::Url(CachedDirectUrlDist {
name: dist.name,
url: dist.url,
path,
}),
Dist::Source(SourceDist::Registry(dist)) => Self::Registry(CachedRegistryDist {
name: dist.name,
version: dist.version,
path,
}),
Dist::Source(SourceDist::DirectUrl(dist)) => Self::Url(CachedDirectUrlDist {
name: dist.name,
url: dist.url,
path,
}),
Dist::Source(SourceDist::Git(dist)) => Self::Url(CachedDirectUrlDist {
name: dist.name,
url: dist.url,
path,
}),
}
}
@ -123,19 +113,19 @@ impl CachedDistribution {
/// Return the [`DirectUrl`] of the distribution, if it exists.
pub fn direct_url(&self) -> Result<Option<DirectUrl>> {
match self {
CachedDistribution::Registry(_) => Ok(None),
CachedDistribution::Url(dist) => DirectUrl::try_from(&dist.url).map(Some),
CachedDist::Registry(_) => Ok(None),
CachedDist::Url(dist) => DirectUrl::try_from(&dist.url).map(Some),
}
}
}
impl CachedDirectUrlDistribution {
impl CachedDirectUrlDist {
pub fn from_url(name: PackageName, url: Url, path: PathBuf) -> Self {
Self { name, url, path }
}
}
impl CachedRegistryDistribution {
impl CachedRegistryDist {
/// Try to parse a distribution from a cached directory name (like `django-5.0a1`).
pub fn try_from_path(path: &Path) -> Result<Option<Self>> {
let Some(file_name) = path.file_name() else {

View file

@ -7,33 +7,33 @@ use pep440_rs::Version;
use puffin_normalize::PackageName;
use pypi_types::DirectUrl;
use crate::{BaseDistribution, VersionOrUrl};
use crate::{Metadata, VersionOrUrl};
/// A built distribution (wheel) that exists in a virtual environment.
#[derive(Debug, Clone)]
pub enum InstalledDistribution {
pub enum InstalledDist {
/// The distribution was derived from a registry, like `PyPI`.
Registry(InstalledRegistryDistribution),
Registry(InstalledRegistryDist),
/// The distribution was derived from an arbitrary URL.
Url(InstalledDirectUrlDistribution),
Url(InstalledDirectUrlDist),
}
#[derive(Debug, Clone)]
pub struct InstalledRegistryDistribution {
pub struct InstalledRegistryDist {
pub name: PackageName,
pub version: Version,
pub path: PathBuf,
}
#[derive(Debug, Clone)]
pub struct InstalledDirectUrlDistribution {
pub struct InstalledDirectUrlDist {
pub name: PackageName,
pub version: Version,
pub url: DirectUrl,
pub path: PathBuf,
}
impl BaseDistribution for InstalledRegistryDistribution {
impl Metadata for InstalledRegistryDist {
fn name(&self) -> &PackageName {
&self.name
}
@ -43,7 +43,7 @@ impl BaseDistribution for InstalledRegistryDistribution {
}
}
impl BaseDistribution for InstalledDirectUrlDistribution {
impl Metadata for InstalledDirectUrlDist {
fn name(&self) -> &PackageName {
&self.name
}
@ -54,7 +54,7 @@ impl BaseDistribution for InstalledDirectUrlDistribution {
}
}
impl BaseDistribution for InstalledDistribution {
impl Metadata for InstalledDist {
fn name(&self) -> &PackageName {
match self {
Self::Registry(dist) => dist.name(),
@ -70,7 +70,7 @@ impl BaseDistribution for InstalledDistribution {
}
}
impl InstalledDistribution {
impl InstalledDist {
/// Try to parse a distribution from a `.dist-info` directory name (like `django-5.0a1.dist-info`).
///
/// See: <https://packaging.python.org/en/latest/specifications/recording-installed-packages/#recording-installed-packages>
@ -89,14 +89,14 @@ impl InstalledDistribution {
let name = PackageName::from_str(name)?;
let version = Version::from_str(version).map_err(|err| anyhow!(err))?;
return if let Some(direct_url) = Self::direct_url(path)? {
Ok(Some(Self::Url(InstalledDirectUrlDistribution {
Ok(Some(Self::Url(InstalledDirectUrlDist {
name,
version,
url: direct_url,
path: path.to_path_buf(),
})))
} else {
Ok(Some(Self::Registry(InstalledRegistryDistribution {
Ok(Some(Self::Registry(InstalledRegistryDist {
name,
version,
path: path.to_path_buf(),

View file

@ -36,29 +36,29 @@ impl std::fmt::Display for VersionOrUrl<'_> {
}
#[derive(Debug, Clone)]
pub enum Distribution {
Built(BuiltDistribution),
Source(SourceDistribution),
pub enum Dist {
Built(BuiltDist),
Source(SourceDist),
}
#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum BuiltDistribution {
Registry(RegistryBuiltDistribution),
DirectUrl(DirectUrlBuiltDistribution),
pub enum BuiltDist {
Registry(RegistryBuiltDist),
DirectUrl(DirectUrlBuiltDist),
}
#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum SourceDistribution {
Registry(RegistrySourceDistribution),
DirectUrl(DirectUrlSourceDistribution),
Git(GitSourceDistribution),
pub enum SourceDist {
Registry(RegistrySourceDist),
DirectUrl(DirectUrlSourceDist),
Git(GitSourceDist),
}
/// A built distribution (wheel) that exists in a registry, like `PyPI`.
#[derive(Debug, Clone)]
pub struct RegistryBuiltDistribution {
pub struct RegistryBuiltDist {
pub name: PackageName,
pub version: Version,
pub file: File,
@ -66,14 +66,14 @@ pub struct RegistryBuiltDistribution {
/// A built distribution (wheel) that exists at an arbitrary URL.
#[derive(Debug, Clone)]
pub struct DirectUrlBuiltDistribution {
pub struct DirectUrlBuiltDist {
pub name: PackageName,
pub url: Url,
}
/// A source distribution that exists in a registry, like `PyPI`.
#[derive(Debug, Clone)]
pub struct RegistrySourceDistribution {
pub struct RegistrySourceDist {
pub name: PackageName,
pub version: Version,
pub file: File,
@ -81,32 +81,32 @@ pub struct RegistrySourceDistribution {
/// A source distribution that exists at an arbitrary URL.
#[derive(Debug, Clone)]
pub struct DirectUrlSourceDistribution {
pub struct DirectUrlSourceDist {
pub name: PackageName,
pub url: Url,
}
/// A source distribution that exists in a Git repository.
#[derive(Debug, Clone)]
pub struct GitSourceDistribution {
pub struct GitSourceDist {
pub name: PackageName,
pub url: Url,
}
impl Distribution {
/// Create a [`Distribution`] for a registry-based distribution.
impl Dist {
/// Create a [`Dist`] for a registry-based distribution.
pub fn from_registry(name: PackageName, version: Version, file: File) -> Self {
if Path::new(&file.filename)
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("whl"))
{
Self::Built(BuiltDistribution::Registry(RegistryBuiltDistribution {
Self::Built(BuiltDist::Registry(RegistryBuiltDist {
name,
version,
file,
}))
} else {
Self::Source(SourceDistribution::Registry(RegistrySourceDistribution {
Self::Source(SourceDist::Registry(RegistrySourceDist {
name,
version,
file,
@ -114,28 +114,22 @@ impl Distribution {
}
}
/// Create a [`Distribution`] for a URL-based distribution.
/// Create a [`Dist`] for a URL-based distribution.
pub fn from_url(name: PackageName, url: Url) -> Self {
if url.scheme().starts_with("git+") {
Self::Source(SourceDistribution::Git(GitSourceDistribution { name, url }))
Self::Source(SourceDist::Git(GitSourceDist { name, url }))
} else if Path::new(url.path())
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("whl"))
{
Self::Built(BuiltDistribution::DirectUrl(DirectUrlBuiltDistribution {
name,
url,
}))
Self::Built(BuiltDist::DirectUrl(DirectUrlBuiltDist { name, url }))
} else {
Self::Source(SourceDistribution::DirectUrl(DirectUrlSourceDistribution {
name,
url,
}))
Self::Source(SourceDist::DirectUrl(DirectUrlSourceDist { name, url }))
}
}
}
impl BaseDistribution for RegistryBuiltDistribution {
impl Metadata for RegistryBuiltDist {
fn name(&self) -> &PackageName {
&self.name
}
@ -145,7 +139,7 @@ impl BaseDistribution for RegistryBuiltDistribution {
}
}
impl BaseDistribution for DirectUrlBuiltDistribution {
impl Metadata for DirectUrlBuiltDist {
fn name(&self) -> &PackageName {
&self.name
}
@ -155,7 +149,7 @@ impl BaseDistribution for DirectUrlBuiltDistribution {
}
}
impl BaseDistribution for RegistrySourceDistribution {
impl Metadata for RegistrySourceDist {
fn name(&self) -> &PackageName {
&self.name
}
@ -165,7 +159,7 @@ impl BaseDistribution for RegistrySourceDistribution {
}
}
impl BaseDistribution for DirectUrlSourceDistribution {
impl Metadata for DirectUrlSourceDist {
fn name(&self) -> &PackageName {
&self.name
}
@ -175,7 +169,7 @@ impl BaseDistribution for DirectUrlSourceDistribution {
}
}
impl BaseDistribution for GitSourceDistribution {
impl Metadata for GitSourceDist {
fn name(&self) -> &PackageName {
&self.name
}
@ -185,7 +179,7 @@ impl BaseDistribution for GitSourceDistribution {
}
}
impl BaseDistribution for SourceDistribution {
impl Metadata for SourceDist {
fn name(&self) -> &PackageName {
match self {
Self::Registry(dist) => dist.name(),
@ -203,7 +197,7 @@ impl BaseDistribution for SourceDistribution {
}
}
impl BaseDistribution for BuiltDistribution {
impl Metadata for BuiltDist {
fn name(&self) -> &PackageName {
match self {
Self::Registry(dist) => dist.name(),
@ -219,7 +213,7 @@ impl BaseDistribution for BuiltDistribution {
}
}
impl BaseDistribution for Distribution {
impl Metadata for Dist {
fn name(&self) -> &PackageName {
match self {
Self::Built(dist) => dist.name(),
@ -235,7 +229,7 @@ impl BaseDistribution for Distribution {
}
}
impl RemoteDistribution for RegistryBuiltDistribution {
impl RemoteSource for RegistryBuiltDist {
fn filename(&self) -> Result<&str> {
Ok(&self.file.filename)
}
@ -245,7 +239,7 @@ impl RemoteDistribution for RegistryBuiltDistribution {
}
}
impl RemoteDistribution for RegistrySourceDistribution {
impl RemoteSource for RegistrySourceDist {
fn filename(&self) -> Result<&str> {
Ok(&self.file.filename)
}
@ -255,7 +249,7 @@ impl RemoteDistribution for RegistrySourceDistribution {
}
}
impl RemoteDistribution for DirectUrlBuiltDistribution {
impl RemoteSource for DirectUrlBuiltDist {
fn filename(&self) -> Result<&str> {
self.url
.path_segments()
@ -273,7 +267,7 @@ impl RemoteDistribution for DirectUrlBuiltDistribution {
}
}
impl RemoteDistribution for DirectUrlSourceDistribution {
impl RemoteSource for DirectUrlSourceDist {
fn filename(&self) -> Result<&str> {
self.url
.path_segments()
@ -291,7 +285,7 @@ impl RemoteDistribution for DirectUrlSourceDistribution {
}
}
impl RemoteDistribution for GitSourceDistribution {
impl RemoteSource for GitSourceDist {
fn filename(&self) -> Result<&str> {
self.url
.path_segments()
@ -309,7 +303,7 @@ impl RemoteDistribution for GitSourceDistribution {
}
}
impl RemoteDistribution for SourceDistribution {
impl RemoteSource for SourceDist {
fn filename(&self) -> Result<&str> {
match self {
Self::Registry(dist) => dist.filename(),
@ -327,7 +321,7 @@ impl RemoteDistribution for SourceDistribution {
}
}
impl RemoteDistribution for BuiltDistribution {
impl RemoteSource for BuiltDist {
fn filename(&self) -> Result<&str> {
match self {
Self::Registry(dist) => dist.filename(),
@ -343,7 +337,7 @@ impl RemoteDistribution for BuiltDistribution {
}
}
impl RemoteDistribution for Distribution {
impl RemoteSource for Dist {
fn filename(&self) -> Result<&str> {
match self {
Self::Built(dist) => dist.filename(),
@ -359,7 +353,7 @@ impl RemoteDistribution for Distribution {
}
}
impl DistributionIdentifier for Url {
impl Identifier for Url {
fn distribution_id(&self) -> String {
puffin_cache::digest(&puffin_cache::CanonicalUrl::new(self))
}
@ -369,7 +363,7 @@ impl DistributionIdentifier for Url {
}
}
impl DistributionIdentifier for File {
impl Identifier for File {
fn distribution_id(&self) -> String {
self.hashes.sha256.clone()
}
@ -379,7 +373,7 @@ impl DistributionIdentifier for File {
}
}
impl DistributionIdentifier for RegistryBuiltDistribution {
impl Identifier for RegistryBuiltDist {
fn distribution_id(&self) -> String {
self.file.distribution_id()
}
@ -389,7 +383,7 @@ impl DistributionIdentifier for RegistryBuiltDistribution {
}
}
impl DistributionIdentifier for RegistrySourceDistribution {
impl Identifier for RegistrySourceDist {
fn distribution_id(&self) -> String {
self.file.distribution_id()
}
@ -399,7 +393,7 @@ impl DistributionIdentifier for RegistrySourceDistribution {
}
}
impl DistributionIdentifier for DirectUrlBuiltDistribution {
impl Identifier for DirectUrlBuiltDist {
fn distribution_id(&self) -> String {
self.url.distribution_id()
}
@ -409,7 +403,7 @@ impl DistributionIdentifier for DirectUrlBuiltDistribution {
}
}
impl DistributionIdentifier for DirectUrlSourceDistribution {
impl Identifier for DirectUrlSourceDist {
fn distribution_id(&self) -> String {
self.url.distribution_id()
}
@ -419,7 +413,7 @@ impl DistributionIdentifier for DirectUrlSourceDistribution {
}
}
impl DistributionIdentifier for GitSourceDistribution {
impl Identifier for GitSourceDist {
fn distribution_id(&self) -> String {
self.url.distribution_id()
}
@ -429,7 +423,7 @@ impl DistributionIdentifier for GitSourceDistribution {
}
}
impl DistributionIdentifier for SourceDistribution {
impl Identifier for SourceDist {
fn distribution_id(&self) -> String {
match self {
Self::Registry(dist) => dist.distribution_id(),
@ -447,7 +441,7 @@ impl DistributionIdentifier for SourceDistribution {
}
}
impl DistributionIdentifier for BuiltDistribution {
impl Identifier for BuiltDist {
fn distribution_id(&self) -> String {
match self {
Self::Registry(dist) => dist.distribution_id(),
@ -463,7 +457,7 @@ impl DistributionIdentifier for BuiltDistribution {
}
}
impl DistributionIdentifier for Distribution {
impl Identifier for Dist {
fn distribution_id(&self) -> String {
match self {
Self::Built(dist) => dist.distribution_id(),

View file

@ -3,14 +3,12 @@ use puffin_cache::CanonicalUrl;
use puffin_normalize::PackageName;
use crate::{
AnyDistribution, BuiltDistribution, CachedDirectUrlDistribution, CachedDistribution,
CachedRegistryDistribution, DirectUrlBuiltDistribution, DirectUrlSourceDistribution,
Distribution, GitSourceDistribution, InstalledDirectUrlDistribution, InstalledDistribution,
InstalledRegistryDistribution, RegistryBuiltDistribution, RegistrySourceDistribution,
SourceDistribution, VersionOrUrl,
AnyDist, BuiltDist, CachedDirectUrlDist, CachedDist, CachedRegistryDist, DirectUrlBuiltDist,
DirectUrlSourceDist, Dist, GitSourceDist, InstalledDirectUrlDist, InstalledDist,
InstalledRegistryDist, RegistryBuiltDist, RegistrySourceDist, SourceDist, VersionOrUrl,
};
pub trait BaseDistribution {
pub trait Metadata {
/// Return the normalized [`PackageName`] of the distribution.
fn name(&self) -> &PackageName;
@ -35,7 +33,7 @@ pub trait BaseDistribution {
}
}
pub trait RemoteDistribution {
pub trait RemoteSource {
/// Return an appropriate filename for the distribution.
fn filename(&self) -> Result<&str>;
@ -43,7 +41,7 @@ pub trait RemoteDistribution {
fn size(&self) -> Option<usize>;
}
pub trait DistributionIdentifier {
pub trait Identifier {
/// Return a unique resource identifier for the distribution, like a SHA-256 hash of the
/// distribution's contents.
fn distribution_id(&self) -> String;
@ -58,91 +56,91 @@ pub trait DistributionIdentifier {
}
// Implement `Display` for all known types that implement `DistributionIdentifier`.
impl std::fmt::Display for AnyDistribution {
impl std::fmt::Display for AnyDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for BuiltDistribution {
impl std::fmt::Display for BuiltDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for CachedDistribution {
impl std::fmt::Display for CachedDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for CachedDirectUrlDistribution {
impl std::fmt::Display for CachedDirectUrlDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for CachedRegistryDistribution {
impl std::fmt::Display for CachedRegistryDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for DirectUrlBuiltDistribution {
impl std::fmt::Display for DirectUrlBuiltDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for DirectUrlSourceDistribution {
impl std::fmt::Display for DirectUrlSourceDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for Distribution {
impl std::fmt::Display for Dist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for GitSourceDistribution {
impl std::fmt::Display for GitSourceDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for InstalledDistribution {
impl std::fmt::Display for InstalledDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for InstalledDirectUrlDistribution {
impl std::fmt::Display for InstalledDirectUrlDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for InstalledRegistryDistribution {
impl std::fmt::Display for InstalledRegistryDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for RegistryBuiltDistribution {
impl std::fmt::Display for RegistryBuiltDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for RegistrySourceDistribution {
impl std::fmt::Display for RegistrySourceDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}
}
impl std::fmt::Display for SourceDistribution {
impl std::fmt::Display for SourceDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
}

View file

@ -8,10 +8,10 @@ use anyhow::Result;
use fs_err::tokio as fs;
use tracing::debug;
use puffin_distribution::{BaseDistribution, Distribution, RemoteDistribution};
use puffin_distribution::{Dist, Metadata, RemoteSource};
use puffin_traits::BuildContext;
use crate::downloader::{DiskWheel, SourceDistributionDownload, WheelDownload};
use crate::downloader::{DiskWheel, SourceDistDownload, WheelDownload};
const BUILT_WHEELS_CACHE: &str = "built-wheels-v0";
@ -39,22 +39,19 @@ impl<'a, T: BuildContext + Send + Sync> Builder<'a, T> {
}
/// Build a set of source distributions.
pub async fn build(
&self,
distributions: Vec<SourceDistributionDownload>,
) -> Result<Vec<WheelDownload>> {
pub async fn build(&self, dists: Vec<SourceDistDownload>) -> Result<Vec<WheelDownload>> {
// Sort the distributions by size.
let mut distributions = distributions;
distributions.sort_unstable_by_key(|distribution| {
Reverse(distribution.remote.size().unwrap_or(usize::MAX))
let mut dists = dists;
dists.sort_unstable_by_key(|distribution| {
Reverse(distribution.dist.size().unwrap_or(usize::MAX))
});
// Build the distributions serially.
let mut builds = Vec::with_capacity(distributions.len());
for distribution in distributions {
debug!("Building source distribution: {distribution}");
let mut builds = Vec::with_capacity(dists.len());
for dist in dists {
debug!("Building source distribution: {dist}");
let result = build_sdist(distribution, self.build_context).await?;
let result = build_sdist(dist, self.build_context).await?;
if let Some(reporter) = self.reporter.as_ref() {
reporter.on_progress(result.remote());
@ -73,14 +70,14 @@ impl<'a, T: BuildContext + Send + Sync> Builder<'a, T> {
/// Build a source distribution into a wheel.
async fn build_sdist<T: BuildContext + Send + Sync>(
distribution: SourceDistributionDownload,
dist: SourceDistDownload,
build_context: &T,
) -> Result<WheelDownload> {
// Create a directory for the wheel.
let wheel_dir = build_context
.cache()
.join(BUILT_WHEELS_CACHE)
.join(distribution.remote.package_id());
.join(dist.dist.package_id());
fs::create_dir_all(&wheel_dir).await?;
// Build the wheel.
@ -89,23 +86,23 @@ async fn build_sdist<T: BuildContext + Send + Sync>(
// point to the wrong commit.
let disk_filename = build_context
.build_source(
&distribution.sdist_file,
distribution.subdirectory.as_deref(),
&dist.sdist_file,
dist.subdirectory.as_deref(),
&wheel_dir,
&distribution.remote.to_string(),
&dist.dist.to_string(),
)
.await?;
let wheel_filename = wheel_dir.join(disk_filename);
Ok(WheelDownload::Disk(DiskWheel {
remote: distribution.remote,
dist: dist.dist,
path: wheel_filename,
}))
}
pub trait Reporter: Send + Sync {
/// Callback to invoke when a source distribution is built.
fn on_progress(&self, distribution: &Distribution);
fn on_progress(&self, dist: &Dist);
/// Callback to invoke when the operation is complete.
fn on_complete(&self);

View file

@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};
use fs_err as fs;
use puffin_distribution::{BaseDistribution, BuiltDistribution, Distribution, SourceDistribution};
use puffin_distribution::{BuiltDist, Dist, Metadata, SourceDist};
static WHEEL_CACHE: &str = "wheels-v0";
@ -24,11 +24,11 @@ impl WheelCache {
fs::create_dir_all(&self.root)
}
/// Return the path at which a given [`Distribution`] would be stored.
pub(crate) fn entry(&self, distribution: &Distribution) -> PathBuf {
/// Return the path at which a given [`Dist`] would be stored.
pub(crate) fn entry(&self, dist: &Dist) -> PathBuf {
self.root
.join(CacheShard::from(distribution).segment())
.join(distribution.package_id())
.join(CacheShard::from(dist).segment())
.join(dist.package_id())
}
/// Returns a handle to the wheel cache directory.
@ -58,14 +58,14 @@ impl CacheShard {
}
}
impl From<&Distribution> for CacheShard {
fn from(distribution: &Distribution) -> Self {
match distribution {
Distribution::Built(BuiltDistribution::Registry(_)) => Self::Registry,
Distribution::Built(BuiltDistribution::DirectUrl(_)) => Self::Url,
Distribution::Source(SourceDistribution::Registry(_)) => Self::Registry,
Distribution::Source(SourceDistribution::DirectUrl(_)) => Self::Url,
Distribution::Source(SourceDistribution::Git(_)) => Self::Url,
impl From<&Dist> for CacheShard {
fn from(dist: &Dist) -> Self {
match dist {
Dist::Built(BuiltDist::Registry(_)) => Self::Registry,
Dist::Built(BuiltDist::DirectUrl(_)) => Self::Url,
Dist::Source(SourceDist::Registry(_)) => Self::Registry,
Dist::Source(SourceDist::DirectUrl(_)) => Self::Url,
Dist::Source(SourceDist::Git(_)) => Self::Url,
}
}
}

View file

@ -11,9 +11,7 @@ use url::Url;
use puffin_client::RegistryClient;
use puffin_distribution::direct_url::{DirectArchiveUrl, DirectGitUrl};
use puffin_distribution::{
BuiltDistribution, Distribution, RemoteDistribution, SourceDistribution,
};
use puffin_distribution::{BuiltDist, Dist, RemoteSource, SourceDist};
use puffin_git::GitSource;
use crate::locks::Locks;
@ -57,26 +55,26 @@ impl<'a> Downloader<'a> {
}
/// Download a set of distributions.
pub async fn download(&self, distributions: Vec<Distribution>) -> Result<Vec<Download>> {
pub async fn download(&self, dists: Vec<Dist>) -> Result<Vec<Download>> {
// Sort the distributions by size.
let mut distributions = distributions;
distributions.sort_unstable_by_key(|distribution| {
let mut dists = dists;
dists.sort_unstable_by_key(|distribution| {
Reverse(distribution.size().unwrap_or(usize::MAX))
});
// Fetch the distributions in parallel.
let mut fetches = JoinSet::new();
let mut downloads = Vec::with_capacity(distributions.len());
for distribution in distributions {
if self.no_build && matches!(distribution, Distribution::Source(_)) {
let mut downloads = Vec::with_capacity(dists.len());
for dist in dists {
if self.no_build && matches!(dist, Dist::Source(_)) {
bail!(
"Building source distributions is disabled, not downloading {}",
distribution
dist
);
}
fetches.spawn(fetch_distribution(
distribution.clone(),
fetches.spawn(fetch(
dist.clone(),
self.client.clone(),
self.cache.to_path_buf(),
self.locks.clone(),
@ -102,17 +100,17 @@ impl<'a> Downloader<'a> {
}
/// Download a built distribution (wheel) or source distribution (sdist).
async fn fetch_distribution(
distribution: Distribution,
async fn fetch(
dist: Dist,
client: RegistryClient,
cache: PathBuf,
locks: Arc<Locks>,
) -> Result<Download> {
let lock = locks.acquire(&distribution).await;
let lock = locks.acquire(&dist).await;
let _guard = lock.lock().await;
match &distribution {
Distribution::Built(BuiltDistribution::Registry(wheel)) => {
match &dist {
Dist::Built(BuiltDist::Registry(wheel)) => {
// Fetch the wheel.
let url = Url::parse(&wheel.file.url)?;
let reader = client.stream_external(&url).await?;
@ -120,7 +118,7 @@ async fn fetch_distribution(
// If the file is greater than 5MB, write it to disk; otherwise, keep it in memory.
let file_size = ByteSize::b(wheel.file.size as u64);
if file_size >= ByteSize::mb(5) {
debug!("Fetching disk-based wheel from registry: {distribution} ({file_size})");
debug!("Fetching disk-based wheel from registry: {dist} ({file_size})");
// Download the wheel to a temporary file.
let temp_dir = tempfile::tempdir_in(cache)?.into_path();
@ -130,11 +128,11 @@ async fn fetch_distribution(
tokio::io::copy(&mut reader.compat(), &mut writer).await?;
Ok(Download::Wheel(WheelDownload::Disk(DiskWheel {
remote: distribution,
dist,
path: wheel_file,
})))
} else {
debug!("Fetching in-memory wheel from registry: {distribution} ({file_size})");
debug!("Fetching in-memory wheel from registry: {dist} ({file_size})");
// Read into a buffer.
let mut buffer = Vec::with_capacity(wheel.file.size);
@ -142,13 +140,13 @@ async fn fetch_distribution(
tokio::io::copy(&mut reader, &mut buffer).await?;
Ok(Download::Wheel(WheelDownload::InMemory(InMemoryWheel {
remote: distribution,
dist,
buffer,
})))
}
}
Distribution::Built(BuiltDistribution::DirectUrl(wheel)) => {
Dist::Built(BuiltDist::DirectUrl(wheel)) => {
debug!("Fetching disk-based wheel from URL: {}", &wheel.url);
// Fetch the wheel.
@ -162,12 +160,12 @@ async fn fetch_distribution(
tokio::io::copy(&mut reader.compat(), &mut writer).await?;
Ok(Download::Wheel(WheelDownload::Disk(DiskWheel {
remote: distribution,
dist,
path: wheel_file,
})))
}
Distribution::Source(SourceDistribution::Registry(sdist)) => {
Dist::Source(SourceDist::Registry(sdist)) => {
debug!(
"Fetching source distribution from registry: {}",
&sdist.file.url
@ -183,14 +181,14 @@ async fn fetch_distribution(
let mut writer = tokio::fs::File::create(&sdist_file).await?;
tokio::io::copy(&mut reader.compat(), &mut writer).await?;
Ok(Download::SourceDistribution(SourceDistributionDownload {
remote: distribution,
Ok(Download::SourceDist(SourceDistDownload {
dist,
sdist_file,
subdirectory: None,
}))
}
Distribution::Source(SourceDistribution::DirectUrl(sdist)) => {
Dist::Source(SourceDist::DirectUrl(sdist)) => {
debug!("Fetching source distribution from URL: {}", sdist.url);
let DirectArchiveUrl { url, subdirectory } = DirectArchiveUrl::from(&sdist.url);
@ -205,14 +203,14 @@ async fn fetch_distribution(
let mut writer = tokio::fs::File::create(&sdist_file).await?;
tokio::io::copy(&mut reader, &mut writer).await?;
Ok(Download::SourceDistribution(SourceDistributionDownload {
remote: distribution,
Ok(Download::SourceDist(SourceDistDownload {
dist,
sdist_file,
subdirectory,
}))
}
Distribution::Source(SourceDistribution::Git(sdist)) => {
Dist::Source(SourceDist::Git(sdist)) => {
debug!("Fetching source distribution from Git: {}", sdist.url);
let DirectGitUrl { url, subdirectory } = DirectGitUrl::try_from(&sdist.url)?;
@ -223,8 +221,8 @@ async fn fetch_distribution(
.await??
.into();
Ok(Download::SourceDistribution(SourceDistributionDownload {
remote: distribution,
Ok(Download::SourceDist(SourceDistDownload {
dist,
sdist_file,
subdirectory,
}))
@ -244,7 +242,7 @@ pub trait Reporter: Send + Sync {
#[derive(Debug)]
pub struct InMemoryWheel {
/// The remote distribution from which this wheel was downloaded.
pub(crate) remote: Distribution,
pub(crate) dist: Dist,
/// The contents of the wheel.
pub(crate) buffer: Vec<u8>,
}
@ -253,7 +251,7 @@ pub struct InMemoryWheel {
#[derive(Debug)]
pub struct DiskWheel {
/// The remote distribution from which this wheel was downloaded.
pub(crate) remote: Distribution,
pub(crate) dist: Dist,
/// The path to the downloaded wheel.
pub(crate) path: PathBuf,
}
@ -266,20 +264,20 @@ pub enum WheelDownload {
}
impl WheelDownload {
/// Return the [`Distribution`] from which this wheel was downloaded.
pub fn remote(&self) -> &Distribution {
/// Return the [`Dist`] from which this wheel was downloaded.
pub fn remote(&self) -> &Dist {
match self {
WheelDownload::InMemory(wheel) => &wheel.remote,
WheelDownload::Disk(wheel) => &wheel.remote,
WheelDownload::InMemory(wheel) => &wheel.dist,
WheelDownload::Disk(wheel) => &wheel.dist,
}
}
}
/// A downloaded source distribution.
#[derive(Debug, Clone)]
pub struct SourceDistributionDownload {
pub struct SourceDistDownload {
/// The remote distribution from which this source distribution was downloaded.
pub(crate) remote: Distribution,
pub(crate) dist: Dist,
/// The path to the downloaded archive or directory.
pub(crate) sdist_file: PathBuf,
/// The subdirectory within the archive or directory.
@ -290,14 +288,14 @@ pub struct SourceDistributionDownload {
#[derive(Debug)]
pub enum Download {
Wheel(WheelDownload),
SourceDistribution(SourceDistributionDownload),
SourceDist(SourceDistDownload),
}
impl std::fmt::Display for Download {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Download::Wheel(wheel) => write!(f, "{wheel}"),
Download::SourceDistribution(sdist) => write!(f, "{sdist}"),
Download::SourceDist(sdist) => write!(f, "{sdist}"),
}
}
}
@ -305,14 +303,14 @@ impl std::fmt::Display for Download {
impl std::fmt::Display for WheelDownload {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
WheelDownload::InMemory(wheel) => write!(f, "{}", wheel.remote),
WheelDownload::Disk(wheel) => write!(f, "{}", wheel.remote),
WheelDownload::InMemory(wheel) => write!(f, "{}", wheel.dist),
WheelDownload::Disk(wheel) => write!(f, "{}", wheel.dist),
}
}
}
impl std::fmt::Display for SourceDistributionDownload {
impl std::fmt::Display for SourceDistDownload {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.remote)
write!(f, "{}", self.dist)
}
}

View file

@ -1,7 +1,7 @@
use anyhow::{Context, Error, Result};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use puffin_distribution::CachedDistribution;
use puffin_distribution::CachedDist;
use puffin_interpreter::Virtualenv;
pub struct Installer<'a> {
@ -36,7 +36,7 @@ impl<'a> Installer<'a> {
}
/// Install a set of wheels into a Python virtual environment.
pub fn install(self, wheels: &[CachedDistribution]) -> Result<()> {
pub fn install(self, wheels: &[CachedDist]) -> Result<()> {
tokio::task::block_in_place(|| {
wheels.par_iter().try_for_each(|wheel| {
let location = install_wheel_rs::InstallLocation::new(
@ -69,7 +69,7 @@ impl<'a> Installer<'a> {
pub trait Reporter: Send + Sync {
/// Callback to invoke when a dependency is resolved.
fn on_install_progress(&self, wheel: &CachedDistribution);
fn on_install_progress(&self, wheel: &CachedDist);
/// Callback to invoke when the resolution is complete.
fn on_install_complete(&self);

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
use fxhash::FxHashMap;
use tokio::sync::Mutex;
use puffin_distribution::DistributionIdentifier;
use puffin_distribution::Identifier;
/// A set of locks used to prevent concurrent access to the same resource.
#[derive(Debug, Default)]
@ -11,12 +11,9 @@ pub(crate) struct Locks(Mutex<FxHashMap<String, Arc<Mutex<()>>>>);
impl Locks {
/// Acquire a lock on the given resource.
pub(crate) async fn acquire(
&self,
distribution: &impl DistributionIdentifier,
) -> Arc<Mutex<()>> {
pub(crate) async fn acquire(&self, dist: &impl Identifier) -> Arc<Mutex<()>> {
let mut map = self.0.lock().await;
map.entry(distribution.resource_id())
map.entry(dist.resource_id())
.or_insert_with(|| Arc::new(Mutex::new(())))
.clone()
}

View file

@ -5,7 +5,7 @@ use tracing::debug;
use pep508_rs::{Requirement, VersionOrUrl};
use puffin_distribution::direct_url::DirectUrl;
use puffin_distribution::{CachedDistribution, InstalledDistribution};
use puffin_distribution::{CachedDist, InstalledDist};
use puffin_interpreter::Virtualenv;
use crate::url_index::UrlIndex;
@ -15,7 +15,7 @@ use crate::{RegistryIndex, SitePackages};
pub struct InstallPlan {
/// The distributions that are not already installed in the current environment, but are
/// available in the local cache.
pub local: Vec<CachedDistribution>,
pub local: Vec<CachedDist>,
/// The distributions that are not already installed in the current environment, and are
/// not available in the local cache.
@ -23,7 +23,7 @@ pub struct InstallPlan {
/// The distributions that are already installed in the current environment, and are
/// _not_ necessary to satisfy the requirements.
pub extraneous: Vec<InstalledDistribution>,
pub extraneous: Vec<InstalledDist>,
}
impl InstallPlan {
@ -62,7 +62,7 @@ impl InstallPlan {
// If the requirement comes from a direct URL, check by URL.
Some(VersionOrUrl::Url(url)) => {
if let InstalledDistribution::Url(distribution) = &distribution {
if let InstalledDist::Url(distribution) = &distribution {
if let Ok(direct_url) = DirectUrl::try_from(url) {
if let Ok(direct_url) = pypi_types::DirectUrl::try_from(&direct_url)
{
@ -89,14 +89,14 @@ impl InstallPlan {
.filter(|dist| requirement.is_satisfied_by(&dist.version))
{
debug!("Requirement already cached: {distribution}");
local.push(CachedDistribution::Registry(distribution.clone()));
local.push(CachedDist::Registry(distribution.clone()));
continue;
}
}
Some(VersionOrUrl::Url(url)) => {
if let Some(distribution) = url_index.get(&requirement.name, url) {
debug!("Requirement already cached: {distribution}");
local.push(CachedDistribution::Url(distribution.clone()));
local.push(CachedDist::Url(distribution.clone()));
continue;
}
}

View file

@ -4,14 +4,14 @@ use std::path::Path;
use fs_err as fs;
use tracing::warn;
use puffin_distribution::{BaseDistribution, CachedRegistryDistribution};
use puffin_distribution::{CachedRegistryDist, Metadata};
use puffin_normalize::PackageName;
use crate::cache::{CacheShard, WheelCache};
/// A local index of distributions that originate from a registry, like `PyPI`.
#[derive(Debug, Default)]
pub struct RegistryIndex(HashMap<PackageName, CachedRegistryDistribution>);
pub struct RegistryIndex(HashMap<PackageName, CachedRegistryDist>);
impl RegistryIndex {
/// Build an index of cached distributions from a directory.
@ -37,7 +37,7 @@ impl RegistryIndex {
}
};
if file_type.is_dir() {
match CachedRegistryDistribution::try_from_path(&path) {
match CachedRegistryDist::try_from_path(&path) {
Ok(None) => {}
Ok(Some(dist_info)) => {
index.insert(dist_info.name().clone(), dist_info);
@ -60,7 +60,7 @@ impl RegistryIndex {
}
/// Returns a distribution from the index, if it exists.
pub fn get(&self, name: &PackageName) -> Option<&CachedRegistryDistribution> {
pub fn get(&self, name: &PackageName) -> Option<&CachedRegistryDist> {
self.0.get(name)
}
}

View file

@ -3,12 +3,12 @@ use std::collections::BTreeMap;
use anyhow::Result;
use fs_err as fs;
use puffin_distribution::{BaseDistribution, InstalledDistribution};
use puffin_distribution::{InstalledDist, Metadata};
use puffin_interpreter::Virtualenv;
use puffin_normalize::PackageName;
#[derive(Debug, Default)]
pub struct SitePackages(BTreeMap<PackageName, InstalledDistribution>);
pub struct SitePackages(BTreeMap<PackageName, InstalledDist>);
impl SitePackages {
/// Build an index of installed packages from the given Python executable.
@ -18,7 +18,7 @@ impl SitePackages {
for entry in fs::read_dir(venv.site_packages())? {
let entry = entry?;
if entry.file_type()?.is_dir() {
if let Some(dist_info) = InstalledDistribution::try_from_path(&entry.path())? {
if let Some(dist_info) = InstalledDist::try_from_path(&entry.path())? {
index.insert(dist_info.name().clone(), dist_info);
}
}
@ -28,24 +28,24 @@ impl SitePackages {
}
/// Returns an iterator over the installed distributions.
pub fn distributions(&self) -> impl Iterator<Item = &InstalledDistribution> {
pub fn distributions(&self) -> impl Iterator<Item = &InstalledDist> {
self.0.values()
}
/// Returns the version of the given package, if it is installed.
pub fn get(&self, name: &PackageName) -> Option<&InstalledDistribution> {
pub fn get(&self, name: &PackageName) -> Option<&InstalledDist> {
self.0.get(name)
}
/// Remove the given package from the index, returning its version if it was installed.
pub fn remove(&mut self, name: &PackageName) -> Option<InstalledDistribution> {
pub fn remove(&mut self, name: &PackageName) -> Option<InstalledDist> {
self.0.remove(name)
}
}
impl IntoIterator for SitePackages {
type Item = (PackageName, InstalledDistribution);
type IntoIter = std::collections::btree_map::IntoIter<PackageName, InstalledDistribution>;
type Item = (PackageName, InstalledDist);
type IntoIter = std::collections::btree_map::IntoIter<PackageName, InstalledDist>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()

View file

@ -1,13 +1,11 @@
use anyhow::Result;
use puffin_distribution::InstalledDistribution;
use puffin_distribution::InstalledDist;
/// Uninstall a package from the specified Python environment.
pub async fn uninstall(
distribution: &InstalledDistribution,
) -> Result<install_wheel_rs::Uninstall> {
pub async fn uninstall(dist: &InstalledDist) -> Result<install_wheel_rs::Uninstall> {
let uninstall = tokio::task::spawn_blocking({
let path = distribution.path().to_owned();
let path = dist.path().to_owned();
move || install_wheel_rs::uninstall_wheel(&path)
})
.await??;

View file

@ -8,7 +8,7 @@ use rayon::iter::ParallelIterator;
use tracing::debug;
use zip::ZipArchive;
use puffin_distribution::{CachedDistribution, Distribution, DistributionIdentifier};
use puffin_distribution::{CachedDist, Dist, Identifier};
use crate::cache::WheelCache;
use crate::downloader::WheelDownload;
@ -33,7 +33,7 @@ impl Unzipper {
&self,
downloads: Vec<WheelDownload>,
target: &Path,
) -> Result<Vec<CachedDistribution>> {
) -> Result<Vec<CachedDist>> {
// Create the wheel cache subdirectory, if necessary.
let wheel_cache = WheelCache::new(target);
wheel_cache.init()?;
@ -82,7 +82,7 @@ impl Unzipper {
}
let path = wheel_cache.entry(&remote);
wheels.push(CachedDistribution::from_remote(remote, path));
wheels.push(CachedDist::from_remote(remote, path));
}
if let Some(reporter) = self.reporter.as_ref() {
@ -149,7 +149,7 @@ fn unzip_archive<R: Send + Read + Seek + HasLength>(reader: R, target: &Path) ->
pub trait Reporter: Send + Sync {
/// Callback to invoke when a wheel is unzipped.
fn on_unzip_progress(&self, distribution: &Distribution);
fn on_unzip_progress(&self, dist: &Dist);
/// Callback to invoke when the operation is complete.
fn on_unzip_complete(&self);

View file

@ -4,7 +4,7 @@ use fxhash::FxHashMap;
use tracing::warn;
use url::Url;
use puffin_distribution::{CachedDirectUrlDistribution, DistributionIdentifier};
use puffin_distribution::{CachedDirectUrlDist, Identifier};
use puffin_normalize::PackageName;
use crate::cache::{CacheShard, WheelCache};
@ -49,12 +49,12 @@ impl UrlIndex {
}
/// Returns a distribution from the index, if it exists.
pub(crate) fn get(&self, name: &PackageName, url: &Url) -> Option<CachedDirectUrlDistribution> {
pub(crate) fn get(&self, name: &PackageName, url: &Url) -> Option<CachedDirectUrlDist> {
// TODO(charlie): This takes advantage of the fact that for URL dependencies, the package ID
// and distribution ID are identical. We should either change the cache layout to use
// distribution IDs, or implement package ID for URL.
let path = self.0.get(&url.distribution_id())?;
Some(CachedDirectUrlDistribution::from_url(
Some(CachedDirectUrlDist::from_url(
name.clone(),
url.clone(),
path.clone(),

View file

@ -4,7 +4,7 @@ use pubgrub::range::Range;
use pep508_rs::{Requirement, VersionOrUrl};
use puffin_normalize::PackageName;
use crate::file::DistributionFile;
use crate::file::DistFile;
use crate::prerelease_mode::PreReleaseStrategy;
use crate::pubgrub::PubGrubVersion;
use crate::resolution_mode::ResolutionStrategy;
@ -150,7 +150,7 @@ impl CandidateSelector {
/// Select the first-matching [`Candidate`] from a set of candidate versions and files,
/// preferring wheels over sdists.
fn select_candidate<'a>(
versions: impl Iterator<Item = (&'a PubGrubVersion, &'a DistributionFile)>,
versions: impl Iterator<Item = (&'a PubGrubVersion, &'a DistFile)>,
package_name: &PackageName,
range: &Range<PubGrubVersion>,
allow_prerelease: AllowPreRelease,
@ -158,7 +158,7 @@ impl CandidateSelector {
#[derive(Debug)]
enum PreReleaseCandidate<'a> {
NotNecessary,
IfNecessary(&'a PubGrubVersion, &'a DistributionFile),
IfNecessary(&'a PubGrubVersion, &'a DistFile),
}
let mut prerelease = None;
@ -222,5 +222,5 @@ pub(crate) struct Candidate {
/// The version of the package.
pub(crate) version: PubGrubVersion,
/// The file of the package.
pub(crate) file: DistributionFile,
pub(crate) file: DistFile,
}

View file

@ -9,7 +9,7 @@ use tracing::debug;
use distribution_filename::WheelFilename;
use platform_tags::Tags;
use puffin_client::RegistryClient;
use puffin_distribution::{DirectUrlBuiltDistribution, DistributionIdentifier, RemoteDistribution};
use puffin_distribution::{DirectUrlBuiltDist, Identifier, RemoteSource};
use pypi_types::Metadata21;
use crate::distribution::cached_wheel::CachedWheel;
@ -17,10 +17,10 @@ use crate::distribution::cached_wheel::CachedWheel;
const REMOTE_WHEELS_CACHE: &str = "remote-wheels-v0";
/// Fetch a built distribution from a remote source, or from a local cache.
pub(crate) struct BuiltDistributionFetcher<'a>(&'a Path);
pub(crate) struct BuiltDistFetcher<'a>(&'a Path);
impl<'a> BuiltDistributionFetcher<'a> {
/// Initialize a [`BuiltDistributionFetcher`] from a [`BuildContext`].
impl<'a> BuiltDistFetcher<'a> {
/// Initialize a [`BuiltDistFetcher`] from a [`BuildContext`].
pub(crate) fn new(cache: &'a Path) -> Self {
Self(cache)
}
@ -28,10 +28,10 @@ impl<'a> BuiltDistributionFetcher<'a> {
/// Read the [`Metadata21`] from a wheel, if it exists in the cache.
pub(crate) fn find_dist_info(
&self,
distribution: &DirectUrlBuiltDistribution,
dist: &DirectUrlBuiltDist,
tags: &Tags,
) -> Result<Option<Metadata21>> {
CachedWheel::find_in_cache(distribution, tags, self.0.join(REMOTE_WHEELS_CACHE))
CachedWheel::find_in_cache(dist, tags, self.0.join(REMOTE_WHEELS_CACHE))
.as_ref()
.map(|wheel| CachedWheel::read_dist_info(wheel).context("Failed to read dist info"))
.transpose()
@ -40,21 +40,21 @@ impl<'a> BuiltDistributionFetcher<'a> {
/// Download a wheel, storing it in the cache.
pub(crate) async fn download_wheel(
&self,
distribution: &DirectUrlBuiltDistribution,
dist: &DirectUrlBuiltDist,
client: &RegistryClient,
) -> Result<Metadata21> {
debug!("Downloading: {distribution}");
let reader = client.stream_external(&distribution.url).await?;
debug!("Downloading: {dist}");
let reader = client.stream_external(&dist.url).await?;
// Create a directory for the wheel.
let wheel_dir = self
.0
.join(REMOTE_WHEELS_CACHE)
.join(distribution.distribution_id());
.join(dist.distribution_id());
fs::create_dir_all(&wheel_dir).await?;
// Download the wheel.
let wheel_filename = distribution.filename()?;
let wheel_filename = dist.filename()?;
let wheel_file = wheel_dir.join(wheel_filename);
let mut writer = tokio::fs::File::create(&wheel_file).await?;
tokio::io::copy(&mut reader.compat(), &mut writer).await?;
@ -63,7 +63,7 @@ impl<'a> BuiltDistributionFetcher<'a> {
let wheel = CachedWheel::new(wheel_file, WheelFilename::from_str(wheel_filename)?);
let metadata21 = wheel.read_dist_info()?;
debug!("Finished downloading: {distribution}");
debug!("Finished downloading: {dist}");
Ok(metadata21)
}
}

View file

@ -7,7 +7,7 @@ use zip::ZipArchive;
use distribution_filename::WheelFilename;
use install_wheel_rs::find_dist_info;
use platform_tags::Tags;
use puffin_distribution::DistributionIdentifier;
use puffin_distribution::Identifier;
use pypi_types::Metadata21;
/// A cached wheel built from a remote source.
@ -23,12 +23,12 @@ impl CachedWheel {
}
/// Search for a wheel matching the tags that was built from the given distribution.
pub(super) fn find_in_cache<T: DistributionIdentifier>(
distribution: &T,
pub(super) fn find_in_cache<T: Identifier>(
dist: &T,
tags: &Tags,
cache: impl AsRef<Path>,
) -> Option<Self> {
let wheel_dir = cache.as_ref().join(distribution.distribution_id());
let wheel_dir = cache.as_ref().join(dist.distribution_id());
let Ok(read_dir) = fs_err::read_dir(wheel_dir) else {
return None;
};

View file

@ -1,8 +1,6 @@
pub(crate) use built_distribution::BuiltDistributionFetcher;
pub(crate) use source_distribution::{
Reporter as SourceDistributionReporter, SourceDistributionFetcher,
};
pub(crate) use built_dist::BuiltDistFetcher;
pub(crate) use source_dist::{Reporter as SourceDistributionReporter, SourceDistFetcher};
mod built_distribution;
mod built_dist;
mod cached_wheel;
mod source_distribution;
mod source_dist;

View file

@ -15,7 +15,7 @@ use distribution_filename::WheelFilename;
use platform_tags::Tags;
use puffin_client::RegistryClient;
use puffin_distribution::direct_url::{DirectArchiveUrl, DirectGitUrl};
use puffin_distribution::{DistributionIdentifier, RemoteDistribution, SourceDistribution};
use puffin_distribution::{Identifier, RemoteSource, SourceDist};
use puffin_git::{GitSource, GitUrl};
use puffin_traits::BuildContext;
use pypi_types::Metadata21;
@ -27,13 +27,13 @@ const BUILT_WHEELS_CACHE: &str = "built-wheels-v0";
const GIT_CACHE: &str = "git-v0";
/// Fetch and build a source distribution from a remote source, or from a local cache.
pub(crate) struct SourceDistributionFetcher<'a, T: BuildContext> {
pub(crate) struct SourceDistFetcher<'a, T: BuildContext> {
build_context: &'a T,
reporter: Option<Arc<dyn Reporter>>,
}
impl<'a, T: BuildContext> SourceDistributionFetcher<'a, T> {
/// Initialize a [`SourceDistributionFetcher`] from a [`BuildContext`].
impl<'a, T: BuildContext> SourceDistFetcher<'a, T> {
/// Initialize a [`SourceDistFetcher`] from a [`BuildContext`].
pub(crate) fn new(build_context: &'a T) -> Self {
Self {
build_context,
@ -53,11 +53,11 @@ impl<'a, T: BuildContext> SourceDistributionFetcher<'a, T> {
/// Read the [`Metadata21`] from a built source distribution, if it exists in the cache.
pub(crate) fn find_dist_info(
&self,
distribution: &SourceDistribution,
dist: &SourceDist,
tags: &Tags,
) -> Result<Option<Metadata21>> {
CachedWheel::find_in_cache(
distribution,
dist,
tags,
self.build_context.cache().join(BUILT_WHEELS_CACHE),
)
@ -69,17 +69,17 @@ impl<'a, T: BuildContext> SourceDistributionFetcher<'a, T> {
/// Download and build a source distribution, storing the built wheel in the cache.
pub(crate) async fn download_and_build_sdist(
&self,
distribution: &SourceDistribution,
dist: &SourceDist,
client: &RegistryClient,
) -> Result<Metadata21> {
debug!("Building: {distribution}");
debug!("Building: {dist}");
if self.build_context.no_build() {
bail!("Building source distributions is disabled");
}
let (sdist_file, subdirectory) = match distribution {
SourceDistribution::Registry(sdist) => {
let (sdist_file, subdirectory) = match dist {
SourceDist::Registry(sdist) => {
debug!(
"Fetching source distribution from registry: {}",
sdist.file.url
@ -98,7 +98,7 @@ impl<'a, T: BuildContext> SourceDistributionFetcher<'a, T> {
(sdist_file, None)
}
SourceDistribution::DirectUrl(sdist) => {
SourceDist::DirectUrl(sdist) => {
debug!("Fetching source distribution from URL: {}", sdist.url);
let DirectArchiveUrl { url, subdirectory } = DirectArchiveUrl::from(&sdist.url);
@ -116,7 +116,7 @@ impl<'a, T: BuildContext> SourceDistributionFetcher<'a, T> {
(sdist_file, subdirectory)
}
SourceDistribution::Git(sdist) => {
SourceDist::Git(sdist) => {
debug!("Fetching source distribution from Git: {}", sdist.url);
let DirectGitUrl { url, subdirectory } = DirectGitUrl::try_from(&sdist.url)?;
@ -140,7 +140,7 @@ impl<'a, T: BuildContext> SourceDistributionFetcher<'a, T> {
.build_context
.cache()
.join(BUILT_WHEELS_CACHE)
.join(distribution.distribution_id());
.join(dist.distribution_id());
fs::create_dir_all(&wheel_dir).await?;
// Build the wheel.
@ -150,7 +150,7 @@ impl<'a, T: BuildContext> SourceDistributionFetcher<'a, T> {
&sdist_file,
subdirectory.as_deref(),
&wheel_dir,
&distribution.to_string(),
&dist.to_string(),
)
.await?;
@ -161,7 +161,7 @@ impl<'a, T: BuildContext> SourceDistributionFetcher<'a, T> {
);
let metadata21 = wheel.read_dist_info()?;
debug!("Finished building: {distribution}");
debug!("Finished building: {dist}");
Ok(metadata21)
}
@ -173,8 +173,8 @@ impl<'a, T: BuildContext> SourceDistributionFetcher<'a, T> {
/// This method takes into account various normalizations that are independent from the Git
/// layer. For example: removing `#subdirectory=pkg_dir`-like fragments, and removing `git+`
/// prefix kinds.
pub(crate) async fn precise(&self, distribution: &SourceDistribution) -> Result<Option<Url>> {
let SourceDistribution::Git(sdist) = distribution else {
pub(crate) async fn precise(&self, dist: &SourceDist) -> Result<Option<Url>> {
let SourceDist::Git(sdist) = dist else {
return Ok(None);
};

View file

@ -6,7 +6,7 @@ use thiserror::Error;
use url::Url;
use pep508_rs::Requirement;
use puffin_distribution::{BuiltDistribution, SourceDistribution};
use puffin_distribution::{BuiltDist, SourceDist};
use puffin_normalize::PackageName;
use crate::pubgrub::{PubGrubPackage, PubGrubVersion};
@ -48,7 +48,7 @@ pub enum ResolveError {
DisallowedUrl(PackageName, Url),
#[error("Failed to fetch wheel metadata from: {filename}")]
RegistryBuiltDistribution {
RegistryBuiltDist {
filename: String,
// TODO(konstin): Gives this a proper error type
#[source]
@ -56,7 +56,7 @@ pub enum ResolveError {
},
#[error("Failed to fetch wheel metadata from: {url}")]
UrlBuiltDistribution {
UrlBuiltDist {
url: Url,
// TODO(konstin): Gives this a proper error type
#[source]
@ -64,7 +64,7 @@ pub enum ResolveError {
},
#[error("Failed to build distribution: {filename}")]
RegistrySourceDistribution {
RegistrySourceDist {
filename: String,
// TODO(konstin): Gives this a proper error type
#[source]
@ -72,7 +72,7 @@ pub enum ResolveError {
},
#[error("Failed to build distribution from URL: {url}")]
UrlSourceDistribution {
UrlSourceDist {
url: Url,
// TODO(konstin): Gives this a proper error type
#[source]
@ -112,30 +112,30 @@ impl From<pubgrub::error::PubGrubError<PubGrubPackage, Range<PubGrubVersion>>> f
}
impl ResolveError {
pub fn from_source_distribution(distribution: SourceDistribution, err: anyhow::Error) -> Self {
match distribution {
SourceDistribution::Registry(sdist) => Self::RegistrySourceDistribution {
pub fn from_source_dist(dist: SourceDist, err: anyhow::Error) -> Self {
match dist {
SourceDist::Registry(sdist) => Self::RegistrySourceDist {
filename: sdist.file.filename.clone(),
err,
},
SourceDistribution::DirectUrl(sdist) => Self::UrlSourceDistribution {
SourceDist::DirectUrl(sdist) => Self::UrlSourceDist {
url: sdist.url.clone(),
err,
},
SourceDistribution::Git(sdist) => Self::UrlSourceDistribution {
SourceDist::Git(sdist) => Self::UrlSourceDist {
url: sdist.url.clone(),
err,
},
}
}
pub fn from_built_distribution(distribution: BuiltDistribution, err: anyhow::Error) -> Self {
match distribution {
BuiltDistribution::Registry(wheel) => Self::RegistryBuiltDistribution {
pub fn from_built_dist(dist: BuiltDist, err: anyhow::Error) -> Self {
match dist {
BuiltDist::Registry(wheel) => Self::RegistryBuiltDist {
filename: wheel.file.filename.clone(),
err,
},
BuiltDistribution::DirectUrl(wheel) => Self::UrlBuiltDistribution {
BuiltDist::DirectUrl(wheel) => Self::UrlBuiltDist {
url: wheel.url.clone(),
err,
},

View file

@ -10,7 +10,7 @@ pub(crate) struct WheelFile(pub(crate) File);
pub(crate) struct SdistFile(pub(crate) File);
#[derive(Debug, Clone)]
pub(crate) enum DistributionFile {
pub(crate) enum DistFile {
Wheel(WheelFile),
Sdist(SdistFile),
}
@ -43,19 +43,19 @@ impl From<SdistFile> for File {
}
}
impl From<WheelFile> for DistributionFile {
impl From<WheelFile> for DistFile {
fn from(wheel: WheelFile) -> Self {
Self::Wheel(wheel)
}
}
impl From<SdistFile> for DistributionFile {
impl From<SdistFile> for DistFile {
fn from(sdist: SdistFile) -> Self {
Self::Sdist(sdist)
}
}
impl DistributionFile {
impl DistFile {
pub(crate) fn filename(&self) -> &str {
match self {
Self::Wheel(wheel) => wheel.filename.as_str(),
@ -64,11 +64,11 @@ impl DistributionFile {
}
}
impl From<DistributionFile> for File {
fn from(file: DistributionFile) -> Self {
impl From<DistFile> for File {
fn from(file: DistFile) -> Self {
match file {
DistributionFile::Wheel(wheel) => wheel.into(),
DistributionFile::Sdist(sdist) => sdist.into(),
DistFile::Wheel(wheel) => wheel.into(),
DistFile::Sdist(sdist) => sdist.into(),
}
}
}

View file

@ -9,24 +9,24 @@ use anyhow::Result;
use futures::{StreamExt, TryFutureExt};
use fxhash::FxHashMap;
use distribution_filename::{SourceDistributionFilename, WheelFilename};
use distribution_filename::{SourceDistFilename, WheelFilename};
use pep508_rs::{Requirement, VersionOrUrl};
use platform_tags::Tags;
use puffin_client::RegistryClient;
use puffin_distribution::Distribution;
use puffin_distribution::Dist;
use puffin_normalize::PackageName;
use pypi_types::{File, SimpleJson};
use crate::error::ResolveError;
use crate::resolution::Resolution;
pub struct DistributionFinder<'a> {
pub struct DistFinder<'a> {
tags: &'a Tags,
client: &'a RegistryClient,
reporter: Option<Box<dyn Reporter>>,
}
impl<'a> DistributionFinder<'a> {
impl<'a> DistFinder<'a> {
/// Initialize a new distribution finder.
pub fn new(tags: &'a Tags, client: &'a RegistryClient) -> Self {
Self {
@ -66,7 +66,7 @@ impl<'a> DistributionFinder<'a> {
.ready_chunks(32);
// Resolve the requirements.
let mut resolution: FxHashMap<PackageName, Distribution> =
let mut resolution: FxHashMap<PackageName, Dist> =
FxHashMap::with_capacity_and_hasher(requirements.len(), BuildHasherDefault::default());
// Push all the requirements into the package sink.
@ -77,7 +77,7 @@ impl<'a> DistributionFinder<'a> {
}
Some(VersionOrUrl::Url(url)) => {
let package_name = requirement.name.clone();
let package = Distribution::from_url(package_name.clone(), url.clone());
let package = Dist::from_url(package_name.clone(), url.clone());
resolution.insert(package_name, package);
}
}
@ -126,7 +126,7 @@ impl<'a> DistributionFinder<'a> {
}
/// select a version that satisfies the requirement, preferring wheels to source distributions.
fn select(&self, requirement: &Requirement, files: Vec<File>) -> Option<Distribution> {
fn select(&self, requirement: &Requirement, files: Vec<File>) -> Option<Dist> {
let mut fallback = None;
for file in files.into_iter().rev() {
if let Ok(wheel) = WheelFilename::from_str(file.filename.as_str()) {
@ -134,17 +134,13 @@ impl<'a> DistributionFinder<'a> {
continue;
}
if requirement.is_satisfied_by(&wheel.version) {
return Some(Distribution::from_registry(
wheel.distribution,
wheel.version,
file,
));
return Some(Dist::from_registry(wheel.name, wheel.version, file));
}
} else if let Ok(sdist) =
SourceDistributionFilename::parse(file.filename.as_str(), &requirement.name)
SourceDistFilename::parse(file.filename.as_str(), &requirement.name)
{
if requirement.is_satisfied_by(&sdist.version) {
fallback = Some(Distribution::from_registry(sdist.name, sdist.version, file));
fallback = Some(Dist::from_registry(sdist.name, sdist.version, file));
}
}
}
@ -166,7 +162,7 @@ enum Response {
pub trait Reporter: Send + Sync {
/// Callback to invoke when a package is resolved to a specific distribution.
fn on_progress(&self, wheel: &Distribution);
fn on_progress(&self, wheel: &Dist);
/// Callback to invoke when the resolution is complete.
fn on_complete(&self);

View file

@ -1,5 +1,5 @@
pub use error::ResolveError;
pub use finder::{DistributionFinder, Reporter as FinderReporter};
pub use finder::{DistFinder, Reporter as FinderReporter};
pub use manifest::Manifest;
pub use prerelease_mode::PreReleaseMode;
pub use pubgrub::ResolutionFailureReporter;

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
use fxhash::FxHashMap;
use tokio::sync::Mutex;
use puffin_distribution::DistributionIdentifier;
use puffin_distribution::Identifier;
/// A set of locks used to prevent concurrent access to the same resource.
#[derive(Debug, Default)]
@ -11,10 +11,7 @@ pub(crate) struct Locks(Mutex<FxHashMap<String, Arc<Mutex<()>>>>);
impl Locks {
/// Acquire a lock on the given resource.
pub(crate) async fn acquire(
&self,
distribution: &impl DistributionIdentifier,
) -> Arc<Mutex<()>> {
pub(crate) async fn acquire(&self, distribution: &impl Identifier) -> Arc<Mutex<()>> {
let mut map = self.0.lock().await;
map.entry(distribution.resource_id())
.or_insert_with(|| Arc::new(Mutex::new(())))

View file

@ -11,7 +11,7 @@ use waitmap::WaitMap;
use pep440_rs::{Version, VersionSpecifier, VersionSpecifiers};
use pep508_rs::{Requirement, VersionOrUrl};
use puffin_distribution::{BaseDistribution, BuiltDistribution, Distribution, SourceDistribution};
use puffin_distribution::{BuiltDist, Dist, Metadata, SourceDist};
use puffin_normalize::PackageName;
use pypi_types::File;
@ -19,21 +19,21 @@ use crate::pubgrub::{PubGrubPackage, PubGrubPriority, PubGrubVersion};
/// A set of packages pinned at specific versions.
#[derive(Debug, Default)]
pub struct Resolution(FxHashMap<PackageName, Distribution>);
pub struct Resolution(FxHashMap<PackageName, Dist>);
impl Resolution {
/// Create a new resolution from the given pinned packages.
pub(crate) fn new(packages: FxHashMap<PackageName, Distribution>) -> Self {
pub(crate) fn new(packages: FxHashMap<PackageName, Dist>) -> Self {
Self(packages)
}
/// Return the distribution for the given package name, if it exists.
pub fn get(&self, package_name: &PackageName) -> Option<&Distribution> {
pub fn get(&self, package_name: &PackageName) -> Option<&Dist> {
self.0.get(package_name)
}
/// Iterate over the [`Distribution`] entities in this resolution.
pub fn into_distributions(self) -> impl Iterator<Item = Distribution> {
/// Iterate over the [`Dist`] entities in this resolution.
pub fn into_distributions(self) -> impl Iterator<Item = Dist> {
self.0.into_values()
}
@ -51,7 +51,7 @@ impl Resolution {
/// A complete resolution graph in which every node represents a pinned package and every edge
/// represents a dependency between two pinned packages.
#[derive(Debug)]
pub struct Graph(petgraph::graph::Graph<Distribution, (), petgraph::Directed>);
pub struct Graph(petgraph::graph::Graph<Dist, (), petgraph::Directed>);
impl Graph {
/// Create a new graph from the resolved `PubGrub` state.
@ -77,8 +77,7 @@ impl Graph {
.and_then(|versions| versions.get(&version))
.unwrap()
.clone();
let pinned_package =
Distribution::from_registry(package_name.clone(), version, file);
let pinned_package = Dist::from_registry(package_name.clone(), version, file);
let index = graph.add_node(pinned_package);
inverse.insert(package_name, index);
@ -87,7 +86,7 @@ impl Graph {
let url = redirects
.get(url)
.map_or_else(|| url.clone(), |url| url.value().clone());
let pinned_package = Distribution::from_url(package_name.clone(), url);
let pinned_package = Dist::from_url(package_name.clone(), url);
let index = graph.add_node(pinned_package);
inverse.insert(package_name, index);
@ -143,7 +142,7 @@ impl Graph {
self.0
.node_indices()
.map(|node| match &self.0[node] {
Distribution::Built(BuiltDistribution::Registry(wheel)) => Requirement {
Dist::Built(BuiltDist::Registry(wheel)) => Requirement {
name: wheel.name.clone(),
extras: None,
version_or_url: Some(VersionOrUrl::VersionSpecifier(VersionSpecifiers::from(
@ -151,13 +150,13 @@ impl Graph {
))),
marker: None,
},
Distribution::Built(BuiltDistribution::DirectUrl(wheel)) => Requirement {
Dist::Built(BuiltDist::DirectUrl(wheel)) => Requirement {
name: wheel.name.clone(),
extras: None,
version_or_url: Some(VersionOrUrl::Url(wheel.url.clone())),
marker: None,
},
Distribution::Source(SourceDistribution::Registry(sdist)) => Requirement {
Dist::Source(SourceDist::Registry(sdist)) => Requirement {
name: sdist.name.clone(),
extras: None,
version_or_url: Some(VersionOrUrl::VersionSpecifier(VersionSpecifiers::from(
@ -165,13 +164,13 @@ impl Graph {
))),
marker: None,
},
Distribution::Source(SourceDistribution::DirectUrl(sdist)) => Requirement {
Dist::Source(SourceDist::DirectUrl(sdist)) => Requirement {
name: sdist.name.clone(),
extras: None,
version_or_url: Some(VersionOrUrl::Url(sdist.url.clone())),
marker: None,
},
Distribution::Source(SourceDistribution::Git(sdist)) => Requirement {
Dist::Source(SourceDist::Git(sdist)) => Requirement {
name: sdist.name.clone(),
extras: None,
version_or_url: Some(VersionOrUrl::Url(sdist.url.clone())),

View file

@ -17,25 +17,23 @@ use tracing::{debug, error, trace};
use url::Url;
use waitmap::WaitMap;
use distribution_filename::{SourceDistributionFilename, WheelFilename};
use distribution_filename::{SourceDistFilename, WheelFilename};
use pep508_rs::{MarkerEnvironment, Requirement};
use platform_tags::Tags;
use puffin_cache::CanonicalUrl;
use puffin_client::RegistryClient;
use puffin_distribution::{
BaseDistribution, BuiltDistribution, DirectUrlSourceDistribution, Distribution,
DistributionIdentifier, GitSourceDistribution, SourceDistribution, VersionOrUrl,
BuiltDist, DirectUrlSourceDist, Dist, GitSourceDist, Identifier, Metadata, SourceDist,
VersionOrUrl,
};
use puffin_normalize::{ExtraName, PackageName};
use puffin_traits::BuildContext;
use pypi_types::{File, Metadata21, SimpleJson};
use crate::candidate_selector::CandidateSelector;
use crate::distribution::{
BuiltDistributionFetcher, SourceDistributionFetcher, SourceDistributionReporter,
};
use crate::distribution::{BuiltDistFetcher, SourceDistFetcher, SourceDistributionReporter};
use crate::error::ResolveError;
use crate::file::{DistributionFile, SdistFile, WheelFile};
use crate::file::{DistFile, SdistFile, WheelFile};
use crate::locks::Locks;
use crate::manifest::Manifest;
use crate::pubgrub::{
@ -293,8 +291,8 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
// Emit a request to fetch the metadata for this distribution.
if in_flight.insert_url(url) {
priorities.add(package_name.clone());
let distribution = Distribution::from_url(package_name.clone(), url.clone());
request_sink.unbounded_send(Request::Distribution(distribution))?;
let distribution = Dist::from_url(package_name.clone(), url.clone());
request_sink.unbounded_send(Request::Dist(distribution))?;
}
}
}
@ -331,12 +329,12 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
// Emit a request to fetch the metadata for this version.
if in_flight.insert_file(&candidate.file) {
let distribution = Distribution::from_registry(
let distribution = Dist::from_registry(
candidate.package_name.clone(),
candidate.version.clone().into(),
candidate.file.clone().into(),
);
request_sink.unbounded_send(Request::Distribution(distribution))?;
request_sink.unbounded_send(Request::Dist(distribution))?;
}
}
Ok(())
@ -423,12 +421,12 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
// Emit a request to fetch the metadata for this version.
if in_flight.insert_file(&candidate.file) {
let distribution = Distribution::from_registry(
let distribution = Dist::from_registry(
candidate.package_name.clone(),
candidate.version.clone().into(),
candidate.file.clone().into(),
);
request_sink.unbounded_send(Request::Distribution(distribution))?;
request_sink.unbounded_send(Request::Dist(distribution))?;
}
let version = candidate.version.clone();
@ -543,24 +541,24 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
let version = PubGrubVersion::from(filename.version.clone());
match version_map.entry(version) {
std::collections::btree_map::Entry::Occupied(mut entry) => {
if matches!(entry.get(), DistributionFile::Sdist(_)) {
if matches!(entry.get(), DistFile::Sdist(_)) {
// Wheels get precedence over source distributions.
entry.insert(DistributionFile::from(WheelFile(file)));
entry.insert(DistFile::from(WheelFile(file)));
}
}
std::collections::btree_map::Entry::Vacant(entry) => {
entry.insert(DistributionFile::from(WheelFile(file)));
entry.insert(DistFile::from(WheelFile(file)));
}
}
}
} else if let Ok(filename) =
SourceDistributionFilename::parse(file.filename.as_str(), &package_name)
SourceDistFilename::parse(file.filename.as_str(), &package_name)
{
let version = PubGrubVersion::from(filename.version.clone());
if let std::collections::btree_map::Entry::Vacant(entry) =
version_map.entry(version)
{
entry.insert(DistributionFile::from(SdistFile(file)));
entry.insert(DistFile::from(SdistFile(file)));
}
}
}
@ -569,26 +567,26 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
.packages
.insert(package_name.clone(), version_map);
}
Response::Distribution(Distribution::Built(distribution), metadata, ..) => {
Response::Dist(Dist::Built(distribution), metadata, ..) => {
trace!("Received built distribution metadata for: {distribution}");
self.index
.distributions
.insert(distribution.distribution_id(), metadata);
}
Response::Distribution(Distribution::Source(distribution), metadata, precise) => {
Response::Dist(Dist::Source(distribution), metadata, precise) => {
trace!("Received source distribution metadata for: {distribution}");
self.index
.distributions
.insert(distribution.distribution_id(), metadata);
if let Some(precise) = precise {
match distribution {
SourceDistribution::DirectUrl(sdist) => {
SourceDist::DirectUrl(sdist) => {
self.index.redirects.insert(sdist.url.clone(), precise);
}
SourceDistribution::Git(sdist) => {
SourceDist::Git(sdist) => {
self.index.redirects.insert(sdist.url.clone(), precise);
}
SourceDistribution::Registry(_) => {}
SourceDist::Registry(_) => {}
}
}
}
@ -610,17 +608,17 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
}
// Fetch wheel metadata.
Request::Distribution(Distribution::Built(distribution)) => {
Request::Dist(Dist::Built(distribution)) => {
let metadata =
match &distribution {
BuiltDistribution::Registry(wheel) => {
BuiltDist::Registry(wheel) => {
self.client
.wheel_metadata(wheel.file.clone())
.map_err(ResolveError::Client)
.await?
}
BuiltDistribution::DirectUrl(wheel) => {
let fetcher = BuiltDistributionFetcher::new(self.build_context.cache());
BuiltDist::DirectUrl(wheel) => {
let fetcher = BuiltDistFetcher::new(self.build_context.cache());
match fetcher.find_dist_info(wheel, self.tags) {
Ok(Some(metadata)) => {
debug!("Found wheel metadata in cache: {wheel}");
@ -630,10 +628,7 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
debug!("Downloading wheel: {wheel}");
fetcher.download_wheel(wheel, self.client).await.map_err(
|err| {
ResolveError::from_built_distribution(
distribution.clone(),
err,
)
ResolveError::from_built_dist(distribution.clone(), err)
},
)?
}
@ -641,10 +636,7 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
error!("Failed to read wheel from cache: {err}");
fetcher.download_wheel(wheel, self.client).await.map_err(
|err| {
ResolveError::from_built_distribution(
distribution.clone(),
err,
)
ResolveError::from_built_dist(distribution.clone(), err)
},
)?
}
@ -659,30 +651,26 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
});
}
Ok(Response::Distribution(
Distribution::Built(distribution),
metadata,
None,
))
Ok(Response::Dist(Dist::Built(distribution), metadata, None))
}
// Fetch source distribution metadata.
Request::Distribution(Distribution::Source(sdist)) => {
Request::Dist(Dist::Source(sdist)) => {
let lock = self.locks.acquire(&sdist).await;
let _guard = lock.lock().await;
let fetcher = if let Some(reporter) = &self.reporter {
SourceDistributionFetcher::new(self.build_context).with_reporter(Facade {
SourceDistFetcher::new(self.build_context).with_reporter(Facade {
reporter: reporter.clone(),
})
} else {
SourceDistributionFetcher::new(self.build_context)
SourceDistFetcher::new(self.build_context)
};
let precise = fetcher
.precise(&sdist)
.await
.map_err(|err| ResolveError::from_source_distribution(sdist.clone(), err))?;
.map_err(|err| ResolveError::from_source_dist(sdist.clone(), err))?;
let task = self
.reporter
@ -692,19 +680,17 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
let metadata = {
// Insert the `precise`, if it exists.
let sdist = match sdist.clone() {
SourceDistribution::DirectUrl(sdist) => {
SourceDistribution::DirectUrl(DirectUrlSourceDistribution {
SourceDist::DirectUrl(sdist) => {
SourceDist::DirectUrl(DirectUrlSourceDist {
url: precise.clone().unwrap_or_else(|| sdist.url.clone()),
..sdist
})
}
SourceDistribution::Git(sdist) => {
SourceDistribution::Git(GitSourceDistribution {
url: precise.clone().unwrap_or_else(|| sdist.url.clone()),
..sdist
})
}
sdist @ SourceDistribution::Registry(_) => sdist,
SourceDist::Git(sdist) => SourceDist::Git(GitSourceDist {
url: precise.clone().unwrap_or_else(|| sdist.url.clone()),
..sdist
}),
sdist @ SourceDist::Registry(_) => sdist,
};
match fetcher.find_dist_info(&sdist, self.tags) {
@ -717,18 +703,14 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
fetcher
.download_and_build_sdist(&sdist, self.client)
.await
.map_err(|err| {
ResolveError::from_source_distribution(sdist.clone(), err)
})?
.map_err(|err| ResolveError::from_source_dist(sdist.clone(), err))?
}
Err(err) => {
error!("Failed to read source distribution from cache: {err}",);
fetcher
.download_and_build_sdist(&sdist, self.client)
.await
.map_err(|err| {
ResolveError::from_source_distribution(sdist.clone(), err)
})?
.map_err(|err| ResolveError::from_source_dist(sdist.clone(), err))?
}
}
};
@ -746,11 +728,7 @@ impl<'a, Context: BuildContext + Sync> Resolver<'a, Context> {
}
}
Ok(Response::Distribution(
Distribution::Source(sdist),
metadata,
precise,
))
Ok(Response::Dist(Dist::Source(sdist), metadata, precise))
}
}
}
@ -790,10 +768,10 @@ pub trait Reporter: Send + Sync {
fn on_complete(&self);
/// Callback to invoke when a source distribution build is kicked off.
fn on_build_start(&self, distribution: &SourceDistribution) -> usize;
fn on_build_start(&self, dist: &SourceDist) -> usize;
/// Callback to invoke when a source distribution build is complete.
fn on_build_complete(&self, distribution: &SourceDistribution, id: usize);
fn on_build_complete(&self, dist: &SourceDist, id: usize);
/// Callback to invoke when a repository checkout begins.
fn on_checkout_start(&self, url: &Url, rev: &str) -> usize;
@ -824,7 +802,7 @@ enum Request {
/// A request to fetch the metadata for a package.
Package(PackageName),
/// A request to fetch the metadata for a built or source distribution.
Distribution(Distribution),
Dist(Dist),
}
#[derive(Debug)]
@ -833,10 +811,10 @@ enum Response {
/// The returned metadata for a package hosted on a registry.
Package(PackageName, SimpleJson),
/// The returned metadata for a distribution.
Distribution(Distribution, Metadata21, Option<Url>),
Dist(Dist, Metadata21, Option<Url>),
}
pub(crate) type VersionMap = BTreeMap<PubGrubVersion, DistributionFile>;
pub(crate) type VersionMap = BTreeMap<PubGrubVersion, DistFile>;
/// In-memory index of in-flight network requests. Any request in an [`InFlight`] state will be
/// eventually be inserted into an [`Index`].
@ -855,10 +833,10 @@ impl InFlight {
self.packages.insert(package_name.clone())
}
fn insert_file(&mut self, file: &DistributionFile) -> bool {
fn insert_file(&mut self, file: &DistFile) -> bool {
match file {
DistributionFile::Wheel(file) => self.files.insert(file.hashes.sha256.clone()),
DistributionFile::Sdist(file) => self.files.insert(file.hashes.sha256.clone()),
DistFile::Wheel(file) => self.files.insert(file.hashes.sha256.clone()),
DistFile::Sdist(file) => self.files.insert(file.hashes.sha256.clone()),
}
}