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