def wrapper(*fargs, **fkwargs): # Typically the task folder is the index, so we will create # indices that start with decorator-<task> result = None # The watcher is required, first keyword argument if not args: bot.error( "A watcher name is required for the psutils decorator.") return result # Get a watcher to save results to watcher = get_watcher(args[0], create=kwargs.get("create", False)) # Start the function runner = ProcessRunner( seconds=kwargs.get("seconds", 3), skip=kwargs.get("skip", []), include=kwargs.get("include", []), only=kwargs.get("only", []), ) runner.run(func, *fargs, **fkwargs) result = runner.wait("monitor_pid_task") # Save results (finishing runs) - key is folder created name = kwargs.get("name", func.__name__) key = "decorator-psutils-%s" % name results = {key: runner.timepoints} watcher.finish_runs(results) # Return function result to the user return result
def main(args, extra): '''add a task for a watcher ''' # Required - will print help if not provided name = args.watcher[0] task = args.task[0] if not task.startswith('task'): example = 'watchme add-task watcher task-cpu func@cpu_task type@psutils' bot.exit('Task name must start with "task", e.g., %s' % example) # Exit if the user doesn't provide any parameters if extra is None: bot.exit('Please provide parameters to add to your watcher (key@value)') # Type can also be an argument watcher_type = args.watcher_type params = [] for param in extra: if param.startswith('type@'): watcher_type = param.replace('type@', '') else: params.append(param) # Get the watcher to interact with, must already exist watcher = get_watcher(name, base=args.base, create=False) # Add the task. Will exit if not a valid type, or parameters watcher.add_task(task=task, task_type=watcher_type, params=params, force=args.force, active=args.active)
def main(args, extra): """activate one or more watchers""" # Required - will print help if not provided name = args.watcher[0] # Exit if the user doesn't provide a time if extra is None: bot.exit("Please provide a time frame (@daily, @hourly, @weekly, etc.)") # Determine the time to use if "@daily" in extra: minute, hour, month, day, weekday = 0, 0, "*", "*", "*" elif "@hourly" in extra: minute, hour, month, day, weekday = 0, "*", "*", "*", "*" elif "@weekly" in extra: minute, hour, month, day, weekday = 0, 0, "*", "*", 0 elif "@monthly" in extra: minute, hour, month, day, weekday = 0, 0, 1, "*", "*" elif "@yearly" in extra: minute, hour, month, day, weekday = 0, 0, 1, 1, "*" else: if len(extra) != 5: message = """Please enter a frequency (@weekly) or use a valid cron timestamp (see https://crontab.guru/).""" bot.exit(message) minute, hour, month, day, weekday = extra # Schedule the watcher watcher = get_watcher(name, base=args.base) watcher.schedule(minute, hour, month, day, weekday, force=args.force)
def main(args, extra): """protect or freeze a watcher, or disable.""" # Required - will print help if not provided name = args.watcher[0] watcher = get_watcher(name, base=args.base, create=False) if args.action in ["on", "off"]: watcher.protect(args.action) elif args.action == "freeze": watcher.freeze() elif args.action == "unfreeze": watcher.unfreeze()
def main(args, extra): """activate one or more watchers""" # Doesn't work if watcher not provided watcher = args.watcher[0] watcher = get_watcher(watcher, base=args.base) # The user is deactivating the entire watcher if extra is None: watcher.activate() else: for name in extra: watcher.activate(name)
def main(args, extra): """activate one or more watchers""" # Required - will print help if not provided name = args.watcher[0] watcher = get_watcher(name, base=args.base, create=False) # If user wants to see create command, must have list of tasks if args.create_command is True and extra is None: extra = [x.name for x in watcher.get_tasks()] # Show the create command, or inspect a task watcher.inspect(extra, create_command=args.create_command)
def main(args, extra): '''run a watcher, optionally supplying one or more regular expressions to check for. ''' # Required - will print help if not provided name = args.watcher[0] watcher = get_watcher(name, base=args.base, create=False) # If a set of task names or regular expressions are provided: if extra is not None: extra = "(%s)" % '|'.join(extra) # Run the watcher, providing regular expressions to match tasks watcher.run(regexp=extra, parallel=not args.serial, test=args.test, show_progress=not args.no_progress)
def main(args, extra): """monitor a task (from the command line), meaning wrapping it with multiprocessing, getting the id, and returning a result (command line or written to file) """ # The first argument will either be part of a command, or a watcher watcher = args.watcher # The entire user command is the extra arguments command = extra # If the user provides a watcher, we are saving to it if watcher not in get_watchers(args.base, quiet=True): command = [watcher] + command watcher = None else: watcher = get_watcher(watcher, base=args.base, create=False) command = " ".join(command) runner = TerminalRunner( command, skip=args.skip, include=args.include, only=args.only, seconds=args.seconds, ) runner.run() timepoints = runner.wait(args.func) # The output folder depends on the watcher func prefix = "decorator-psutils" if args.func == "gpu_task": prefix = "decorator-gpu" # If we don't have a watcher, print to terminal if watcher is None or args.test is True: print(json.dumps(timepoints)) # Otherwise save to watcher task folder else: name = args.name if name is None: name = command.replace(" ", "-") name = "%s-%s" % (prefix, name) watcher.finish_runs({name: timepoints})
def main(args, extra): """activate one or more watchers""" # Required - will print help if not provided name = args.watcher[0] watcher = get_watcher(name, base=args.base, create=False) # If delete is true, remove entire watcher, only if not protected or frozen if args.delete: watcher.delete() else: # Exit if the user doesn't provide any tasks to remove if extra is None: bot.exit("Provide tasks to remove, or --delete for entire watcher.") for task in extra: # Remove the task, if it exists watcher.remove_task(task)
def main(args, extra): '''monitor a task (from the command line), meaning wrapping it with multiprocessing, getting the id, and returning a result (command line or written to file) ''' # The first argument will either be part of a command, or a watcher watcher = args.watcher # The entire user command is the extra arguments command = extra # If the user provides a watcher, we are saving to it if watcher not in get_watchers(args.base, quiet=True): command = [watcher] + command watcher = None else: watcher = get_watcher(watcher, base=args.base, create=False) command = ' '.join(command) runner = TerminalRunner(command, skip=args.skip, include=args.include, only=args.only, seconds=args.seconds) runner.run() timepoints = runner.wait() # If we don't have a watcher, print to terminal if watcher is None or args.test is True: print(json.dumps(timepoints)) # Otherwise save to watcher task folder else: name = args.name if name is None: name = command.replace(' ', '-') name = 'decorator-psutils-%s' % name watcher.finish_runs({name: timepoints})
def main(args, extra): """edit the configuration for a watcher task""" # Required - will print help if not provided name = args.watcher[0] action = args.action[0] task = args.task[0] # Get the watcher (exits if doesn't exist) watcher = get_watcher(name, base=args.base) # Exit if the user doesn't provide a time if extra is None: bot.exit("Please provide one or more items to %s" % action) key = extra[0] value = None if action in ["add", "update"]: if len(extra) != 2: bot.exit("You must do watchme <watcher> add <key> <value>") value = extra[1] # Ensure the task exists watcher.edit_task(task, action, key, value)
def main(args, extra): """export temporal data for a watcher """ # Required - will print help if not provided name = args.watcher[0] task = args.task[0] filename = args.filename[0] if not task.startswith("task") and not task.startswith("decorator"): example = "watchme export watcher task-reddit result.txt" bot.exit('Task name must start with "task" or "decorator": %s' % example) # Use the output file, or a temporary file out = args.out # Get the watcher to interact with, must already exist watcher = get_watcher(name, base=args.base, create=False) if out is not None: if os.path.exists(out) and args.force is False: bot.exit("%s exists! Use --force to overwrite." % out) # Export the data to file result = watcher.export_dict(task=task, filename=filename, name=name, export_json=args.json, base=args.base) if result is not None: if out is None: print(json.dumps(result, indent=4)) else: write_json(result, out) bot.info("Result written to %s" % out)
def main(args, extra): '''activate one or more watchers ''' # Required - will print help if not provided name = args.watcher[0] task = args.task[0] filename = args.filename[0] if not task.startswith('task'): example = 'watchme add watcher task-reddit url@https://www.reddit.com' bot.exit('Task name must start with "task", e.g., %s' % example) # Use the output file, or a temporary file out = args.out # Get the watcher to interact with, must already exist watcher = get_watcher(name, base=args.base, create=False) if out is not None: if os.path.exists(out) and args.force is False: bot.exit('%s exists! Use --force to overwrite.' % out) # Export the data to file result = watcher.export_dict(task=task, filename=filename, name=name, export_json=args.json, base=args.base) if result != None: if out == None: print(json.dumps(result, indent=4)) else: write_json(result, out) bot.info('Result written to %s' % out)
def setUp(self): self.base = tempfile.mkdtemp() self.repo = create_watcher('pancakes', base=self.base) self.cli = get_watcher('pancakes', base=self.base)