From 90c04dc6d68bd90b7bc9994edba61c45da84790f Mon Sep 17 00:00:00 2001 From: Bas Zalmstra Date: Tue, 28 Nov 2023 13:54:48 +0100 Subject: [PATCH 1/2] feat: remove empty folders on python uninstall --- Cargo.lock | 2 +- src/environment.rs | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 314364b96..6142c446d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2922,7 +2922,7 @@ dependencies = [ [[package]] name = "rattler_installs_packages" version = "0.1.0" -source = "git+https://github.com/prefix-dev/rattler_installs_packages?branch=main#2ee92fdabfd2c73d52b5d6702b39268b48897a41" +source = "git+https://github.com/prefix-dev/rattler_installs_packages?branch=main#94d965f055cb23dc82a821c53a3338d4dcc74bd7" dependencies = [ "async-once-cell", "async-trait", diff --git a/src/environment.rs b/src/environment.rs index a125fa42b..6d398f4cf 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -257,9 +257,6 @@ async fn update_python_distributions( .dist_info .strip_prefix(site_package_path) .expect("the dist-info path must be a sub-path of the site-packages path"); - rip::uninstall::uninstall_distribution(&prefix.root().join(site_package_path), relative_dist_info) - .into_diagnostic() - .with_context(|| format!("could not uninstall python package {}-{}. Manually remove the `.pixi/env` folder and try again.", &python_distribution.name, &python_distribution.version))?; // HACK: Also remove the HASH file that pixi writes. Ignore the error if its there. We // should probably actually add this file to the RECORD. @@ -269,6 +266,10 @@ async fn update_python_distributions( .join(&python_distribution.dist_info) .join("HASH"), ); + + rip::uninstall::uninstall_distribution(&prefix.root().join(site_package_path), relative_dist_info) + .into_diagnostic() + .with_context(|| format!("could not uninstall python package {}-{}. Manually remove the `.pixi/env` folder and try again.", &python_distribution.name, &python_distribution.version))?; } } @@ -534,14 +535,15 @@ fn remove_old_python_distributions( .dist_info .strip_prefix(site_package_path) .expect("the dist-info path must be a sub-path of the site-packages path"); - rip::uninstall::uninstall_distribution(&prefix.root().join(site_package_path), relative_dist_info) - .into_diagnostic() - .with_context(|| format!("could not uninstall python package {}-{}. Manually remove the `.pixi/env` folder and try again.", &python_package.name, &python_package.version))?; // HACK: Also remove the HASH file that pixi writes. Ignore the error if its there. We // should probably actually add this file to the RECORD. let _ = std::fs::remove_file(prefix.root().join(&python_package.dist_info).join("HASH")); + rip::uninstall::uninstall_distribution(&prefix.root().join(site_package_path), relative_dist_info) + .into_diagnostic() + .with_context(|| format!("could not uninstall python package {}-{}. Manually remove the `.pixi/env` folder and try again.", &python_package.name, &python_package.version))?; + pb.inc(1); } From 2c61a0a21a8ca37d1b02f9100b594564548a41d2 Mon Sep 17 00:00:00 2001 From: Bas Zalmstra Date: Tue, 28 Nov 2023 15:46:34 +0100 Subject: [PATCH 2/2] refactor: create single function for pixi uninstall --- src/environment.rs | 73 +++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 40 deletions(-) diff --git a/src/environment.rs b/src/environment.rs index 6d398f4cf..001e3c9da 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -248,28 +248,11 @@ async fn update_python_distributions( let site_package_path = install_paths.site_packages(); for python_distribution in python_distributions_to_remove { - tracing::info!( - "uninstalling python package {}-{}", - &python_distribution.name, - &python_distribution.version - ); - let relative_dist_info = python_distribution - .dist_info - .strip_prefix(site_package_path) - .expect("the dist-info path must be a sub-path of the site-packages path"); - - // HACK: Also remove the HASH file that pixi writes. Ignore the error if its there. We - // should probably actually add this file to the RECORD. - let _ = std::fs::remove_file( - prefix - .root() - .join(&python_distribution.dist_info) - .join("HASH"), - ); - - rip::uninstall::uninstall_distribution(&prefix.root().join(site_package_path), relative_dist_info) - .into_diagnostic() - .with_context(|| format!("could not uninstall python package {}-{}. Manually remove the `.pixi/env` folder and try again.", &python_distribution.name, &python_distribution.version))?; + uninstall_pixi_installed_distribution( + &prefix, + &site_package_path, + &python_distribution, + )?; } } @@ -520,29 +503,12 @@ fn remove_old_python_distributions( // Remove the python packages let site_package_path = install_paths.site_packages(); for python_package in current_python_packages { - tracing::info!( - "uninstalling python package from previous python version {}-{}", - &python_package.name, - &python_package.version - ); - pb.set_message(format!( "{} {}", &python_package.name, &python_package.version )); - let relative_dist_info = python_package - .dist_info - .strip_prefix(site_package_path) - .expect("the dist-info path must be a sub-path of the site-packages path"); - - // HACK: Also remove the HASH file that pixi writes. Ignore the error if its there. We - // should probably actually add this file to the RECORD. - let _ = std::fs::remove_file(prefix.root().join(&python_package.dist_info).join("HASH")); - - rip::uninstall::uninstall_distribution(&prefix.root().join(site_package_path), relative_dist_info) - .into_diagnostic() - .with_context(|| format!("could not uninstall python package {}-{}. Manually remove the `.pixi/env` folder and try again.", &python_package.name, &python_package.version))?; + uninstall_pixi_installed_distribution(&prefix, &site_package_path, &python_package)?; pb.inc(1); } @@ -550,6 +516,33 @@ fn remove_old_python_distributions( Ok(()) } +/// Uninstalls a python distribution that was previously installed by pixi. +fn uninstall_pixi_installed_distribution( + prefix: &&Prefix, + site_package_path: &&Path, + python_package: &Distribution, +) -> miette::Result<()> { + tracing::info!( + "uninstalling python package {}-{}", + &python_package.name, + &python_package.version + ); + let relative_dist_info = python_package + .dist_info + .strip_prefix(site_package_path) + .expect("the dist-info path must be a sub-path of the site-packages path"); + + // HACK: Also remove the HASH file that pixi writes. Ignore the error if its there. We + // should probably actually add this file to the RECORD. + let _ = std::fs::remove_file(prefix.root().join(&python_package.dist_info).join("HASH")); + + rip::uninstall::uninstall_distribution(&prefix.root().join(site_package_path), relative_dist_info) + .into_diagnostic() + .with_context(|| format!("could not uninstall python package {}-{}. Manually remove the `.pixi/env` folder and try again.", &python_package.name, &python_package.version))?; + + Ok(()) +} + /// Determine which python packages we can leave untouched and which python packages should be /// removed. fn determine_python_distributions_to_remove_and_install<'p>(