'''
Created on June 28, 2013

__author__ = "Elizabeth 'pidge' Flanagan"
__copyright__ = "Copyright 2012-2013, Intel Corp."
__credits__ = ["Elizabeth Flanagan"]
__license__ = "GPL"
__version__ = "2.0"
__maintainer__ = "Elizabeth Flanagan"
__email__ = "elizabeth.flanagan@intel.com"
'''
from buildbot.status.mail import MailNotifier
from buildbot.status.mail import *
from buildbot import interfaces, util, config
from buildbot.process.users import users
from buildbot.status import base
from buildbot.status.results import FAILURE, SUCCESS, WARNINGS, EXCEPTION, Results
from twisted.python import log

class YoctoMailNotifier(MailNotifier):
    def __init__(self, fromaddr, mode=("failing"),
                 categories=None, branches=[], yoctorepos=[], builders=None, addLogs=False,
                 relayhost="localhost", buildSetSummary=False,
                 subject="buildbot %(result)s in %(title)s on %(builder)s",
                 lookup=None, extraRecipients=[],
                 sendToInterestedUsers=True, customMesg=None,
                 messageFormatter=defaultMessage, extraHeaders=None,
                 addPatch=True, useTls=False, 
                 smtpUser=None, smtpPassword=None, smtpPort=25):
        self.fromaddr = fromaddr
        self.mode=mode
        self.categories = categories
        self.branches = branches
        self.yoctorepos = yoctorepos
        self.builders = builders
        self.addLogs = addLogs
        self.relayhost = relayhost
        self.buildSetSummary = buildSetSummary
        self.subject = subject
        self.lookup = lookup
        self.extraRecipients = extraRecipients
        self.sendToInterestedUsers = sendToInterestedUsers
        self.customMesg = customMesg
        self.messageFormatter = messageFormatter
        self.extraHeaders = extraHeaders
        self.addPatch = addPatch
        self.useTls = useTls
        self.smtpUser = smtpUser
        self.smtpPassword = smtpPassword
        self.smtpPort = smtpPort
        MailNotifier.__init__(self, fromaddr, mode=self.mode,
                categories = self.categories, builders = self.builders,
                addLogs = self.addLogs, relayhost = self.relayhost,
                buildSetSummary = self.buildSetSummary,
                subject = self.subject,
                lookup = self.lookup,
                extraRecipients = self.extraRecipients,
                sendToInterestedUsers = self.sendToInterestedUsers,
                customMesg = self.customMesg, 
                messageFormatter = self.messageFormatter,
                extraHeaders = self.extraHeaders, addPatch = self.addPatch,
                useTls = self.useTls, smtpUser = self.smtpUser,
                smtpPassword = self.smtpPassword, smtpPort = self.smtpPort)

    def isMailNeeded(self, build, results):
        # here is where we actually do something.
        builder = build.getBuilder()
        repo=build.getProperty("repository")
        branch=build.getProperty("branch")
        log.msg(repo)
        log.msg(branch)
        buildme = False
        if self.builders is not None and builder.name not in self.builders:
            return False # ignore this build
        if self.categories is not None and \
               builder.category not in self.categories:
            return False # ignore this build

        if self.yoctorepos is not None and repo not in self.yoctorepos:
            return False # ignore this build
        elif self.yoctorepos is None and self.branches is not None and branch in self.branches:
            return False
        elif self.yoctorepos is not None and repo in self.yoctorepos and self.branches is None:
            buildme = True
        else:
	    buildme = True

        if buildme is True:
            prev = build.getPreviousBuild()
            if "change" in self.mode:
                if prev and prev.getResults() != results:
                    return True
            if "failing" in self.mode and results == FAILURE:
                return True
            if "passing" in self.mode and results == SUCCESS:
                return True
            if "problem" in self.mode and results == FAILURE:
                if prev and prev.getResults() != FAILURE:
                    return True
            if "warnings" in self.mode and results == WARNINGS:
                return True
            if "exception" in self.mode and results == EXCEPTION:
                return True
            return False

@@ -3366,20 +3366,16 @@
 nd repo 
-not 
 in self.
@@ -3402,81 +3402,113 @@
     
-return False # ignore this build%0A        elif self.yoctorepos is None and
+if self.branches is not None and branch in self.branches:%0A                buildme = True%0A            elif
  sel
@@ -3533,32 +3533,36 @@
 None and branch 
+not 
 in self.branches
@@ -3567,32 +3567,36 @@
 es:%0A            
+    
 return False%0A   
@@ -3587,24 +3587,44 @@
 return False
+ # ignore this build
 %0A        eli
@@ -3654,32 +3654,36 @@
 t None and repo 
+not 
 in self.yoctorep
@@ -3684,20 +3684,32 @@
 ctorepos
- and
+:%0A            if
  self.br
@@ -3722,74 +3722,201 @@
  is 
-None:%0A            buildme = True%0A        else:%0A%09    buildme = True
+not None and branch in self.branches:%0A                buildme = True%0A            elif self.branches is not None and branch not in self.branches:%0A                return False # ignore this build
 %0A%0A  

Fix some logic around when to build wl branches