summaryrefslogtreecommitdiff
path: root/post-build-report.py
blob: 295434910c3c18e21bd76a0299a08238b5edf9be (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/python
import os
import sys
import json
import httplib
import logging
import mimetypes

from urlparse import urljoin, urlsplit


logging.basicConfig(format='%(levelname)s:  %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
httplib.HTTPConnection.debuglevel = 1


RESULT_ENDPOINT = "/api/result/"


def encode_multipart_formdata(fields, files):
    LIMIT = '----------lImIt_of_THE_fIle_eW_$'
    CRLF = '\r\n'
    L = []
    for key, value in fields.iteritems():
        L.append('--' + LIMIT)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for key, value in files.iteritems():
        L.append('--' + LIMIT)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, key))
        L.append('Content-Type: %s' % get_content_type(key))
        L.append('')
        L.append(value.read())
    L.append('--' + LIMIT + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % LIMIT
    return content_type, body

def get_content_type(filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'


def _push_object(auth_pw, backend_url, endpoint, params, files):
    usplit = urlsplit(backend_url)
    url = urljoin(backend_url, endpoint)

    logger.info("Submitting to URL: %s" % url)
    conn = None
    if usplit.scheme.lower() == "http":
        conn = httplib.HTTP(usplit.netloc)
    if usplit.scheme.lower() == "https":
        conn = httplib.HTTPS(usplit.netloc)

    if conn is None:
        print "Unknown scheme: %s" % usplit.scheme
        sys.exit(1)

    content_type, body = encode_multipart_formdata(params, files)
    conn.putrequest('POST', endpoint)
    conn.putheader("Authorization", "Token %s" % auth_pw)
    conn.putheader('Content-Type', content_type)
    conn.putheader('Content-Length', str(len(body)))
    conn.putheader('Host', usplit.netloc)
    conn.endheaders()
    conn.set_debuglevel(-1)
    conn.send(body)
    errcode, errmsg, headers = conn.getreply()
    logger.info("return code: %s" % errcode)
    logger.info(errmsg)
    logger.info(headers)


    if errcode < 300:
        return conn.file.read()
    else:
        logger.warn(errcode)
        logger.warn(errmsg)
    return []


def _get_manifest(workspace_path):
    manifest_path = os.path.join(workspace_path, "pinned-manifest.xml")
    print "Searching for: %s" % manifest_path
    if os.path.exists(manifest_path):
        with open(manifest_path, "r") as manifest_file:
            return manifest_file.read()
    print "Manifest not found"
    return None


def _get_files(workspace_path):
    files_dict = {}
    for dirpath, dirnames, filenames in os.walk(workspace_path):
        for result_file_name in filenames:
            if result_file_name.endswith(".json"):
                files_dict.update({result_file_name: open(os.path.join(dirpath, result_file_name), 'rb')})
    return files_dict


def _results(workspace_path):
    benchmarks = {
        "Boot.oat size": ['boot_oat_size_ARM_32_Quick.txt',
                          'boot_oat_size_ARM_64_Quick.txt',
                          'boot_oat_size_x86_32_Quick.txt',
                          'boot_oat_size_x86_64_Quick.txt',
                          'boot_oat_size_x86_64_Optimizing.txt',
                          'boot_oat_size_x86_32_Optimizing.txt',
                          'boot_oat_size_ARM_64_Optimizing.txt',
                          'boot_oat_size_ARM_32_Optimizing.txt',
                          'boot_oat_size_mips_64_Optimizing.txt',
                          'boot_oat_size_mips_32_Quick.txt'],

        "Oat Execution Time": ['avg_oat_time_ARM_32_Quick.txt',
                               'avg_oat_time_ARM_64_Quick.txt',
                               'avg_oat_time_x86_64_Quick.txt',
                               'avg_oat_time_x86_32_Quick.txt',
                               'avg_oat_time_x86_64_Optimizing.txt',
                               'avg_oat_time_x86_32_Optimizing.txt',
                               'avg_oat_time_ARM_32_Optimizing.txt',
                               'avg_oat_time_ARM_64_Optimizing.txt',
                               'avg_oat_time_mips_64_Optimizing.txt',
                               'avg_oat_time_mips_32_Quick.txt']
        }

    val = []

    for benchmark, subscores in benchmarks.items():
        for subscore in subscores:
            path = os.path.join(workspace_path, subscore)
            print path, os.path.exists(path)
            if os.path.exists(path):
                raw = open(path, 'r').read().strip()
                measurement = float(raw.replace("YVALUE=", ""))

                name = (subscore
                        .replace("avg_oat_time_", "")
                        .replace("boot_oat_size_", "")
                        .replace(".txt", "")
                        .replace("_", " "))

                val.append({
                    "benchmark": benchmark,
                    "name": name,
                    "measurement": measurement
                })

    return val


if __name__ == '__main__':
    jenkins_project_name = os.environ.get("SOURCE_PROJECT_NAME")

    jenkins_build_number = os.environ.get("SOURCE_BUILD_NUMBER")
    jenkins_build_id = os.environ.get("SOURCE_BUILD_ID", jenkins_build_number)
    jenkins_build_url = os.environ.get("SOURCE_BUILD_URL")

    branch_name = os.environ.get("SOURCE_BRANCH_NAME", "")

    art_url = os.environ.get("ART_URL", "http://localhost:8000/")
    art_token = None
    if urlsplit(art_url).netloc.startswith('art-reports'):
        art_token = os.environ.get("ART_TOKEN")
    elif urlsplit(art_url).netloc.startswith('android-qa-reports'):
        art_token = os.environ.get("ART_TOKEN_ANDROID_REPORTS")
    else:
        art_token = os.environ.get("ART_TOKEN")

    manifest = _get_manifest("./artifacts")
    test_jobs = os.environ.get("LAVA_JOB_IDS", None)
    results = _results("./artifacts")

    if jenkins_build_number is None:
        print "Build number not set. Exiting!"
        sys.exit(1)
    if jenkins_project_name is None:
        print "Project name not set. Exiting!"
        sys.exit(1)
    if jenkins_build_url is None:
        print "Build URL not set. Exiting!"
        sys.exit(1)
    if art_token is None:
        print "ART token not set. Exiting!"
        sys.exit(1)
    if not manifest:
        print "Manifest missing. Exiting!"
        sys.exit(1)

    print "Registered test jobs: %s" % test_jobs

    params = {
        'name': jenkins_project_name,

        'build_id': jenkins_build_id,
        'build_url': jenkins_build_url,
        'build_number': jenkins_build_number,

        'test_jobs': test_jobs,
        'manifest': manifest,
        'branch_name': branch_name,

        "gerrit_change_number": os.environ.get("SOURCE_GERRIT_CHANGE_NUMBER", ""),
        "gerrit_patchset_number":os.environ.get("SOURCE_GERRIT_PATCHSET_NUMBER", ""),
        "gerrit_change_url": os.environ.get("SOURCE_GERRIT_CHANGE_URL", ""),
        "gerrit_change_id": os.environ.get("SOURCE_GERRIT_CHANGE_ID", ""),

        "results": results
    }

    if not results:
        params.pop("results")

    files = {}
    if test_jobs is None:
        params.pop('test_jobs')
        files = _get_files("./artifacts")
    _push_object(art_token, art_url, RESULT_ENDPOINT, params, files)
    params.pop('manifest')
    print params