1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2020-2021 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16""" 17 18import os 19import fnmatch 20import sys 21import argparse 22import distutils.dir_util as dir_util 23import distutils.file_util as file_util 24import json 25import subprocess 26 27# all sub system list, must be lowercase. 28_SUB_SYSTEM_LIST = [ 29 "kernel", 30 "hiviewdfx", 31 "communication", 32 "security", 33 "updater", 34 "sstsutils", 35 "utils", 36 "uikit", 37 "multimedia", 38 "hdf", 39 "appexecfwk", 40 "distributedschedule", 41 "startup", 42 "sensors", 43 "sample", 44 "iothardware", 45 "open_posix_testsuite", 46] 47_GRADLE_PARAMS = ["SIGN_USER_NAME", "SIGN_PWD", "SIGN_MATERIAL_PATH", 48 "SIGN_PROFILE", "SIGN_CERT_FILE"] 49 50_DEFAULT_USER_NAME = "user" 51_DEFAULT_PWD = "decrypt_pwd" 52 53 54def main(): 55 parser = argparse.ArgumentParser() 56 parser.add_argument('--method_name', help='', required=True) 57 parser.add_argument('--arguments', help='', 58 required=True) # format key=value#key=value 59 args = parser.parse_args() 60 this_module = sys.modules[__name__] 61 method = getattr(this_module, args.method_name) 62 arguments = {} 63 for argument in args.arguments.split("#"): 64 key_value = argument.strip().split("=") 65 if len(key_value) != 2: 66 raise ValueError("Wrong format : '%s' % argument") 67 arguments.setdefault(key_value[0].strip(), key_value[1].strip()) 68 method(**arguments) 69 return 0 70 71 72def read_file(input_file): 73 if not os.path.exists(input_file): 74 return "" 75 76 with open(input_file, 'r') as input_f: 77 content = input_f.read().strip() 78 return content 79 80 81def write_file(output_file, content, append): 82 file_dir = os.path.dirname(os.path.abspath(output_file)) 83 if not os.path.exists(file_dir): 84 os.makedirs(file_dir) 85 mode = 'a+' if append else 'w' 86 with open(output_file, mode) as output_f: 87 output_f.write("%s\n" % content) 88 89 90def copy_file(output, sources="", source_dirs="", to_dir=True): 91 """ 92 copy source files or source dir to output. 93 if sources is not empty, the output can be file(will be created 94 automatically) 95 or directory(must be exist). 96 :param output: If source_dirs is not empty, output must be directory. 97 :param sources: source files is separated by dot 98 :param source_dirs: source directory is separated by dot 99 :param to_dir: output is directory or not 100 :return: 101 """ 102 if not sources and not source_dirs: 103 raise Exception( 104 "sources or source_dirs parameter must be specified one") 105 _output = output.strip() 106 _sources = sources.strip() 107 _source_dirs = source_dirs.strip() 108 _parent_output = os.path.dirname(_output) 109 try: 110 if to_dir and not os.path.exists(_output): 111 os.makedirs(_output) 112 if not to_dir and not os.path.exists(_parent_output): 113 os.makedirs(_parent_output) 114 except OSError: 115 if not os.path.isdir(_output): 116 raise 117 if _sources: 118 _copy_files(_sources.split(","), _output) 119 120 if _source_dirs: 121 _copy_dir(_source_dirs.split(","), _output) 122 123 return 0 124 125 126def _copy_files(sources, output): 127 for source_file in sources: 128 source_file = source_file.strip() 129 if os.path.isfile(source_file): 130 file_util.copy_file(source_file, output) 131 132 133def _copy_dir(sources, output): 134 for source_file in sources: 135 source_file = source_file.strip() 136 if os.path.isdir(source_file): 137 dir_util.copy_tree(source_file, output) 138 139 140def gen_suite_out(suite_output_prefix, suite_names, out_suffix): 141 outputs = [] 142 _suite_output_prefix = suite_output_prefix.strip() 143 _dirname_suffix = out_suffix.strip().rstrip(os.sep) 144 for suite in suite_names.split(","): 145 path = "%s%s/%s" % ( 146 _suite_output_prefix, suite.strip(), _dirname_suffix) 147 outputs.append(path) 148 print(path) 149 return outputs 150 151 152def get_subsystem_name(path): 153 subsystem_name = "" 154 for subsystem in _SUB_SYSTEM_LIST: 155 subsystem_path = "/%s/" % (subsystem) 156 _path = path.lower() 157 if subsystem_path in _path: 158 subsystem_name = subsystem 159 break 160 subsystem_path = "/%s/_lite" % (subsystem) 161 if subsystem_path in _path: 162 subsystem_name = subsystem 163 break 164 sys.stdout.write(subsystem_name) 165 return subsystem_name 166 167 168def get_modulename_by_buildtarget(module_list_file, build_target): 169 if not os.path.exists(module_list_file): 170 return "" 171 module_info_data = {} 172 with open(module_list_file, "r") as module_file: 173 module_info_data = json.load(module_file) 174 for module in module_info_data: 175 if module_info_data[module]["build_target_name"] == build_target: 176 sys.stdout.write(module) 177 return module 178 return "" 179 180 181def glob(path, filename_pattern): 182 files = [] 183 for dir_path, _, files in os.walk(path): 184 for filename in fnmatch.filter(files, filename_pattern): 185 files.append(os.path.join(dir_path, filename)) 186 return files 187 188 189def cmd_popen(cmd): 190 proc = subprocess.Popen(cmd) 191 proc.wait() 192 ret_code = proc.returncode 193 if ret_code != 0: 194 raise Exception("{} failed, return code is {}".format(cmd, ret_code)) 195 196 197def build_js_hap(**kwargs): 198 if not check_env(): 199 return 200 if "project_path" not in kwargs or "out_put_dir" not in kwargs: 201 return 202 project_path = kwargs.get("project_path") 203 gradle_dir = os.path.join(project_path, "gradle") 204 os.chdir(gradle_dir) 205 build_clean = ["gradle", "clean"] 206 cmd_popen(build_clean) 207 if "SIGN_USER_NAME" not in kwargs: 208 pass 209 210 gradle_parm_cmd = get_gradle_cmd(**kwargs) 211 # build sign debug hap 212 build_hap_cmd = ["gradle"] 213 build_hap_cmd.extend(gradle_parm_cmd) 214 build_hap_cmd.append("entry:aD") 215 cmd_popen(build_hap_cmd) 216 217 # build sign ohos test hap 218 build_test_hap_cmd = ["gradle"] 219 build_test_hap_cmd.extend(gradle_parm_cmd) 220 build_test_hap_cmd.append("entry:aDOT") 221 cmd_popen(build_test_hap_cmd) 222 223 gradle_output_dir = os.path.join(gradle_dir, "entry", "build", "outputs") 224 if not os.path.exists(gradle_output_dir): 225 return 226 out_put_dir = kwargs.get("out_put_dir") 227 if not os.path.exists(out_put_dir): 228 os.makedirs(out_put_dir) 229 hap_name = kwargs.get("hap_name") 230 for root, _, files in os.walk(gradle_output_dir): 231 for file in files: 232 if file.endswith(".hap") and "unsigned" not in file: 233 if "debugOhosTest" in root: 234 target_file = "{}_OhosTest.hap".format(hap_name) 235 else: 236 target_file = "{}.hap".format(hap_name) 237 file_util.copy_file(os.path.join(root, file), 238 os.path.join(out_put_dir.rstrip(','), 239 target_file)) 240 241 242def get_gradle_cmd(**kwargs): 243 cmd = [] 244 if kwargs: 245 kwargs.setdefault("SIGN_USER_NAME", _DEFAULT_USER_NAME) 246 kwargs.setdefault("SIGN_PWD", _DEFAULT_PWD) 247 for param in _GRADLE_PARAMS: 248 if param in kwargs: 249 cmd.append("-P{}={}".format(param, kwargs.get(param))) 250 return cmd 251 252 253def check_env(): 254 """ 255 check all the env for js hap build 256 return: return true if all env ready, otherwise return false 257 """ 258 env_list = ['OHOS_SDK_HOME'] 259 for env in env_list: 260 if not os.environ.get(env): 261 print("the env {} not set, skip build!".format(env)) 262 return False 263 else: 264 return True 265 266 267if __name__ == '__main__': 268 sys.exit(main()) 269