4
4
import os
5
5
import shutil
6
6
import site
7
+ import re
7
8
from optparse import SUPPRESS_HELP , Values
8
9
from typing import List , Optional
9
10
@@ -574,7 +575,15 @@ def _handle_target_dir(
574
575
shutil .rmtree (target_item_dir )
575
576
else :
576
577
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 )
578
587
shutil .move (os .path .join (lib_dir , item ), target_item_dir )
579
588
580
589
def _determine_conflicts (
@@ -781,3 +790,17 @@ def create_os_error_message(
781
790
)
782
791
783
792
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