@@ -213,21 +213,28 @@ def _takes_arg(f: typing.Callable, arg_name: str) -> bool:
213213
214214def unpack_source (
215215 ctx : context .WorkContext ,
216+ req : Requirement ,
217+ version : Version ,
216218 source_filename : pathlib .Path ,
217219) -> tuple [pathlib .Path , bool ]:
218- sdist_root_name = _sdist_root_name (source_filename )
219- unpack_dir = ctx .work_dir / sdist_root_name
220+ # sdist names are less standardized and the names of the directories they
221+ # contain are also not very standard. Force the names into a predictable
222+ # form based on the override module name for the requirement.
223+ req_name = overrides .pkgname_to_override_module (req .name )
224+ expected_name = f"{ req_name } -{ version } "
225+
226+ # The unpack_dir is a parent dir where we put temporary outputs during the
227+ # build process, including the unpacked source in a subdirectory.
228+ unpack_dir = ctx .work_dir / expected_name
220229 if unpack_dir .exists ():
221230 if ctx .cleanup :
222231 logger .debug ("cleaning up %s" , unpack_dir )
223232 shutil .rmtree (unpack_dir )
224233 else :
225234 logger .info ("reusing %s" , unpack_dir )
226235 return (unpack_dir / unpack_dir .name , False )
227- # We create a unique directory based on the sdist name, but that
228- # may not be the same name as the root directory of the content in
229- # the sdist (due to case, punctuation, etc.), so after we unpack
230- # it look for what was created.
236+
237+ # sdists might be tarballs or zip files.
231238 logger .debug ("unpacking %s to %s" , source_filename , unpack_dir )
232239 if str (source_filename ).endswith (".tar.gz" ):
233240 with tarfile .open (source_filename , "r" ) as t :
@@ -242,18 +249,26 @@ def unpack_source(
242249 else :
243250 raise ValueError (f"Do not know how to unpack source archive { source_filename } " )
244251
245- # if tarball named foo-2.3.1.tar.gz was downloaded, then ensure that after unpacking, the source directory's path is foo-2.3.1/foo-2.3.1
252+ # We create a unique directory based on the requirement name, but that may
253+ # not be the same name as the root directory of the content in the sdist
254+ # (due to case, punctuation, etc.), so after we unpack it look for what was
255+ # created and ensure the extracted directory matches the override module
256+ # name and version of the requirement.
246257 unpacked_root_dir = next (iter (unpack_dir .glob ("*" )))
247- new_unpacked_root_dir = unpacked_root_dir .parent / sdist_root_name
248- if unpacked_root_dir . name != new_unpacked_root_dir . name :
258+ if unpacked_root_dir .name != expected_name :
259+ desired_name = unpacked_root_dir . parent / expected_name
249260 try :
250- shutil .move (str (unpacked_root_dir ), str (new_unpacked_root_dir ))
261+ shutil .move (
262+ str (unpacked_root_dir ),
263+ str (desired_name ),
264+ )
251265 except Exception as err :
252266 raise Exception (
253- f"Could not rename { unpacked_root_dir .name } to { new_unpacked_root_dir . name } : { err } "
267+ f"Could not rename { unpacked_root_dir .name } to { desired_name } : { err } "
254268 ) from err
269+ unpacked_root_dir = desired_name
255270
256- return (new_unpacked_root_dir , True )
271+ return (unpacked_root_dir , True )
257272
258273
259274def patch_source (
@@ -366,7 +381,12 @@ def default_prepare_source(
366381 source_filename : pathlib .Path ,
367382 version : Version ,
368383) -> tuple [pathlib .Path , bool ]:
369- source_root_dir , is_new = unpack_source (ctx , source_filename )
384+ source_root_dir , is_new = unpack_source (
385+ ctx = ctx ,
386+ req = req ,
387+ version = version ,
388+ source_filename = source_filename ,
389+ )
370390 if is_new :
371391 prepare_new_source (
372392 ctx = ctx ,
0 commit comments