From bf3614c18749569f244e28e407064ef98441aa0c Mon Sep 17 00:00:00 2001 From: Adrien CABARBAYE Date: Tue, 17 Dec 2019 10:13:50 +0000 Subject: [PATCH] [MacOs] [FIX] mbed-ls does not show any output when STLink external probe connected (#211) * ensures the xml is valid before parsing it * Decodes the XML content before encoding it again in the same way as what prettify does Ensures BeautifulSoup can be installed properly for all versions of python / platforms Ensures lxml parser is also installed properly when needed * Update src/mbed_os_tools/detect/darwin.py Co-Authored-By: Graham Hammond * Update requirements.txt Added a comment * Update azure-pipelines.yml Removed a condition which is no longer required * Update darwin.py making the comment more explicit * Update darwin.py Fix Pep8 issue --- azure-pipelines.yml | 4 ++++ packages/mbed-ls/requirements.txt | 2 +- requirements.txt | 3 +++ src/mbed_os_tools/detect/darwin.py | 19 ++++++++++++++----- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index aa662f923c..d3e716d74d 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -79,6 +79,10 @@ jobs: - script: "python -m pip install --upgrade pip && pip install -r test_requirements.txt" displayName: "Install mbed-os test dependencies" + - + script: "pip install lxml" + env: { STATIC_DEPS: true } + displayName: "Install lxml separately on Linux/MacOS" - script: 'pip install --user "urllib3<1.25"' displayName: "Fix dependency issue for requests package" diff --git a/packages/mbed-ls/requirements.txt b/packages/mbed-ls/requirements.txt index 13c3bf4d30..28e5e1a408 100644 --- a/packages/mbed-ls/requirements.txt +++ b/packages/mbed-ls/requirements.txt @@ -1,2 +1,2 @@ PrettyTable>=0.7.2 -mbed-os-tools>=0.0.9,<0.1.0 \ No newline at end of file +mbed-os-tools>=0.0.9,<0.1.0 diff --git a/requirements.txt b/requirements.txt index 0ccdc16dea..2bba47d29f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,6 @@ junit-xml>=1.0,<2.0 lockfile six>=1.0,<2.0 colorama>=0.3,<0.5 +# When using beautiful soup, the XML parser needs to be installed independently. It is only needed on macOs though. +beautifulsoup4 +lxml; sys_platform == 'darwin' diff --git a/src/mbed_os_tools/detect/darwin.py b/src/mbed_os_tools/detect/darwin.py index f4786e3fcf..17489ff45e 100644 --- a/src/mbed_os_tools/detect/darwin.py +++ b/src/mbed_os_tools/detect/darwin.py @@ -17,6 +17,8 @@ import subprocess import platform +from bs4 import BeautifulSoup + try: from plistlib import loads except ImportError: @@ -40,7 +42,13 @@ def _plist_from_popen(popen): if not out: return [] try: - return loads(out) + # Beautiful soup ensures the XML is properly formed after it is parsed + # so that it can be used by other less lenient commands without problems + xml_representation = BeautifulSoup(out.decode('utf8'), 'xml') + if not xml_representation.get_text(): + # The output is not in the XML format + return loads(out) + return loads(xml_representation.decode().encode('utf8')) except ExpatError: return [] @@ -85,9 +93,9 @@ def _dfs_usb_info(obj, parents): """ output = {} if ( - "BSD Name" in obj - and obj["BSD Name"].startswith("disk") - and mbed_volume_name_match.search(obj["IORegistryEntryName"]) + "BSD Name" in obj + and obj["BSD Name"].startswith("disk") + and mbed_volume_name_match.search(obj["IORegistryEntryName"]) ): disk_id = obj["BSD Name"] usb_info = {"serial": None, "vendor_id": None, "product_id": None, "tty": None} @@ -177,12 +185,13 @@ def _volumes(self): if self.mac_version >= 10.11: cmp_par = "-c" + usb_tree = [] for usb_controller in usb_controllers: ioreg_usb = subprocess.Popen( ["ioreg", "-a", "-r", cmp_par, usb_controller, "-l"], stdout=subprocess.PIPE, ) - usb_tree = _plist_from_popen(ioreg_usb) + usb_tree.extend(_plist_from_popen(ioreg_usb)) r = {}