Skip to content

Commit

Permalink
Merge pull request #23 from ARMmbed/devel_platform_mock
Browse files Browse the repository at this point in the history
Add new switch --mock used to map not existing platforms (devel prototyping support)
  • Loading branch information
PrzemekWirkus committed Sep 28, 2015
2 parents eac21bb + 6338d3f commit 3433f67
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
__pycache__/
*.py[cod]

# mbed-ls related files
.mbedls-mock

# C extensions
*.so

Expand Down
58 changes: 55 additions & 3 deletions mbed_lstools/lstools_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import re
import os
import json
import os.path

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

# If there is a local mocking data use it and add / override manufacture_ids
mock_ids = self.mock_read()
if mock_ids:
for mid in mock_ids:
self.manufacture_ids[mid] = mock_ids[mid]

# Which OSs are supported by this module
# Note: more than one OS can be supported by mbed-lstools_* module
os_supported = []
Expand Down Expand Up @@ -155,7 +162,52 @@ def __init__(self):
"RIOT": "RIOT",
}

#
MOCK_FILE_NAME = '.mbedls-mock'

def mock_read(self):
"""! Load mocking data from local file
@return Curent mocking configuration (dictionary)
"""
if os.path.isfile(self.MOCK_FILE_NAME):
if self.DEBUG_FLAG:
self.debug(self.mock_read.__name__, "reading %s"% self.MOCK_FILE_NAME)
with open(self.MOCK_FILE_NAME, "r") as f:
return json.load(f)
return {}

def mock_write(self, mock_ids):
# Write current mocking structure
if self.DEBUG_FLAG:
self.debug(self.mock_write.__name__, "writing %s"% self.MOCK_FILE_NAME)
with open(self.MOCK_FILE_NAME, "w") as f:
f.write(json.dumps(mock_ids, indent=4))

def mock_manufacture_ids(self, mid, platform_name, oper='+'):
"""! Replace (or add if manufacture id doesn't exist) entry in self.manufacture_ids
@param oper '+' add new mock / override existing entry
'-' remove mid from mocking entry
@return Mocked structure (json format)
"""
mock_ids = self.mock_read()

# Operations on mocked structure
if oper == '+':
mock_ids[mid] = platform_name
if self.DEBUG_FLAG:
self.debug(self.mock_manufacture_ids.__name__, "mock_ids['%s'] = '%s'"% (mid, platform_name))
elif oper in ['-', '!']:
if mid in mock_ids:
mock_ids.pop(mid)
if self.DEBUG_FLAG:
self.debug(self.mock_manufacture_ids.__name__, "removing '%s' mock"% mid)
elif mid == '*':
mock_ids = {} # Zero mocking set
if self.DEBUG_FLAG:
self.debug(self.mock_manufacture_ids.__name__, "zero mocking set")

self.mock_write(mock_ids)
return mock_ids

# Note: 'Ven_SEGGER' - This is used to detect devices from EFM family, they use Segger J-LInk to wrap MSD and CDC
usb_vendor_list = ['Ven_MBED', 'Ven_SEGGER']

Expand Down Expand Up @@ -205,7 +257,7 @@ def list_platforms(self):
result = []
mbeds = self.list_mbeds()
for i, val in enumerate(mbeds):
platform_name = val['platform_name']
platform_name = str(val['platform_name'])
if platform_name not in result:
result.append(platform_name)
return result
Expand All @@ -217,7 +269,7 @@ def list_platforms_ext(self):
result = {}
mbeds = self.list_mbeds()
for i, val in enumerate(mbeds):
platform_name = val['platform_name']
platform_name = str(val['platform_name'])
if platform_name not in result:
result[platform_name] = 1
else:
Expand Down
27 changes: 26 additions & 1 deletion mbed_lstools/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ def cmd_parser_setup():
action="store_true",
help='Parser friendly verbose mode')

parser.add_option('-m', '--mock',
dest='mock_platform',
help='Add locally manufacturers id and platform name. Example --mock=12B4:NEW_PLATFORM')

parser.add_option('-j', '--json',
dest='json',
default=False,
Expand Down Expand Up @@ -143,7 +147,28 @@ def mbedls_main():

mbeds.DEBUG_FLAG = opts.debug

if opts.json:
if opts.mock_platform:
if opts.mock_platform == '*':
if opts.json:
print json.dumps(mbeds.mock_read(), indent=4)

for token in opts.mock_platform.split(','):
if ':' in token:
oper = '+' # Default
mid, platform_name = token.split(':')
if mid and mid[0] in ['+', '-']:
oper = mid[0] # Operation (character)
mid = mid[1:] # We remove operation character
mbeds.mock_manufacture_ids(mid, platform_name, oper=oper)
elif token and token[0] in ['-', '!']:
# Operations where do not specify data after colon: --mock=-1234,-7678
oper = token[0]
mid = token[1:]
mbeds.mock_manufacture_ids(mid, 'dummy', oper=oper)
if opts.json:
print json.dumps(mbeds.mock_read(), indent=4)

elif opts.json:
print json.dumps(mbeds.list_mbeds_ext(), indent=4, sort_keys=True)

elif opts.json_by_target_id:
Expand Down

0 comments on commit 3433f67

Please sign in to comment.