A small script to demonstrate the usage of the mbed build service.

Dependents:   CMSIS-DAP

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
Committer:
Dan Ros
Date:
Fri May 23 11:26:06 2014 +0100
Revision:
1:b9b05345596d
Parent:
0:ac6e08cf16fe
Child:
2:09e9170d8a4e
added symbols support

Who changed what in which revision?

UserRevisionLine numberNew 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
$SUDO_USER 0:ac6e08cf16fe 5 python mbedapi.py --repo http://mbed.org/users/dan/code/pubtest/ --user dan --api http://mbed.org --platform mbed-LPC1768 --destdir /tmp/ --debug 2
$SUDO_USER 0:ac6e08cf16fe 6
$SUDO_USER 0:ac6e08cf16fe 7 #This will compile http://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
$SUDO_USER 0:ac6e08cf16fe 39 r = requests.post(args.api + "/api/v2/tasks/compiler/start/", data=payload, auth=auth)
$SUDO_USER 0:ac6e08cf16fe 40
$SUDO_USER 0:ac6e08cf16fe 41 logging.debug(r.content)
$SUDO_USER 0:ac6e08cf16fe 42
$SUDO_USER 0:ac6e08cf16fe 43 if r.status_code != 200:
$SUDO_USER 0:ac6e08cf16fe 44 raise Exception("Error while talking to the mbed API")
$SUDO_USER 0:ac6e08cf16fe 45
$SUDO_USER 0:ac6e08cf16fe 46 uuid = json.loads(r.content)['result']['data']['task_id']
$SUDO_USER 0:ac6e08cf16fe 47 logging.debug("Task accepted and given ID: %s"%uuid)
$SUDO_USER 0:ac6e08cf16fe 48 success = False
$SUDO_USER 0:ac6e08cf16fe 49
$SUDO_USER 0:ac6e08cf16fe 50
$SUDO_USER 0:ac6e08cf16fe 51 #poll for output
$SUDO_USER 0:ac6e08cf16fe 52 for check in range(0,40):
$SUDO_USER 0:ac6e08cf16fe 53 logging.debug("Checking for output: cycle %s of %s"%(check, 10))
$SUDO_USER 0:ac6e08cf16fe 54 time.sleep(2)
$SUDO_USER 0:ac6e08cf16fe 55 r = requests.get(args.api + "/api/v2/tasks/compiler/output/%s"%uuid, auth=auth)
$SUDO_USER 0:ac6e08cf16fe 56 logging.debug(r.content)
$SUDO_USER 0:ac6e08cf16fe 57 response = json.loads(r.content)
$SUDO_USER 0:ac6e08cf16fe 58 messages = response['result']['data']['new_messages']
$SUDO_USER 0:ac6e08cf16fe 59 percent = 0
$SUDO_USER 0:ac6e08cf16fe 60 for message in messages:
$SUDO_USER 0:ac6e08cf16fe 61 if message.get('message'):
$SUDO_USER 0:ac6e08cf16fe 62 if message.get('type') != 'debug':
$SUDO_USER 0:ac6e08cf16fe 63 logging.info("[%s] %s"%(message['type'], message['message']))
$SUDO_USER 0:ac6e08cf16fe 64 if message.get('action'):
$SUDO_USER 0:ac6e08cf16fe 65 if message.get('percent'):
$SUDO_USER 0:ac6e08cf16fe 66 percent = message['percent']
$SUDO_USER 0:ac6e08cf16fe 67 logging.info("[%s%% - %s] %s "%(percent, message['action'], message.get('file', '')))
$SUDO_USER 0:ac6e08cf16fe 68
$SUDO_USER 0:ac6e08cf16fe 69 if response['result']['data']['task_complete']:
$SUDO_USER 0:ac6e08cf16fe 70 logging.info("Task completed.")
$SUDO_USER 0:ac6e08cf16fe 71 success = response['result']['data']['compilation_success']
$SUDO_USER 0:ac6e08cf16fe 72 logging.info("Compile success: %s"%(success))
$SUDO_USER 0:ac6e08cf16fe 73 break
$SUDO_USER 0:ac6e08cf16fe 74
$SUDO_USER 0:ac6e08cf16fe 75 #now download
$SUDO_USER 0:ac6e08cf16fe 76 if success:
$SUDO_USER 0:ac6e08cf16fe 77 logging.info("Downloading your binary")
$SUDO_USER 0:ac6e08cf16fe 78 params = {'repomode': True, 'program': response['result']['data']['program'], 'binary': response['result']['data']['binary'] }
$SUDO_USER 0:ac6e08cf16fe 79 r = requests.get(args.api + "/api/v2/tasks/compiler/bin/", params=params, auth=auth)
$SUDO_USER 0:ac6e08cf16fe 80 destination = os.path.join(args.destdir, response['result']['data']['binary'])
$SUDO_USER 0:ac6e08cf16fe 81
$SUDO_USER 0:ac6e08cf16fe 82 with open(destination, 'wb') as fd:
$SUDO_USER 0:ac6e08cf16fe 83 for chunk in r.iter_content(1024):
$SUDO_USER 0:ac6e08cf16fe 84 fd.write(chunk)
$SUDO_USER 0:ac6e08cf16fe 85
$SUDO_USER 0:ac6e08cf16fe 86 logging.info("Finished!")
$SUDO_USER 0:ac6e08cf16fe 87
$SUDO_USER 0:ac6e08cf16fe 88
$SUDO_USER 0:ac6e08cf16fe 89 if __name__ == "__main__":
$SUDO_USER 0:ac6e08cf16fe 90 import argparse
$SUDO_USER 0:ac6e08cf16fe 91 parser = argparse.ArgumentParser(description='Build an mbed repository.')
$SUDO_USER 0:ac6e08cf16fe 92 parser.add_argument('--user', type=str, help='Your username on mbed.', required=True)
$SUDO_USER 0:ac6e08cf16fe 93 parser.add_argument('--api', type=str, help='URL to API server', required=True, default='https://mbed.org')
$SUDO_USER 0:ac6e08cf16fe 94 parser.add_argument('--repo', type=str, help='URL of repository to build.', required=True)
$SUDO_USER 0:ac6e08cf16fe 95 parser.add_argument('--platform', type=str, help='Platform name', required=True)
$SUDO_USER 0:ac6e08cf16fe 96 parser.add_argument('--destdir', type=str, help='Binary destination directory', required=True)
$SUDO_USER 0:ac6e08cf16fe 97 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 98 parser.add_argument('--extra_symbols', type=str, help='Provide extra symbols to build system', required=False, action='append')
$SUDO_USER 0:ac6e08cf16fe 99 parser.add_argument('--clean', action='store_true', help='Force clean build')
$SUDO_USER 0:ac6e08cf16fe 100 parser.add_argument('--debug', help='Show debugging info', required=False)
$SUDO_USER 0:ac6e08cf16fe 101
$SUDO_USER 0:ac6e08cf16fe 102 args = parser.parse_args()
$SUDO_USER 0:ac6e08cf16fe 103 if args.debug:
$SUDO_USER 0:ac6e08cf16fe 104 logging.basicConfig(level=logging.DEBUG)
$SUDO_USER 0:ac6e08cf16fe 105 else:
$SUDO_USER 0:ac6e08cf16fe 106 logging.basicConfig(level=logging.INFO)
$SUDO_USER 0:ac6e08cf16fe 107 build_repo(args)
$SUDO_USER 0:ac6e08cf16fe 108
$SUDO_USER 0:ac6e08cf16fe 109