Apply normcase to line from easy-install.pth (#3451)

Thanks for the suggestion from
https://github.com/astral-sh/uv/pull/3415#discussion_r1591772942

Also it looks like you improved `egg-link` parsing in
e23c91f52e
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)
This commit is contained in:
Shantanu 2024-05-08 01:40:21 -07:00 committed by GitHub
parent 1ad6aa8a23
commit 962fde29b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 4 deletions

View File

@ -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);
};

View File

@ -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.
@ -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());

View File

@ -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<()> {
@ -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`.