@@ -59,6 +59,7 @@ def create_archive(
5959 compression : int = 6 ,
6060 test_run : bool = False ,
6161 backend : Optional [StorageBackend ] = None ,
62+ temp_dir : Optional [Union [str , Path ]] = None ,
6263 ** traversal_rules : bool ,
6364) -> Path :
6465 """Export AiiDA data to an archive file.
@@ -139,6 +140,12 @@ def create_archive(
139140
140141 :param backend: the backend to export from. If not specified, the default backend is used.
141142
143+ :param temp_dir: Directory to use for temporary files during archive creation.
144+ If not specified, a temporary directory will be created in the same directory as the output file
145+ with a '.aiida-export-' prefix. This parameter is useful when the output directory has limited
146+ space or when you want to use a specific filesystem (e.g., faster storage) for temporary operations.
147+ The directory must exist and be writable.
148+
142149 :param traversal_rules: graph traversal rules. See :const:`aiida.common.links.GraphTraversalRules`
143150 what rule names are toggleable and what the defaults are.
144151
@@ -179,6 +186,25 @@ def querybuilder():
179186 name : traversal_rules .get (name , rule .default ) for name , rule in GraphTraversalRules .EXPORT .value .items ()
180187 }
181188
189+ # Handle temporary directory configuration
190+ if temp_dir is not None :
191+ temp_dir = Path (temp_dir )
192+ if not temp_dir .exists ():
193+ msg = f"Specified temporary directory '{ temp_dir } ' does not exist"
194+ raise ArchiveExportError (msg )
195+ if not temp_dir .is_dir ():
196+ msg = f"Specified temporary directory '{ temp_dir } ' is not a directory"
197+ raise ArchiveExportError (msg )
198+ # Check if directory is writable
199+ if not temp_dir .is_writable ():
200+ msg = f"Specified temporary directory '{ temp_dir } ' is not writable"
201+ raise ArchiveExportError ()
202+ tmp_prefix = None # Use default tempfile prefix
203+ else :
204+ # Create temporary directory in the same folder as the output file
205+ temp_dir = filename .parent
206+ tmp_prefix = '.aiida-export-'
207+
182208 initial_summary = get_init_summary (
183209 archive_version = archive_format .latest_version ,
184210 outfile = filename ,
@@ -283,10 +309,10 @@ def querybuilder():
283309 # Create and open the archive for writing.
284310 # We create in a temp dir then move to final place at end,
285311 # so that the user cannot end up with a half written archive on errors
286- import ipdb ; ipdb .set_trace ()
287- base_temp_dir = '/mount' # or whatever directory you want to use
288- with tempfile .TemporaryDirectory (dir = base_temp_dir ) as tmpdir :
289- # with tempfile.TemporaryDirectory() as tmpdir:
312+ # import ipdb; ipdb.set_trace()
313+ temp_dir = Path ( '/mount' ) # or whatever directory you want to use
314+ with tempfile .TemporaryDirectory (dir = temp_dir , prefix = tmp_prefix ) as tmpdir :
315+ # NOTE: Add the `tmp_prefix` to the directory or file?
290316 tmp_filename = Path (tmpdir ) / 'export.zip'
291317 with archive_format .open (tmp_filename , mode = 'x' , compression = compression ) as writer :
292318 # add metadata
0 commit comments