3 Copyright (c) 2011-2015 ARM Limited
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
17 Author: Przemyslaw Wirkus <Przemyslaw.Wirkus@arm.com>
25 from os
import access, F_OK
26 from sys
import stdout
27 from time
import sleep
28 from subprocess
import call
33 """! Base class for all plugins used with host tests
42 name =
"HostTestPluginBase"
46 required_parameters = []
64 def setup(self, *args, **kwargs):
65 """ Configure plugin, this function should be called before plugin execute() method is used.
69 def execute(self, capability, *args, **kwargs):
70 """! Executes capability by name
71 @param capability Capability name
72 @param args Additional arguments
73 @param kwargs Additional arguments
74 @details Each capability e.g. may directly just call some command line program or execute building pythonic function
75 @return Capability call return value
81 @return Returns true if plugin works (supportes) under certain OS
82 @os_name String describing OS.
83 See self.mbed_os_support() and self.mbed_os_info()
84 @details In some cases a plugin will not work under particular OS
85 mainly because command / software used to implement plugin
86 functionality is not available e.g. on MacOS or Linux.
94 """! Function prints error in console and exits always with False
95 @param text Text to print
101 """! Function prints notification in console and exits always with True
102 @param text Text to print
103 @param NL Deprecated! Newline will be added behind text if this flag is True
110 """ Function prints char on stdout
117 """! Waits until destination_disk is ready and can be accessed by e.g. copy commands
118 @return True if mount point was ready in given time, False otherwise
119 @param destination_disk Mount point (disk) which will be checked for readiness
120 @param init_delay - Initial delay time before first access check
121 @param loop_delay - polling delay for access check
122 @param timeout Mount point pooling timeout in seconds
129 new_destination_disk = destination_disk
133 self.
print_plugin_infoprint_plugin_info(
"Waiting up to %d sec for '%s' mount point (current is '%s')..."% (timeout, target_id, destination_disk))
135 timeout = int(timeout / timeout_step)
136 for i
in range(timeout):
139 mbeds = mbed_lstools.create()
140 mbed_list = mbeds.list_mbeds()
142 mbed_target = next((x
for x
in mbed_list
if x[
'target_id']==target_id),
None)
144 if mbed_target
is not None:
146 if 'mount_point' in mbed_target
and mbed_target[
'mount_point']
is not None:
147 new_destination_disk = mbed_target[
'mount_point']
151 if new_destination_disk != destination_disk:
153 self.
print_plugin_infoprint_plugin_info(
"Mount point for '%s' changed from '%s' to '%s'..."% (target_id, destination_disk, new_destination_disk))
154 destination_disk = new_destination_disk
159 if not access(destination_disk, F_OK):
160 self.
print_plugin_infoprint_plugin_info(
"Waiting for mount point '%s' to be ready..."% destination_disk, NL=
False)
163 if access(destination_disk, F_OK):
169 self.
print_plugin_errorprint_plugin_error(
"mount {} is not accessible ...".format(destination_disk))
171 return (result, destination_disk)
174 """! Function checks (using mbed-ls) and updates serial port name information for DUT with specified target_id.
175 If no target_id is specified function returns old serial port name.
176 @param serial_port Current serial port name
177 @param target_id Target ID of a device under test which serial port will be checked and updated if needed
178 @param timeout Serial port pooling timeout in seconds
179 @return Tuple with result (always True) and serial port read from mbed-ls
182 new_serial_port =
None
187 self.
print_plugin_infoprint_plugin_info(
"Waiting up to %d sec for '%s' serial port (current is '%s')..."% (timeout, target_id, serial_port))
189 timeout = int(timeout / timeout_step)
190 for i
in range(timeout):
192 mbeds = mbed_lstools.create()
193 mbed_list = mbeds.list_mbeds()
195 mbed_target = next((x
for x
in mbed_list
if x[
'target_id']==target_id),
None)
197 if mbed_target
is not None:
199 if 'serial_port' in mbed_target
and mbed_target[
'serial_port']
is not None:
200 new_serial_port = mbed_target[
'serial_port']
201 if new_serial_port != serial_port:
203 self.
print_plugin_infoprint_plugin_info(
"Serial port for tid='%s' changed from '%s' to '%s'..." % (target_id, serial_port, new_serial_port))
207 new_serial_port = serial_port
209 return new_serial_port
212 """! This function should be ran each time we call execute() to check if none of the required parameters is missing
213 @param capability Capability name
214 @param args Additional parameters
215 @param kwargs Additional parameters
216 @return Returns True if all parameters are passed to plugin, else return False
218 missing_parameters = []
220 if parameter
not in kwargs:
221 missing_parameters.append(parameter)
222 if len(missing_parameters):
223 self.
print_plugin_errorprint_plugin_error(
"execute parameter(s) '%s' missing!"% (
', '.join(missing_parameters)))
228 """! Runs command from command line.
229 @param cmd Command to execute
230 @param shell True if shell command should be executed (eg. ls, ps)
231 @details Function prints 'cmd' return code if execution failed
232 @return True if command successfully executed
236 ret = call(cmd, shell=shell)
240 except Exception
as e:
247 """! Returns information about host OS
248 @return Returns tuple with information about OS and host platform
258 """! Function used to determine host OS
259 @return Returns None if host OS is unknown, else string with name
260 @details This function should be ported for new OS support
264 if (os_info[0] ==
'nt' and os_info[1] ==
'Windows'):
266 elif (os_info[0] ==
'posix' and os_info[1] ==
'Linux' and (
'Ubuntu' in os_info[3])):
268 elif (os_info[0] ==
'posix' and os_info[1] ==
'Linux'):
269 result =
'LinuxGeneric'
270 elif (os_info[0] ==
'posix' and os_info[1] ==
'Darwin'):
Yet another logger flavour.
Base class for all plugins used with host tests.
def mbed_os_info(self)
Returns information about host OS.
def print_plugin_info(self, text, NL=True)
Function prints notification in console and exits always with True.
def run_command(self, cmd, shell=True)
Runs command from command line.
def is_os_supported(self, os_name=None)
def print_plugin_char(self, char)
def check_mount_point_ready(self, destination_disk, init_delay=0.2, loop_delay=0.25, target_id=None, timeout=60)
Waits until destination_disk is ready and can be accessed by e.g.
def execute(self, capability, *args, **kwargs)
Executes capability by name.
def print_plugin_error(self, text)
Interface helper methods - overload only if you need to have custom behaviour.
def check_serial_port_ready(self, serial_port, target_id=None, timeout=60)
Function checks (using mbed-ls) and updates serial port name information for DUT with specified targe...
def setup(self, *args, **kwargs)
Interface methods.
def mbed_os_support(self)
Function used to determine host OS.
def check_parameters(self, capability, *args, **kwargs)
This function should be ran each time we call execute() to check if none of the required parameters i...