def cache_or_load_file(path, creator, loader):
 if os.path.exists(path):
  return loader(path)
 file_name = os.path.basename(path)
 temp_dir = tempfile.mkdtemp()
 temp_path = os.path.join(temp_dir, file_name)
 lock_path = os.path.join(_dataset_root, '_create_lock')
 try:
  content = creator(temp_path)
  with filelock.FileLock(lock_path):
   if (not os.path.exists(path)):
    shutil.move(temp_path, path)
 finally:
  shutil.rmtree(temp_dir)
 return content

def cache_or_load_file ( path , creator , loader ) : if os . path . exists ( path ) : return loader ( path ) file_name = os . path . basename ( path ) temp_dir = tempfile . mkdtemp ( ) temp_path = os . path . join ( temp_dir , file_name ) lock_path = os . path . join ( _dataset_root , '_create_lock' ) try : content = creator ( temp_path ) with filelock . FileLock ( lock_path ) : if ( not os . path . exists ( path ) ) : shutil . move ( temp_path , path ) finally : shutil . rmtree ( temp_dir ) return content
caches a file if it does not exist .