Skip to content

Added interface version information to mbed detect command. #5077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 5, 2017
Merged
39 changes: 36 additions & 3 deletions tools/detect_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@
# Imports related to mbed build api
from tools.build_api import mcu_toolchain_matrix
from tools.test_api import get_autodetected_MUTS_list
from tools.test_api import get_module_avail
from argparse import ArgumentParser

try:
import mbed_lstools
except:
pass

def main():
"""Entry Point"""
Expand Down Expand Up @@ -75,15 +80,17 @@ def main():
count = 0
for mut in muts.values():
if re.match(mcu_filter, mut['mcu']):
interface_version = get_interface_version(mut['disk'])
print ""
print "[mbed] Detected %s, port %s, mounted %s" % \
(mut['mcu'], mut['port'], mut['disk'])
print "[mbed] Detected %s, port %s, mounted %s, interface version %s:" % \
(mut['mcu'], mut['port'], mut['disk'], interface_version)

print "[mbed] Supported toolchains for %s" % mut['mcu']
print mcu_toolchain_matrix(platform_filter=mut['mcu'])
count += 1

if count == 0:
print "[mbed] No mbed targets where detected on your system."
print "[mbed] No mbed targets were detected on your system."

except KeyboardInterrupt:
print "\n[CTRL+c] exit"
Expand All @@ -92,6 +99,32 @@ def main():
traceback.print_exc(file=sys.stdout)
print "[ERROR] %s" % str(exc)
sys.exit(1)

def get_interface_version(mount_point):
""" Function returns interface version from the target mounted on the specified mount point

mount_point can be acquired via the following:
muts = get_autodetected_MUTS_list()
for mut in muts.values():
mount_point = mut['disk']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not clear to me why I need an example of what to do with the parameter here. Are you suggesting that a user might be able to acquire a mount_point with the example code? If so, could you state that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a new commit with a bit more clarity, apologies for the confusion ;)


@param mount_point Name of disk where platform is connected to host machine.
"""
if get_module_avail('mbed_lstools'):
try :
mbeds = mbed_lstools.create()
details_txt = mbeds.get_details_txt(mount_point)

if 'Interface Version' in details_txt:
return details_txt['Interface Version']

elif 'Version' in details_txt:
return details_txt['Version']

except :
return 'unknown'

return 'unknown'

if __name__ == '__main__':
main()
83 changes: 83 additions & 0 deletions tools/test/detect_targets_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
mbed SDK
Copyright (c) 2017 ARM Limited

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.
"""

import unittest
from tools.detect_targets import get_interface_version
from tools.test_api import get_autodetected_MUTS_list

"""
Tests for detect_targets.py
"""

class DetectTargetsTests(unittest.TestCase):
"""
Test cases for Detect Target functionality
"""

def setUp(self):
"""
Called before each test case

:return:
"""
# Gather a valid mount point from the host machine
muts = get_autodetected_MUTS_list()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be mocked so that we don't depend on this test machine having some connected devices.


count = 0

for mut in muts.values():

count += 1
self.valid_mount_point = mut['disk']
break
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very out of place. Count is only ever going to be 1 or 0.


# If no targets are found, there is no way to determine Host OS mount point.
# Function should handle failure gracefully regardless of a mount point being present.
# Therefore it is safe to assume a valid mount point.
if count is 0:
self.valid_mount_point = "D:"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed by the test_interface_version_valid_mount_point function, so it should be assigned unconditionally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could we handle the case where there was no mount point (no device connected to the host), and across cases where host OS differs.. IE a valid mount point on Win 10 could be D: but a valid mount point in Linux is something like /media/{username}/DAPLink? There would need to be some condition that has to make an assumption at a certain point.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could we handle the case where there was no mount point (no device connected to the host), and across cases where host OS differs.

Test 'em all?

There would need to be some condition that has to make an assumption at a certain point.

Yes, but that assumption can be guaranteed with mocking.


self.invalid_mount_point = "23z/e\n"
self.missing_mount_point = None

def tearDown(self):
"""
Nothing to tear down.
Called after each test case

:return:
"""
pass

def test_interface_version_valid_mount_point(self):

interface_version = get_interface_version(self.valid_mount_point)
assert len(interface_version) > 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a strong guarantee. I would like to see an assertion like the others in this commit: asserting that the interface version is a particular string/number.


def test_interface_version_invalid_mount_point(self):

interface_version = get_interface_version(self.invalid_mount_point)
assert interface_version == 'unknown'

def test_interface_version_missing_mount_point(self):

interface_version = get_interface_version(self.missing_mount_point)
assert interface_version == 'unknown'


if __name__ == '__main__':
unittest.main()
3 changes: 1 addition & 2 deletions tools/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1643,11 +1643,10 @@ def detect_database_verbose(db_url):


def get_module_avail(module_name):
""" This function returns True if module_name is already impored module
""" This function returns True if module_name is already imported module
"""
return module_name in sys.modules.keys()


def get_autodetected_MUTS_list(platform_name_filter=None):
oldError = None
if os.name == 'nt':
Expand Down