You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

host_test_plugins.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """
  2. mbed SDK
  3. Copyright (c) 2011-2013 ARM Limited
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. """
  14. from os import access, F_OK
  15. from sys import stdout
  16. from time import sleep
  17. from subprocess import call
  18. class HostTestPluginBase:
  19. """ Base class for all plug-ins used with host tests.
  20. """
  21. ###########################################################################
  22. # Interface:
  23. ###########################################################################
  24. ###########################################################################
  25. # Interface attributes defining plugin name, type etc.
  26. ###########################################################################
  27. name = "HostTestPluginBase" # Plugin name, can be plugin class name
  28. type = "BasePlugin" # Plugin type: ResetMethod, Copymethod etc.
  29. capabilities = [] # Capabilities names: what plugin can achieve
  30. # (e.g. reset using some external command line tool)
  31. stable = False # Determine if plugin is stable and can be used
  32. ###########################################################################
  33. # Interface methods
  34. ###########################################################################
  35. def setup(self, *args, **kwargs):
  36. """ Configure plugin, this function should be called before plugin execute() method is used.
  37. """
  38. return False
  39. def execute(self, capabilitity, *args, **kwargs):
  40. """ Executes capability by name.
  41. Each capability e.g. may directly just call some command line
  42. program or execute building pythonic function
  43. """
  44. return False
  45. ###########################################################################
  46. # Interface helper methods - overload only if you need to have custom behaviour
  47. ###########################################################################
  48. def print_plugin_error(self, text):
  49. """ Function prints error in console and exits always with False
  50. """
  51. print "Plugin error: %s::%s: %s"% (self.name, self.type, text)
  52. return False
  53. def print_plugin_info(self, text, NL=True):
  54. """ Function prints notification in console and exits always with True
  55. """
  56. if NL:
  57. print "Plugin info: %s::%s: %s"% (self.name, self.type, text)
  58. else:
  59. print "Plugin info: %s::%s: %s"% (self.name, self.type, text),
  60. return True
  61. def print_plugin_char(self, char):
  62. """ Function prints char on stdout
  63. """
  64. stdout.write(char)
  65. stdout.flush()
  66. return True
  67. def check_mount_point_ready(self, destination_disk, init_delay=0.2, loop_delay=0.25):
  68. """ Checks if destination_disk is ready and can be accessed by e.g. copy commands
  69. @init_delay - Initial delay time before first access check
  70. @loop_delay - pooling delay for access check
  71. """
  72. if not access(destination_disk, F_OK):
  73. self.print_plugin_info("Waiting for mount point '%s' to be ready..."% destination_disk, NL=False)
  74. sleep(init_delay)
  75. while not access(destination_disk, F_OK):
  76. sleep(loop_delay)
  77. self.print_plugin_char('.')
  78. def check_parameters(self, capabilitity, *args, **kwargs):
  79. """ This function should be ran each time we call execute()
  80. to check if none of the required parameters is missing.
  81. """
  82. missing_parameters = []
  83. for parameter in self.required_parameters:
  84. if parameter not in kwargs:
  85. missing_parameters.append(parameter)
  86. if len(missing_parameters) > 0:
  87. self.print_plugin_error("execute parameter(s) '%s' missing!"% (', '.join(parameter)))
  88. return False
  89. return True
  90. def run_command(self, cmd, shell=True):
  91. """ Runs command from command line.
  92. """
  93. result = True
  94. try:
  95. ret = call(cmd, shell=shell)
  96. if ret:
  97. self.print_plugin_error("[ret=%d] Command: %s"% (int(ret), cmd))
  98. return False
  99. except Exception as e:
  100. result = False
  101. self.print_plugin_error("[ret=%d] Command: %s"% (int(ret), cmd))
  102. self.print_plugin_error(str(e))
  103. return result