• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2020 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19import sys
20import os
21
22from core.constants import ToolCommandType
23from core.utils import get_file_list
24from core.utils import get_file_list_by_postfix
25from core.utils import get_build_output_path
26from core.utils import scan_support_product
27from core.config.config_manager import UserConfigManager
28from core.config.config_manager import FrameworkConfigManager
29from core.config.parse_parts_config import ParsePartsConfig
30
31CMD_KEY_PRODUCTLIST = "productlist"
32CMD_KEY_TYPELIST = "typelist"
33CMD_KEY_SUBSYSTEMLIST = "subsystemlist"
34CMD_KEY_PARTLIST = "partlist"
35CMD_KEY_MODULELIST = "modulelist"
36
37
38TOOL_VERSION_INFO = """Welcome to DeveloperTest V1.0.0.
39"""
40
41HLEP_COMMAND_INFOMATION = """use help [follow command] for more information:
42    """ + \
43    "show: " + """Display a list of supported show command.
44    """ + \
45    "run:  " + """Display a list of supported run command.
46    """ + \
47    "list: " + """Display a list of supported device.
48    """ + \
49    "quit: " + """Exit the test framework application.
50"""
51
52SUPPORT_COMMAND_SHOW = """use show [follow command] for more information:
53    """ + \
54    "productlist" + """
55    """ + \
56    "typelist" + """
57    """ + \
58    "subsystemlist" + """
59    """ + \
60    "partlist" + """
61    """ + \
62    "modulelist" + """
63"""
64
65RUNCASES_INFOMATION = """run:
66    This command is used to execute the selected testcases.
67    It includes a series of processes such as use case compilation, \
68execution, and result collection.
69
70usage: run [-p PRODUCTFORM]
71           [-t [TESTTYPE [TESTTYPE ...]]]
72           [-ss [SUBSYSTEM [SUBSYSTEM ...]]]
73           [-tp [TESTPART [TESTPART ...]]]
74           [-tm TESTMODULE]
75           [-ts TESTSUIT]
76           [-tc TESTCASE]
77           [-tl TESTLEVEL]
78
79optional arguments:
80  -p PRODUCTFORM, --productform PRODUCTFORM
81                        Specified product form
82  -t [TESTTYPE [TESTTYPE ...]], --testtype [TESTTYPE [TESTTYPE ...]]
83                        Specify test type(UT,MST,ST,PERF,ALL)
84  -ss [SUBSYSTEM [SUBSYSTEM ...]], --subsystem [SUBSYSTEM [SUBSYSTEM ...]]
85                        Specify test subsystem
86  -tp [TESTPART [TESTPART ...]], --testpart [TESTPART [TESTPART ...]]
87                        Specify test testpart
88  -tm TESTMODULE, --testmodule TESTMODULE
89                        Specified test module
90  -ts TESTSUIT, --testsuit TESTSUIT
91                        Specify test suit
92  -tc TESTCASE, --testcase TESTCASE
93                        Specify test case
94  -tl TESTLEVEL, --testlevel TESTLEVEL
95
96Examples:
97    run -t UT
98    run -t UT -ss aafwk
99    run -t UT -ss aafwk -tm base_test
100    run -t UT -ss aafwk -tm base_test -ts base_test
101    run -t UT -ss aafwk -tm base_test -ts base_test -tl 2
102    run -t UT -ss aafwk -tm base_test -ts base_test -tc \
103AAFwkBaseTest.*
104    run -t UT -ss aafwk -tm base_test -ts base_test -tc \
105AAFwkBaseTest.object_test_001
106    run -t MST
107    ...
108    run -t ALL
109    ...
110"""
111
112LIST_INFOMATION = "list\n" + """
113    This command is used to display device list.
114"""
115
116QUIT_INFOMATION = "quit\n" + """
117    This command is used to exit the test framework application.
118"""
119
120
121#############################################################################
122#############################################################################
123
124def select_user_input(data_list):
125    data_item_count = len(data_list)
126    select_item_value = ""
127    select_item_index = -1
128
129    if len(data_list) != 0:
130        count = 0
131        while True:
132            input_data = input("")
133            if "" != input_data and input_data.isdigit():
134                input_num = int(input_data)
135                if input_num > 0 and (input_num <= data_item_count):
136                    select_item_index = input_num - 1
137                    select_item_value = data_list[input_num - 1]
138                    break
139                else:
140                    print("The data you entered is out of range, \
141                        please re-enter:")
142                    count += 1
143            else:
144                if "" == input_data:
145                    select_item_index = 0
146                    select_item_value = data_list[0]
147                    break
148                else:
149                    print("You entered a non-numeric character, \
150                        please re-enter:")
151                    count += 1
152
153            if count >= 3:
154                print("You entered the error three times in a row, \
155                    exit the frame.")
156                quit()
157                sys.exit(0)
158        return select_item_value, select_item_index
159
160
161def select_productform():
162    select_value = "phone"
163    scan_product_list = scan_support_product()
164    config_product_list = \
165        FrameworkConfigManager().get_framework_config("productform")
166    productform_list = scan_product_list + config_product_list
167    if len(productform_list) != 0:
168        print("Please select the current tested product form:")
169        for index, element in enumerate(productform_list):
170            print("%d. %s" % (index + 1, element))
171        print("default is [1] %s" % productform_list[0])
172        select_value, _ = select_user_input(productform_list)
173    print(select_value)
174    return select_value
175
176
177def show_wizard_mode():
178    wizard_data_dic = {}
179    print("+++++++++++++++++++++++++++++++++++++++++++++")
180
181    productform = select_productform()
182    if productform == "":
183        productform = "phone"
184    wizard_data_dic["productform"] = productform
185
186    print("+++++++++++++++++++++++++++++++++++++++++++++")
187    print("The environment is ready, please use the run command to test.")
188    return wizard_data_dic
189
190
191#############################################################################
192#############################################################################
193
194def display_help_info(para_list):
195    if len(para_list) == 0 or para_list[0] != ToolCommandType.TOOLCMD_KEY_HELP:
196        print("This command is not support.")
197        return
198
199    if len(para_list) > 1:
200        display_help_command_info(para_list[1])
201    else:
202        print(TOOL_VERSION_INFO)
203        print(HLEP_COMMAND_INFOMATION)
204
205
206def display_show_info(para_list, productform):
207    if len(para_list) == 0 or para_list[0] != ToolCommandType.TOOLCMD_KEY_SHOW:
208        print("This command is not support.")
209        return
210
211    if len(para_list) > 1:
212        display_show_command_info(para_list[1], productform)
213    else:
214        print(SUPPORT_COMMAND_SHOW)
215
216
217#############################################################################
218#############################################################################
219
220def get_module_list_from_output_dir(product_form):
221    module_path_list = []
222    all_product_list = scan_support_product()
223    if product_form in all_product_list:
224        module_list_file_path = os.path.join(
225            get_build_output_path(product_form),
226            "module_list_files")
227    else:
228        module_list_file_path = os.path.join(
229            get_build_output_path(product_form),
230            "test_info",
231            "module_list_files")
232    print(module_list_file_path)
233    if os.path.exists(module_list_file_path):
234        file_list = get_file_list_by_postfix(module_list_file_path, ".mlf")
235        for file in file_list:
236            module_path = \
237                file[len(module_list_file_path) + 1: file.rfind(os.sep)]
238            if module_path != "" and module_path not in module_path_list:
239                module_path_list.append(module_path)
240    else:
241        print("%s does not exist." % module_list_file_path)
242    module_path_list.sort()
243    return module_path_list
244
245
246def get_module_list_from_case_dir(test_case_dir):
247    file_list = []
248    test_case_tests_path = test_case_dir
249    if not os.path.exists(test_case_tests_path):
250        return file_list
251
252    for test_type in os.listdir(test_case_tests_path):
253        file_path = os.path.join(test_case_tests_path, test_type)
254        for dirs in os.walk(file_path):
255            files = get_file_list(find_path=dirs[0])
256            for file_name in files:
257                if "" != file_name and -1 == file_name.find(__file__):
258                    file_name = os.path.join(dirs[0], file_name)
259                    if os.path.isfile(file_name):
260                        file_name = file_name[len(file_path) + 1: \
261                            file_name.rfind(os.sep)]
262                        file_list.append(file_name)
263    return file_list
264
265
266def get_module_list(product_form):
267    module_path_list = []
268    testcase_dir = UserConfigManager().get_test_cases_dir()
269    if testcase_dir == "":
270        module_path_list = get_module_list_from_output_dir(product_form)
271    else:
272        module_path_list = get_module_list_from_case_dir(testcase_dir)
273    return module_path_list
274
275
276#############################################################################
277#############################################################################
278
279
280def show_product_list():
281    print("List of currently supported productform:")
282    scan_product_list = scan_support_product()
283    config_product_list = \
284        FrameworkConfigManager().get_framework_config("productform")
285    productform_list = scan_product_list + config_product_list
286    if 0 != len(productform_list):
287        for index, element in enumerate(productform_list):
288            print("    %d. %s" % (index + 1, element))
289    else:
290        print("No category specified.")
291
292
293def show_testtype_list():
294    print("List of currently supported test types:")
295    testtype_list = FrameworkConfigManager().get_framework_config(
296        "test_category")
297    if 0 != len(testtype_list):
298        for index, element in enumerate(testtype_list):
299            print("    %d. %s" % (index + 1, element))
300    else:
301        print("No category specified.")
302
303
304def show_subsystem_list(product_form):
305    print("List of currently supported subsystem names:")
306    parser = ParsePartsConfig(product_form)
307    subsystem_name_list = parser.get_subsystem_name_list()
308    if len(subsystem_name_list) == 0:
309        return
310
311    subsystem_name_list.sort()
312    for index, element in enumerate(subsystem_name_list):
313        print("    %d. %s" % (index + 1, element))
314
315
316def show_partname_list(product_form):
317    print("List of currently supported part names:")
318    parser = ParsePartsConfig(product_form)
319    subsystem_name_list = parser.get_subsystem_name_list()
320    if len(subsystem_name_list) == 0:
321        return
322
323    subsystem_name_list.sort()
324    subsystem_infos = parser.get_subsystem_infos()
325    for subsystem in subsystem_name_list:
326        print("%s:" % subsystem)
327        part_name_list = subsystem_infos[subsystem]
328        part_name_list.sort()
329        for index, element in enumerate(part_name_list):
330            print("    %d. %s" % (index + 1, element))
331
332
333def show_module_list(product_form):
334    print("List of currently supported module names:")
335    subsystem_name_list = []
336    subsystem_module_list = get_module_list(product_form)
337
338    for item in subsystem_module_list:
339        if item != "":
340            subsystem_name = item.split(os.sep)[0]
341            if subsystem_name not in subsystem_name_list:
342                subsystem_name_list.append(subsystem_name)
343
344    for subsystem_name in subsystem_name_list:
345        print("%s:" % subsystem_name)
346        index = 0
347        module_value_list = []
348        for item in subsystem_module_list:
349            find_key = subsystem_name + os.sep
350            pos_subsystem = item.find(find_key)
351            if pos_subsystem >= 0:
352                subsystem_module_dir = \
353                    item[pos_subsystem + len(find_key):len(item)]
354                module_value = subsystem_module_dir.split(os.sep)[0]
355                if module_value not in module_value_list:
356                    module_value_list.append(module_value)
357                    index += 1
358                    print("    %d. %s" % (index, module_value))
359
360
361def display_help_command_info(command):
362    if command == ToolCommandType.TOOLCMD_KEY_SHOW:
363        print(SUPPORT_COMMAND_SHOW)
364    elif command == ToolCommandType.TOOLCMD_KEY_RUN:
365        print(RUNCASES_INFOMATION)
366    elif command == ToolCommandType.TOOLCMD_KEY_LIST:
367        print(LIST_INFOMATION)
368    elif command == ToolCommandType.TOOLCMD_KEY_QUIT:
369        print(QUIT_INFOMATION)
370    else:
371        print("'%s' command no help information." % command)
372
373
374def display_show_command_info(command, product_form="phone"):
375    if command == CMD_KEY_PRODUCTLIST:
376        show_product_list()
377    elif command == CMD_KEY_TYPELIST:
378        show_testtype_list()
379    elif command == CMD_KEY_SUBSYSTEMLIST:
380        show_subsystem_list(product_form)
381    elif command == CMD_KEY_PARTLIST:
382        show_partname_list(product_form)
383    elif command == CMD_KEY_MODULELIST:
384        show_module_list(product_form)
385    else:
386        print("This command is not support.")
387
388
389#############################################################################
390#############################################################################
391