IDFApp.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http:#www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """ IDF Test Applications """
  15. import subprocess
  16. import App
  17. import json
  18. import os
  19. import sys
  20. class IDFApp(App.BaseApp):
  21. """
  22. Implements common esp-idf application behavior.
  23. idf applications should inherent from this class and overwrite method get_binary_path.
  24. """
  25. IDF_DOWNLOAD_CONFIG_FILE = "download.config"
  26. IDF_FLASH_ARGS_FILE = "flasher_args.json"
  27. def __init__(self, app_path):
  28. super(IDFApp, self).__init__(app_path)
  29. self.idf_path = self.get_sdk_path()
  30. self.binary_path = self.get_binary_path(app_path)
  31. self.elf_file = self._get_elf_file_path(self.binary_path)
  32. assert os.path.exists(self.binary_path)
  33. if self.IDF_DOWNLOAD_CONFIG_FILE not in os.listdir(self.binary_path):
  34. if self.IDF_FLASH_ARGS_FILE not in os.listdir(self.binary_path):
  35. msg = ("Neither {} nor {} exists. "
  36. "Try to run 'make print_flash_cmd | tail -n 1 > {}/{}' "
  37. "or 'idf.py build' "
  38. "for resolving the issue."
  39. "").format(self.IDF_DOWNLOAD_CONFIG_FILE, self.IDF_FLASH_ARGS_FILE,
  40. self.binary_path, self.IDF_DOWNLOAD_CONFIG_FILE)
  41. raise AssertionError(msg)
  42. self.flash_files, self.flash_settings = self._parse_flash_download_config()
  43. self.partition_table = self._parse_partition_table()
  44. @classmethod
  45. def get_sdk_path(cls):
  46. idf_path = os.getenv("IDF_PATH")
  47. assert idf_path
  48. assert os.path.exists(idf_path)
  49. return idf_path
  50. def _get_sdkconfig_paths(self):
  51. """
  52. returns list of possible paths where sdkconfig could be found
  53. Note: could be overwritten by a derived class to provide other locations or order
  54. """
  55. return [os.path.join(self.binary_path, "sdkconfig"), os.path.join(self.binary_path, "..", "sdkconfig")]
  56. def get_sdkconfig(self):
  57. """
  58. reads sdkconfig and returns a dictionary with all configuredvariables
  59. :param sdkconfig_file: location of sdkconfig
  60. :raise: AssertionError: if sdkconfig file does not exist in defined paths
  61. """
  62. d = {}
  63. sdkconfig_file = None
  64. for i in self._get_sdkconfig_paths():
  65. if os.path.exists(i):
  66. sdkconfig_file = i
  67. break
  68. assert sdkconfig_file is not None
  69. with open(sdkconfig_file) as f:
  70. for line in f:
  71. configs = line.split('=')
  72. if len(configs) == 2:
  73. d[configs[0]] = configs[1]
  74. return d
  75. def get_binary_path(self, app_path):
  76. """
  77. get binary path according to input app_path.
  78. subclass must overwrite this method.
  79. :param app_path: path of application
  80. :return: abs app binary path
  81. """
  82. pass
  83. @staticmethod
  84. def _get_elf_file_path(binary_path):
  85. ret = ""
  86. file_names = os.listdir(binary_path)
  87. for fn in file_names:
  88. if os.path.splitext(fn)[1] == ".elf":
  89. ret = os.path.join(binary_path, fn)
  90. return ret
  91. def _parse_flash_download_config(self):
  92. """
  93. Parse flash download config from build metadata files
  94. Sets self.flash_files, self.flash_settings
  95. (Called from constructor)
  96. Returns (flash_files, flash_settings)
  97. """
  98. if self.IDF_FLASH_ARGS_FILE in os.listdir(self.binary_path):
  99. # CMake version using build metadata file
  100. with open(os.path.join(self.binary_path, self.IDF_FLASH_ARGS_FILE), "r") as f:
  101. args = json.load(f)
  102. flash_files = [(offs,file) for (offs,file) in args["flash_files"].items() if offs != ""]
  103. flash_settings = args["flash_settings"]
  104. else:
  105. # GNU Make version uses download.config arguments file
  106. with open(os.path.join(self.binary_path, self.IDF_DOWNLOAD_CONFIG_FILE), "r") as f:
  107. args = f.readlines()[-1].split(" ")
  108. flash_files = []
  109. flash_settings = {}
  110. for idx in range(0, len(args), 2): # process arguments in pairs
  111. if args[idx].startswith("--"):
  112. # strip the -- from the command line argument
  113. flash_settings[args[idx][2:]] = args[idx + 1]
  114. else:
  115. # offs, filename
  116. flash_files.append((args[idx], args[idx + 1]))
  117. # make file offsets into integers, make paths absolute
  118. flash_files = [(int(offs, 0), os.path.join(self.binary_path, path.strip())) for (offs, path) in flash_files]
  119. return (flash_files, flash_settings)
  120. def _parse_partition_table(self):
  121. """
  122. Parse partition table contents based on app binaries
  123. Returns partition_table data
  124. (Called from constructor)
  125. """
  126. partition_tool = os.path.join(self.idf_path,
  127. "components",
  128. "partition_table",
  129. "gen_esp32part.py")
  130. assert os.path.exists(partition_tool)
  131. errors = []
  132. # self.flash_files is sorted based on offset in order to have a consistent result with different versions of
  133. # Python
  134. for (_, path) in sorted(self.flash_files, key=lambda elem: elem[0]):
  135. if 'partition' in os.path.split(path)[1]:
  136. partition_file = os.path.join(self.binary_path, path)
  137. process = subprocess.Popen([sys.executable, partition_tool, partition_file],
  138. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  139. (raw_data, raw_error) = process.communicate()
  140. if isinstance(raw_error, bytes):
  141. raw_error = raw_error.decode()
  142. if 'Traceback' in raw_error:
  143. # Some exception occured. It is possible that we've tried the wrong binary file.
  144. errors.append((path, raw_error))
  145. continue
  146. if isinstance(raw_data, bytes):
  147. raw_data = raw_data.decode()
  148. break
  149. else:
  150. traceback_msg = os.linesep.join(['{} {}:{}{}'.format(partition_tool,
  151. p,
  152. os.linesep,
  153. msg) for p, msg in errors])
  154. raise ValueError("No partition table found for IDF binary path: {}{}{}".format(self.binary_path,
  155. os.linesep,
  156. traceback_msg))
  157. partition_table = dict()
  158. for line in raw_data.splitlines():
  159. if line[0] != "#":
  160. try:
  161. _name, _type, _subtype, _offset, _size, _flags = line.split(",")
  162. if _size[-1] == "K":
  163. _size = int(_size[:-1]) * 1024
  164. elif _size[-1] == "M":
  165. _size = int(_size[:-1]) * 1024 * 1024
  166. else:
  167. _size = int(_size)
  168. except ValueError:
  169. continue
  170. partition_table[_name] = {
  171. "type": _type,
  172. "subtype": _subtype,
  173. "offset": _offset,
  174. "size": _size,
  175. "flags": _flags
  176. }
  177. return partition_table
  178. class Example(IDFApp):
  179. def _get_sdkconfig_paths(self):
  180. """
  181. overrides the parent method to provide exact path of sdkconfig for example tests
  182. """
  183. return [os.path.join(self.binary_path, "..", "sdkconfig")]
  184. def get_binary_path(self, app_path):
  185. # build folder of example path
  186. path = os.path.join(self.idf_path, app_path, "build")
  187. if not os.path.exists(path):
  188. # search for CI build folders
  189. app = os.path.basename(app_path)
  190. example_path = os.path.join(self.idf_path, "build_examples", "example_builds")
  191. for dirpath, dirnames, files in os.walk(example_path):
  192. if dirnames:
  193. if dirnames[0] == app:
  194. path = os.path.join(example_path, dirpath, dirnames[0], "build")
  195. break
  196. else:
  197. raise OSError("Failed to find example binary")
  198. return path
  199. class UT(IDFApp):
  200. def get_binary_path(self, app_path):
  201. """
  202. :param app_path: app path or app config
  203. :return: binary path
  204. """
  205. if not app_path:
  206. app_path = "default"
  207. path = os.path.join(self.idf_path, app_path)
  208. if not os.path.exists(path):
  209. while True:
  210. # try to get by config
  211. if app_path == "default":
  212. # it's default config, we first try to get form build folder of unit-test-app
  213. path = os.path.join(self.idf_path, "tools", "unit-test-app", "build")
  214. if os.path.exists(path):
  215. # found, use bin in build path
  216. break
  217. # ``make ut-build-all-configs`` or ``make ut-build-CONFIG`` will copy binary to output folder
  218. path = os.path.join(self.idf_path, "tools", "unit-test-app", "output", app_path)
  219. if os.path.exists(path):
  220. break
  221. raise OSError("Failed to get unit-test-app binary path")
  222. return path
  223. class SSC(IDFApp):
  224. def get_binary_path(self, app_path):
  225. # TODO: to implement SSC get binary path
  226. return app_path
  227. class AT(IDFApp):
  228. def get_binary_path(self, app_path):
  229. # TODO: to implement AT get binary path
  230. return app_path