diff --git a/build_ext_components.py b/build_ext_components.py index f9640351a2200d4352f3f8cf24bf7b8dc3ea1bc1..5715b4a6a11d841ce9dd37d98025c83899fd7e36 100755 --- a/build_ext_components.py +++ b/build_ext_components.py @@ -30,12 +30,14 @@ def cmd_exec(command, temp_file, error_log_path): proc = subprocess.Popen(cmd, stdout=temp_file, stderr=temp_file, - universal_newlines=True) + encoding='utf-8') proc.wait() ret_code = proc.returncode if ret_code != 0: + temp_file.close() copyfile(temp_file.name, error_log_path) + os.remove(temp_file.name) return ret_code return ret_code @@ -58,23 +60,25 @@ def main(): if args.path: curr_dir = os.getcwd() os.chdir(args.path) - with NamedTemporaryFile(mode='wt') as temp_file: - if args.prebuilts: - status = cmd_exec(args.prebuilts, temp_file, args.out_dir[0]) - if status != 0: - return status - if args.command: - if '&&' in args.command: - command = args.command.split('&&') - for data in command: - status = cmd_exec(data, temp_file, args.out_dir[0]) - if status != 0: - return status - else: - status = cmd_exec(args.command, temp_file, args.out_dir[0]) + temp_file = NamedTemporaryFile(mode='wt', delete=False) + if args.prebuilts: + status = cmd_exec(args.prebuilts, temp_file, args.out_dir[0]) + if status != 0: + return status + if args.command: + if '&&' in args.command: + command = args.command.split('&&') + for data in command: + status = cmd_exec(data, temp_file, args.out_dir[0]) if status != 0: return status - copyfile(temp_file.name, args.target_dir[0]) + else: + status = cmd_exec(args.command, temp_file, args.out_dir[0]) + if status != 0: + return status + temp_file.close() + copyfile(temp_file.name, args.target_dir[0]) + os.remove(temp_file.name) os.chdir(curr_dir) return 0 diff --git a/hb/__init__.py b/hb/__init__.py index 95b586a446fc5ed9c812661812ead7fe71c91e2f..0f703fc3d7707d7628f0c4c62e139076a5fd4e0d 100755 --- a/hb/__init__.py +++ b/hb/__init__.py @@ -21,7 +21,8 @@ import os def get_config_path(): search_path = os.getcwd() - while search_path != '/': + root_path = os.path.abspath(os.sep) + while search_path != root_path: config_path = os.path.join(search_path, 'ohos_config.json') if os.path.isfile(config_path): return config_path diff --git a/hb/build/build_process.py b/hb/build/build_process.py index eb0894c3d68f2517467cad5cb3fcfc30638a7984..71679eddbe62af24cb1150bb3964754027d2be7a 100755 --- a/hb/build/build_process.py +++ b/hb/build/build_process.py @@ -188,16 +188,15 @@ class Build(): self.config.out_path = os.path.join(self.config.root_path, 'out', board) - gn_device_path = os.path.dirname(device_path).\ - replace(self.config.root_path, '/') - gn_kernel_path = device_path.replace(self.config.root_path, '/') + gn_device_path = os.path.dirname(device_path) + gn_kernel_path = device_path self.register_args('ohos_build_target', [gn_device_path]) self.register_args('device_path', gn_kernel_path) self.register_args('ohos_kernel_type', kernel) else: # Compile product in "hb set" - self.register_args('product_path', self.config.gn_product_path) - self.register_args('device_path', self.config.gn_device_path) + self.register_args('product_path', self.config.product_path) + self.register_args('device_path', self.config.device_path) self.register_args('ohos_kernel_type', self.config.kernel) product_json = os.path.join(self.config.product_path, diff --git a/hb/common/config.py b/hb/common/config.py index 0e17254760ce99c4b2efe2922b445e1d84647969..e07d7154a4bd3e74dd653822feab51c1c467d7a9 100755 --- a/hb/common/config.py +++ b/hb/common/config.py @@ -16,6 +16,7 @@ # limitations under the License. import os +import platform from distutils.spawn import find_executable from hb import CONFIG_JSON @@ -145,14 +146,27 @@ class Config(metaclass=Singleton): def vendor_path(self): return os.path.join(self.root_path, 'vendor') + @property + def build_tools_path(self): + platform_name = platform.system() + if platform_name == 'Linux': + return os.path.join(self.root_path, + 'prebuilts', + 'build-tools', + 'linux-x86', + 'bin') + if platform_name == 'Windows': + return os.path.join(self.root_path, + 'prebuilts', + 'build-tools', + 'win-x86', + 'bin') + + raise Exception(f'unidentified platform: {platform_name}') + @property def gn_path(self): - repo_gn_path = os.path.join(self.root_path, - 'prebuilts', - 'build-tools', - 'linux-x86', - 'bin', - 'gn') + repo_gn_path = os.path.join(self.build_tools_path, 'gn') if os.path.isfile(repo_gn_path): return repo_gn_path @@ -164,12 +178,7 @@ class Config(metaclass=Singleton): @property def ninja_path(self): - repo_ninja_path = os.path.join(self.root_path, - 'prebuilts', - 'build-tools', - 'linux-x86', - 'bin', - 'ninja') + repo_ninja_path = os.path.join(self.build_tools_path, 'ninja') if os.path.isfile(repo_ninja_path): return repo_ninja_path diff --git a/hb/common/device.py b/hb/common/device.py index db1ee0398b696dac876af466a951947c0195c332..3f17a6484040fff364118d4cf6d9432531aeea7d 100755 --- a/hb/common/device.py +++ b/hb/common/device.py @@ -86,7 +86,7 @@ class Device(): kernel_pattern = r'kernel_type ?= ?"{}"'.format(kernel) version_pattern = r'kernel_version ?= ?"{}"'.format(version) - with open(config, 'rt') as config_file: + with open(config, 'rt', encoding='utf-8') as config_file: data = config_file.read() return re.search(kernel_pattern, data) and\ re.search(version_pattern, data) @@ -96,7 +96,7 @@ class Device(): kernel_pattern = r'kernel_type ?= ?"(\w+)"' version_pattern = r'kernel_version ?= ?"([a-zA-Z0-9._]*)"' - with open(config, 'rt') as config_file: + with open(config, 'rt', encoding='utf-8') as config_file: data = config_file.read() kernel_list = re.findall(kernel_pattern, data) version_list = re.findall(version_pattern, data) diff --git a/hb/common/utils.py b/hb/common/utils.py index 0d9703a22890d27ef5ad04b8269b2d73471e4d58..c22f05fe6f6b00c197d0dcdddf38bf175d91ab30 100755 --- a/hb/common/utils.py +++ b/hb/common/utils.py @@ -56,7 +56,7 @@ def read_json_file(input_file): def dump_json_file(dump_file, json_data): - with open(dump_file, 'wt') as json_file: + with open(dump_file, 'wt', encoding='utf-8') as json_file: json.dump(json_data, json_file, ensure_ascii=False, @@ -75,11 +75,11 @@ def exec_command(cmd, log_path='out/build.log', **kwargs): useful_info_pattern = re.compile(r'\[\d+/\d+\].+') is_log_filter = kwargs.pop('log_filter', False) - with open(log_path, 'at') as log_file: + with open(log_path, 'at', encoding='utf-8') as log_file: process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - universal_newlines=True, + encoding='utf-8', **kwargs) for line in iter(process.stdout.readline, ''): if is_log_filter: @@ -94,7 +94,7 @@ def exec_command(cmd, log_path='out/build.log', **kwargs): ret_code = process.returncode if ret_code != 0: - with open(log_path, 'at') as log_file: + with open(log_path, 'at', encoding='utf-8') as log_file: for line in iter(process.stderr.readline, ''): if 'ninja: warning' in line: log_file.write(line) @@ -112,7 +112,7 @@ def exec_command(cmd, log_path='out/build.log', **kwargs): def get_failed_log(log_path): - with open(log_path, 'rt') as log_file: + with open(log_path, 'rt', encoding='utf-8') as log_file: data = log_file.read() failed_pattern = re.compile(r'(\[\d+/\d+\].*?)(?=\[\d+/\d+\]|' 'ninja: build stopped)', re.DOTALL) @@ -123,7 +123,7 @@ def get_failed_log(log_path): error_log = os.path.join(os.path.dirname(log_path), 'error.log') if os.path.isfile(error_log): - with open(error_log, 'rt') as log_file: + with open(error_log, 'rt', encoding='utf-8') as log_file: hb_error(log_file.read()) diff --git a/hb/cts/cts.py b/hb/cts/cts.py index f2928e788c050a1c9427259eb46e3a09d01700b8..c0fcc9feffd3a9e6a947f9faee8048f6a283fa3e 100755 --- a/hb/cts/cts.py +++ b/hb/cts/cts.py @@ -222,7 +222,7 @@ class Subsystem(): component_list[index]['deps'] = component_cls.get_real_deps() self.json_content['components'] = component_list - with open(self.json_path, 'wt') as file: + with open(self.json_path, 'wt', encoding='utf-8') as file: json.dump(self.json_content, file, ensure_ascii=False, indent=2) diff --git a/hb/deps/gen_deps.py b/hb/deps/gen_deps.py index 5377f982da94bef3529b291636b17475e21a06e3..5d31aef220a24f0b1d1188745447acaa73b0db78 100755 --- a/hb/deps/gen_deps.py +++ b/hb/deps/gen_deps.py @@ -72,7 +72,7 @@ def gen_deps(subsystems, products): status = exec_command(args_factory(args)) except Exception: status = 1 - with open(config.log_path, 'rt') as log_file: + with open(config.log_path, 'rt', encoding='utf-8') as log_file: log = log_file.read() else: log = '' diff --git a/setup.py b/setup.py index 7404cc5de04181df0308aa8230bfb9f251b22d0a..aac65c2443408dd44a2a1ed03cb6be2dea43106e 100755 --- a/setup.py +++ b/setup.py @@ -22,12 +22,12 @@ from setuptools import setup WORK_PATH = os.path.dirname('__file__') README_PATH = os.path.join(WORK_PATH, 'README.md') LICENSE_PATH = os.path.join(WORK_PATH, 'LICENSE') -LONG_DESCRIPTION = open(README_PATH).read() -LICENSE = open(LICENSE_PATH).read() +LONG_DESCRIPTION = open(README_PATH, 'rt', encoding='utf-8').read() +LICENSE = open(LICENSE_PATH, 'rt', encoding='utf-8').read() setup( name='ohos-build', - version='0.1.1', + version='0.2.0', description='OHOS build command line tool', long_description=LONG_DESCRIPTION, url='', diff --git a/utils.py b/utils.py index a134df27f1d582040a25b51017c53d6cc75741cd..8c93e90c82656a868ad95200aa1d796d7f410c17 100755 --- a/utils.py +++ b/utils.py @@ -53,24 +53,24 @@ def read_json_file(input_file): def exec_command(cmd, log_path='out/build.log', **kwargs): - with open(log_path, 'at') as f: + with open(log_path, 'at', encoding='utf-8') as log_file: process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - universal_newlines=True, + encoding='utf-8', **kwargs) for line in iter(process.stdout.readline, ''): sys.stdout.write(line) - f.write(line) + log_file.write(line) process.wait() ret_code = process.returncode if ret_code != 0: - with open(log_path, 'at') as f: + with open(log_path, 'at', encoding='utf-8') as log_file: for line in iter(process.stderr.readline, ''): sys.stdout.write(line) - f.write(line) + log_file.write(line) print('you can check build log in {}'.format(log_path)) raise Exception("{} failed, return code is {}".format(cmd, ret_code))