# -*- coding: utf-8 -*- # # This snippet illustrates example usage # of shutil.cleartree() # # This class executes remotely(e.g. via execnet) over all cluster nodes. class Cleanup(Task): '''\ Clean space. Valid resources(returned by Task.list_task_resources()) shouldn't be deleted. ''' def execute(self): result = 0 tasks_dir = self.settings.TASKS_DIR # the directory structure is /[0..10]/[0..10]/[10..1000000] for i in range(0,10): for j in range(0,10): section_path = os.path.join(tasks_dir, str(i), str(j)) logging.debug('Inspecting section: {0}'.format(section_path)) if not os.path.isdir(section_path): continue for dir_name in os.listdir(section_path): try: task_id = int(dir_name) except: logging.debug('Incorrect task directory: {0}'.format(dir_name)) continue if task_id == self.id: continue logging.debug('Task id: {0}'.format(task_id)) task_dir = os.path.join(section_path, dir_name) start_size = get_dir_size(task_dir) logging.debug('Start size: {0}'.format(start_size)) task = tasks.manager.load(task_id) if task: resources_path = set([]) for resource in resources.manager.list_task_resources(task_id): if self.host not in resource.get_hosts(all=True): continue resources_path.add(resource.abs_path()) # the main purpose of shutil.cleartree() # is to remove unnecessary files from tree, where # patterns of ignored or deleted files are computed # dinamically. shutil.cleartree(task_dir, ignore=shutil.ignore_patterns(*resources_path)) finish_size = get_dir_size(task_dir) logging.debug('Finish size: {0}'.format(finish_size)) logging.debug('{0} kilobytes cleared'.format(finish_size)) result += finish_size - start_size self.set_info("{0} kilobytes cleared".format(result))