Repository navigation
Expand file tree
/
Copy pathmbedapi.py
More file actions
160 lines (135 loc) · 5.85 KB
/
Copy pathmbedapi.py
File metadata and controls
160 lines (135 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#
# DAPLink Interface Firmware
# Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Usage example:
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
This will compile http://developer.mbed.org/users/dan/code/pubtest/
for the 1768 and download the result.
Examples of options:
--extra_symbols "foo=bar,x=y"
--replace_file "main.cpp:/tmp/replace_main.cpp"
(can be repeated)
"""
import os
import getpass
import json
import time
import requests
import logging
MBED_API_SERVER = 'https://os.mbed.com/'
def build_repo(user, password, repo, platform, destdir,
replace='', symbols='', clean=False, api=MBED_API_SERVER):
payload = {
'clean': clean,
'platform': platform,
'repo': repo,
'extra_symbols': symbols
}
if replace:
replace = []
for pair in replace:
dest = pair.split(':')[0]
src = pair.split(':')[1]
print(dest)
cwd = os.getcwd()
srcfile = open(os.path.join(cwd, src), 'r')
replace.append({dest: srcfile.read()})
payload['replace'] = json.dumps(replace)
logging.debug("Payload is: %s" % payload)
auth = (user, password,)
# send task to api
logging.debug(api + "/api/v2/tasks/compiler/start/" + "| data: " +
str(payload))
r = requests.post(api + "/api/v2/tasks/compiler/start/",
data=payload, auth=auth)
logging.debug(r.content)
if r.status_code != 200:
raise Exception("Error while talking to the mbed API")
uuid = json.loads(r.content)['result']['data']['task_id']
logging.debug("Task accepted and given ID: %s" % uuid)
success = False
# poll for output
for check in range(0, 40):
logging.debug("Checking for output: cycle %s of %s" % (check, 10))
time.sleep(2)
r = requests.get(api + "/api/v2/tasks/compiler/output/%s" %
uuid, auth=auth)
logging.debug(r.content)
response = json.loads(r.content)
messages = response['result']['data']['new_messages']
percent = 0
for message in messages:
if message.get('message'):
if message.get('type') != 'debug':
logging.info("[%s] %s" % (message['type'],
message['message']))
if message.get('action'):
if message.get('percent'):
percent = message['percent']
logging.info("[%s%% - %s] %s " % (percent, message['action'],
message.get('file', '')))
if response['result']['data']['task_complete']:
logging.info("Task completed.")
success = response['result']['data']['compilation_success']
logging.info("Compile success: %s" % (success))
break
# now download
if success:
logging.info("Downloading your binary")
params = {
'repomode': True,
'program': response['result']['data']['program'],
'binary': response['result']['data']['binary'],
'task_id': uuid
}
r = requests.get(api + "/api/v2/tasks/compiler/bin/",
params=params, auth=auth)
destination = os.path.join(destdir,
response['result']['data']['binary'])
with open(destination, 'wb') as fd:
for chunk in r.iter_content(1024):
fd.write(chunk)
logging.info("Finished!")
else:
raise Exception("Failed to build platform %s" % platform)
return destination
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Build an mbed repository.')
parser.add_argument('--user', type=str, help='Your username on mbed.', required=True)
parser.add_argument('--password', type=str, help='Your password on mbed.', default=None, required=False)
parser.add_argument('--api', type=str, help='URL to API server', required=False, default=MBED_API_SERVER)
parser.add_argument('--repo', type=str, help='URL of repository to build.', required=True)
parser.add_argument('--platform', type=str, help='Platform name', required=True)
parser.add_argument('--destdir', type=str, help='Binary destination directory', required=True)
parser.add_argument('--replace_file', type=str, help='Replace file and build. Can be repeated. Syntax: remotepath:localpath', required=False, action='append')
parser.add_argument('--extra_symbols', type=str, help='Provide extra symbols to build system', required=False, action='append')
parser.add_argument('--clean', action='store_true', help='Force clean build')
parser.add_argument('--debug', help='Show debugging info', required=False)
args = parser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
if args.password is None:
args.password = getpass.getpass('mbed password: ')
build_repo(args.user, args.password, args.repo, args.platform,
args.destdir, args.replace_file, args.extra_symbols,
args.clean, args.api)