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
15"""
16class for handling Test Apps. Currently it provides the following features:
17
181. get SDK path
192. get SDK tools
203. parse application info from its path. for example:
21    * provide download info
22    * provide partition table info
23
24Test Apps should inherent from BaseApp class and overwrite the methods.
25"""
26import os
27import sys
28import time
29
30# timestamp used for calculate log folder name
31LOG_FOLDER_TIMESTAMP = time.time()
32
33
34class BaseApp(object):
35    """
36    Base Class for App.
37    Defines the mandatory methods that App need to implement.
38    Also implements some common methods.
39
40    :param app_path: the path for app.
41    :param config_name: app configuration to be tested
42    :param target: build target
43    """
44
45    def __init__(self, app_path, config_name=None, target=None):
46        pass
47
48    @classmethod
49    def get_sdk_path(cls):
50        """
51        get sdk path.
52
53        subclass must overwrite this method.
54
55        :return: abs sdk path
56        """
57        pass
58
59    @classmethod
60    def get_tools(cls):
61        """
62        get SDK related tools for applications
63
64        subclass must overwrite this method.
65
66        :return: tuple, abs path of each tool
67        """
68        pass
69
70    @classmethod
71    def get_log_folder(cls, test_suite_name):
72        """
73        By default log folder is ``${SDK_PATH}/TEST_LOGS/${test_suite_name}_${timestamp}``.
74
75        The log folder name is consist once start running, ensure all logs of will be put into the same folder.
76
77        :param test_suite_name: the test suite name, by default it's the base file name for main module
78        :return: the log folder path
79        """
80        if not test_suite_name:
81            test_suite_name = os.path.splitext(os.path.basename(sys.modules['__main__'].__file__))[0]
82        sdk_path = cls.get_sdk_path()
83        log_folder = os.path.join(sdk_path, 'TEST_LOGS',
84                                  test_suite_name +
85                                  time.strftime('_%m%d_%H_%M_%S', time.localtime(LOG_FOLDER_TIMESTAMP)))
86        if not os.path.exists(log_folder):
87            os.makedirs(log_folder)
88        return log_folder
89
90    def process_app_info(self):
91        """
92        parse built app info for DUTTool
93
94        subclass must overwrite this method.
95
96        :return: required info for specific DUTTool
97        """
98        pass
99