Skip to content

Commit

Permalink
Apply normcase to line from easy-install.pth (#3451)
Browse files Browse the repository at this point in the history
Thanks for the suggestion from
#3415 (comment)

Also it looks like you improved `egg-link` parsing in
e23c91f
so copying the changes over to the other parse site (happy to move this
to a helper too, if so lmk where to put it)
  • Loading branch information
hauntsaninja authored May 8, 2024
1 parent 1ad6aa8 commit 962fde2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
11 changes: 8 additions & 3 deletions crates/distribution-types/src/installed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,14 @@ impl InstalledDist {
// https://setuptools.pypa.io/en/latest/deprecated/python_eggs.html#egg-links
// https://github.com/pypa/pip/blob/946f95d17431f645da8e2e0bf4054a72db5be766/src/pip/_internal/metadata/importlib/_envs.py#L86-L108
let contents = fs::read_to_string(path)?;
let target = if let Some(line) = contents.lines().find(|line| !line.is_empty()) {
PathBuf::from(line.trim())
} else {
let Some(target) = contents.lines().find_map(|line| {
let line = line.trim();
if line.is_empty() {
None
} else {
Some(PathBuf::from(line))
}
}) else {
warn!("Invalid `.egg-link` file: {path:?}");
return Ok(None);
};
Expand Down
11 changes: 11 additions & 0 deletions crates/install-wheel-rs/src/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ pub fn uninstall_egg(egg_info: &Path) -> Result<Uninstall, Error> {
})
}

fn normcase(s: &str) -> String {
if cfg!(windows) {
s.replace('/', "\\").to_lowercase()
} else {
s.to_owned()
}
}

static EASY_INSTALL_PTH: Lazy<Mutex<i32>> = Lazy::new(Mutex::default);

/// Uninstall the legacy editable represented by the `.egg-link` file.
Expand All @@ -233,6 +241,9 @@ pub fn uninstall_legacy_editable(egg_link: &Path) -> Result<Uninstall, Error> {
})
.ok_or_else(|| Error::InvalidEggLink(egg_link.to_path_buf()))?;

// This comes from `pkg_resources.normalize_path`
let target_line = normcase(target_line);

match fs::remove_file(egg_link) {
Ok(()) => {
debug!("Removed file: {}", egg_link.display());
Expand Down
10 changes: 9 additions & 1 deletion crates/uv/tests/pip_uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,14 @@ fn uninstall_egg_info() -> Result<()> {
Ok(())
}

fn normcase(s: &str) -> String {
if cfg!(windows) {
s.replace('/', "\\").to_lowercase()
} else {
s.to_owned()
}
}

/// Uninstall a legacy editable package in a virtual environment.
#[test]
fn uninstall_legacy_editable() -> Result<()> {
Expand Down Expand Up @@ -502,7 +510,7 @@ Version: 0.22.0

site_packages.child("easy-install.pth").write_str(&format!(
"something\n{}\nanother thing\n",
target.path().to_str().unwrap()
normcase(target.path().to_str().unwrap())
))?;

// Run `pip uninstall`.
Expand Down

0 comments on commit 962fde2

Please sign in to comment.