Skip to content

Commit 54dfb89

Browse files
committed
Fix - issue pypa#12890
1 parent 8c7df92 commit 54dfb89

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/pip/_internal/commands/install.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import shutil
66
import site
7+
import re
78
from optparse import SUPPRESS_HELP, Values
89
from typing import List, Optional
910

@@ -574,7 +575,15 @@ def _handle_target_dir(
574575
shutil.rmtree(target_item_dir)
575576
else:
576577
os.remove(target_item_dir)
577-
578+
if item.endswith((".dist-info", ".egg-info")):
579+
matched_info_dir = _contain_info_dir(item, target_dir)
580+
if matched_info_dir:
581+
if not upgrade:
582+
continue
583+
else:
584+
info_dir = os.path.join(target_dir, matched_info_dir)
585+
if os.path.exists(info_dir):
586+
shutil.rmtree(info_dir)
578587
shutil.move(os.path.join(lib_dir, item), target_item_dir)
579588

580589
def _determine_conflicts(
@@ -781,3 +790,17 @@ def create_os_error_message(
781790
)
782791

783792
return "".join(parts).strip() + "\n"
793+
794+
def _contain_info_dir(item: str, target_dir: str) -> str:
795+
"""Determine whether the item is a metadata_location (.dist-info or egg-info for legacy).
796+
If there is another metadata_location for the package in the 'target_dir',
797+
return found metadata_location that has been found.
798+
"""
799+
raw_name = item.rpartition(".")[0].partition("-")[0]
800+
dist_info_re = re.compile(rf"{raw_name}-[a-z0-9_.!+-]+\.dist-info$", re.IGNORECASE)
801+
egg_info_re = re.compile(rf"{raw_name}[^\s/\\]*\.egg-info$", re.IGNORECASE)
802+
for path in os.listdir(target_dir):
803+
if dist_info_re.match(path) or egg_info_re.match(path):
804+
return path
805+
806+
return None

0 commit comments

Comments
 (0)