]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/workspace_tools/host_tests/host_tests_plugins/host_test_plugins.py
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / workspace_tools / host_tests / host_tests_plugins / host_test_plugins.py
1 """
2 mbed SDK
3 Copyright (c) 2011-2013 ARM Limited
4
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
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
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.
16 """
17
18 from os import access, F_OK
19 from sys import stdout
20 from time import sleep
21 from subprocess import call
22
23
24 class HostTestPluginBase:
25     """ Base class for all plug-ins used with host tests.
26     """
27     ###########################################################################
28     # Interface:
29     ###########################################################################
30
31     ###########################################################################
32     # Interface attributes defining plugin name, type etc.
33     ###########################################################################
34     name = "HostTestPluginBase" # Plugin name, can be plugin class name
35     type = "BasePlugin"         # Plugin type: ResetMethod, Copymethod etc.
36     capabilities = []           # Capabilities names: what plugin can achieve
37                                 # (e.g. reset using some external command line tool)
38     stable = False              # Determine if plugin is stable and can be used
39
40     ###########################################################################
41     # Interface methods
42     ###########################################################################
43     def setup(self, *args, **kwargs):
44         """ Configure plugin, this function should be called before plugin execute() method is used.
45         """
46         return False
47
48     def execute(self, capabilitity, *args, **kwargs):
49         """ Executes capability by name.
50             Each capability e.g. may directly just call some command line
51             program or execute building pythonic function
52         """
53         return False
54
55     ###########################################################################
56     # Interface helper methods - overload only if you need to have custom behaviour
57     ###########################################################################
58     def print_plugin_error(self, text):
59         """ Function prints error in console and exits always with False
60         """
61         print "Plugin error: %s::%s: %s"% (self.name, self.type, text)
62         return False
63
64     def print_plugin_info(self, text, NL=True):
65         """ Function prints notification in console and exits always with True
66         """
67         if NL:
68             print "Plugin info: %s::%s: %s"% (self.name, self.type, text)
69         else:
70             print "Plugin info: %s::%s: %s"% (self.name, self.type, text),
71         return True
72
73     def print_plugin_char(self, char):
74         """ Function prints char on stdout
75         """
76         stdout.write(char)
77         stdout.flush()
78         return True
79
80     def check_mount_point_ready(self, destination_disk, init_delay=0.2, loop_delay=0.25):
81         """ Checks if destination_disk is ready and can be accessed by e.g. copy commands
82             @init_delay - Initial delay time before first access check
83             @loop_delay - pooling delay for access check
84         """
85         if not access(destination_disk, F_OK):
86             self.print_plugin_info("Waiting for mount point '%s' to be ready..."% destination_disk, NL=False)
87             sleep(init_delay)
88             while not access(destination_disk, F_OK):
89                 sleep(loop_delay)
90                 self.print_plugin_char('.')
91
92     def check_parameters(self, capabilitity, *args, **kwargs):
93         """ This function should be ran each time we call execute()
94             to check if none of the required parameters is missing.
95         """
96         missing_parameters = []
97         for parameter in self.required_parameters:
98             if parameter not in kwargs:
99                 missing_parameters.append(parameter)
100         if len(missing_parameters) > 0:
101             self.print_plugin_error("execute parameter(s) '%s' missing!"% (', '.join(parameter)))
102             return False
103         return True
104
105     def run_command(self, cmd, shell=True):
106         """ Runs command from command line.
107         """
108         result = True
109         try:
110             ret = call(cmd, shell=shell)
111             if ret:
112                 self.print_plugin_error("[ret=%d] Command: %s"% (int(ret), cmd))
113                 return False
114         except Exception as e:
115             result = False
116             self.print_plugin_error("[ret=%d] Command: %s"% (int(ret), cmd))
117             self.print_plugin_error(str(e))
118         return result