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:
$SUDO_USER
Date:
Tue May 13 17:05:58 2014 +0100
Revision:
0:ac6e08cf16fe
Child:
1:b9b05345596d
Added file

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
$SUDO_USER 0:ac6e08cf16fe 9
$SUDO_USER 0:ac6e08cf16fe 10
$SUDO_USER 0:ac6e08cf16fe 11 """
$SUDO_USER 0:ac6e08cf16fe 12 import os, getpass, sys, json, time, requests, logging
$SUDO_USER 0:ac6e08cf16fe 13
$SUDO_USER 0:ac6e08cf16fe 14
$SUDO_USER 0:ac6e08cf16fe 15 def build_repo(args):
$SUDO_USER 0:ac6e08cf16fe 16
$SUDO_USER 0:ac6e08cf16fe 17 payload = {'clean':args.clean, 'platform':args.platform, 'repo':args.repo}
$SUDO_USER 0:ac6e08cf16fe 18
$SUDO_USER 0:ac6e08cf16fe 19 if args.replace_file:
$SUDO_USER 0:ac6e08cf16fe 20 replace = []
$SUDO_USER 0:ac6e08cf16fe 21 for pair in args.replace_file:
$SUDO_USER 0:ac6e08cf16fe 22 dest = pair.split(':')[0]
$SUDO_USER 0:ac6e08cf16fe 23 src = pair.split(':')[1]
$SUDO_USER 0:ac6e08cf16fe 24 print dest
$SUDO_USER 0:ac6e08cf16fe 25 cwd = os.getcwd()
$SUDO_USER 0:ac6e08cf16fe 26 srcfile = open(os.path.join(cwd, src), 'r')
$SUDO_USER 0:ac6e08cf16fe 27 replace.append({dest:srcfile.read()})
$SUDO_USER 0:ac6e08cf16fe 28
$SUDO_USER 0:ac6e08cf16fe 29 payload['replace'] = json.dumps(replace)
$SUDO_USER 0:ac6e08cf16fe 30 logging.debug("Payload is: %s"%payload)
$SUDO_USER 0:ac6e08cf16fe 31
$SUDO_USER 0:ac6e08cf16fe 32 auth = (args.user, getpass.getpass('mbed password: '),)
$SUDO_USER 0:ac6e08cf16fe 33
$SUDO_USER 0:ac6e08cf16fe 34 #send task to api
$SUDO_USER 0:ac6e08cf16fe 35 r = requests.post(args.api + "/api/v2/tasks/compiler/start/", data=payload, auth=auth)
$SUDO_USER 0:ac6e08cf16fe 36
$SUDO_USER 0:ac6e08cf16fe 37 logging.debug(r.content)
$SUDO_USER 0:ac6e08cf16fe 38
$SUDO_USER 0:ac6e08cf16fe 39 if r.status_code != 200:
$SUDO_USER 0:ac6e08cf16fe 40 raise Exception("Error while talking to the mbed API")
$SUDO_USER 0:ac6e08cf16fe 41
$SUDO_USER 0:ac6e08cf16fe 42 uuid = json.loads(r.content)['result']['data']['task_id']
$SUDO_USER 0:ac6e08cf16fe 43 logging.debug("Task accepted and given ID: %s"%uuid)
$SUDO_USER 0:ac6e08cf16fe 44 success = False
$SUDO_USER 0:ac6e08cf16fe 45
$SUDO_USER 0:ac6e08cf16fe 46
$SUDO_USER 0:ac6e08cf16fe 47 #poll for output
$SUDO_USER 0:ac6e08cf16fe 48 for check in range(0,40):
$SUDO_USER 0:ac6e08cf16fe 49 logging.debug("Checking for output: cycle %s of %s"%(check, 10))
$SUDO_USER 0:ac6e08cf16fe 50 time.sleep(2)
$SUDO_USER 0:ac6e08cf16fe 51 r = requests.get(args.api + "/api/v2/tasks/compiler/output/%s"%uuid, auth=auth)
$SUDO_USER 0:ac6e08cf16fe 52 logging.debug(r.content)
$SUDO_USER 0:ac6e08cf16fe 53 response = json.loads(r.content)
$SUDO_USER 0:ac6e08cf16fe 54 messages = response['result']['data']['new_messages']
$SUDO_USER 0:ac6e08cf16fe 55 percent = 0
$SUDO_USER 0:ac6e08cf16fe 56 for message in messages:
$SUDO_USER 0:ac6e08cf16fe 57 if message.get('message'):
$SUDO_USER 0:ac6e08cf16fe 58 if message.get('type') != 'debug':
$SUDO_USER 0:ac6e08cf16fe 59 logging.info("[%s] %s"%(message['type'], message['message']))
$SUDO_USER 0:ac6e08cf16fe 60 if message.get('action'):
$SUDO_USER 0:ac6e08cf16fe 61 if message.get('percent'):
$SUDO_USER 0:ac6e08cf16fe 62 percent = message['percent']
$SUDO_USER 0:ac6e08cf16fe 63 logging.info("[%s%% - %s] %s "%(percent, message['action'], message.get('file', '')))
$SUDO_USER 0:ac6e08cf16fe 64
$SUDO_USER 0:ac6e08cf16fe 65 if response['result']['data']['task_complete']:
$SUDO_USER 0:ac6e08cf16fe 66 logging.info("Task completed.")
$SUDO_USER 0:ac6e08cf16fe 67 success = response['result']['data']['compilation_success']
$SUDO_USER 0:ac6e08cf16fe 68 logging.info("Compile success: %s"%(success))
$SUDO_USER 0:ac6e08cf16fe 69 break
$SUDO_USER 0:ac6e08cf16fe 70
$SUDO_USER 0:ac6e08cf16fe 71 #now download
$SUDO_USER 0:ac6e08cf16fe 72 if success:
$SUDO_USER 0:ac6e08cf16fe 73 logging.info("Downloading your binary")
$SUDO_USER 0:ac6e08cf16fe 74 params = {'repomode': True, 'program': response['result']['data']['program'], 'binary': response['result']['data']['binary'] }
$SUDO_USER 0:ac6e08cf16fe 75 r = requests.get(args.api + "/api/v2/tasks/compiler/bin/", params=params, auth=auth)
$SUDO_USER 0:ac6e08cf16fe 76 destination = os.path.join(args.destdir, response['result']['data']['binary'])
$SUDO_USER 0:ac6e08cf16fe 77
$SUDO_USER 0:ac6e08cf16fe 78 with open(destination, 'wb') as fd:
$SUDO_USER 0:ac6e08cf16fe 79 for chunk in r.iter_content(1024):
$SUDO_USER 0:ac6e08cf16fe 80 fd.write(chunk)
$SUDO_USER 0:ac6e08cf16fe 81
$SUDO_USER 0:ac6e08cf16fe 82 logging.info("Finished!")
$SUDO_USER 0:ac6e08cf16fe 83
$SUDO_USER 0:ac6e08cf16fe 84
$SUDO_USER 0:ac6e08cf16fe 85 if __name__ == "__main__":
$SUDO_USER 0:ac6e08cf16fe 86 import argparse
$SUDO_USER 0:ac6e08cf16fe 87 parser = argparse.ArgumentParser(description='Build an mbed repository.')
$SUDO_USER 0:ac6e08cf16fe 88 parser.add_argument('--user', type=str, help='Your username on mbed.', required=True)
$SUDO_USER 0:ac6e08cf16fe 89 parser.add_argument('--api', type=str, help='URL to API server', required=True, default='https://mbed.org')
$SUDO_USER 0:ac6e08cf16fe 90 parser.add_argument('--repo', type=str, help='URL of repository to build.', required=True)
$SUDO_USER 0:ac6e08cf16fe 91 parser.add_argument('--platform', type=str, help='Platform name', required=True)
$SUDO_USER 0:ac6e08cf16fe 92 parser.add_argument('--destdir', type=str, help='Binary destination directory', required=True)
$SUDO_USER 0:ac6e08cf16fe 93 parser.add_argument('--replace_file', type=str, help='Replace file and build. Can be repeated. Syntax: remotepath:localpath', required=False, action='append')
$SUDO_USER 0:ac6e08cf16fe 94 parser.add_argument('--clean', action='store_true', help='Force clean build')
$SUDO_USER 0:ac6e08cf16fe 95 parser.add_argument('--debug', help='Show debugging info', required=False)
$SUDO_USER 0:ac6e08cf16fe 96
$SUDO_USER 0:ac6e08cf16fe 97 args = parser.parse_args()
$SUDO_USER 0:ac6e08cf16fe 98 if args.debug:
$SUDO_USER 0:ac6e08cf16fe 99 logging.basicConfig(level=logging.DEBUG)
$SUDO_USER 0:ac6e08cf16fe 100 else:
$SUDO_USER 0:ac6e08cf16fe 101 logging.basicConfig(level=logging.INFO)
$SUDO_USER 0:ac6e08cf16fe 102 build_repo(args)
$SUDO_USER 0:ac6e08cf16fe 103
$SUDO_USER 0:ac6e08cf16fe 104