Esempio n. 1
0
    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)
Esempio n. 2
0
    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)
Esempio n. 3
0
    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)