@@ -712,6 +712,28 @@ def cleanup(self, delete: bool = False):
712712 # Delete the working directory.
713713 rmtree (self ._outer_dir )
714714
715+ def archive_to_fobj (self , fobj : typing .IO [bytes ]) -> bool :
716+ """Archive the directory where the sandbox operated.
717+
718+ fobj: the file object the archive will be written to.
719+ return: whether archiving succeeded.
720+ """
721+ metadata_path = self .get_root_path ()
722+ box_dir = self ._home
723+ with tarfile .open (fileobj = fobj , mode = 'w:gz' ) as tar_file :
724+ try :
725+ # Add metadata files
726+ for x in os .listdir (metadata_path ):
727+ tar_file .add (os .path .join (metadata_path , x ), x )
728+ # Add the box directory
729+ tar_file .add (box_dir , "box" )
730+ except Exception :
731+ logger .warning (
732+ "Failed to add files to sandbox archive" , exc_info = True
733+ )
734+ return False
735+ return True
736+
715737 def archive (self , file_cacher : FileCacher ) -> str | None :
716738 """Archive the directory where the sandbox operated.
717739
@@ -724,20 +746,9 @@ def archive(self, file_cacher: FileCacher) -> str | None:
724746
725747 with tempfile .TemporaryFile (dir = self .temp_dir ) as sandbox_archive :
726748 # Archive the working directory
727- metadata_path = self .get_root_path ()
728- box_dir = self ._home
729- with tarfile .open (fileobj = sandbox_archive , mode = 'w:gz' ) as tar_file :
730- try :
731- # Add metadata files
732- for x in os .listdir (metadata_path ):
733- tar_file .add (os .path .join (metadata_path , x ), x )
734- # Add the box directory
735- tar_file .add (box_dir , "box" )
736- except Exception :
737- logger .warning (
738- "Failed to add files to sandbox archive" , exc_info = True
739- )
740-
749+ ok = self .archive_to_fobj (sandbox_archive )
750+ if not ok :
751+ return None
741752 # Put archive to FS
742753 sandbox_archive .seek (0 )
743754 return file_cacher .put_file_from_fobj (
0 commit comments