def create_dir_if_not_exists(self, dir_path): """ This method recursively creates a directory if not exists If the path exists and is not a directory raise an exception. :param str dir_path: full path for the directory """ _logger.debug('Create directory %s if it does not exists' % dir_path) exists = self.exists(dir_path) if exists: is_dir = self.cmd('test', args=['-d', dir_path]) if is_dir != 0: raise FsOperationFailed( 'A file with the same name already exists') else: return False else: # Make parent directories if needed mkdir_ret = self.cmd('mkdir', args=['-p', dir_path]) if mkdir_ret == 0: return True else: raise FsOperationFailed('mkdir execution failed')
def delete_if_exists(self, path): """ This method check for the existence of a path. If it exists, then is removed using a rm -fr command, and returns True. If the command fails an exception is raised. If the path does not exists returns False :param path the full path for the directory """ _logger.debug('Delete path %s if exists' % path) exists = self.exists(path, False) if exists: rm_ret = self.cmd('rm -fr %s' % path) if rm_ret == 0: return True else: raise FsOperationFailed('rm execution failed') else: return False
def check_directory_exists(self, dir_path): """ Check for the existence of a directory in path. if the directory exists returns true. if the directory does not exists returns false. if exists a file and is not a directory raises an exception :param dir_path full path for the directory """ _logger.debug('Check if directory %s exists' % dir_path) exists = self.exists(dir_path) if exists: is_dir = self.cmd('test -d %s' % dir_path) if is_dir != 0: raise FsOperationFailed( 'A file with the same name exists, but is not a directory') else: return True else: return False