buildbot doesn't accept my MailNotifier's IEMailLookup object

927 Views Asked by At

A number of people in my organization have different email names from perforce names, so I need to create an IEmailLookup derivation that overrides getAddress to do my evil bidding:

(From my master.cfg)

class MyIEmailLookup:
    from buildbot import interfaces
    __implements__ = interfaces.IEmailLookup

    def getAddresses(user):
        address_dict = {"user1", "user_one@our_domain.com"}
        try:
            address = address_dict[user]
        except KeyError:
            address = user + "@our_domain.com"
        return address

maillookup = MyIEmailLookup()

from buildbot.status import mail

c['status'].append(mail.MailNotifier(....
                                     ....
                                     lookup=maillookup
                                    ))

I've tried any number of permutations, but I either get:

Traceback (most recent call last):
  File "/Library/Python/2.6/site-packages/buildbot-0.8.3p1-py2.6.egg/buildbot/scripts/runner.py", line 1071, in doCheckConfig
    ConfigLoader(configFileName=configFileName)
  File "/Library/Python/2.6/site-packages/buildbot-0.8.3p1-py2.6.egg/buildbot/scripts/checkconfig.py", line 46, in __init__
    self.loadConfig(configFile, check_synchronously_only=True)
  File "/Library/Python/2.6/site-packages/buildbot-0.8.3p1-py2.6.egg/buildbot/master.py", line 727, in loadConfig
    exec f in localDict
  File "/Users/playbuilder/buildbot/master.cfg", line 207, in <module>
    lookup=maillookup
  File "/Library/Python/2.6/site-packages/buildbot-0.8.3p1-py2.6.egg/buildbot/status/mail.py", line 293, in __init__
    assert interfaces.IEmailLookup.providedBy(lookup)
AssertionError

...or any other number of issues, dependant upon how I try to implement the IEmailLookup interface.

I'm using buildbot 0.8.3p1 and python 2.6.1.

I see precious few examples of how to do this, and every one of them fails in my context. What am I missing here?

4

There are 4 best solutions below

2
On BEST ANSWER

I just solved this problem myself.

First you need to add (somewhere at the top of the file)

from zope.interface import implements

and then change

__implements__ = interfaces.IEmailLookup

to

if implements:
    implements( interfaces.IEmailLookup )
else:
    __implements__ = interfaces.IEmailLookup
0
On

If you want to fetch email from perforce user, you can use this class:

# .-----------------------.
# | Perforce Email Lookup |
# `-----------------------'

from twisted.internet import defer, utils
from buildbot.util import ComparableMixin
from buildbot.interfaces import IEmailLookup
from zope.interface import implements
import os
import re

class PerforceEmailLookup(ComparableMixin):
    implements(IEmailLookup)

    compare_attrs = ["p4port", "p4user", "p4passwd", "p4bin"]

    env_vars = ["P4CLIENT", "P4PORT", "P4PASSWD", "P4USER",
                "P4CHARSET"]

    def __init__(self,
                 p4port   = None,
                 p4user   = None,
                 p4passwd = None,
                 p4bin    = 'p4'):
        self.p4port   = p4port
        self.p4user   = p4user
        self.p4passwd = p4passwd
        self.p4bin    = p4bin
        self.email_re = re.compile(r"Email:\s+(?P<email>\S+@\S+)\s*$")

    def _get_process_output(self, args):
        env = dict([(e, os.environ.get(e)) for e in self.env_vars if os.environ.get(e)])
        d = utils.getProcessOutput(self.p4bin, args, env)
        return d

    @defer.deferredGenerator    
    def getAddress(self, name):

        if '@' in name:
            yield name
            return

        args = []
        if self.p4port:
            args.extend(['-p', self.p4port])
        if self.p4user:
            args.extend(['-u', self.p4user])
        if self.p4passwd:
            args.extend(['-P', self.p4passwd])
        args.extend(['user', '-o', name])

        wfd = defer.waitForDeferred(self._get_process_output(args))
        yield wfd
        result = wfd.getResult()

        for line in result.split('\n'):
            line = line.strip()
            if not line: continue
            m = self.email_re.match(line)
            if m:
                yield m.group('email')
                return

        yield name

usage would look like:

c['status'].append(
    MailNotifier(
        sendToInterestedUsers   = True,
        mode                    = 'failing',
        lookup                  = PerforceEmailLookup(
                                        p4port   = "perforce:1666",
                                        p4user   = "buildbot",
                                        p4passwd = "buildbot")))
0
On

Try this:

    from buildbot.interfaces import IEmailLookup
    from buildbot.util import ComparableMixin
    from zope.interface import implements
    class lookup_example_email(ComparableMixin):
        implements(IEmailLookup)
        def getAddress(self,user):
            return "%[email protected]"%(user)

...

mn = MailNotifier(..., lookup=lookup_example_email(), extraRecipients=m)
0
On

Here's the piece of code I use which works with buildbot 2.3.1 in python3.6.

from buildbot.interfaces import IEmailLookup
from buildbot.util import ComparableMixin
from zope.interface import implementer

@implementer(IEmailLookup)
class EmailMap(ComparableMixin):

    def getAddress(self, name):
        return f'{name}@xxxxx'