11"""Model installation class."""
22
3+ import gc
34import locale
45import os
56import re
@@ -187,6 +188,21 @@ def register_path(
187188 config .source_type = ModelSourceType .Path
188189 return self ._register (model_path , config )
189190
191+ # TODO: Replace this with a proper fix for underlying problem of Windows holding open
192+ # the file when it needs to be moved.
193+ @staticmethod
194+ def _move_with_retries (src : Path , dst : Path , attempts : int = 5 , delay : float = 1.0 ) -> None :
195+ """Workaround for Windows file-handle issues when moving files."""
196+ for tries_left in range (attempts , 0 , - 1 ):
197+ try :
198+ move (src , dst )
199+ return
200+ except PermissionError :
201+ gc .collect ()
202+ if tries_left == 1 :
203+ raise
204+ time .sleep (delay )
205+
190206 def install_path (
191207 self ,
192208 model_path : Union [Path , str ],
@@ -205,7 +221,7 @@ def install_path(
205221 dest_dir .mkdir (parents = True )
206222 dest_path = dest_dir / model_path .name if model_path .is_file () else dest_dir
207223 if model_path .is_file ():
208- move (model_path , dest_path )
224+ self . _move_with_retries (model_path , dest_path ) # Windows workaround TODO: fix root cause
209225 elif model_path .is_dir ():
210226 # Move the contents of the directory, not the directory itself
211227 for item in model_path .iterdir ():
0 commit comments