Skip to content

Commit 3433f67

Browse files
committed
Merge pull request #23 from ARMmbed/devel_platform_mock
Add new switch --mock used to map not existing platforms (devel prototyping support)
2 parents eac21bb + 6338d3f commit 3433f67

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
__pycache__/
33
*.py[cod]
44

5+
# mbed-ls related files
6+
.mbedls-mock
7+
58
# C extensions
69
*.so
710

mbed_lstools/lstools_base.py

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import re
1919
import os
2020
import json
21+
import os.path
2122

2223
class MbedLsToolsBase:
2324
""" Base class for mbed-lstools, defines mbed-ls tools interface for mbed-enabled devices detection for various hosts
@@ -29,6 +30,12 @@ def __init__(self):
2930
self.DEBUG_FLAG = False # Used to enable debug code / prints
3031
self.ERRORLEVEL_FLAG = 0 # Used to return success code to environment
3132

33+
# If there is a local mocking data use it and add / override manufacture_ids
34+
mock_ids = self.mock_read()
35+
if mock_ids:
36+
for mid in mock_ids:
37+
self.manufacture_ids[mid] = mock_ids[mid]
38+
3239
# Which OSs are supported by this module
3340
# Note: more than one OS can be supported by mbed-lstools_* module
3441
os_supported = []
@@ -155,7 +162,52 @@ def __init__(self):
155162
"RIOT": "RIOT",
156163
}
157164

158-
#
165+
MOCK_FILE_NAME = '.mbedls-mock'
166+
167+
def mock_read(self):
168+
"""! Load mocking data from local file
169+
@return Curent mocking configuration (dictionary)
170+
"""
171+
if os.path.isfile(self.MOCK_FILE_NAME):
172+
if self.DEBUG_FLAG:
173+
self.debug(self.mock_read.__name__, "reading %s"% self.MOCK_FILE_NAME)
174+
with open(self.MOCK_FILE_NAME, "r") as f:
175+
return json.load(f)
176+
return {}
177+
178+
def mock_write(self, mock_ids):
179+
# Write current mocking structure
180+
if self.DEBUG_FLAG:
181+
self.debug(self.mock_write.__name__, "writing %s"% self.MOCK_FILE_NAME)
182+
with open(self.MOCK_FILE_NAME, "w") as f:
183+
f.write(json.dumps(mock_ids, indent=4))
184+
185+
def mock_manufacture_ids(self, mid, platform_name, oper='+'):
186+
"""! Replace (or add if manufacture id doesn't exist) entry in self.manufacture_ids
187+
@param oper '+' add new mock / override existing entry
188+
'-' remove mid from mocking entry
189+
@return Mocked structure (json format)
190+
"""
191+
mock_ids = self.mock_read()
192+
193+
# Operations on mocked structure
194+
if oper == '+':
195+
mock_ids[mid] = platform_name
196+
if self.DEBUG_FLAG:
197+
self.debug(self.mock_manufacture_ids.__name__, "mock_ids['%s'] = '%s'"% (mid, platform_name))
198+
elif oper in ['-', '!']:
199+
if mid in mock_ids:
200+
mock_ids.pop(mid)
201+
if self.DEBUG_FLAG:
202+
self.debug(self.mock_manufacture_ids.__name__, "removing '%s' mock"% mid)
203+
elif mid == '*':
204+
mock_ids = {} # Zero mocking set
205+
if self.DEBUG_FLAG:
206+
self.debug(self.mock_manufacture_ids.__name__, "zero mocking set")
207+
208+
self.mock_write(mock_ids)
209+
return mock_ids
210+
159211
# Note: 'Ven_SEGGER' - This is used to detect devices from EFM family, they use Segger J-LInk to wrap MSD and CDC
160212
usb_vendor_list = ['Ven_MBED', 'Ven_SEGGER']
161213

@@ -205,7 +257,7 @@ def list_platforms(self):
205257
result = []
206258
mbeds = self.list_mbeds()
207259
for i, val in enumerate(mbeds):
208-
platform_name = val['platform_name']
260+
platform_name = str(val['platform_name'])
209261
if platform_name not in result:
210262
result.append(platform_name)
211263
return result
@@ -217,7 +269,7 @@ def list_platforms_ext(self):
217269
result = {}
218270
mbeds = self.list_mbeds()
219271
for i, val in enumerate(mbeds):
220-
platform_name = val['platform_name']
272+
platform_name = str(val['platform_name'])
221273
if platform_name not in result:
222274
result[platform_name] = 1
223275
else:

mbed_lstools/main.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ def cmd_parser_setup():
8787
action="store_true",
8888
help='Parser friendly verbose mode')
8989

90+
parser.add_option('-m', '--mock',
91+
dest='mock_platform',
92+
help='Add locally manufacturers id and platform name. Example --mock=12B4:NEW_PLATFORM')
93+
9094
parser.add_option('-j', '--json',
9195
dest='json',
9296
default=False,
@@ -143,7 +147,28 @@ def mbedls_main():
143147

144148
mbeds.DEBUG_FLAG = opts.debug
145149

146-
if opts.json:
150+
if opts.mock_platform:
151+
if opts.mock_platform == '*':
152+
if opts.json:
153+
print json.dumps(mbeds.mock_read(), indent=4)
154+
155+
for token in opts.mock_platform.split(','):
156+
if ':' in token:
157+
oper = '+' # Default
158+
mid, platform_name = token.split(':')
159+
if mid and mid[0] in ['+', '-']:
160+
oper = mid[0] # Operation (character)
161+
mid = mid[1:] # We remove operation character
162+
mbeds.mock_manufacture_ids(mid, platform_name, oper=oper)
163+
elif token and token[0] in ['-', '!']:
164+
# Operations where do not specify data after colon: --mock=-1234,-7678
165+
oper = token[0]
166+
mid = token[1:]
167+
mbeds.mock_manufacture_ids(mid, 'dummy', oper=oper)
168+
if opts.json:
169+
print json.dumps(mbeds.mock_read(), indent=4)
170+
171+
elif opts.json:
147172
print json.dumps(mbeds.list_mbeds_ext(), indent=4, sort_keys=True)
148173

149174
elif opts.json_by_target_id:

0 commit comments

Comments
 (0)