Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers host_test_plugins.py Source File

host_test_plugins.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2011-2013 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009     http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 from __future__ import print_function
00018 
00019 from os import access, F_OK
00020 from sys import stdout
00021 from time import sleep
00022 from subprocess import call
00023 
00024 
00025 class HostTestPluginBase :
00026     """ Base class for all plug-ins used with host tests.
00027     """
00028     ###########################################################################
00029     # Interface:
00030     ###########################################################################
00031 
00032     ###########################################################################
00033     # Interface attributes defining plugin name, type etc.
00034     ###########################################################################
00035     name = "HostTestPluginBase" # Plugin name, can be plugin class name
00036     type = "BasePlugin"         # Plugin type: ResetMethod, Copymethod etc.
00037     capabilities = []           # Capabilities names: what plugin can achieve
00038                                 # (e.g. reset using some external command line tool)
00039     stable = False              # Determine if plugin is stable and can be used
00040 
00041     ###########################################################################
00042     # Interface methods
00043     ###########################################################################
00044     def setup(self, *args, **kwargs):
00045         """ Configure plugin, this function should be called before plugin execute() method is used.
00046         """
00047         return False
00048 
00049     def execute (self, capabilitity, *args, **kwargs):
00050         """ Executes capability by name.
00051             Each capability e.g. may directly just call some command line
00052             program or execute building pythonic function
00053         """
00054         return False
00055 
00056     ###########################################################################
00057     # Interface helper methods - overload only if you need to have custom behaviour
00058     ###########################################################################
00059     def print_plugin_error(self, text):
00060         """ Function prints error in console and exits always with False
00061         """
00062         print("Plugin error: %s::%s: %s" % (self.name, self.type , text))
00063         return False
00064 
00065     def print_plugin_info (self, text, NL=True):
00066         """ Function prints notification in console and exits always with True
00067         """
00068         print("Plugin info: %s::%s: %s"% (self.name, self.type , text))
00069         return True
00070 
00071     def print_plugin_char (self, char):
00072         """ Function prints char on stdout
00073         """
00074         stdout.write(char)
00075         stdout.flush()
00076         return True
00077 
00078     def check_mount_point_ready (self, destination_disk, init_delay=0.2, loop_delay=0.25):
00079         """ Checks if destination_disk is ready and can be accessed by e.g. copy commands
00080             @init_delay - Initial delay time before first access check
00081             @loop_delay - pooling delay for access check
00082         """
00083         if not access(destination_disk, F_OK):
00084             self.print_plugin_info ("Waiting for mount point '%s' to be ready..."% destination_disk, NL=False)
00085             sleep(init_delay)
00086             while not access(destination_disk, F_OK):
00087                 sleep(loop_delay)
00088                 self.print_plugin_char ('.')
00089 
00090     def check_parameters (self, capabilitity, *args, **kwargs):
00091         """ This function should be ran each time we call execute()
00092             to check if none of the required parameters is missing.
00093         """
00094         missing_parameters = []
00095         for parameter in self.required_parameters:
00096             if parameter not in kwargs:
00097                 missing_parameters.append(parameter)
00098         if len(missing_parameters) > 0:
00099             self.print_plugin_error("execute parameter(s) '%s' missing!"% (', '.join(parameter)))
00100             return False
00101         return True
00102 
00103     def run_command (self, cmd, shell=True):
00104         """ Runs command from command line.
00105         """
00106         result = True
00107         ret = 0
00108         try:
00109             ret = call(cmd, shell=shell)
00110             if ret:
00111                 self.print_plugin_error("[ret=%d] Command: %s"% (int(ret), cmd))
00112                 return False
00113         except Exception as e:
00114             result = False
00115             self.print_plugin_error("[ret=%d] Command: %s"% (int(ret), cmd))
00116             self.print_plugin_error(str(e))
00117         return result