提交 de554e92 编写于 作者: P pilipala195 提交者: yangming_ha

!8 build tool download logic optimized

Signed-off-by: Nyangming_ha <yangming_ha@163.com>
上级 a7e9ddeb
......@@ -45,21 +45,21 @@ CONFIG_STRUCT = {
VERSION = '0.4.3'
BUILD_TOOLS_URL = {
"gn": {
"linux-x86": 'https://repo.huaweicloud.com/harmonyos/compiler/gn/' +\
'1717/linux/gn-linux-x86-1717.tar.gz',
"windows-amd64": 'https://repo.huaweicloud.com/harmonyos/compiler/gn/' +\
'1744/windows/gn-windows-amd64.zip'
},
"ninja": {
"linux-x86": 'https://repo.huaweicloud.com/harmonyos/compiler/ninja/' +\
'1.10.1/linux/ninja-linux-x86-1.10.1.tar.gz',
"windows": 'https://repo.huaweicloud.com/harmonyos/compiler/ninja/' +\
'1.9.0/windows/ninja-win.zip'
},
"clang": {
"linux": 'https://repo.huaweicloud.com/harmonyos/compiler/clang/' +\
BUILD_TOOLS_CFG = {
"Linux": {
"build_tools_path": 'prebuilts/build-tools/linux-x86/bin',
"gn": 'https://repo.huaweicloud.com/harmonyos/compiler/gn/' +\
'1717/linux/gn-linux-x86-1717.tar.gz',
"ninja": 'https://repo.huaweicloud.com/harmonyos/compiler/ninja/' +\
'1.10.1/linux/ninja-linux-x86-1.10.1.tar.gz',
"clang": 'https://repo.huaweicloud.com/harmonyos/compiler/clang/' +\
'10.0.1-62608/linux/llvm.tar.gz'
},
"Windows": {
"build_tools_path": 'prebuilts\\build-tools\\win-x86\\bin',
"gn": 'https://repo.huaweicloud.com/harmonyos/compiler/gn/' +\
'1744/windows/gn-windows-amd64.zip',
"ninja": 'https://repo.huaweicloud.com/harmonyos/compiler/ninja/' +\
'1.9.0/windows/ninja-win.zip'
}
}
......@@ -21,13 +21,13 @@ from distutils.spawn import find_executable
from hb import CONFIG_JSON
from hb import CONFIG_STRUCT
from hb import BUILD_TOOLS_URL
from hb import BUILD_TOOLS_CFG
from hb.common.utils import read_json_file
from hb.common.utils import dump_json_file
from hb.common.utils import Singleton
from hb.common.utils import OHOSException
from hb.common.utils import download_tool
from hb.common.utils import extract_tool
from hb.common.utils import makedirs
class Config(metaclass=Singleton):
......@@ -44,6 +44,7 @@ class Config(metaclass=Singleton):
self._patch_cache = config_content.get('patch_cache', None)
self._out_path = None
self.fs_attr = set()
self.platform = platform.system()
@property
def root_path(self):
......@@ -160,21 +161,11 @@ class Config(metaclass=Singleton):
@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 OHOSException(f'unidentified platform: {platform_name}')
try:
tools_path = BUILD_TOOLS_CFG[self.platform]['build_tools_path']
return os.path.join(self.root_path, tools_path)
except KeyError:
raise OHOSException(f'unidentified platform: {self.platform}')
@property
def gn_path(self):
......@@ -184,17 +175,12 @@ class Config(metaclass=Singleton):
return repo_gn_path
# gn not install, download and extract it.
if not os.path.exists(self.build_tools_path):
os.makedirs(self.build_tools_path)
host = platform.system()
if host == 'Linux':
gn_url = BUILD_TOOLS_URL["gn"]["linux-x86"]
gn_dst = os.path.join(self.build_tools_path, 'gn.tar.gz')
elif host == 'Windows':
gn_url = BUILD_TOOLS_URL["gn"]["windows-amd64"]
gn_dst = os.path.join(self.build_tools_path, 'gn.zip')
download_tool(gn_url, gn_dst)
extract_tool(gn_dst, self.build_tools_path)
makedirs(self.build_tools_path, exist_ok=True)
gn_url = BUILD_TOOLS_CFG[self.platform].get('gn')
gn_dst = os.path.join(self.build_tools_path, 'gn_pkg')
download_tool(gn_url, gn_dst, tgt_dir=self.build_tools_path)
return repo_gn_path
@property
......@@ -205,15 +191,10 @@ class Config(metaclass=Singleton):
return repo_ninja_path
# ninja not install, download and extract.
host = platform.system()
if host == 'Linux':
ninja_url = BUILD_TOOLS_URL["ninja"]["linux-x86"]
ninja_dst = os.path.join(self.build_tools_path, 'ninja.tar.gz')
elif host == 'Windows':
ninja_url = BUILD_TOOLS_URL["ninja"]["windows"]
ninja_dst = os.path.join(self.build_tools_path, 'ninja.zip')
download_tool(ninja_url, ninja_dst)
extract_tool(ninja_dst, self.build_tools_path)
ninja_url = BUILD_TOOLS_CFG[self.platform].get('ninja')
ninja_dst = os.path.join(self.build_tools_path, 'ninja_pkg')
download_tool(ninja_url, ninja_dst, tgt_dir=self.build_tools_path)
return repo_ninja_path
@property
......@@ -239,16 +220,13 @@ class Config(metaclass=Singleton):
return env_clang_path
# need auto download and extract clang.
clang_path = os.path.join('prebuilts',
'clang',
'ohos',
'linux-x86_64')
if not os.path.exists(clang_path):
os.makedirs(clang_path)
clang_url = BUILD_TOOLS_URL["clang"]["linux"]
clang_dst = os.path.join(clang_path, 'llvm.tar.gz')
download_tool(clang_url, clang_dst)
extract_tool(clang_dst, clang_path)
clang_path = os.path.abspath(os.path.join(repo_clang_path,
os.pardir))
makedirs(clang_path, exist_ok=True)
clang_url = BUILD_TOOLS_CFG[self.platform].get('clang')
clang_dst = os.path.join(clang_path, 'clang_pkg')
download_tool(clang_url, clang_dst, tgt_dir=clang_path)
return f'//{repo_clang_path}'
@property
......
......@@ -235,7 +235,7 @@ class OHOSException(Exception):
pass
def download_tool(url, dst):
def download_tool(url, dst, tgt_dir=None):
try:
res = requests.get(url, stream=True, timeout=(5, 9))
except OSError:
......@@ -259,17 +259,20 @@ def download_tool(url, dst):
download_size += len(chunk)
download_percent = round(float(download_size / total_size * 100), 2)
print('Progress: %s%%\r' % download_percent, end=' ')
hb_info(f'Download complete!')
hb_info('Download complete!')
except OSError:
raise OHOSException(f'{url} download failed, please install it manually!')
if tgt_dir is not None:
extract_tool(dst, tgt_dir)
def extract_tool(src, tgt_dir):
hb_info(f'Extracting to {tgt_dir}, please wait...')
try:
if src.endswith(('.tar.gz', '.gz', '.tar')):
if tarfile.is_tarfile(src):
ef = tarfile.open(src)
elif src.endswith('.zip'):
elif zipfile.is_zipfile(src):
ef = zipfile.ZipFile(src)
else:
raise OHOSException(f'Extract file type not support!')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册