@@ -28,19 +28,26 @@ def unzip_sdist_to_directory(containing_folder: str) -> str:
2828 tars = glob .glob (os .path .join (containing_folder , "*.tar.gz" ))
2929 return unzip_file_to_directory (tars [0 ], containing_folder )
3030
31-
3231def unzip_file_to_directory (path_to_zip_file : str , extract_location : str ) -> str :
3332 if path_to_zip_file .endswith (".zip" ):
3433 with zipfile .ZipFile (path_to_zip_file , "r" ) as zip_ref :
3534 zip_ref .extractall (extract_location )
3635 extracted_dir = os .path .basename (os .path .splitext (path_to_zip_file )[0 ])
3736 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 :
4039 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" )
4451
4552def move_and_rename (source_location ):
4653 new_location = os .path .join (os .path .dirname (source_location ), "unzipped" )
0 commit comments