• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2021 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 os
20import sys
21import json
22import shutil
23
24from core.constants import JsTestConst
25from xdevice import platform_logger
26
27LOG = platform_logger("PretreatTargets")
28
29
30##############################################################################
31##############################################################################
32
33class PretreatTargets(object):
34    def __init__(self, target_list):
35        self.path_list = []
36        self.name_list = []
37        self.target_list = target_list
38
39    def pretreat_targets_from_list(self):
40        path_list, name_list = self._parse_target_info()
41        self._pretreat_by_target_name(path_list, name_list)
42
43    def disassemble_targets_from_list(self):
44        self._disassemble_by_target_name(self.path_list, self.name_list)
45
46    def _parse_target_info(self):
47        path_list = []
48        name_list = []
49
50        for line in self.target_list:
51            path = line.split(':')[0][2:]
52            name = line.split(':')[1].split('(')[0]
53            path_list.append(path)
54            name_list.append(name)
55
56        return path_list, name_list
57
58    def _pretreat_by_target_name(self, path_list, name_list):
59        for name, path in zip(name_list, path_list):
60            if name.endswith("JsTest"):
61                if self._pretreat_js_target(path, name):
62                    self.path_list.append(path)
63                    self.name_list.append(name)
64                    LOG.info("js test %s pretreat success" % name)
65
66    def _pretreat_js_target(self, path, name):
67        template_path = os.path.join(sys.framework_root_dir, "libs",
68                                     "js_template", "src")
69        target_path = os.path.join(sys.source_code_root_path, path)
70        config_path = os.path.join(target_path, "config.json")
71        gn_path = os.path.join(target_path, "BUILD.gn")
72        gn_bak_path = os.path.join(target_path, "BuildBak")
73        test_path = os.path.join(target_path, "src", "main", "js",
74                                 "default", "test")
75        if not os.path.exists(config_path):
76            LOG.error("js test needs config.json file")
77            return False
78        if not os.path.exists(gn_path):
79            LOG.error("js test needs BUILD.gn file")
80            return False
81        LOG.info("target_path: %s" % target_path)
82
83        #modify BUILD.gn file to compile hap
84        output_path = self._parse_output_path_in_gn(gn_path)
85        if output_path == "":
86            LOG.error(" BUILD.gn needs 'module_output_path'")
87            return
88        os.rename(gn_path, gn_bak_path)
89        template_args = {'output_path': output_path, 'suite_name': name}
90        with open(gn_path, 'w') as filehandle:
91            filehandle.write(JsTestConst.BUILD_GN_FILE_TEMPLATE %
92                             template_args)
93
94        #copy js hap template to target path
95        shutil.copytree(template_path, os.path.join(target_path, "src"))
96        shutil.copy(config_path, os.path.join(target_path, "src", "main"))
97        file_name = os.listdir(target_path)
98        for file in file_name:
99            if file.endswith(".js"):
100                LOG.info("file: %s" % file)
101                shutil.copy(os.path.join(target_path, file), test_path)
102                with open(os.path.join(test_path, "List.test.js"), 'a') \
103                        as list_data:
104                    list_data.write("require('./%s')\n" % file)
105
106        #modify i18n json file
107        i18n_path = os.path.join(target_path, "src", "main", "js",
108                                 "default", "i18n", "en-US.json")
109        json_data = ""
110        with open(i18n_path, 'r') as i18n_file:
111            lines = i18n_file.readlines()
112            for line in lines:
113                if "TargetName" in line:
114                    line = line.replace("TargetName", name)
115                json_data += line
116        with open(i18n_path, 'w') as i18n_file:
117            i18n_file.write(json_data)
118        return True
119
120    def _parse_output_path_in_gn(self, gn_path):
121        output_path = ""
122        with open(gn_path, 'r') as gn_file:
123            for line in gn_file.readlines():
124                if line.startswith("module_output_path"):
125                    output_path = line.split()[2].strip('"')
126                    break
127        return output_path
128
129    def _disassemble_by_target_name(self, path_list, name_list):
130        for name, path in zip(name_list, path_list):
131            LOG.info("name: %s path: %s" % (name, path))
132            if name.endswith("JsTest"):
133                self._disassemble_js_target(path, name)
134                LOG.info("js test %s disassemble success" % name)
135
136    def _disassemble_js_target(self, path, name):
137        target_path = os.path.join(sys.source_code_root_path, path)
138        src_path = os.path.join(target_path, "src")
139        gn_path = os.path.join(target_path, "BUILD.gn")
140        gn_bak_path = os.path.join(target_path, "BuildBak")
141
142        if os.path.exists(src_path):
143            shutil.rmtree(src_path)
144        if os.path.exists(gn_path) and os.path.exists(gn_bak_path):
145            os.remove(gn_path)
146            os.rename(gn_bak_path, gn_path)
147
148##############################################################################
149##############################################################################
150