Remove __pycache__ directories when uninstalling (#397)

According to the [packaging
documentation](https://packaging.python.org/en/latest/specifications/binary-distribution-format/#binary-distribution-format),
"uninstallers should be smart enough to remove .pyc even if it is not
mentioned in RECORD". Previously, we weren't handling this case, so if
you installed via Puffin, then imported a file (to trigger bytecode
compilation), then uninstalled, we'd leave spare `__pycache__`
directories around.

Closes https://github.com/astral-sh/puffin/issues/395.
This commit is contained in:
Charlie Marsh 2023-11-10 11:55:33 -08:00 committed by GitHub
parent 63f7f65190
commit e8108cb28b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 1 deletions

View file

@ -64,6 +64,19 @@ pub fn uninstall_wheel(dist_info: &Path) -> Result<Uninstall, Error> {
break;
}
// If the directory contains a `__pycache__` directory, always remove it. `__pycache__`
// may or may not be listed in the RECORD, but installers are expected to be smart
// enough to remove it either way.
let pycache = path.join("__pycache__");
match fs::remove_dir_all(&pycache) {
Ok(()) => {
debug!("Removed directory: {}", pycache.display());
dir_count += 1;
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
Err(err) => return Err(err.into()),
}
// Try to read from the directory. If it doesn't exist, assume we deleted it in a
// previous iteration.
let mut read_dir = match fs::read_dir(path) {