def unfreeze(self): """freeze a watcher, meaning that it along with its tasks cannot be deleted. This does not prevent the user from manual editing. """ self._set_status("watcher", "frozen", "off") git_commit(self.repo, self.name, "UNFREEZE") self.print_section("watcher")
def deactivate(self, task=None): '''turn the active status of a watcher to false. If a task is provided, update the config value for the task to be false. ''' # If no task defined, user wants to deactiate watcher message = self._active_status('false', task) git_commit(self.repo, self.name, message)
def unfreeze(self): '''freeze a watcher, meaning that it along with its tasks cannot be deleted. This does not prevent the user from manual editing. ''' self._set_status('watcher', 'frozen', 'off') git_commit(self.repo, self.name, "UNFREEZE") self.print_section('watcher')
def protect(self, status="on"): '''protect a watcher, meaning that it cannot be deleted. This does not influence removing a task. To freeze the entire watcher, use the freeze() function. ''' self._set_status('watcher', 'protected', status) git_commit(self.repo, self.name, "PROTECT %s" % status) self.print_section('watcher')
def finish_runs(self, results): '''finish runs should take a dictionary of results, with keys as the folder name, and for each, depending on the result type, write the result to file (or update file) and then commit to git. Parameters ========== results: a dictionary of tasks, with keys as the task name, and values as the result. ''' if results is None: return for name, result in results.items(): task_folder = os.path.join(self.repo, name) if name.startswith('task'): task = self.get_task(name) # A decorator is run on the fly (not in config) elif name.startswith('decorator'): task = self.get_decorator(name) # We only allow tasks and decorators else: bot.warning('%s is not task or decorator, skipping.' % name) continue # Ensure that the task folder exists if not os.path.exists(task_folder): mkdir_p(task_folder) git_add(self.repo, task_folder) # Files to be added to the repo via git after files = task.write_results(result, self.repo) # Add files to git, and commit files.append(write_timestamp(repo=self.repo, task=name)) git_add(repo=self.repo, files=files) git_commit(repo=self.repo, task=self.name, message="ADD results %s" % name)
def _add_task(self, task, force=False, active='true'): '''add a new task to the watcher, meaning we: 1. Check first that the task doesn't already exist (if the task exists, we only add if force is set to true) 2. Validate the task (depends on the task) 3. write the task to the helper config file, if valid. Parameters ========== task: the Task object to add, should have a name and params and be child of watchme.tasks.TaskBase force: if task already exists, overwrite active: add the task as active (default "true") ''' self.load_config() if active not in ["true", "false"]: bot.exit('Active must be "true" or "false"') # Don't overwrite a section that already exists if task.name in self.config.sections(): if not force: bot.exit('%s exists, use --force to overwrite.' % task.name) self.remove_section(task.name, save=False) # Add the new section self.config[task.name] = task.export_params(active=active) self.print_section(task.name) self.save() # If the task folder doesn't exist, recreate it. task_folder = os.path.join(self.repo, task.name) if not os.path.exists(task_folder): mkdir_p(task_folder) git_add(self.repo, task.name) # Commit changes git_commit(repo=self.repo, task=self.name, message="ADD task %s" % task.name)
def remove_task(self, task): '''remove a task from the watcher repo, if it exists, and the watcher is not frozen. Parameters ========== task: the name of the task to remove ''' if self.get_section(task) is not None: if self.is_frozen(): bot.exit('watcher is frozen, unfreeze first.') self.remove_section(task) # If the task has a folder, remove the entire thing repo = os.path.join(self.repo, task) if os.path.exists(repo): shutil.rmtree(repo) bot.info('%s removed successfully.' % task) git_commit(self.repo, self.name, "REMOVE task %s" % task) else: bot.warning('Task %s does not exist.' % task)
def activate(self, task=None): '''turn the active status of a watcher to True ''' message = self._active_status('true', task) git_commit(self.repo, self.name, message)
def finish_runs(self, results): '''finish runs should take a dictionary of results, with keys as the folder name, and for each, depending on the result type, write the result to file (or update file) and then commit to git. Parameters ========== results: a dictionary of tasks, with keys as the task name, and values as the result. ''' for name, result in results.items(): task_folder = os.path.join(self.repo, name) task = self.get_task(name, save=True) # Files to be added via Git after files = [] # Ensure that the task folder exists if not os.path.exists(task_folder): mkdir_p(task_folder) git_add(self.repo, task_folder) # Case 1. The result is a list if isinstance(result, list): # Get rid of Nones, if the user accidentally added result = [r for r in result if r] if len(result) == 0: bot.error('%s returned empty list of results.' % name) # json output is specified elif task.params.get('save_as') == 'json': bot.debug('Saving single list as one json...') files.append(task._save_json(result, self.repo)) elif task.params.get('save_as') == 'json': bot.debug('Saving single list as multiple json...') files += task._save_json_list(result, self.repo) # Otherwise, sniff for list of paths elif os.path.exists(result[0]): bot.debug('Found list of paths...') files += task._save_files_list(result, self.repo) # Finally, assume just writing text to file else: bot.debug('Saving content from list to file...') files += task._save_text_list(result, self.repo) # Case 2. The result is a string elif isinstance(result, str): # if it's a path to a file, just save to repository if os.path.exists(result): files.append(task._save_file(result, self.repo)) # Otherwise, it's a string that needs to be saved to file else: files.append(task._save_text(result, self.repo)) # Case 3. The result is a dictionary elif isinstance(result, dict): files.append(task._save_json(result, self.repo)) elif result == None: bot.error('Result for task %s is None' % name) else: bot.error('Unsupported result format %s' % type(result)) # Get rid of None results (don't check excessively for None above) files = [f for f in files if f] # Add files to git, and commit files.append(write_timestamp(repo=self.repo, task=name)) git_add(repo=self.repo, files=files) git_commit(repo=self.repo, task=self.name, message="ADD results %s" % name)
def activate(self, task=None): """turn the active status of a watcher to True """ message = self._active_status("true", task) git_commit(self.repo, self.name, message)