summaryrefslogtreecommitdiff
path: root/classes/tinderclient.bbclass
blob: e8e9ba7c4272442239be0dd3daa434bc8291457b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def tinder_tinder_time():
    import time
    return time.strftime('%m/%d/%Y %H:%M:%S', time.localtime())

def tinder_send_email(data, header, log):
    import smtplib
    from email.MIMEText import MIMEText
    msg = MIMEText(header +'\n' + log)
    msg['Subject'] = bb.data.getVar('TINDER_SUBJECT',data, True) or "Tinder-Client build log"
    msg['To']      = bb.data.getVar('TINDER_MAILTO' ,data, True)
    msg['From']    = bb.data.getVar('TINDER_FROM',   data, True)


    s = smtplib.SMTP()
    s.connect()
    s.sendmail(bb.data.getVar('TINDER_FROM', data, True), [bb.data.getVar('TINDER_MAILTO', data, True)], msg.as_string())
    s.close()

def tinder_send_http(data, header, log):
    cont = header + '\n' + log
    import httplib
    conn = httplib.HTPPConnection(bb.data.getVar('TINDER_HOST',data, True))
    conn.request("POST", bb.data.getVar('TINDER_URL',data,True),body=cont)
    conn.close() 


# Prepare tinderbox mail header
def tinder_prepare_mail_header(data, status):
    import bb

    str  = "tinderbox: administrator: %s\n" % bb.data.getVar('TINDER_ADMIN', data, True)
    str += "tinderbox: starttime: %s\n"     % bb.data.getVar('BUILDSTART', data, True) or bb.data.getVar('TINDER_START', data, True)
    str += "tinderbox: buildname: %s\n"     % bb.data.getVar('TINDER_BUILD', data, True)
    str += "tinderbox: errorparser: %s\n"   % bb.data.getVar('TINDER_ERROR', data, True)
    str += "tinderbox: status: %s\n"        % status
    str += "tinderbox: timenow: %s\n"       % tinder_tinder_time()
    str += "tinderbox: tree: %s\n"          % bb.data.getVar('TINDER_TREE', data, True)
    str += "tinderbox: buildfamily: %s\n"   % "unix"
    str += "tinderbox: END"

    return str

def tinder_do_tinder_report(event):
    """
    Report to the tinderbox. Either we will report every step
    (depending on TINDER_VERBOSE_REPORT) at the end we will send the
    tinderclient.log
    """
    from bb.event import getName
    from bb import data, make, mkdirhier
    import os, glob

    # variables
    name = getName(event)
    log  = ""
    header = ""
    verbose = data.getVar('TINDER_VERBOSE_REPORT', event.data, True) == "1"

    # Check what we need to do Build* shows we start or are done
    if name == "BuildStarted":
        header = tinder_prepare_mail_header(event.data, 'building')
        # generate
        for var in os.environ:
            log += "%s=%s\n" % (var, os.environ[var])

        mkdirhier(data.getVar('TMPDIR', event.data, True))
        file = open(data.getVar('TINDER_LOG', event.data, True), 'w')
        file.write(log)

        if not verbose:
            header = ""

    if name == "PkgFailed" or name == "BuildCompleted":
        status = 'build_failed'
	if name == "BuildCompleted":
	    status = "success"
        header = tinder_prepare_mail_header(event.data, status)
        # append the log
        log_file = data.getVar('TINDER_LOG', event.data, True)
        file     = open(log_file, 'r')
        for line in file.readlines():
            log += line

    if verbose and name == "TaskStarted":
        header = tinder_prepare_mail_header(event.data, 'building')
        log    = "Task %s started" % event.task

    if verbose and name == "PkgStarted":
        header = tinder_prepare_mail_header(event.data, 'building')
        log    = "Package %s started" % data.getVar('P', event.data, True)

    if verbose and name == "PkgSucceeded":
        header = tinder_prepare_mail_header(event.data, 'building')
        log    = "Package %s done" % data.getVar('P', event.data, True)

    # Append the Task Log
    if name == "TaskSucceeded" or name == "TaskFailed":
        log_file = glob.glob("%s/log.%s.*" % (data.getVar('T', event.data, True), event.task))

        if len(log_file) != 0:
            to_file  = data.getVar('TINDER_LOG', event.data, True)
            log_txt  = open(log_file[0], 'r').readlines()
            to_file  = open(to_file, 'a')

            to_file.writelines(log_txt)

            # append to the log
            if verbose:
                header = tinder_prepare_mail_header(event.data, 'building')
                for line in log_txt:
                    log += line

    # now mail the log
    if len(log) == 0 or len(header) == 0:
        return

    log_post_method = tinder_send_email
    if bb.data.getVar('TINDER_SENDLOG', event.data, True) == "http":
	log_post_method = tinder_send_http

    log_post_method(event.data, header, log)


addhandler tinderclient_eventhandler
python tinderclient_eventhandler() {
    from bb import note, error, data
    from bb.event import NotHandled

    do_tinder_report = data.getVar('TINDER_REPORT', e.data, True)
    if do_tinder_report and do_tinder_report == "1":
        tinder_do_tinder_report(e)

    return NotHandled
}