Skip to content

Commit 8f3f64b

Browse files
committed
prevent crash that occurs when changing models.yaml on windows systems
Windows does not support an atomic `os.rename()` operation. This PR changes it to `os.replace()`, which does the same thing.
1 parent dba0280 commit 8f3f64b

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

ldm/invoke/model_cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def commit(self,config_file_path:str):
298298
with open(tmpfile, 'w') as outfile:
299299
outfile.write(self.preamble())
300300
outfile.write(yaml_str)
301-
os.rename(tmpfile,config_file_path)
301+
os.replace(tmpfile,config_file_path)
302302

303303
def preamble(self):
304304
'''

ldm/modules/image_degradation/utils_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def mkdir_and_rename(path):
193193
if os.path.exists(path):
194194
new_name = path + '_archived_' + get_timestamp()
195195
print('Path already exists. Rename it to [{:s}]'.format(new_name))
196-
os.rename(path, new_name)
196+
os.replace(path, new_name)
197197
os.makedirs(path)
198198

199199

scripts/preload_models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def migrate_models_ckpt():
260260
rename = yes_or_no(f'Ok to rename it to "{new_name}" for future reference?')
261261
if rename:
262262
print(f'model.ckpt => {new_name}')
263-
os.rename(os.path.join(Model_dir,'model.ckpt'),os.path.join(Model_dir,new_name))
263+
os.replace(os.path.join(Model_dir,'model.ckpt'),os.path.join(Model_dir,new_name))
264264

265265
#---------------------------------------------
266266
def download_weight_datasets(models:dict, access_token:str):
@@ -347,12 +347,12 @@ def update_config_file(successfully_downloaded:dict,opt:dict):
347347
try:
348348
if os.path.exists(Config_file):
349349
print(f'** {Config_file} exists. Renaming to {Config_file}.orig')
350-
os.rename(Config_file,f'{Config_file}.orig')
350+
os.replace(Config_file,f'{Config_file}.orig')
351351
tmpfile = os.path.join(os.path.dirname(Config_file),'new_config.tmp')
352352
with open(tmpfile, 'w') as outfile:
353353
outfile.write(Config_preamble)
354354
outfile.write(yaml)
355-
os.rename(tmpfile,Config_file)
355+
os.replace(tmpfile,Config_file)
356356

357357
except Exception as e:
358358
print(f'**Error creating config file {Config_file}: {str(e)} **')

0 commit comments

Comments
 (0)