def ensure_directory_exists(base_directory): """Ensure the directory passed as the argument exists. If the given directory is a broken Junction or Symlink, remove it. Then try creating the directory and if that fails, try to mitigate the problem by setting the parent directory to read-write and retrying the directory creation. If that fails, raise an AdminRequiredError. """ try: if paths.is_broken_junction(base_directory): Logger.info('torrent_utils: Removing potentially broken Junction: {}'.format(base_directory)) os.rmdir(base_directory) paths.mkdir_p(base_directory) except OSError: # Try fixing the situation by setting parent directory to read-write set_node_read_write(os.path.dirname(base_directory)) try: # Try again if paths.is_broken_junction(base_directory): Logger.info('torrent_utils: Removing potentially broken Junction: {}'.format(base_directory)) os.rmdir(base_directory) paths.mkdir_p(base_directory) except OSError: error_message = get_admin_error('directory cannot be created or is not valid', base_directory) Logger.error(error_message) raise AdminRequiredError(error_message)
def _save_to_file(self, filename, contents): """Save to file while ensuring the directory is created.""" directories = os.path.dirname(filename) if directories and not os.path.isdir(directories): mkdir_p(directories) with open(filename, "w") as text_file: text_file.write(contents)
def _set_launcher_moddir(self, launcher_moddir): """ interceptor for launcher_moddir sets the user defined launcher_moddir and ensures it is created. If something goes wrong nothing is done """ Logger.info('Settings: Ensuring mod dir exists - {}'.format(launcher_moddir)) try: mkdir_p(launcher_moddir) except OSError: fallback_moddir = self.get('launcher_moddir') # TODO: Show a regular message box, not a win32 message box MessageBox('Could not create directory {}\nFalling back to {}'.format( launcher_moddir, fallback_moddir), 'Error while setting mod directory') return ModelInterceptorError() return launcher_moddir
def save_file(url, data): """Save the file contents to the cache. The contents of the file are saved to a temporary file and then moved to ensure that no truncated file is present in the cache. """ # Ensure the directory exists paths.mkdir_p(get_cache_directory()) path = map_file(url) tmp_path = path + '_tmp' f = open(tmp_path, 'wb') f.write(data) f.close() # Ensure the file does not exist (would raise an exception on Windows with context.ignore_nosuchfile_exception(): os.unlink(path) os.rename(tmp_path, path)