Clone of official tools

Committer:
screamer
Date:
Thu May 19 19:44:41 2016 +0100
Revision:
0:66f3b5499f7f
Child:
13:ab47a20b66f0
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:66f3b5499f7f 1 """
screamer 0:66f3b5499f7f 2 mbed SDK
screamer 0:66f3b5499f7f 3 Copyright (c) 2011-2013 ARM Limited
screamer 0:66f3b5499f7f 4
screamer 0:66f3b5499f7f 5 Licensed under the Apache License, Version 2.0 (the "License");
screamer 0:66f3b5499f7f 6 you may not use this file except in compliance with the License.
screamer 0:66f3b5499f7f 7 You may obtain a copy of the License at
screamer 0:66f3b5499f7f 8
screamer 0:66f3b5499f7f 9 http://www.apache.org/licenses/LICENSE-2.0
screamer 0:66f3b5499f7f 10
screamer 0:66f3b5499f7f 11 Unless required by applicable law or agreed to in writing, software
screamer 0:66f3b5499f7f 12 distributed under the License is distributed on an "AS IS" BASIS,
screamer 0:66f3b5499f7f 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
screamer 0:66f3b5499f7f 14 See the License for the specific language governing permissions and
screamer 0:66f3b5499f7f 15 limitations under the License.
screamer 0:66f3b5499f7f 16 """
screamer 0:66f3b5499f7f 17 import sys
screamer 0:66f3b5499f7f 18 import argparse
screamer 0:66f3b5499f7f 19 import xml.etree.ElementTree as ET
screamer 0:66f3b5499f7f 20 import requests
screamer 0:66f3b5499f7f 21 import urlparse
screamer 0:66f3b5499f7f 22
screamer 0:66f3b5499f7f 23 def create_headers(args):
screamer 0:66f3b5499f7f 24 return { 'X-Api-Key': args.api_key }
screamer 0:66f3b5499f7f 25
screamer 0:66f3b5499f7f 26 def finish_command(command, response):
screamer 0:66f3b5499f7f 27 print(command, response.status_code, response.reason)
screamer 0:66f3b5499f7f 28 print(response.text)
screamer 0:66f3b5499f7f 29
screamer 0:66f3b5499f7f 30 if response.status_code < 400:
screamer 0:66f3b5499f7f 31 sys.exit(0)
screamer 0:66f3b5499f7f 32 else:
screamer 0:66f3b5499f7f 33 sys.exit(2)
screamer 0:66f3b5499f7f 34
screamer 0:66f3b5499f7f 35 def create_build(args):
screamer 0:66f3b5499f7f 36 build = {}
screamer 0:66f3b5499f7f 37 build['buildType'] = args.build_type
screamer 0:66f3b5499f7f 38 build['number'] = args.build_number
screamer 0:66f3b5499f7f 39 build['source'] = args.build_source
screamer 0:66f3b5499f7f 40 build['status'] = 'running'
screamer 0:66f3b5499f7f 41
screamer 0:66f3b5499f7f 42 r = requests.post(urlparse.urljoin(args.url, "api/builds"), headers=create_headers(args), json=build)
screamer 0:66f3b5499f7f 43
screamer 0:66f3b5499f7f 44 if r.status_code < 400:
screamer 0:66f3b5499f7f 45 if args.property_file_format:
screamer 0:66f3b5499f7f 46 print("MBED_BUILD_ID=" + r.text)
screamer 0:66f3b5499f7f 47 else:
screamer 0:66f3b5499f7f 48 print(r.text)
screamer 0:66f3b5499f7f 49
screamer 0:66f3b5499f7f 50 sys.exit(0)
screamer 0:66f3b5499f7f 51 else:
screamer 0:66f3b5499f7f 52 sys.exit(2)
screamer 0:66f3b5499f7f 53
screamer 0:66f3b5499f7f 54 def finish_build(args):
screamer 0:66f3b5499f7f 55 data = {}
screamer 0:66f3b5499f7f 56 data['status'] = 'completed'
screamer 0:66f3b5499f7f 57
screamer 0:66f3b5499f7f 58 r = requests.put(urlparse.urljoin(args.url, "api/builds/" + args.build_id), headers=create_headers(args), json=data)
screamer 0:66f3b5499f7f 59 finish_command('finish-build', r)
screamer 0:66f3b5499f7f 60
screamer 0:66f3b5499f7f 61 def promote_build(args):
screamer 0:66f3b5499f7f 62 data = {}
screamer 0:66f3b5499f7f 63 data['buildType'] = 'Release'
screamer 0:66f3b5499f7f 64
screamer 0:66f3b5499f7f 65 r = requests.put(urlparse.urljoin(args.url, "api/builds/" + args.build_id), headers=create_headers(args), json=data)
screamer 0:66f3b5499f7f 66 finish_command('promote-build', r)
screamer 0:66f3b5499f7f 67
screamer 0:66f3b5499f7f 68 def abort_build(args):
screamer 0:66f3b5499f7f 69 data = {}
screamer 0:66f3b5499f7f 70 data['status'] = 'aborted'
screamer 0:66f3b5499f7f 71
screamer 0:66f3b5499f7f 72 r = requests.put(urlparse.urljoin(args.url, "api/builds/" + args.build_id), headers=create_headers(args), json=data)
screamer 0:66f3b5499f7f 73 finish_command('abort-build', r)
screamer 0:66f3b5499f7f 74
screamer 0:66f3b5499f7f 75 def add_project_runs(args):
screamer 0:66f3b5499f7f 76 '''
screamer 0:66f3b5499f7f 77 -------------------------------------
screamer 0:66f3b5499f7f 78 Notes on 'project_run_data' structure:
screamer 0:66f3b5499f7f 79 --------------------------------------
screamer 0:66f3b5499f7f 80 'projectRuns' - Tree structure used to keep track of what projects have
screamer 0:66f3b5499f7f 81 been logged in different report files. The tree is organized as follows:
screamer 0:66f3b5499f7f 82
screamer 0:66f3b5499f7f 83 'projectRuns': { - Root element of tree
screamer 0:66f3b5499f7f 84
screamer 0:66f3b5499f7f 85 'hostOs': { - Host OS on which project was built/tested
screamer 0:66f3b5499f7f 86 - ex. windows, linux, or mac
screamer 0:66f3b5499f7f 87
screamer 0:66f3b5499f7f 88 'platform': { - Platform for which project was built/tested
screamer 0:66f3b5499f7f 89 (Corresponds to platform names in targets.py)
screamer 0:66f3b5499f7f 90 - ex. K64F, LPC1768, NRF51822, etc.
screamer 0:66f3b5499f7f 91
screamer 0:66f3b5499f7f 92 'toolchain': { - Toolchain with which project was built/tested
screamer 0:66f3b5499f7f 93 (Corresponds to TOOLCHAIN_CLASSES names in toolchains/__init__.py)
screamer 0:66f3b5499f7f 94 - ex. ARM, uARM, GCC_ARM, etc.
screamer 0:66f3b5499f7f 95
screamer 0:66f3b5499f7f 96 'project': { - Project that was build/tested
screamer 0:66f3b5499f7f 97 (Corresponds to test id in tests.py or library id in libraries.py)
screamer 0:66f3b5499f7f 98 - For tests, ex. MBED_A1, MBED_11, DTCT_1 etc.
screamer 0:66f3b5499f7f 99 - For libraries, ex. MBED, RTX, RTOS, etc.
screamer 0:66f3b5499f7f 100
screamer 0:66f3b5499f7f 101 },
screamer 0:66f3b5499f7f 102 ...
screamer 0:66f3b5499f7f 103 },
screamer 0:66f3b5499f7f 104 ...
screamer 0:66f3b5499f7f 105 },
screamer 0:66f3b5499f7f 106 ...
screamer 0:66f3b5499f7f 107 }
screamer 0:66f3b5499f7f 108 }
screamer 0:66f3b5499f7f 109
screamer 0:66f3b5499f7f 110 'platforms_set' - Set of all the platform names mentioned in the given report files
screamer 0:66f3b5499f7f 111
screamer 0:66f3b5499f7f 112 'toolchains_set' - Set of all the toolchain names mentioned in the given report files
screamer 0:66f3b5499f7f 113
screamer 0:66f3b5499f7f 114 'names_set' - Set of all the project names mentioned in the given report files
screamer 0:66f3b5499f7f 115
screamer 0:66f3b5499f7f 116 'hostOses_set' - Set of all the host names given (only given by the command line arguments)
screamer 0:66f3b5499f7f 117 '''
screamer 0:66f3b5499f7f 118
screamer 0:66f3b5499f7f 119 project_run_data = {}
screamer 0:66f3b5499f7f 120 project_run_data['projectRuns'] = {}
screamer 0:66f3b5499f7f 121 project_run_data['platforms_set'] = set()
screamer 0:66f3b5499f7f 122 project_run_data['vendors_set'] = set()
screamer 0:66f3b5499f7f 123 project_run_data['toolchains_set'] = set()
screamer 0:66f3b5499f7f 124 project_run_data['names_set'] = set()
screamer 0:66f3b5499f7f 125 project_run_data['hostOses_set'] = set()
screamer 0:66f3b5499f7f 126 project_run_data['hostOses_set'].add(args.host_os)
screamer 0:66f3b5499f7f 127
screamer 0:66f3b5499f7f 128 add_report(project_run_data, args.build_report, True, args.build_id, args.host_os)
screamer 0:66f3b5499f7f 129
screamer 0:66f3b5499f7f 130 if (args.test_report):
screamer 0:66f3b5499f7f 131 add_report(project_run_data, args.test_report, False, args.build_id, args.host_os)
screamer 0:66f3b5499f7f 132
screamer 0:66f3b5499f7f 133 ts_data = format_project_run_data(project_run_data)
screamer 0:66f3b5499f7f 134 r = requests.post(urlparse.urljoin(args.url, "api/projectRuns"), headers=create_headers(args), json=ts_data)
screamer 0:66f3b5499f7f 135 finish_command('add-project-runs', r)
screamer 0:66f3b5499f7f 136
screamer 0:66f3b5499f7f 137 def format_project_run_data(project_run_data):
screamer 0:66f3b5499f7f 138 ts_data = {}
screamer 0:66f3b5499f7f 139 ts_data['projectRuns'] = []
screamer 0:66f3b5499f7f 140
screamer 0:66f3b5499f7f 141 for hostOs in project_run_data['projectRuns'].values():
screamer 0:66f3b5499f7f 142 for platform in hostOs.values():
screamer 0:66f3b5499f7f 143 for toolchain in platform.values():
screamer 0:66f3b5499f7f 144 for project in toolchain.values():
screamer 0:66f3b5499f7f 145 ts_data['projectRuns'].append(project)
screamer 0:66f3b5499f7f 146
screamer 0:66f3b5499f7f 147 ts_data['platforms'] = list(project_run_data['platforms_set'])
screamer 0:66f3b5499f7f 148 ts_data['vendors'] = list(project_run_data['vendors_set'])
screamer 0:66f3b5499f7f 149 ts_data['toolchains'] = list(project_run_data['toolchains_set'])
screamer 0:66f3b5499f7f 150 ts_data['names'] = list(project_run_data['names_set'])
screamer 0:66f3b5499f7f 151 ts_data['hostOses'] = list(project_run_data['hostOses_set'])
screamer 0:66f3b5499f7f 152
screamer 0:66f3b5499f7f 153 return ts_data
screamer 0:66f3b5499f7f 154
screamer 0:66f3b5499f7f 155 def find_project_run(projectRuns, project):
screamer 0:66f3b5499f7f 156 keys = ['hostOs', 'platform', 'toolchain', 'project']
screamer 0:66f3b5499f7f 157
screamer 0:66f3b5499f7f 158 elem = projectRuns
screamer 0:66f3b5499f7f 159
screamer 0:66f3b5499f7f 160 for key in keys:
screamer 0:66f3b5499f7f 161 if not project[key] in elem:
screamer 0:66f3b5499f7f 162 return None
screamer 0:66f3b5499f7f 163
screamer 0:66f3b5499f7f 164 elem = elem[project[key]]
screamer 0:66f3b5499f7f 165
screamer 0:66f3b5499f7f 166 return elem
screamer 0:66f3b5499f7f 167
screamer 0:66f3b5499f7f 168 def add_project_run(projectRuns, project):
screamer 0:66f3b5499f7f 169 keys = ['hostOs', 'platform', 'toolchain']
screamer 0:66f3b5499f7f 170
screamer 0:66f3b5499f7f 171 elem = projectRuns
screamer 0:66f3b5499f7f 172
screamer 0:66f3b5499f7f 173 for key in keys:
screamer 0:66f3b5499f7f 174 if not project[key] in elem:
screamer 0:66f3b5499f7f 175 elem[project[key]] = {}
screamer 0:66f3b5499f7f 176
screamer 0:66f3b5499f7f 177 elem = elem[project[key]]
screamer 0:66f3b5499f7f 178
screamer 0:66f3b5499f7f 179 elem[project['project']] = project
screamer 0:66f3b5499f7f 180
screamer 0:66f3b5499f7f 181 def update_project_run_results(project_to_update, project, is_build):
screamer 0:66f3b5499f7f 182 if is_build:
screamer 0:66f3b5499f7f 183 project_to_update['buildPass'] = project['buildPass']
screamer 0:66f3b5499f7f 184 project_to_update['buildResult'] = project['buildResult']
screamer 0:66f3b5499f7f 185 project_to_update['buildOutput'] = project['buildOutput']
screamer 0:66f3b5499f7f 186 else:
screamer 0:66f3b5499f7f 187 project_to_update['testPass'] = project['testPass']
screamer 0:66f3b5499f7f 188 project_to_update['testResult'] = project['testResult']
screamer 0:66f3b5499f7f 189 project_to_update['testOutput'] = project['testOutput']
screamer 0:66f3b5499f7f 190
screamer 0:66f3b5499f7f 191 def update_project_run(projectRuns, project, is_build):
screamer 0:66f3b5499f7f 192 found_project = find_project_run(projectRuns, project)
screamer 0:66f3b5499f7f 193 if found_project:
screamer 0:66f3b5499f7f 194 update_project_run_results(found_project, project, is_build)
screamer 0:66f3b5499f7f 195 else:
screamer 0:66f3b5499f7f 196 add_project_run(projectRuns, project)
screamer 0:66f3b5499f7f 197
screamer 0:66f3b5499f7f 198 def add_report(project_run_data, report_file, is_build, build_id, host_os):
screamer 0:66f3b5499f7f 199 tree = None
screamer 0:66f3b5499f7f 200
screamer 0:66f3b5499f7f 201 try:
screamer 0:66f3b5499f7f 202 tree = ET.parse(report_file)
screamer 0:66f3b5499f7f 203 except:
screamer 0:66f3b5499f7f 204 print(sys.exc_info()[0])
screamer 0:66f3b5499f7f 205 print('Invalid path to report: %s', report_file)
screamer 0:66f3b5499f7f 206 sys.exit(1)
screamer 0:66f3b5499f7f 207
screamer 0:66f3b5499f7f 208 test_suites = tree.getroot()
screamer 0:66f3b5499f7f 209
screamer 0:66f3b5499f7f 210 for test_suite in test_suites:
screamer 0:66f3b5499f7f 211 platform = ""
screamer 0:66f3b5499f7f 212 toolchain = ""
screamer 0:66f3b5499f7f 213 vendor = ""
screamer 0:66f3b5499f7f 214 for properties in test_suite.findall('properties'):
screamer 0:66f3b5499f7f 215 for property in properties.findall('property'):
screamer 0:66f3b5499f7f 216 if property.attrib['name'] == 'target':
screamer 0:66f3b5499f7f 217 platform = property.attrib['value']
screamer 0:66f3b5499f7f 218 project_run_data['platforms_set'].add(platform)
screamer 0:66f3b5499f7f 219 elif property.attrib['name'] == 'toolchain':
screamer 0:66f3b5499f7f 220 toolchain = property.attrib['value']
screamer 0:66f3b5499f7f 221 project_run_data['toolchains_set'].add(toolchain)
screamer 0:66f3b5499f7f 222 elif property.attrib['name'] == 'vendor':
screamer 0:66f3b5499f7f 223 vendor = property.attrib['value']
screamer 0:66f3b5499f7f 224 project_run_data['vendors_set'].add(vendor)
screamer 0:66f3b5499f7f 225
screamer 0:66f3b5499f7f 226 for test_case in test_suite.findall('testcase'):
screamer 0:66f3b5499f7f 227 projectRun = {}
screamer 0:66f3b5499f7f 228 projectRun['build'] = build_id
screamer 0:66f3b5499f7f 229 projectRun['hostOs'] = host_os
screamer 0:66f3b5499f7f 230 projectRun['platform'] = platform
screamer 0:66f3b5499f7f 231 projectRun['toolchain'] = toolchain
screamer 0:66f3b5499f7f 232 projectRun['project'] = test_case.attrib['classname'].split('.')[-1]
screamer 0:66f3b5499f7f 233 projectRun['vendor'] = vendor
screamer 0:66f3b5499f7f 234
screamer 0:66f3b5499f7f 235 project_run_data['names_set'].add(projectRun['project'])
screamer 0:66f3b5499f7f 236
screamer 0:66f3b5499f7f 237 should_skip = False
screamer 0:66f3b5499f7f 238 skips = test_case.findall('skipped')
screamer 0:66f3b5499f7f 239
screamer 0:66f3b5499f7f 240 if skips:
screamer 0:66f3b5499f7f 241 should_skip = skips[0].attrib['message'] == 'SKIP'
screamer 0:66f3b5499f7f 242
screamer 0:66f3b5499f7f 243 if not should_skip:
screamer 0:66f3b5499f7f 244 system_outs = test_case.findall('system-out')
screamer 0:66f3b5499f7f 245
screamer 0:66f3b5499f7f 246 output = ""
screamer 0:66f3b5499f7f 247 if system_outs:
screamer 0:66f3b5499f7f 248 output = system_outs[0].text
screamer 0:66f3b5499f7f 249
screamer 0:66f3b5499f7f 250 if is_build:
screamer 0:66f3b5499f7f 251 projectRun['buildOutput'] = output
screamer 0:66f3b5499f7f 252 else:
screamer 0:66f3b5499f7f 253 projectRun['testOutput'] = output
screamer 0:66f3b5499f7f 254
screamer 0:66f3b5499f7f 255 errors = test_case.findall('error')
screamer 0:66f3b5499f7f 256 failures = test_case.findall('failure')
screamer 0:66f3b5499f7f 257 projectRunPass = None
screamer 0:66f3b5499f7f 258 result = None
screamer 0:66f3b5499f7f 259
screamer 0:66f3b5499f7f 260 if errors:
screamer 0:66f3b5499f7f 261 projectRunPass = False
screamer 0:66f3b5499f7f 262 result = errors[0].attrib['message']
screamer 0:66f3b5499f7f 263 elif failures:
screamer 0:66f3b5499f7f 264 projectRunPass = False
screamer 0:66f3b5499f7f 265 result = failures[0].attrib['message']
screamer 0:66f3b5499f7f 266 elif skips:
screamer 0:66f3b5499f7f 267 projectRunPass = True
screamer 0:66f3b5499f7f 268 result = skips[0].attrib['message']
screamer 0:66f3b5499f7f 269 else:
screamer 0:66f3b5499f7f 270 projectRunPass = True
screamer 0:66f3b5499f7f 271 result = 'OK'
screamer 0:66f3b5499f7f 272
screamer 0:66f3b5499f7f 273 if is_build:
screamer 0:66f3b5499f7f 274 projectRun['buildPass'] = projectRunPass
screamer 0:66f3b5499f7f 275 projectRun['buildResult'] = result
screamer 0:66f3b5499f7f 276 else:
screamer 0:66f3b5499f7f 277 projectRun['testPass'] = projectRunPass
screamer 0:66f3b5499f7f 278 projectRun['testResult'] = result
screamer 0:66f3b5499f7f 279
screamer 0:66f3b5499f7f 280 update_project_run(project_run_data['projectRuns'], projectRun, is_build)
screamer 0:66f3b5499f7f 281
screamer 0:66f3b5499f7f 282 def main(arguments):
screamer 0:66f3b5499f7f 283 # Register and parse command line arguments
screamer 0:66f3b5499f7f 284 parser = argparse.ArgumentParser()
screamer 0:66f3b5499f7f 285 parser.add_argument('-u', '--url', required=True, help='url to ci site')
screamer 0:66f3b5499f7f 286 parser.add_argument('-k', '--api-key', required=True, help='api-key for posting data')
screamer 0:66f3b5499f7f 287
screamer 0:66f3b5499f7f 288 subparsers = parser.add_subparsers(help='subcommand help')
screamer 0:66f3b5499f7f 289
screamer 0:66f3b5499f7f 290 create_build_parser = subparsers.add_parser('create-build', help='create a new build')
screamer 0:66f3b5499f7f 291 create_build_parser.add_argument('-b', '--build-number', required=True, help='build number')
screamer 0:66f3b5499f7f 292 create_build_parser.add_argument('-T', '--build-type', choices=['Nightly', 'Limited', 'Pull_Request', 'Release_Candidate'], required=True, help='type of build')
screamer 0:66f3b5499f7f 293 create_build_parser.add_argument('-s', '--build-source', required=True, help='url to source of build')
screamer 0:66f3b5499f7f 294 create_build_parser.add_argument('-p', '--property-file-format', action='store_true', help='print result in the property file format')
screamer 0:66f3b5499f7f 295 create_build_parser.set_defaults(func=create_build)
screamer 0:66f3b5499f7f 296
screamer 0:66f3b5499f7f 297 finish_build_parser = subparsers.add_parser('finish-build', help='finish a running build')
screamer 0:66f3b5499f7f 298 finish_build_parser.add_argument('-b', '--build-id', required=True, help='build id')
screamer 0:66f3b5499f7f 299 finish_build_parser.set_defaults(func=finish_build)
screamer 0:66f3b5499f7f 300
screamer 0:66f3b5499f7f 301 finish_build_parser = subparsers.add_parser('promote-build', help='promote a build to a release')
screamer 0:66f3b5499f7f 302 finish_build_parser.add_argument('-b', '--build-id', required=True, help='build id')
screamer 0:66f3b5499f7f 303 finish_build_parser.set_defaults(func=promote_build)
screamer 0:66f3b5499f7f 304
screamer 0:66f3b5499f7f 305 abort_build_parser = subparsers.add_parser('abort-build', help='abort a running build')
screamer 0:66f3b5499f7f 306 abort_build_parser.add_argument('-b', '--build-id', required=True, help='build id')
screamer 0:66f3b5499f7f 307 abort_build_parser.set_defaults(func=abort_build)
screamer 0:66f3b5499f7f 308
screamer 0:66f3b5499f7f 309 add_project_runs_parser = subparsers.add_parser('add-project-runs', help='add project runs to a build')
screamer 0:66f3b5499f7f 310 add_project_runs_parser.add_argument('-b', '--build-id', required=True, help='build id')
screamer 0:66f3b5499f7f 311 add_project_runs_parser.add_argument('-r', '--build-report', required=True, help='path to junit xml build report')
screamer 0:66f3b5499f7f 312 add_project_runs_parser.add_argument('-t', '--test-report', required=False, help='path to junit xml test report')
screamer 0:66f3b5499f7f 313 add_project_runs_parser.add_argument('-o', '--host-os', required=True, help='host os on which test was run')
screamer 0:66f3b5499f7f 314 add_project_runs_parser.set_defaults(func=add_project_runs)
screamer 0:66f3b5499f7f 315
screamer 0:66f3b5499f7f 316 args = parser.parse_args(arguments)
screamer 0:66f3b5499f7f 317 args.func(args)
screamer 0:66f3b5499f7f 318
screamer 0:66f3b5499f7f 319 if __name__ == '__main__':
screamer 0:66f3b5499f7f 320 main(sys.argv[1:])