Exemplo n.º 1
0
    def __init__(self,
                 repomgr: RepositoryManager,
                 rulesdir: Optional[str] = None,
                 rulestext: Optional[str] = None):
        self._repomgr = repomgr
        self._rules = []

        hasher = hashlib.sha256()

        if isinstance(rulestext, str):
            hasher.update(rulestext.encode('utf-8'))
            for ruledata in yaml.safe_load(rulestext):
                self._add_rule(ruledata)
        elif isinstance(rulesdir, str):
            rulefiles: List[str] = []

            for root, dirs, files in os.walk(rulesdir):
                rulefiles += [
                    os.path.join(root, f) for f in files if f.endswith('.yaml')
                ]
                dirs[:] = [d for d in dirs if not d.startswith('.')]

            for rulefile in sorted(rulefiles):
                with open(rulefile) as data:
                    ruleset_text = data.read()
                    hasher.update(ruleset_text.encode('utf-8'))

                    rules = yaml.safe_load(ruleset_text)
                    if rules:  # may be None for empty file
                        for rule in rules:
                            self._add_rule(rule)
        else:
            raise RuntimeError('rulesdir or rulestext must be defined')

        self._hash = hasher.hexdigest()

        self._ruleblocks = []

        current_name_rules: List[Rule] = []

        def flush_current_name_rules() -> None:
            nonlocal current_name_rules
            if len(current_name_rules) >= NAMEMAP_BLOCK_MIN_SIZE:
                self._ruleblocks.append(NameMapRuleBlock(current_name_rules))
            elif current_name_rules:
                self._ruleblocks.extend(
                    [SingleRuleBlock(rule) for rule in current_name_rules])
            current_name_rules = []

        for rule in self._rules:
            if rule.names:
                current_name_rules.append(rule)
            else:
                flush_current_name_rules()
                self._ruleblocks.append(SingleRuleBlock(rule))

        flush_current_name_rules()

        self._optruleblocks = self._ruleblocks
        self._packages_processed = 0
Exemplo n.º 2
0
 def flush_current_name_rules():
     nonlocal current_name_rules
     if len(current_name_rules) >= NAMEMAP_BLOCK_MIN_SIZE:
         self.ruleblocks.append(NameMapRuleBlock(current_name_rules))
     elif current_name_rules:
         self.ruleblocks.extend([SingleRuleBlock(rule) for rule in current_name_rules])
     current_name_rules = []
Exemplo n.º 3
0
    def __init__(self, ruleset: Ruleset, rulesets: set[str],
                 statistics: RuleMatchStatistics) -> None:
        self._ruleblocks = []

        current_name_rules: list[Rule] = []

        def flush_current_name_rules() -> None:
            nonlocal current_name_rules
            if len(current_name_rules) >= NAMEMAP_BLOCK_MIN_SIZE:
                self._ruleblocks.append(NameMapRuleBlock(current_name_rules))
            elif current_name_rules:
                self._ruleblocks.extend(
                    [SingleRuleBlock(rule) for rule in current_name_rules])
            current_name_rules = []

        for rule in ruleset.get_rules():
            if rule.rulesets is not None and rule.rulesets.isdisjoint(
                    rulesets):
                continue
            if rule.norulesets is not None and not rule.norulesets.isdisjoint(
                    rulesets):
                continue

            if rule.names:
                current_name_rules.append(rule)
            else:
                flush_current_name_rules()
                self._ruleblocks.append(SingleRuleBlock(rule))

        flush_current_name_rules()

        self._optruleblocks = self._ruleblocks

        self._statistics = statistics
        self._recalc_opt_ruleblocks()
Exemplo n.º 4
0
    def __init__(self, repomgr, rulesdir=None, rulestext=None):
        self.repomgr = repomgr
        self.rules = []

        if rulestext:
            for ruledata in yaml.safe_load(rulestext):
                self._add_rule(ruledata)
        else:
            rulefiles = []

            for root, dirs, files in os.walk(rulesdir):
                rulefiles += [
                    os.path.join(root, f) for f in files if f.endswith('.yaml')
                ]
                dirs[:] = [d for d in dirs if not d.startswith('.')]

            for rulefile in sorted(rulefiles):
                with open(rulefile) as data:
                    for ruledata in yaml.safe_load(data):
                        self._add_rule(ruledata)

        self.ruleblocks = []

        current_name_rules = []

        def flush_current_name_rules():
            nonlocal current_name_rules
            if len(current_name_rules) >= NAMEMAP_BLOCK_MIN_SIZE:
                self.ruleblocks.append(NameMapRuleBlock(current_name_rules))
            elif current_name_rules:
                self.ruleblocks.extend(
                    [SingleRuleBlock(rule) for rule in current_name_rules])
            current_name_rules = []

        for rule in self.rules:
            if rule.names:
                current_name_rules.append(rule)
            else:
                flush_current_name_rules()
                self.ruleblocks.append(SingleRuleBlock(rule))

        flush_current_name_rules()

        self.optruleblocks = self.ruleblocks
        self.packages_processed = 0