A small script to demonstrate the usage of the mbed build service.
Example for using the mbed compile API
Prerequisites
- An account at developer.mbed.org (you will be asked for your password in order to compile) Get an account
- Python
- Python requests
On MacOS X you can install python requests as follows
install python requests on OS X
sudo easy_install requests
Usage
usage example for mbed API
python mbedapi.py --repo http://developer.mbed.org/users/dan/code/pubtest/ --user JonnyA --api http://developer.mbed.org --platform mbed-LPC1768 --destdir /tmp/ --debug 2
mbedapi.py@3:b71ebe79e1a5, 2015-07-03 (annotated)
- Committer:
- stevep
- Date:
- Fri Jul 03 15:58:46 2015 +0100
- Revision:
- 3:b71ebe79e1a5
- Parent:
- 2:09e9170d8a4e
Add task_id parameter to binary download
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
$SUDO_USER | 0:ac6e08cf16fe | 1 | """ |
$SUDO_USER | 0:ac6e08cf16fe | 2 | |
$SUDO_USER | 0:ac6e08cf16fe | 3 | Usage example: |
$SUDO_USER | 0:ac6e08cf16fe | 4 | |
Jonathan Austin |
2:09e9170d8a4e | 5 | python mbedapi.py --repo http://developer.mbed.org/users/dan/code/pubtest/ --user dan --api http://developer.mbed.org --platform mbed-LPC1768 --destdir /tmp/ --debug 2 |
$SUDO_USER | 0:ac6e08cf16fe | 6 | |
Jonathan Austin |
2:09e9170d8a4e | 7 | #This will compile http://developer.mbed.org/users/dan/code/pubtest/ for the 1768 and download the result. |
$SUDO_USER | 0:ac6e08cf16fe | 8 | |
Dan Ros |
1:b9b05345596d | 9 | Examples of options: |
Dan Ros |
1:b9b05345596d | 10 | --extra_symbols "foo=bar,x=y" |
$SUDO_USER | 0:ac6e08cf16fe | 11 | |
Dan Ros |
1:b9b05345596d | 12 | --replace_file "main.cpp:/tmp/replace_main.cpp" |
Dan Ros |
1:b9b05345596d | 13 | (can be repeated) |
$SUDO_USER | 0:ac6e08cf16fe | 14 | |
$SUDO_USER | 0:ac6e08cf16fe | 15 | """ |
$SUDO_USER | 0:ac6e08cf16fe | 16 | import os, getpass, sys, json, time, requests, logging |
$SUDO_USER | 0:ac6e08cf16fe | 17 | |
$SUDO_USER | 0:ac6e08cf16fe | 18 | |
$SUDO_USER | 0:ac6e08cf16fe | 19 | def build_repo(args): |
$SUDO_USER | 0:ac6e08cf16fe | 20 | |
Dan Ros |
1:b9b05345596d | 21 | payload = {'clean':args.clean, 'platform':args.platform, 'repo':args.repo, 'extra_symbols': args.extra_symbols} |
$SUDO_USER | 0:ac6e08cf16fe | 22 | |
$SUDO_USER | 0:ac6e08cf16fe | 23 | if args.replace_file: |
$SUDO_USER | 0:ac6e08cf16fe | 24 | replace = [] |
$SUDO_USER | 0:ac6e08cf16fe | 25 | for pair in args.replace_file: |
$SUDO_USER | 0:ac6e08cf16fe | 26 | dest = pair.split(':')[0] |
$SUDO_USER | 0:ac6e08cf16fe | 27 | src = pair.split(':')[1] |
$SUDO_USER | 0:ac6e08cf16fe | 28 | print dest |
$SUDO_USER | 0:ac6e08cf16fe | 29 | cwd = os.getcwd() |
$SUDO_USER | 0:ac6e08cf16fe | 30 | srcfile = open(os.path.join(cwd, src), 'r') |
$SUDO_USER | 0:ac6e08cf16fe | 31 | replace.append({dest:srcfile.read()}) |
$SUDO_USER | 0:ac6e08cf16fe | 32 | |
$SUDO_USER | 0:ac6e08cf16fe | 33 | payload['replace'] = json.dumps(replace) |
$SUDO_USER | 0:ac6e08cf16fe | 34 | logging.debug("Payload is: %s"%payload) |
$SUDO_USER | 0:ac6e08cf16fe | 35 | |
$SUDO_USER | 0:ac6e08cf16fe | 36 | auth = (args.user, getpass.getpass('mbed password: '),) |
$SUDO_USER | 0:ac6e08cf16fe | 37 | |
$SUDO_USER | 0:ac6e08cf16fe | 38 | #send task to api |
Jonathan Austin |
2:09e9170d8a4e | 39 | logging.debug(args.api + "/api/v2/tasks/compiler/start/" + "| data: " + str(payload)) |
$SUDO_USER | 0:ac6e08cf16fe | 40 | r = requests.post(args.api + "/api/v2/tasks/compiler/start/", data=payload, auth=auth) |
$SUDO_USER | 0:ac6e08cf16fe | 41 | |
$SUDO_USER | 0:ac6e08cf16fe | 42 | logging.debug(r.content) |
$SUDO_USER | 0:ac6e08cf16fe | 43 | |
$SUDO_USER | 0:ac6e08cf16fe | 44 | if r.status_code != 200: |
$SUDO_USER | 0:ac6e08cf16fe | 45 | raise Exception("Error while talking to the mbed API") |
$SUDO_USER | 0:ac6e08cf16fe | 46 | |
$SUDO_USER | 0:ac6e08cf16fe | 47 | uuid = json.loads(r.content)['result']['data']['task_id'] |
$SUDO_USER | 0:ac6e08cf16fe | 48 | logging.debug("Task accepted and given ID: %s"%uuid) |
$SUDO_USER | 0:ac6e08cf16fe | 49 | success = False |
$SUDO_USER | 0:ac6e08cf16fe | 50 | |
$SUDO_USER | 0:ac6e08cf16fe | 51 | |
$SUDO_USER | 0:ac6e08cf16fe | 52 | #poll for output |
$SUDO_USER | 0:ac6e08cf16fe | 53 | for check in range(0,40): |
$SUDO_USER | 0:ac6e08cf16fe | 54 | logging.debug("Checking for output: cycle %s of %s"%(check, 10)) |
$SUDO_USER | 0:ac6e08cf16fe | 55 | time.sleep(2) |
$SUDO_USER | 0:ac6e08cf16fe | 56 | r = requests.get(args.api + "/api/v2/tasks/compiler/output/%s"%uuid, auth=auth) |
$SUDO_USER | 0:ac6e08cf16fe | 57 | logging.debug(r.content) |
$SUDO_USER | 0:ac6e08cf16fe | 58 | response = json.loads(r.content) |
$SUDO_USER | 0:ac6e08cf16fe | 59 | messages = response['result']['data']['new_messages'] |
$SUDO_USER | 0:ac6e08cf16fe | 60 | percent = 0 |
$SUDO_USER | 0:ac6e08cf16fe | 61 | for message in messages: |
$SUDO_USER | 0:ac6e08cf16fe | 62 | if message.get('message'): |
$SUDO_USER | 0:ac6e08cf16fe | 63 | if message.get('type') != 'debug': |
$SUDO_USER | 0:ac6e08cf16fe | 64 | logging.info("[%s] %s"%(message['type'], message['message'])) |
$SUDO_USER | 0:ac6e08cf16fe | 65 | if message.get('action'): |
$SUDO_USER | 0:ac6e08cf16fe | 66 | if message.get('percent'): |
$SUDO_USER | 0:ac6e08cf16fe | 67 | percent = message['percent'] |
$SUDO_USER | 0:ac6e08cf16fe | 68 | logging.info("[%s%% - %s] %s "%(percent, message['action'], message.get('file', ''))) |
$SUDO_USER | 0:ac6e08cf16fe | 69 | |
$SUDO_USER | 0:ac6e08cf16fe | 70 | if response['result']['data']['task_complete']: |
$SUDO_USER | 0:ac6e08cf16fe | 71 | logging.info("Task completed.") |
$SUDO_USER | 0:ac6e08cf16fe | 72 | success = response['result']['data']['compilation_success'] |
$SUDO_USER | 0:ac6e08cf16fe | 73 | logging.info("Compile success: %s"%(success)) |
$SUDO_USER | 0:ac6e08cf16fe | 74 | break |
$SUDO_USER | 0:ac6e08cf16fe | 75 | |
$SUDO_USER | 0:ac6e08cf16fe | 76 | #now download |
$SUDO_USER | 0:ac6e08cf16fe | 77 | if success: |
$SUDO_USER | 0:ac6e08cf16fe | 78 | logging.info("Downloading your binary") |
stevep |
3:b71ebe79e1a5 | 79 | params = { |
stevep |
3:b71ebe79e1a5 | 80 | 'repomode': True, |
stevep |
3:b71ebe79e1a5 | 81 | 'program': response['result']['data']['program'], |
stevep |
3:b71ebe79e1a5 | 82 | 'binary': response['result']['data']['binary'], |
stevep |
3:b71ebe79e1a5 | 83 | 'task_id': uuid } |
$SUDO_USER | 0:ac6e08cf16fe | 84 | r = requests.get(args.api + "/api/v2/tasks/compiler/bin/", params=params, auth=auth) |
$SUDO_USER | 0:ac6e08cf16fe | 85 | destination = os.path.join(args.destdir, response['result']['data']['binary']) |
$SUDO_USER | 0:ac6e08cf16fe | 86 | |
$SUDO_USER | 0:ac6e08cf16fe | 87 | with open(destination, 'wb') as fd: |
$SUDO_USER | 0:ac6e08cf16fe | 88 | for chunk in r.iter_content(1024): |
$SUDO_USER | 0:ac6e08cf16fe | 89 | fd.write(chunk) |
$SUDO_USER | 0:ac6e08cf16fe | 90 | |
$SUDO_USER | 0:ac6e08cf16fe | 91 | logging.info("Finished!") |
$SUDO_USER | 0:ac6e08cf16fe | 92 | |
$SUDO_USER | 0:ac6e08cf16fe | 93 | |
$SUDO_USER | 0:ac6e08cf16fe | 94 | if __name__ == "__main__": |
$SUDO_USER | 0:ac6e08cf16fe | 95 | import argparse |
$SUDO_USER | 0:ac6e08cf16fe | 96 | parser = argparse.ArgumentParser(description='Build an mbed repository.') |
$SUDO_USER | 0:ac6e08cf16fe | 97 | parser.add_argument('--user', type=str, help='Your username on mbed.', required=True) |
Jonathan Austin |
2:09e9170d8a4e | 98 | parser.add_argument('--api', type=str, help='URL to API server', required=True, default='https://developer.mbed.org') |
$SUDO_USER | 0:ac6e08cf16fe | 99 | parser.add_argument('--repo', type=str, help='URL of repository to build.', required=True) |
$SUDO_USER | 0:ac6e08cf16fe | 100 | parser.add_argument('--platform', type=str, help='Platform name', required=True) |
$SUDO_USER | 0:ac6e08cf16fe | 101 | parser.add_argument('--destdir', type=str, help='Binary destination directory', required=True) |
$SUDO_USER | 0:ac6e08cf16fe | 102 | parser.add_argument('--replace_file', type=str, help='Replace file and build. Can be repeated. Syntax: remotepath:localpath', required=False, action='append') |
Dan Ros |
1:b9b05345596d | 103 | parser.add_argument('--extra_symbols', type=str, help='Provide extra symbols to build system', required=False, action='append') |
$SUDO_USER | 0:ac6e08cf16fe | 104 | parser.add_argument('--clean', action='store_true', help='Force clean build') |
$SUDO_USER | 0:ac6e08cf16fe | 105 | parser.add_argument('--debug', help='Show debugging info', required=False) |
$SUDO_USER | 0:ac6e08cf16fe | 106 | |
$SUDO_USER | 0:ac6e08cf16fe | 107 | args = parser.parse_args() |
$SUDO_USER | 0:ac6e08cf16fe | 108 | if args.debug: |
$SUDO_USER | 0:ac6e08cf16fe | 109 | logging.basicConfig(level=logging.DEBUG) |
$SUDO_USER | 0:ac6e08cf16fe | 110 | else: |
$SUDO_USER | 0:ac6e08cf16fe | 111 | logging.basicConfig(level=logging.INFO) |
$SUDO_USER | 0:ac6e08cf16fe | 112 | build_repo(args) |
$SUDO_USER | 0:ac6e08cf16fe | 113 | |
$SUDO_USER | 0:ac6e08cf16fe | 114 |