Skip to content

Commit

Permalink
prevent crash that occurs when changing models.yaml on windows systems
Browse files Browse the repository at this point in the history
Windows does not support an atomic `os.rename()` operation. This
PR changes it to `os.replace()`, which does the same thing.
  • Loading branch information
lstein committed Nov 25, 2022
1 parent dba0280 commit 8f3f64b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ldm/invoke/model_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def commit(self,config_file_path:str):
with open(tmpfile, 'w') as outfile:
outfile.write(self.preamble())
outfile.write(yaml_str)
os.rename(tmpfile,config_file_path)
os.replace(tmpfile,config_file_path)

def preamble(self):
'''
Expand Down
2 changes: 1 addition & 1 deletion ldm/modules/image_degradation/utils_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def mkdir_and_rename(path):
if os.path.exists(path):
new_name = path + '_archived_' + get_timestamp()
print('Path already exists. Rename it to [{:s}]'.format(new_name))
os.rename(path, new_name)
os.replace(path, new_name)
os.makedirs(path)


Expand Down
6 changes: 3 additions & 3 deletions scripts/preload_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def migrate_models_ckpt():
rename = yes_or_no(f'Ok to rename it to "{new_name}" for future reference?')
if rename:
print(f'model.ckpt => {new_name}')
os.rename(os.path.join(Model_dir,'model.ckpt'),os.path.join(Model_dir,new_name))
os.replace(os.path.join(Model_dir,'model.ckpt'),os.path.join(Model_dir,new_name))

#---------------------------------------------
def download_weight_datasets(models:dict, access_token:str):
Expand Down Expand Up @@ -347,12 +347,12 @@ def update_config_file(successfully_downloaded:dict,opt:dict):
try:
if os.path.exists(Config_file):
print(f'** {Config_file} exists. Renaming to {Config_file}.orig')
os.rename(Config_file,f'{Config_file}.orig')
os.replace(Config_file,f'{Config_file}.orig')
tmpfile = os.path.join(os.path.dirname(Config_file),'new_config.tmp')
with open(tmpfile, 'w') as outfile:
outfile.write(Config_preamble)
outfile.write(yaml)
os.rename(tmpfile,Config_file)
os.replace(tmpfile,Config_file)

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

0 comments on commit 8f3f64b

Please sign in to comment.