Beispiel #1
0
 def test_nested(self):
     subs = {
         "wat": "another",
     }
     obj = {
         "one": {
             "thing": {
                 "leads": {
                     "to": "%(wat)s",
                     "most": "of the time",
                 }
             }
         }
     }
     target = {
         "one": {
             "thing": {
                 "leads": {
                     "to": "another",
                     "most": "of the time",
                 }
             }
         }
     }
     actual = format_args(obj, subs)
     eq_(actual, target)
Beispiel #2
0
 def construct_query(self, msg):
     subs = construct_substitutions(msg)
     kwargs = format_args(copy.copy(self._d['filter']), subs)
     kwargs = recursive_lambda_factory(kwargs, msg, name='msg')
     kwargs['defer'] = True
     total, pages, query = datanommer.models.Message.grep(**kwargs)
     return total, pages, query
Beispiel #3
0
    def _format_lambda_operation(self, msg):
        """ Format the string representation of a lambda operation.

        The lambda operation can be formatted here to include strings that
        appear in the message being evaluated like
        %(msg.comment.update_submitter)s.  Placeholders like that will have
        their value substituted with whatever appears in the incoming message.
        """
        subs = construct_substitutions(msg)
        operation = format_args(copy.copy(self._d['operation']), subs)
        return operation['lambda']
Beispiel #4
0
    def _format_lambda_operation(self, msg):
        """ Format the string representation of a lambda operation.

        The lambda operation can be formatted here to include strings that
        appear in the message being evaluated like
        %(msg.comment.update_submitter)s.  Placeholders like that will have
        their value substituted with whatever appears in the incoming message.
        """
        subs = construct_substitutions(msg)
        operation = format_args(copy.copy(self._d['operation']), subs)
        return operation['lambda']
Beispiel #5
0
 def test_nested_subs(self):
     subs = {
         'wat': dict(foo="bar")
     }
     obj = {
         'envelope': "%(wat)s",
     }
     target = {
         'envelope': dict(foo="bar"),
     }
     actual = format_args(obj, subs)
     eq_(actual, target)
Beispiel #6
0
 def test_numeric(self):
     subs = {
         "foo.bar.baz": 42,
     }
     obj = {
         "something should be": "%(foo.bar.baz)i",
     }
     target = {
         "something should be": 42,
     }
     actual = format_args(obj, subs)
     eq_(actual, target)
Beispiel #7
0
 def test_simple(self):
     subs = {
         "foo.bar.baz": "value",
     }
     obj = {
         "something should be": "%(foo.bar.baz)s",
     }
     target = {
         "something should be": "value",
     }
     actual = format_args(obj, subs)
     eq_(actual, target)
Beispiel #8
0
    def matches(self, msg):
        """ A pkgdb criteria check checks if a user owns some packages. """

        subs = construct_substitutions(msg)
        expectation = format_args(copy.copy(self._d['owns']), subs)
        expectation = recursive_lambda_factory(expectation, msg, name='msg')

        actual_packages = get_pkgdb_packages_for(
            config=fedmsg_config,
            user=expectation['user'],
        )

        return set(expectation['packages']).issubset(actual_packages)
Beispiel #9
0
    def matches(self, msg):
        """ A pkgdb criteria check checks if a user owns some packages. """

        subs = construct_substitutions(msg)
        expectation = format_args(copy.copy(self._d['owns']), subs)
        expectation = recursive_lambda_factory(expectation, msg, name='msg')

        actual_packages = get_pkgdb_packages_for(
            config=fedmsg_config,
            user=expectation['user'],
        )

        return set(expectation['packages']).issubset(actual_packages)
Beispiel #10
0
    def _construct_query(self, msg):
        """ Construct a datanommer query for this message.

        The "filter" section of this criteria object will be used.  It will
        first be formatted with any substitutions present in the incoming
        message.  This is used, for instance, to construct a query like "give
        me all the messages bearing the same topic as the message that just
        arrived".
        """
        subs = construct_substitutions(msg)
        kwargs = format_args(copy.copy(self._d['filter']), subs)
        kwargs = recursive_lambda_factory(kwargs, msg, name='msg')
        kwargs['defer'] = True
        total, pages, query = datanommer.models.Message.grep(**kwargs)
        return total, pages, query
Beispiel #11
0
    def _construct_query(self, msg):
        """ Construct a datanommer query for this message.

        The "filter" section of this criteria object will be used.  It will
        first be formatted with any substitutions present in the incoming
        message.  This is used, for instance, to construct a query like "give
        me all the messages bearing the same topic as the message that just
        arrived".
        """
        subs = construct_substitutions(msg)
        kwargs = format_args(copy.copy(self._d['filter']), subs)
        kwargs = recursive_lambda_factory(kwargs, msg, name='msg')
        kwargs['defer'] = True
        total, pages, query = datanommer.models.Message.grep(**kwargs)
        return total, pages, query
Beispiel #12
0
 def test_list(self):
     subs = {
         "foo.bar.baz": "value",
     }
     obj = {
         "something should be": [
             "%(foo.bar.baz)s",
             "or this",
         ]
     }
     target = {
         "something should be": [
             "value",
             "or this",
         ]
     }
     actual = format_args(obj, subs)
     eq_(actual, target)
Beispiel #13
0
    def matches(self, msg):

        # First, do a lightweight check to see if the msg matches a pattern.
        if not self.trigger.matches(msg):
            return set()

        # Before proceeding further, let's see who would get this badge if
        # our more heavyweight checks matched up.  If the user specifies a
        # recipient_key, we can use that to extract the potential awardee.  If
        # that is not specified, we just use `msg2usernames`.
        if self.recipient_key:
            subs = construct_substitutions(msg)
            obj = format_args(self.recipient_key, subs)

            if isinstance(obj, (basestring, int, float)):
                obj = [obj]

            # On the way, it is possible for the fedmsg message to contain None
            # for "agent".  A problem here though is that None is not iterable,
            # so let's replace it with an equivalently empty iterable so code
            # further down doesn't freak out.  An instance of this is when a
            # user without a fas account comments on a bodhi update.
            if obj is None:
                obj = []

            awardees = frozenset(obj)

            if self.recipient_nick2fas:
                awardees = frozenset([
                    nick2fas(nick, **fedmsg_config) for nick in awardees
                ])
        else:
            usernames = fedmsg.meta.msg2usernames(msg)
            awardees = usernames.difference(self.banned_usernames)

        # Strip anyone who is an IP address
        awardees = frozenset([
            user for user in awardees if not (
                user.startswith('192.168.') or
                user.startswith('10.')
            )
        ])

        # If no-one would get the badge by default, then no reason to waste
        # time doing any further checks.  No need to query the Tahrir DB.
        if not awardees:
            return awardees

        # Limit awardees to only those who do not already have this badge.
        # Do this only if we have an active connection to the Tahrir DB.
        if self.tahrir:
            awardees = frozenset([
                user for user in awardees
                if not self.tahrir.assertion_exists(
                    self.badge_id, "*****@*****.**" % user
                )])

            # Also, exclude any potential awardees who have opted out.
            awardees = frozenset([
                user for user in awardees
                if not self.tahrir.person_opted_out(
                    "*****@*****.**" % user
                )])

        # If no-one would get the badge at this point, then no reason to waste
        # time doing any further checks.  No need to query datanommer.
        if not awardees:
            return awardees

        # Check our backend criteria -- likely, perform datanommer queries.
        if not self.criteria.matches(msg):
            return set()

        return awardees
Beispiel #14
0
    def matches(self, msg):

        # First, do a lightweight check to see if the msg matches a pattern.
        if not self.trigger.matches(msg):
            return set()

        # Before proceeding further, let's see who would get this badge if
        # our more heavyweight checks matched up.  If the user specifies a
        # recipient_key, we can use that to extract the potential awardee.  If
        # that is not specified, we just use `msg2usernames`.
        if self.recipient_key:
            subs = construct_substitutions(msg)
            obj = format_args(self.recipient_key, subs)

            if isinstance(obj, (basestring, int, float)):
                obj = [obj]

            # On the way, it is possible for the fedmsg message to contain None
            # for "agent".  A problem here though is that None is not iterable,
            # so let's replace it with an equivalently empty iterable so code
            # further down doesn't freak out.  An instance of this is when a
            # user without a fas account comments on a bodhi update.
            if obj is None:
                obj = []

            awardees = frozenset(obj)

            if self.recipient_nick2fas:
                awardees = frozenset(
                    [nick2fas(nick, **fedmsg_config) for nick in awardees])

            if self.recipient_email2fas:
                awardees = frozenset(
                    [email2fas(email, **fedmsg_config) for email in awardees])
        else:
            usernames = fedmsg.meta.msg2usernames(msg)
            awardees = usernames.difference(self.banned_usernames)

        # Strip anyone who is an IP address
        awardees = frozenset([
            user for user in awardees
            if not (user.startswith('192.168.') or user.startswith('10.'))
        ])

        # If no-one would get the badge by default, then no reason to waste
        # time doing any further checks.  No need to query the Tahrir DB.
        if not awardees:
            return awardees

        # Limit awardees to only those who do not already have this badge.
        # Do this only if we have an active connection to the Tahrir DB.
        if self.tahrir:
            awardees = frozenset([
                user for user in awardees if not self.tahrir.assertion_exists(
                    self.badge_id, "*****@*****.**" % user)
            ])

            # Also, exclude any potential awardees who have opted out.
            awardees = frozenset([
                user for user in awardees
                if not self.tahrir.person_opted_out("*****@*****.**" %
                                                    user)
            ])

        # If no-one would get the badge at this point, then no reason to waste
        # time doing any further checks.  No need to query datanommer.
        if not awardees:
            return awardees

        # Check our backend criteria -- likely, perform datanommer queries.
        try:
            if not self.criteria.matches(msg):
                return set()
        except IOError as e:
            log.exception(e)
            return set()

        # Lastly, and this is probably most expensive.  Make sure the person
        # actually has a FAS account before we award anything.
        # https://github.com/fedora-infra/tahrir/issues/225
        awardees = set(
            [u for u in awardees if user_exists_in_fas(fedmsg_config, u)])

        return awardees
Beispiel #15
0
    def matches(self, msg):

        # First, do a lightweight check to see if the msg matches a pattern.
        if not self.trigger.matches(msg):
            return set()

        # Before proceeding further, let's see who would get this badge if
        # our more heavyweight checks matched up.  If the user specifies a
        # recipient_key, we can use that to extract the potential awardee.  If
        # that is not specified, we just use `msg2usernames`.
        if self.recipient_key:
            subs = construct_substitutions(msg)
            obj = format_args(self.recipient_key, subs)

            if isinstance(obj, (basestring, int, float)):
                obj = [obj]

            awardees = set(obj)

            if self.recipient_nick2fas:
                awardees = set([
                    nick2fas(nick, **fedmsg_config) for nick in awardees
                ])
        else:
            usernames = fedmsg.meta.msg2usernames(msg)
            awardees = usernames.difference(self.banned_usernames)

        # Strip anyone who is an IP address
        awardees = set([
            user for user in awardees if not (
                user.startswith('192.168.') or
                user.startswith('10.')
            )
        ])

        # If no-one would get the badge by default, then no reason to waste
        # time doing any further checks.  No need to query the Tahrir DB.
        if not awardees:
            return awardees

        # Limit awardees to only those who do not already have this badge.
        # Do this only if we have an active connection to the Tahrir DB.
        if self.tahrir:
            awardees = set([user for user in awardees
                            if not self.tahrir.assertion_exists(
                                self.badge_id, "*****@*****.**" % user
                            )])

            # Also, exclude any potential awardees who have opted out.
            awardees = set([user for user in awardees
                            if not self.tahrir.person_opted_out(
                                "*****@*****.**" % user
                            )])

        # If no-one would get the badge at this point, then no reason to waste
        # time doing any further checks.  No need to query datanommer.
        if not awardees:
            return awardees

        # Check our backend criteria -- likely, perform datanommer queries.
        if not self.criteria.matches(msg):
            return set()

        return awardees