Example #1
0
 def create_chall(self, configuration=None):
     '''Creates a challenge
     '''
     repo_conf = self.get_conf()
     chall_conf = self.__make_chall_conf(override=configuration)
     if not chall_conf['slug']:
         app_log.warning("aborted challenge creation, slug is empty.")
         return False
     chall_conf_path = self.working_dir().joinpath(
         chall_conf['slug'], repo_conf['files']['chall_conf'])
     chall = Challenge(chall_conf_path, repo_conf)
     if not chall.create():
         return False
     chall.set_conf(chall_conf)
     return True
Example #2
0
    def scan(self, category=None):
        """Yields (category, challenges) tuples

        Keyword Arguments:
            category {[type]} -- [description] (default: {None})

        Yields:
            (str, list(Challenge))
        """
        wd = self.working_dir()
        repo_conf = self.get_conf()
        keep = lambda e: e.is_dir() and not e.name.startswith('.')

        for cat in self._scandirs(wd, keep):
            challenges = []
            for chall in self._scandirs(cat.path, keep):
                chall_conf_path = Path(chall.path).joinpath(
                    repo_conf['files']['config']['challenge'])
                challenges.append(
                    Challenge(self.logger, chall_conf_path, repo_conf))

            challenges = sorted(challenges, key=lambda e: e.slug())

            if category is None:
                yield (cat.name, challenges)
                continue

            if category == cat.name:
                yield (cat.name, challenges)
                break
Example #3
0
    def find_chall(self, slug):
        '''Finds challenge
        '''
        chall_path = self.working_dir().joinpath(slug)

        if not chall_path.is_dir():
            app_log.warning(f"challenge not found: {slug}")
            return None
        repo_conf = self.get_conf()
        chall_conf_path = chall_path.joinpath(repo_conf['files']['chall_conf'])
        return Challenge(chall_conf_path, repo_conf)
Example #4
0
    def create_chall(self, configuration=None):
        """Creates a challenge

        Keyword Arguments:
            configuration {dict} -- [description] (default: {None})

        Returns:
            bool -- [description]
        """
        repo_conf = self.get_conf()
        chall_conf = self.__make_chall_conf(override_conf=configuration)
        chall_conf_path = self.working_dir().joinpath(
            chall_conf['category'], chall_conf['slug'],
            repo_conf['files']['config']['challenge'])

        chall = Challenge(self.logger, chall_conf_path, repo_conf)

        if not chall.create():
            return False

        chall.set_conf(chall_conf)
        return True
Example #5
0
    def __make_chall_conf(self, previous_conf={}, override_conf=None):
        """[summary]

        Keyword Arguments:
            previous_conf {dict} -- [description] (default: {None})
            override_conf {dict} -- [description] (default: {None})

        Returns:
            dict -- [description]
        """
        repo_conf = self.get_conf()

        if previous_conf is None:
            previous_conf = {}

        if override_conf:
            conf = previous_conf
            conf.update(override_conf)
        else:
            flag = previous_conf.get('flag', Challenge.make_flag(repo_conf))
            enabled = previous_conf.get('enabled', False)
            parameters = previous_conf.get('parameters', {})
            name = previous_conf.get('name')
            points = previous_conf.get('points')
            category = previous_conf.get('category')
            standalone = previous_conf.get('standalone')

            name = self.cli.readline("enter challenge name:", default=name)
            points = self.cli.readline("enter number of points:",
                                       default=points,
                                       expect_digit=True)
            category = self.cli.choose_one("select a category:",
                                           choices=repo_conf['categories'],
                                           default=category)
            standalone = self.cli.confirm("is it a standalone challenge?",
                                          default=standalone)

            conf = {
                'name': name,
                'slug': slugify(name),
                'flag': flag,
                'points': points,
                'enabled': enabled,
                'category': category,
                'parameters': parameters,
                'standalone': standalone
            }

        return conf
Example #6
0
    def scan(self, tags=[]):
        '''Returns a list of Challenges containing at least one tag in tags

           Notes:
            An empty list of tags means all tags
        '''
        wd = self.working_dir()
        tags = set(tags)
        repo_conf = self.get_conf()
        keep = lambda entry: entry.is_dir() and not entry.name.startswith('.')
        challenges = []
        for chall_dirent in self._scandirs(wd, keep):
            chall_conf_path = Path(chall_dirent.path).joinpath(
                repo_conf['files']['chall_conf'])
            chall = Challenge(chall_conf_path, repo_conf)
            if not tags or tags.intersection(chall.tags):
                challenges.append(chall)
        return sorted(challenges, key=lambda e: e.slug)
Example #7
0
 def __make_chall_conf(self, prev={}, override=None):
     '''[summary]
     '''
     repo_conf = self.get_conf()
     if prev is None:
         prev = {}
     if override:
         conf = prev
         conf.update(override)
     else:
         flag = prev.get('flag', Challenge.make_flag(repo_conf))
         name = cli.readline("enter challenge name:",
                             default=prev.get('name'))
         tags = cli.choose_many("select one or more tags:",
                                repo_conf['tags'],
                                default=prev.get('tags'))
         points = cli.readline("enter number of points:",
                               default=prev.get('points'),
                               expect_digit=True)
         enabled = prev.get('enabled', False)
         difficulties = repo_conf['difficulties']
         difficulty = cli.choose_one("how difficult is your challenge?",
                                     repo_conf['difficulties'],
                                     default=prev.get('difficulty', ))
         parameters = prev.get('parameters', {})
         standalone = cli.confirm("is it a standalone challenge?",
                                  default=prev.get('standalone'))
         static_url = prev.get('static_url', '')
         company_logo_url = prev.get('company_logo_url', '')
         conf = {
             'name': name,
             'tags': tags,
             'slug': slugify(name),
             'flag': flag,
             'points': points,
             'enabled': enabled,
             'difficulty': difficulty,
             'parameters': parameters,
             'standalone': standalone,
             'static_url': static_url,
             'company_logo_url': company_logo_url
         }
     return conf
Example #8
0
    def find_chall(self, category, slug):
        """Finds challenge

        Arguments:
            category {str} -- [description]
            slug {str} -- [description]

        Returns:
            Challenge -- [description]
        """
        chall_path = self.working_dir().joinpath(category, slug)

        if not chall_path.is_dir():
            self.logger.warning("challenge not found: "
                                "{}/{}".format(category, slug))
            return None

        repo_conf = self.get_conf()

        chall_conf_path = chall_path.joinpath(
            repo_conf['files']['config']['challenge'])

        return Challenge(self.logger, chall_conf_path, repo_conf)