Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect existing .egg-info files in site packages #4082

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 85 additions & 34 deletions crates/distribution-types/src/installed.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::str::FromStr;

Expand All @@ -20,8 +21,10 @@ pub enum InstalledDist {
Registry(InstalledRegistryDist),
/// The distribution was derived from an arbitrary URL.
Url(InstalledDirectUrlDist),
/// The distribution was derived from pre-existing `.egg-info` file (as installed by distutils).
EggInfoFile(InstalledEggInfoFile),
/// The distribution was derived from pre-existing `.egg-info` directory.
EggInfo(InstalledEggInfo),
EggInfoDirectory(InstalledEggInfoDirectory),
/// The distribution was derived from an `.egg-link` pointer.
LegacyEditable(InstalledLegacyEditable),
}
Expand All @@ -44,7 +47,14 @@ pub struct InstalledDirectUrlDist {
}

#[derive(Debug, Clone)]
pub struct InstalledEggInfo {
pub struct InstalledEggInfoFile {
pub name: PackageName,
pub version: Version,
pub path: PathBuf,
}

#[derive(Debug, Clone)]
pub struct InstalledEggInfoDirectory {
pub name: PackageName,
pub version: Version,
pub path: PathBuf,
Expand Down Expand Up @@ -107,27 +117,49 @@ impl InstalledDist {
};
}

// Ex) `zstandard-0.22.0-py3.12.egg-info`
if path.extension().is_some_and(|ext| ext == "egg-info") {
let Some(file_stem) = path.file_stem() else {
return Ok(None);
};
let Some(file_stem) = file_stem.to_str() else {
return Ok(None);
};
let Some((name, version_python)) = file_stem.split_once('-') else {
return Ok(None);
};
let Some((version, _)) = version_python.split_once('-') else {
return Ok(None);
};
let name = PackageName::from_str(name)?;
let version = Version::from_str(version).map_err(|err| anyhow!(err))?;
return Ok(Some(Self::EggInfo(InstalledEggInfo {
name,
version,
path: path.to_path_buf(),
})));
// Ex) `zstandard-0.22.0-py3.12.egg-info`
if path.is_dir() {
let Some(file_stem) = path.file_stem() else {
return Ok(None);
};
let Some(file_stem) = file_stem.to_str() else {
return Ok(None);
};
let Some((name, version_python)) = file_stem.split_once('-') else {
return Ok(None);
};
let Some((version, _)) = version_python.split_once('-') else {
return Ok(None);
};
let name = PackageName::from_str(name)?;
let version = Version::from_str(version).map_err(|err| anyhow!(err))?;
return Ok(Some(Self::EggInfoDirectory(InstalledEggInfoDirectory {
name,
version,
path: path.to_path_buf(),
})));
}

// Ex) `vtk-9.2.6.egg-info`
if path.is_file() {
let Some(file_stem) = path.file_stem() else {
return Ok(None);
};
let Some(file_stem) = file_stem.to_str() else {
return Ok(None);
};
let Some((name, version)) = file_stem.split_once('-') else {
return Ok(None);
};
let name = PackageName::from_str(name)?;
let version = Version::from_str(version).map_err(|err| anyhow!(err))?;
return Ok(Some(Self::EggInfoFile(InstalledEggInfoFile {
name,
version,
path: path.to_path_buf(),
})));
}
}

// Ex) `zstandard.egg-link`
Expand Down Expand Up @@ -199,7 +231,8 @@ impl InstalledDist {
match self {
Self::Registry(dist) => &dist.path,
Self::Url(dist) => &dist.path,
Self::EggInfo(dist) => &dist.path,
Self::EggInfoDirectory(dist) => &dist.path,
Self::EggInfoFile(dist) => &dist.path,
Self::LegacyEditable(dist) => &dist.egg_info,
}
}
Expand All @@ -209,7 +242,8 @@ impl InstalledDist {
match self {
Self::Registry(dist) => &dist.version,
Self::Url(dist) => &dist.version,
Self::EggInfo(dist) => &dist.version,
Self::EggInfoDirectory(dist) => &dist.version,
Self::EggInfoFile(dist) => &dist.version,
Self::LegacyEditable(dist) => &dist.version,
}
}
Expand Down Expand Up @@ -238,13 +272,14 @@ impl InstalledDist {
)
})
}
Self::EggInfo(_) | Self::LegacyEditable(_) => {
Self::EggInfoFile(_) | Self::EggInfoDirectory(_) | Self::LegacyEditable(_) => {
let path = match self {
Self::EggInfo(dist) => dist.path.join("PKG-INFO"),
Self::LegacyEditable(dist) => dist.egg_info.join("PKG-INFO"),
Self::EggInfoFile(dist) => Cow::Borrowed(&dist.path),
Self::EggInfoDirectory(dist) => Cow::Owned(dist.path.join("PKG-INFO")),
Self::LegacyEditable(dist) => Cow::Owned(dist.egg_info.join("PKG-INFO")),
_ => unreachable!(),
};
let contents = fs::read(&path)?;
let contents = fs::read(path.as_ref())?;
pypi_types::Metadata23::parse_metadata(&contents).with_context(|| {
format!(
"Failed to parse `PKG-INFO` file at: {}",
Expand Down Expand Up @@ -278,7 +313,8 @@ impl InstalledDist {
match self {
Self::Registry(_) => None,
Self::Url(dist) => dist.editable.then_some(&dist.url),
Self::EggInfo(_) => None,
Self::EggInfoFile(_) => None,
Self::EggInfoDirectory(_) => None,
Self::LegacyEditable(dist) => Some(&dist.target_url),
}
}
Expand All @@ -288,7 +324,8 @@ impl InstalledDist {
match self {
Self::Registry(_) => false,
Self::Url(dist) => matches!(&*dist.direct_url, DirectUrl::LocalDirectory { .. }),
Self::EggInfo(_) => false,
Self::EggInfoFile(_) => false,
Self::EggInfoDirectory(_) => false,
Self::LegacyEditable(_) => true,
}
}
Expand All @@ -312,7 +349,13 @@ impl Name for InstalledDirectUrlDist {
}
}

impl Name for InstalledEggInfo {
impl Name for InstalledEggInfoFile {
fn name(&self) -> &PackageName {
&self.name
}
}

impl Name for InstalledEggInfoDirectory {
fn name(&self) -> &PackageName {
&self.name
}
Expand All @@ -329,7 +372,8 @@ impl Name for InstalledDist {
match self {
Self::Registry(dist) => dist.name(),
Self::Url(dist) => dist.name(),
Self::EggInfo(dist) => dist.name(),
Self::EggInfoDirectory(dist) => dist.name(),
Self::EggInfoFile(dist) => dist.name(),
Self::LegacyEditable(dist) => dist.name(),
}
}
Expand All @@ -347,7 +391,13 @@ impl InstalledMetadata for InstalledDirectUrlDist {
}
}

impl InstalledMetadata for InstalledEggInfo {
impl InstalledMetadata for InstalledEggInfoFile {
fn installed_version(&self) -> InstalledVersion {
InstalledVersion::Version(&self.version)
}
}

impl InstalledMetadata for InstalledEggInfoDirectory {
fn installed_version(&self) -> InstalledVersion {
InstalledVersion::Version(&self.version)
}
Expand All @@ -364,7 +414,8 @@ impl InstalledMetadata for InstalledDist {
match self {
Self::Registry(dist) => dist.installed_version(),
Self::Url(dist) => dist.installed_version(),
Self::EggInfo(dist) => dist.installed_version(),
Self::EggInfoFile(dist) => dist.installed_version(),
Self::EggInfoDirectory(dist) => dist.installed_version(),
Self::LegacyEditable(dist) => dist.installed_version(),
}
}
Expand Down
24 changes: 21 additions & 3 deletions crates/distribution-types/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::error::Error;
use crate::{
BuiltDist, CachedDirectUrlDist, CachedDist, CachedRegistryDist, DirectUrlBuiltDist,
DirectUrlSourceDist, Dist, DistributionId, GitSourceDist, InstalledDirectUrlDist,
InstalledDist, InstalledRegistryDist, InstalledVersion, LocalDist, PackageId, PathBuiltDist,
PathSourceDist, RegistryBuiltWheel, RegistrySourceDist, ResourceId, SourceDist, VersionId,
VersionOrUrlRef,
InstalledDist, InstalledEggInfoDirectory, InstalledEggInfoFile, InstalledLegacyEditable,
InstalledRegistryDist, InstalledVersion, LocalDist, PackageId, PathBuiltDist, PathSourceDist,
RegistryBuiltWheel, RegistrySourceDist, ResourceId, SourceDist, VersionId, VersionOrUrlRef,
};

pub trait Name {
Expand Down Expand Up @@ -189,6 +189,24 @@ impl std::fmt::Display for InstalledRegistryDist {
}
}

impl std::fmt::Display for InstalledEggInfoFile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.installed_version())
}
}

impl std::fmt::Display for InstalledEggInfoDirectory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.installed_version())
}
}

impl std::fmt::Display for InstalledLegacyEditable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.installed_version())
}
}

impl std::fmt::Display for PathBuiltDist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.name(), self.version_or_url())
Expand Down
7 changes: 3 additions & 4 deletions crates/uv-installer/src/site_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ impl SitePackages {
.filter_map(|read_dir| match read_dir {
Ok(entry) => match entry.file_type() {
Ok(file_type) => (file_type.is_dir()
|| entry
.path()
.extension()
.map_or(false, |ext| ext == "egg-link"))
|| entry.path().extension().map_or(false, |ext| {
ext == "egg-link" || ext == "egg-info"
}))
.then_some(Ok(entry.path())),
Err(err) => Some(Err(err)),
},
Expand Down
11 changes: 7 additions & 4 deletions crates/uv-installer/src/uninstall.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use distribution_types::InstalledDist;
use distribution_types::{InstalledDist, InstalledEggInfoFile};

/// Uninstall a package from the specified Python environment.
pub async fn uninstall(
Expand All @@ -8,12 +8,13 @@ pub async fn uninstall(
let dist = dist.clone();
move || match dist {
InstalledDist::Registry(_) | InstalledDist::Url(_) => {
install_wheel_rs::uninstall_wheel(dist.path())
Ok(install_wheel_rs::uninstall_wheel(dist.path())?)
}
InstalledDist::EggInfo(_) => install_wheel_rs::uninstall_egg(dist.path()),
InstalledDist::EggInfoDirectory(_) => Ok(install_wheel_rs::uninstall_egg(dist.path())?),
InstalledDist::LegacyEditable(dist) => {
install_wheel_rs::uninstall_legacy_editable(&dist.egg_link)
Ok(install_wheel_rs::uninstall_legacy_editable(&dist.egg_link)?)
}
InstalledDist::EggInfoFile(dist) => Err(UninstallError::Distutils(dist)),
}
})
.await??;
Expand All @@ -23,6 +24,8 @@ pub async fn uninstall(

#[derive(thiserror::Error, Debug)]
pub enum UninstallError {
#[error("Unable to uninstall `{0}`. distutils-installed distributions do not include the metadata required to uninstall safely.")]
Distutils(InstalledEggInfoFile),
#[error(transparent)]
Uninstall(#[from] install_wheel_rs::Error),
#[error(transparent)]
Expand Down
5 changes: 4 additions & 1 deletion crates/uv/src/commands/pip/freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ pub(crate) fn pip_freeze(
writeln!(printer.stdout(), "{} @ {}", dist.name().bold(), dist.url)?;
}
}
InstalledDist::EggInfo(dist) => {
InstalledDist::EggInfoFile(dist) => {
writeln!(printer.stdout(), "{}=={}", dist.name().bold(), dist.version)?;
}
InstalledDist::EggInfoDirectory(dist) => {
writeln!(printer.stdout(), "{}=={}", dist.name().bold(), dist.version)?;
}
InstalledDist::LegacyEditable(dist) => {
Expand Down
Loading