提交 8b7e3202 编写于 作者: G gzyang

Adapts to the Windows platform

上级 7c7783e8
......@@ -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
......
......@@ -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
......
......@@ -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,
......
......@@ -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
......
......@@ -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)
......
......@@ -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())
......
......@@ -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)
......
......@@ -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 = ''
......
......@@ -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='',
......
......@@ -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))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册