Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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@2:09e9170d8a4e, 2015-03-02 (annotated)
- Committer:
- Jonathan Austin
- Date:
- Mon Mar 02 15:25:34 2015 +0000
- Revision:
- 2:09e9170d8a4e
- Parent:
- 1:b9b05345596d
- Child:
- 3:b71ebe79e1a5
Update URLs to point to developer.mbed.org
Data gets lost in the redirect from mbed.org-> developer.mbed.org,
which leaves authentication failing:
DEBUG:root:Authorization Required
...
Exception: Error while talking to the mbed API
This change returns the example script to working state.
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") |
$SUDO_USER | 0:ac6e08cf16fe | 79 | params = {'repomode': True, 'program': response['result']['data']['program'], 'binary': response['result']['data']['binary'] } |
$SUDO_USER | 0:ac6e08cf16fe | 80 | r = requests.get(args.api + "/api/v2/tasks/compiler/bin/", params=params, auth=auth) |
$SUDO_USER | 0:ac6e08cf16fe | 81 | destination = os.path.join(args.destdir, response['result']['data']['binary']) |
$SUDO_USER | 0:ac6e08cf16fe | 82 | |
$SUDO_USER | 0:ac6e08cf16fe | 83 | with open(destination, 'wb') as fd: |
$SUDO_USER | 0:ac6e08cf16fe | 84 | for chunk in r.iter_content(1024): |
$SUDO_USER | 0:ac6e08cf16fe | 85 | fd.write(chunk) |
$SUDO_USER | 0:ac6e08cf16fe | 86 | |
$SUDO_USER | 0:ac6e08cf16fe | 87 | logging.info("Finished!") |
$SUDO_USER | 0:ac6e08cf16fe | 88 | |
$SUDO_USER | 0:ac6e08cf16fe | 89 | |
$SUDO_USER | 0:ac6e08cf16fe | 90 | if __name__ == "__main__": |
$SUDO_USER | 0:ac6e08cf16fe | 91 | import argparse |
$SUDO_USER | 0:ac6e08cf16fe | 92 | parser = argparse.ArgumentParser(description='Build an mbed repository.') |
$SUDO_USER | 0:ac6e08cf16fe | 93 | parser.add_argument('--user', type=str, help='Your username on mbed.', required=True) |
Jonathan Austin |
2:09e9170d8a4e | 94 | parser.add_argument('--api', type=str, help='URL to API server', required=True, default='https://developer.mbed.org') |
$SUDO_USER | 0:ac6e08cf16fe | 95 | parser.add_argument('--repo', type=str, help='URL of repository to build.', required=True) |
$SUDO_USER | 0:ac6e08cf16fe | 96 | parser.add_argument('--platform', type=str, help='Platform name', required=True) |
$SUDO_USER | 0:ac6e08cf16fe | 97 | parser.add_argument('--destdir', type=str, help='Binary destination directory', required=True) |
$SUDO_USER | 0:ac6e08cf16fe | 98 | 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 | 99 | parser.add_argument('--extra_symbols', type=str, help='Provide extra symbols to build system', required=False, action='append') |
$SUDO_USER | 0:ac6e08cf16fe | 100 | parser.add_argument('--clean', action='store_true', help='Force clean build') |
$SUDO_USER | 0:ac6e08cf16fe | 101 | parser.add_argument('--debug', help='Show debugging info', required=False) |
$SUDO_USER | 0:ac6e08cf16fe | 102 | |
$SUDO_USER | 0:ac6e08cf16fe | 103 | args = parser.parse_args() |
$SUDO_USER | 0:ac6e08cf16fe | 104 | if args.debug: |
$SUDO_USER | 0:ac6e08cf16fe | 105 | logging.basicConfig(level=logging.DEBUG) |
$SUDO_USER | 0:ac6e08cf16fe | 106 | else: |
$SUDO_USER | 0:ac6e08cf16fe | 107 | logging.basicConfig(level=logging.INFO) |
$SUDO_USER | 0:ac6e08cf16fe | 108 | build_repo(args) |
$SUDO_USER | 0:ac6e08cf16fe | 109 | |
$SUDO_USER | 0:ac6e08cf16fe | 110 |