5 Copyright (c) 2011-2015 ARM Limited
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
25 from collections
import defaultdict
28 from .
import lstools_base
31 logger = logging.getLogger(
"mbedls.main")
32 logger.addHandler(logging.NullHandler())
37 """! Factory used to create host OS specific mbed-lstools object
39 :param kwargs: keyword arguments to pass along to the constructors
40 @return Returns MbedLsTools object or None if host OS is not supported
45 if mbed_os
is not None:
46 if mbed_os ==
'Windows7':
47 from .windows
import MbedLsToolsWin7
48 result = MbedLsToolsWin7(**kwargs)
49 elif mbed_os ==
'LinuxGeneric':
50 from .linux
import MbedLsToolsLinuxGeneric
51 result = MbedLsToolsLinuxGeneric(**kwargs)
52 elif mbed_os ==
'Darwin':
53 from .darwin
import MbedLsToolsDarwin
54 result = MbedLsToolsDarwin(**kwargs)
59 """! Function used to determine if host OS is supported by mbed-lstools
61 @return Returns None if host OS is not supported else return OS short name
63 @details This function should be ported for new OS support
67 if (os_info[0] ==
'nt' and os_info[1] ==
'Windows'):
69 elif (os_info[0] ==
'posix' and os_info[1] ==
'Linux'):
70 result =
'LinuxGeneric'
71 elif (os_info[0] ==
'posix' and os_info[1] ==
'Darwin'):
77 """! Returns information about host OS
79 @return Returns tuple with information about OS and host platform
90 """! Get mbed-ls Python module version string """
92 return pkg_resources.require(
"mbed-ls")[0].version
98 devices = mbeds.list_mbeds(unique_names=
True, read_details_txt=
True)
100 from prettytable
import PrettyTable, HEADER
101 columns = [
'platform_name',
'platform_name_unique',
'mount_point',
102 'serial_port',
'target_id',
'daplink_version']
103 pt = PrettyTable(columns, junction_char=
"|", hrules=HEADER)
106 pt.add_row([d.get(col,
None)
or 'unknown' for col
in columns])
107 print(pt.get_string(border=
not simple, header=
not simple,
108 padding_width=1, sortby=
'platform_name_unique'))
117 for token
in args.mock.split(
','):
120 mid, platform_name = token.split(
':')
121 if mid
and mid[0]
in [
'+',
'-']:
124 mbeds.mock_manufacture_id(mid, platform_name, oper=oper)
125 elif token
and token[0]
in [
'-',
'!']:
129 mbeds.mock_manufacture_id(mid,
'dummy', oper=oper)
131 logger.error(
"Could not parse mock from token: '%s'", token)
134 print(mbeds.list_manufacture_ids())
137 print(json.dumps(mbeds.list_mbeds(unique_names=
True,
138 read_details_txt=
True),
139 indent=4, sort_keys=
True))
142 print(json.dumps({m[
'target_id']: m
for m
143 in mbeds.list_mbeds(unique_names=
True,
144 read_details_txt=
True)},
145 indent=4, sort_keys=
True))
149 for d
in mbeds.list_mbeds():
150 platforms |= set([d[
'platform_name']])
151 print(json.dumps(list(platforms), indent=4, sort_keys=
True))
154 platforms = defaultdict(
lambda: 0)
155 for d
in mbeds.list_mbeds():
156 platforms[d[
'platform_name']] += 1
157 print(json.dumps(platforms, indent=4, sort_keys=
True))
160 """! Parse the command line
162 @return Retrun a namespace that contains:
163 * command - python function to run
164 * skip_retarget - bool indicting to skip retargeting
165 * list_unmounted - list boards that are not mounted
166 * debug - turn on debug logging
168 parser = argparse.ArgumentParser()
169 parser.set_defaults(command=print_table)
171 commands = parser.add_argument_group(
'sub commands')\
172 .add_mutually_exclusive_group()
173 commands.add_argument(
174 '-s',
'--simple', dest=
'command', action=
'store_const',
176 help=
'list attached targets without column headers and borders')
177 commands.add_argument(
178 '-j',
'--json', dest=
'command', action=
'store_const',
180 help=
'list attached targets with detailed information in JSON format')
181 commands.add_argument(
182 '-J',
'--json-by-target-id', dest=
'command', action=
'store_const',
183 const=json_by_target_id,
184 help=
'map attached targets from their target ID to their detailed '
185 'information in JSON format')
186 commands.add_argument(
187 '-p',
'--json-platforms', dest=
'command', action=
'store_const',
188 const=json_platforms,
189 help=
'list attached platform names in JSON format.')
190 commands.add_argument(
191 '-P',
'--json-platforms-ext', dest=
'command', action=
'store_const',
192 const=json_platforms_ext,
193 help=
'map attached platform names to the number of attached boards in '
195 commands.add_argument(
196 '-l',
'--list', dest=
'command', action=
'store_const',
197 const=list_platforms,
198 help=
'list all target IDs and their corresponding platform names '
199 'understood by mbed-ls')
200 commands.add_argument(
201 '--version', dest=
'command', action=
'store_const', const=print_version,
202 help=
'print package version and exit')
203 commands.add_argument(
204 '-m',
'--mock', metavar=
'ID:NAME',
205 help=
'substitute or create a target ID to platform name mapping used'
206 'when invoking mbedls in the current directory')
209 '--skip-retarget', dest=
'skip_retarget', default=
False,
211 help=
'skip parsing and interpretation of the re-target file,'
214 '-u',
'--list-unmounted', dest=
'list_unmounted', default=
False,
216 help=
'list mbeds, regardless of whether they are mounted or not')
218 '-d',
'--debug', dest=
'debug', default=
False, action=
"store_true",
219 help=
'outputs extra debug information useful when creating issues!')
221 args = parser.parse_args(to_parse)
223 args.command = mock_platform
229 colorlog.basicConfig(
230 format=
'%(log_color)s%(levelname)s%(reset)s:%(name)s:%(message)s')
233 logging.basicConfig()
237 """! Function used to drive CLI (command line interface) application
238 @return Function exits with success code
245 root_logger = logging.getLogger(
"mbedls")
247 root_logger.setLevel(logging.DEBUG)
249 root_logger.setLevel(logging.INFO)
254 mbeds =
create(skip_retarget=args.skip_retarget,
255 list_unmounted=args.list_unmounted,
256 force_mock=args.command
is mock_platform)
259 logger.critical(
'This platform is not supported! Pull requests welcome at github.com/ARMmbed/mbed-ls')
262 ret_code = args.command(mbeds, args)
266 logger.debug(
"Return code: %d", ret_code)