@@ -28,19 +28,26 @@ def unzip_sdist_to_directory(containing_folder: str) -> str:
28
28
tars = glob .glob (os .path .join (containing_folder , "*.tar.gz" ))
29
29
return unzip_file_to_directory (tars [0 ], containing_folder )
30
30
31
-
32
31
def unzip_file_to_directory (path_to_zip_file : str , extract_location : str ) -> str :
33
32
if path_to_zip_file .endswith (".zip" ):
34
33
with zipfile .ZipFile (path_to_zip_file , "r" ) as zip_ref :
35
34
zip_ref .extractall (extract_location )
36
35
extracted_dir = os .path .basename (os .path .splitext (path_to_zip_file )[0 ])
37
36
return os .path .join (extract_location , extracted_dir )
38
- else :
39
- with tarfile .open (path_to_zip_file ) as tar_ref :
37
+ elif path_to_zip_file . endswith ( ".tar.gz" ) or path_to_zip_file . endswith ( ".tgz" ) :
38
+ with tarfile .open (path_to_zip_file , "r:gz" ) as tar_ref :
40
39
tar_ref .extractall (extract_location )
41
- extracted_dir = os .path .basename (path_to_zip_file ).replace (".tar.gz" , "" )
42
- return os .path .join (extract_location , extracted_dir )
43
-
40
+ top_level_dir = None
41
+ for member in tar_ref .getmembers ():
42
+ if member .isdir ():
43
+ top_level_dir = member .name .split ('/' )[0 ]
44
+ break
45
+ if top_level_dir :
46
+ return os .path .join (extract_location , top_level_dir )
47
+ else :
48
+ raise ValueError ("Failed to determine the top-level directory in the tar.gz archive" )
49
+ else :
50
+ raise ValueError ("Unsupported file format" )
44
51
45
52
def move_and_rename (source_location ):
46
53
new_location = os .path .join (os .path .dirname (source_location ), "unzipped" )
0 commit comments