Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 7, 2024
1 parent 5dbb8aa commit 16162f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 44 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
# For Ubuntu and Windows, this requires Organization-level configuration
# See: https://docs.github.com/en/actions/using-github-hosted-runners/about-larger-runners/about-larger-runners#about-ubuntu-and-windows-larger-runners
- { os: "ubuntu", runner: "ubuntu-latest-large" }
- { os: "windows", runner: "windows-2019" }
- { os: "windows", runner: "windows-latest-large" }
- { os: "macos", runner: "macos-14" }
fail-fast: false
runs-on:
Expand Down Expand Up @@ -82,10 +82,6 @@ jobs:
- uses: Swatinem/rust-cache@v2
with:
save-if: ${{ github.ref == 'refs/heads/main' }}
- name: "Cargo build"
run: cargo build
- name: "Do thing"
run: ./target/debug/uv venv && ./target/debug/uv pip install lxml && ./target/debug/uv pip uninstall lxml && ./target/debug/uv pip install lxml
- name: "Cargo test"
run: |
cargo nextest run --workspace --status-level skip --failure-output immediate-final --no-fail-fast -j 12 --final-status-level slow
Expand Down
8 changes: 0 additions & 8 deletions crates/distribution-types/src/installed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,6 @@ impl InstalledDist {

/// Read the `METADATA` file from a `.dist-info` directory.
pub fn metadata(&self) -> Result<pypi_types::Metadata21> {
// Print the path.
println!("path: {}", self.path().simplified_display());
// Print everything in the path:
for entry in fs::read_dir(self.path())? {
let entry = entry?;
println!("entry: {}", entry.path().simplified_display());
}

let path = self.path().join("METADATA");
let contents = fs::read(&path)?;
pypi_types::Metadata21::parse(&contents).with_context(|| {
Expand Down
48 changes: 17 additions & 31 deletions crates/install-wheel-rs/src/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use std::collections::BTreeSet;
use std::path::{Component, Path, PathBuf};

use fs_err as fs;
use rustc_hash::FxHashSet;
use tracing::debug;
use uv_fs::Simplified;

use crate::wheel::read_record_file;
use crate::Error;
Expand All @@ -18,46 +16,46 @@ pub fn uninstall_wheel(dist_info: &Path) -> Result<Uninstall, Error> {
};

// Read the RECORD file.
let record_path = dist_info.join("RECORD");
let mut record_file = match fs::File::open(&record_path) {
Ok(record_file) => record_file,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
return Err(Error::MissingRecord(record_path));
}
Err(err) => return Err(err.into()),
let record = {
let record_path = dist_info.join("RECORD");
let mut record_file = match fs::File::open(&record_path) {
Ok(record_file) => record_file,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
return Err(Error::MissingRecord(record_path));
}
Err(err) => return Err(err.into()),
};
read_record_file(&mut record_file)?
};
let record = read_record_file(&mut record_file)?;

let mut file_count = 0usize;
let mut dir_count = 0usize;

// Uninstall the files, keeping track of any directories that are left empty.
let mut visited = BTreeSet::new();
let mut deleted = FxHashSet::default();
for entry in &record {
let path = site_packages.join(&entry.path);
match fs::remove_file(&path) {
Ok(()) => {
println!("Removed file: {}", path.display());
debug!("Removed file: {}", path.display());
file_count += 1;
deleted.insert(normalize_path(&path));
if let Some(parent) = path.parent() {
visited.insert(normalize_path(parent));
}
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(err) => match fs::remove_dir_all(&path) {
Ok(()) => {
println!("Removed directory: {}", path.display());
debug!("Removed directory: {}", path.display());
dir_count += 1;
deleted.insert(normalize_path(&path));
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(_) => return Err(err.into()),
},
}
}


// If any directories were left empty, remove them. Iterate in reverse order such that we visit
// the deepest directories first.
for path in visited.iter().rev() {
Expand All @@ -82,9 +80,8 @@ pub fn uninstall_wheel(dist_info: &Path) -> Result<Uninstall, Error> {
let pycache = path.join("__pycache__");
match fs::remove_dir_all(&pycache) {
Ok(()) => {
println!("Removed directory: {}", pycache.display());
debug!("Removed directory: {}", pycache.display());
dir_count += 1;
deleted.insert(normalize_path(path));
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(err) => return Err(err.into()),
Expand All @@ -98,26 +95,15 @@ pub fn uninstall_wheel(dist_info: &Path) -> Result<Uninstall, Error> {
Err(err) => return Err(err.into()),
};

// Ignore files that were deleted.
let mut entries = read_dir.filter(|entry| {
let Ok(entry) = entry else {
return false;
};
let is_deleted = deleted.contains(&normalize_path(&entry.path()));
println!("entry is deleted: {} : {}", entry.path().simplified_display(), is_deleted);
!is_deleted
});

// If the directory is not empty, we're done.
if entries.next().is_some() {
if read_dir.next().is_some() {
break;
}

fs::remove_dir_all(path)?;
fs::remove_dir(path)?;

println!("Removed directory: {}", path.display());
debug!("Removed directory: {}", path.display());
dir_count += 1;
deleted.insert(normalize_path(path));

if let Some(parent) = path.parent() {
path = parent;
Expand Down

0 comments on commit 16162f9

Please sign in to comment.