1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (c) 2023 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16 17import requests 18import json 19import datetime 20import os 21import sys 22import tarfile 23import subprocess 24import argparse 25import shutil 26 27from urllib.request import urlretrieve 28 29 30def find_top(): 31 cur_dir = os.getcwd() 32 while cur_dir != "/": 33 build_scripts = os.path.join( 34 cur_dir, 'build/config/BUILDCONFIG.gn') 35 if os.path.exists(build_scripts): 36 return cur_dir 37 cur_dir = os.path.dirname(cur_dir) 38 39 40def reporthook(data_download, data_size, total_size): 41 ''' 42 display the progress of download 43 :param data_download: data downloaded 44 :param data_size: data size 45 :param total_size: remote file size 46 :return:None 47 ''' 48 progress = int(0) 49 if progress != int(data_download * data_size * 1000 / total_size): 50 progress = int(data_download * data_size * 1000 / total_size) 51 sys.stdout.flush() 52 53 54def download(download_url, savepath): 55 filename = os.path.basename(download_url) 56 57 if not os.path.isfile(os.path.join(savepath, filename)): 58 print('Downloading data form %s' % download_url) 59 urlretrieve(download_url, os.path.join( 60 savepath, filename), reporthook=reporthook) 61 print('\nDownload finished!') 62 else: 63 print("\nFile exsits!") 64 65 filesize = os.path.getsize(os.path.join(savepath, filename)) 66 print('File size = %.2f Mb' % (filesize / 1024 / 1024)) 67 68 69def extract_file(filename): 70 71 target_dir = os.path.dirname(filename) 72 73 if not os.path.exists(target_dir): 74 os.makedirs(target_dir, exist_ok=True) 75 with tarfile.open(filename, "r:gz") as tar: 76 tar.extractall(target_dir) 77 78 if os.path.exists(os.path.join(target_dir, "daily_build.log")): 79 os.remove(os.path.join(target_dir, "daily_build.log")) 80 if os.path.exists(os.path.join(target_dir, "manifest_tag.xml")): 81 os.remove(os.path.join(target_dir, "manifest_tag.xml")) 82 83 84def npm_install(target_dir, args): 85 86 sdk_zip_file_dir = os.path.join(target_dir, "ohos-sdk/linux") 87 sdk_unzip_dir = os.path.join(sdk_zip_file_dir, args.api_version) 88 89 if os.path.exists(sdk_unzip_dir): 90 shutil.rmtree(sdk_unzip_dir) 91 os.makedirs(sdk_unzip_dir, exist_ok=True) 92 os.chdir(sdk_zip_file_dir) 93 for filename in os.listdir(sdk_zip_file_dir): 94 if filename.endswith('.zip'): 95 subprocess.run(['mv', filename, sdk_unzip_dir]) 96 97 procs = [] 98 os.chdir(sdk_unzip_dir) 99 for filename in os.listdir(sdk_unzip_dir): 100 if filename.endswith('.zip'): 101 cmd = ['unzip', filename] 102 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 103 procs.append(proc) 104 for proc in procs: 105 out, error = proc.communicate(timeout=60) 106 107 108def main(): 109 parser = argparse.ArgumentParser() 110 parser.add_argument('--branch', default='master', help='OHOS branch name') 111 parser.add_argument('--product-name', default='ohos-sdk-full', help='OHOS product name') 112 parser.add_argument('--api-version', default='10', help='OHOS sdk api version') 113 args = parser.parse_args() 114 default_save_path = os.path.join(find_top(), 'prebuilts') 115 if not os.path.exists(default_save_path): 116 os.makedirs(default_save_path, exist_ok=True) 117 print(default_save_path) 118 try: 119 now_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S') 120 last_hour = (datetime.datetime.now() + 121 datetime.timedelta(hours=-72)).strftime('%Y%m%d%H%M%S') 122 123 url = "http://ci.openharmony.cn/api/daily_build/build/tasks" 124 myobj = {"pageNum": 1, 125 "pageSize": 1000, 126 "startTime": "", 127 "endTime": "", 128 "projectName": "openharmony", 129 "branch": args.branch, 130 "component": "", 131 "deviceLevel": "", 132 "hardwareBoard": "", 133 "buildStatus": "success", 134 "buildFailReason": "", 135 "testResult": ""} 136 myobj["startTime"] = str(last_hour) 137 myobj["endTime"] = str(now_time) 138 x = requests.post(url, json=myobj) 139 data = json.loads(x.text) 140 except BaseException: 141 Exception("Unable to establish connection with ci.openharmony.cn") 142 143 products_list = data['data']['dailyBuildVos'] 144 for product in products_list: 145 product_name = product['component'] 146 if product_name == args.product_name: 147 if os.path.exists(os.path.join(default_save_path, product_name)): 148 print('{} already exists. Please backup or delete it first! Download canceled!'.format( 149 os.path.join(default_save_path, product_name))) 150 break 151 152 if product['obsPath'] and os.path.exists(default_save_path): 153 download_url = 'http://download.ci.openharmony.cn/{}'.format(product['obsPath']) 154 save_path2 = default_save_path 155 156 try: 157 download(download_url, savepath=save_path2) 158 print(download_url, "done") 159 except BaseException: 160 161 # remove the incomplete downloaded files 162 if os.path.exists(os.path.join(save_path2, os.path.basename(download_url))): 163 os.remove(os.path.join( 164 save_path2, os.path.basename(download_url))) 165 Exception("Unable to download {}".format(download_url)) 166 167 extract_file(os.path.join( 168 save_path2, os.path.basename(download_url))) 169 npm_install(save_path2, args) 170 break 171 172 173if __name__ == '__main__': 174 sys.exit(main()) 175