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 "validator", 47] 48_GRADLE_PARAMS = ["SIGN_USER_NAME", "SIGN_PWD", "SIGN_MATERIAL_PATH", 49 "SIGN_PROFILE", "SIGN_CERT_FILE"] 50 51_DEFAULT_USER_NAME = "user" 52_DEFAULT_PWD = "decrypt_pwd" 53 54 55def main(): 56 parser = argparse.ArgumentParser() 57 parser.add_argument('--method_name', help='', required=True) 58 parser.add_argument('--arguments', help='', 59 required=True) # format key=value#key=value 60 args = parser.parse_args() 61 this_module = sys.modules[__name__] 62 method = getattr(this_module, args.method_name) 63 arguments = {} 64 for argument in args.arguments.split("#"): 65 key_value = argument.strip().split("=") 66 if len(key_value) != 2: 67 raise ValueError("Wrong format : '%s' % argument") 68 arguments.setdefault(key_value[0].strip(), key_value[1].strip()) 69 method(**arguments) 70 return 0 71 72 73def read_file(input_file): 74 if not os.path.exists(input_file): 75 return "" 76 77 with open(input_file, 'r') as input_f: 78 content = input_f.read().strip() 79 return content 80 81 82def write_file(output_file, content, append): 83 file_dir = os.path.dirname(os.path.abspath(output_file)) 84 if not os.path.exists(file_dir): 85 os.makedirs(file_dir) 86 mode = 'a+' if append else 'w' 87 with open(output_file, mode) as output_f: 88 output_f.write("%s\n" % content) 89 90 91def copy_file(output, sources="", source_dirs="", to_dir=True): 92 """ 93 copy source files or source dir to output. 94 if sources is not empty, the output can be file(will be created 95 automatically) 96 or directory(must be exist). 97 :param output: If source_dirs is not empty, output must be directory. 98 :param sources: source files is separated by dot 99 :param source_dirs: source directory is separated by dot 100 :param to_dir: output is directory or not 101 :return: 102 """ 103 if not sources and not source_dirs: 104 raise Exception( 105 "sources or source_dirs parameter must be specified one") 106 _output = output.strip() 107 _sources = sources.strip() 108 _source_dirs = source_dirs.strip() 109 _parent_output = os.path.dirname(_output) 110 try: 111 if to_dir and not os.path.exists(_output): 112 os.makedirs(_output) 113 if not to_dir and not os.path.exists(_parent_output): 114 os.makedirs(_parent_output) 115 except OSError: 116 if not os.path.isdir(_output): 117 raise 118 if _sources: 119 _copy_files(_sources.split(","), _output) 120 121 if _source_dirs: 122 _copy_dir(_source_dirs.split(","), _output) 123 124 return 0 125 126 127def _copy_files(sources, output): 128 for source_file in sources: 129 source_file = source_file.strip() 130 if os.path.isfile(source_file): 131 file_util.copy_file(source_file, output) 132 133 134def _copy_dir(sources, output): 135 for source_file in sources: 136 source_file = source_file.strip() 137 if os.path.isdir(source_file): 138 dir_util.copy_tree(source_file, output) 139 140 141def gen_suite_out(suite_output_prefix, suite_names, out_suffix): 142 outputs = [] 143 _suite_output_prefix = suite_output_prefix.strip() 144 _dirname_suffix = out_suffix.strip().rstrip(os.sep) 145 for suite in suite_names.split(","): 146 path = "%s%s/%s" % ( 147 _suite_output_prefix, suite.strip(), _dirname_suffix) 148 outputs.append(path) 149 print(path) 150 return outputs 151 152 153def get_subsystem_name(path): 154 subsystem_name = "" 155 for subsystem in _SUB_SYSTEM_LIST: 156 subsystem_path = "/%s/" % (subsystem) 157 _path = path.lower() 158 if subsystem_path in _path: 159 subsystem_name = subsystem 160 break 161 subsystem_path = "/%s/_lite" % (subsystem) 162 if subsystem_path in _path: 163 subsystem_name = subsystem 164 break 165 sys.stdout.write(subsystem_name) 166 return subsystem_name 167 168 169def get_modulename_by_buildtarget(module_list_file, build_target): 170 if not os.path.exists(module_list_file): 171 return "" 172 module_info_data = {} 173 with open(module_list_file, "r") as module_file: 174 module_info_data = json.load(module_file) 175 for module in module_info_data: 176 if module_info_data[module]["build_target_name"] == build_target: 177 sys.stdout.write(module) 178 return module 179 return "" 180 181 182def glob(path, filename_pattern): 183 files = [] 184 for dir_path, _, files in os.walk(path): 185 for filename in fnmatch.filter(files, filename_pattern): 186 files.append(os.path.join(dir_path, filename)) 187 return files 188 189 190def cmd_popen(cmd): 191 proc = subprocess.Popen(cmd) 192 proc.wait() 193 ret_code = proc.returncode 194 if ret_code != 0: 195 raise Exception("{} failed, return code is {}".format(cmd, ret_code)) 196 197 198def build_js_hap(**kwargs): 199 if not check_env(): 200 return 201 if "project_path" not in kwargs or "out_put_dir" not in kwargs: 202 return 203 project_path = kwargs.get("project_path") 204 gradle_dir = os.path.join(project_path, "gradle") 205 os.chdir(gradle_dir) 206 build_clean = ["gradle", "clean"] 207 cmd_popen(build_clean) 208 if "SIGN_USER_NAME" not in kwargs: 209 pass 210 211 gradle_parm_cmd = get_gradle_cmd(**kwargs) 212 # build sign debug hap 213 build_hap_cmd = ["gradle"] 214 build_hap_cmd.extend(gradle_parm_cmd) 215 build_hap_cmd.append("entry:aD") 216 cmd_popen(build_hap_cmd) 217 218 # build sign ohos test hap 219 build_test_hap_cmd = ["gradle"] 220 build_test_hap_cmd.extend(gradle_parm_cmd) 221 build_test_hap_cmd.append("entry:aDOT") 222 cmd_popen(build_test_hap_cmd) 223 224 gradle_output_dir = os.path.join(gradle_dir, "entry", "build", "outputs") 225 if not os.path.exists(gradle_output_dir): 226 return 227 out_put_dir = kwargs.get("out_put_dir") 228 if not os.path.exists(out_put_dir): 229 os.makedirs(out_put_dir) 230 hap_name = kwargs.get("hap_name") 231 for root, _, files in os.walk(gradle_output_dir): 232 for file in files: 233 if file.endswith(".hap") and "unsigned" not in file: 234 if "debugOhosTest" in root: 235 target_file = "{}_OhosTest.hap".format(hap_name) 236 else: 237 target_file = "{}.hap".format(hap_name) 238 file_util.copy_file(os.path.join(root, file), 239 os.path.join(out_put_dir.rstrip(','), 240 target_file)) 241 242 243def get_gradle_cmd(**kwargs): 244 cmd = [] 245 if kwargs: 246 kwargs.setdefault("SIGN_USER_NAME", _DEFAULT_USER_NAME) 247 kwargs.setdefault("SIGN_PWD", _DEFAULT_PWD) 248 for param in _GRADLE_PARAMS: 249 if param in kwargs: 250 cmd.append("-P{}={}".format(param, kwargs.get(param))) 251 return cmd 252 253 254def check_env(): 255 """ 256 check all the env for js hap build 257 return: return true if all env ready, otherwise return false 258 """ 259 env_list = ['OHOS_SDK_HOME'] 260 for env in env_list: 261 if not os.environ.get(env): 262 print("the env {} not set, skip build!".format(env)) 263 return False 264 else: 265 return True 266 267 268if __name__ == '__main__': 269 sys.exit(main()) 270