15f9996aaSopenharmony_ci#!/usr/bin/env python3 25f9996aaSopenharmony_ci# -*- coding: utf-8 -*- 35f9996aaSopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd. 45f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 55f9996aaSopenharmony_ci# you may not use this file except in compliance with the License. 65f9996aaSopenharmony_ci# You may obtain a copy of the License at 75f9996aaSopenharmony_ci# 85f9996aaSopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 95f9996aaSopenharmony_ci# 105f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software 115f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 125f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135f9996aaSopenharmony_ci# See the License for the specific language governing permissions and 145f9996aaSopenharmony_ci# limitations under the License. 155f9996aaSopenharmony_ci 165f9996aaSopenharmony_ci 175f9996aaSopenharmony_ciimport requests 185f9996aaSopenharmony_ciimport json 195f9996aaSopenharmony_ciimport datetime 205f9996aaSopenharmony_ciimport os 215f9996aaSopenharmony_ciimport sys 225f9996aaSopenharmony_ciimport tarfile 235f9996aaSopenharmony_ciimport subprocess 245f9996aaSopenharmony_ciimport argparse 255f9996aaSopenharmony_ciimport shutil 265f9996aaSopenharmony_ci 275f9996aaSopenharmony_cifrom urllib.request import urlretrieve 285f9996aaSopenharmony_ci 295f9996aaSopenharmony_ci 305f9996aaSopenharmony_cidef find_top(): 315f9996aaSopenharmony_ci cur_dir = os.getcwd() 325f9996aaSopenharmony_ci while cur_dir != "/": 335f9996aaSopenharmony_ci build_scripts = os.path.join( 345f9996aaSopenharmony_ci cur_dir, 'build/config/BUILDCONFIG.gn') 355f9996aaSopenharmony_ci if os.path.exists(build_scripts): 365f9996aaSopenharmony_ci return cur_dir 375f9996aaSopenharmony_ci cur_dir = os.path.dirname(cur_dir) 385f9996aaSopenharmony_ci 395f9996aaSopenharmony_ci 405f9996aaSopenharmony_cidef reporthook(data_download, data_size, total_size): 415f9996aaSopenharmony_ci ''' 425f9996aaSopenharmony_ci display the progress of download 435f9996aaSopenharmony_ci :param data_download: data downloaded 445f9996aaSopenharmony_ci :param data_size: data size 455f9996aaSopenharmony_ci :param total_size: remote file size 465f9996aaSopenharmony_ci :return:None 475f9996aaSopenharmony_ci ''' 485f9996aaSopenharmony_ci progress = int(0) 495f9996aaSopenharmony_ci if progress != int(data_download * data_size * 1000 / total_size): 505f9996aaSopenharmony_ci progress = int(data_download * data_size * 1000 / total_size) 515f9996aaSopenharmony_ci sys.stdout.flush() 525f9996aaSopenharmony_ci 535f9996aaSopenharmony_ci 545f9996aaSopenharmony_cidef download(download_url, savepath): 555f9996aaSopenharmony_ci filename = os.path.basename(download_url) 565f9996aaSopenharmony_ci 575f9996aaSopenharmony_ci if not os.path.isfile(os.path.join(savepath, filename)): 585f9996aaSopenharmony_ci print('Downloading data form %s' % download_url) 595f9996aaSopenharmony_ci urlretrieve(download_url, os.path.join( 605f9996aaSopenharmony_ci savepath, filename), reporthook=reporthook) 615f9996aaSopenharmony_ci print('\nDownload finished!') 625f9996aaSopenharmony_ci else: 635f9996aaSopenharmony_ci print("\nFile exsits!") 645f9996aaSopenharmony_ci 655f9996aaSopenharmony_ci filesize = os.path.getsize(os.path.join(savepath, filename)) 665f9996aaSopenharmony_ci print('File size = %.2f Mb' % (filesize / 1024 / 1024)) 675f9996aaSopenharmony_ci 685f9996aaSopenharmony_ci 695f9996aaSopenharmony_cidef extract_file(filename): 705f9996aaSopenharmony_ci 715f9996aaSopenharmony_ci target_dir = os.path.dirname(filename) 725f9996aaSopenharmony_ci 735f9996aaSopenharmony_ci if not os.path.exists(target_dir): 745f9996aaSopenharmony_ci os.makedirs(target_dir, exist_ok=True) 755f9996aaSopenharmony_ci with tarfile.open(filename, "r:gz") as tar: 765f9996aaSopenharmony_ci tar.extractall(target_dir) 775f9996aaSopenharmony_ci 785f9996aaSopenharmony_ci if os.path.exists(os.path.join(target_dir, "daily_build.log")): 795f9996aaSopenharmony_ci os.remove(os.path.join(target_dir, "daily_build.log")) 805f9996aaSopenharmony_ci if os.path.exists(os.path.join(target_dir, "manifest_tag.xml")): 815f9996aaSopenharmony_ci os.remove(os.path.join(target_dir, "manifest_tag.xml")) 825f9996aaSopenharmony_ci 835f9996aaSopenharmony_ci 845f9996aaSopenharmony_cidef npm_install(target_dir, args): 855f9996aaSopenharmony_ci 865f9996aaSopenharmony_ci sdk_zip_file_dir = os.path.join(target_dir, "ohos-sdk/linux") 875f9996aaSopenharmony_ci sdk_unzip_dir = os.path.join(sdk_zip_file_dir, args.api_version) 885f9996aaSopenharmony_ci 895f9996aaSopenharmony_ci if os.path.exists(sdk_unzip_dir): 905f9996aaSopenharmony_ci shutil.rmtree(sdk_unzip_dir) 915f9996aaSopenharmony_ci os.makedirs(sdk_unzip_dir, exist_ok=True) 925f9996aaSopenharmony_ci os.chdir(sdk_zip_file_dir) 935f9996aaSopenharmony_ci for filename in os.listdir(sdk_zip_file_dir): 945f9996aaSopenharmony_ci if filename.endswith('.zip'): 955f9996aaSopenharmony_ci subprocess.run(['mv', filename, sdk_unzip_dir]) 965f9996aaSopenharmony_ci 975f9996aaSopenharmony_ci procs = [] 985f9996aaSopenharmony_ci os.chdir(sdk_unzip_dir) 995f9996aaSopenharmony_ci for filename in os.listdir(sdk_unzip_dir): 1005f9996aaSopenharmony_ci if filename.endswith('.zip'): 1015f9996aaSopenharmony_ci cmd = ['unzip', filename] 1025f9996aaSopenharmony_ci proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 1035f9996aaSopenharmony_ci procs.append(proc) 1045f9996aaSopenharmony_ci for proc in procs: 1055f9996aaSopenharmony_ci out, error = proc.communicate(timeout=60) 1065f9996aaSopenharmony_ci 1075f9996aaSopenharmony_ci 1085f9996aaSopenharmony_cidef main(): 1095f9996aaSopenharmony_ci parser = argparse.ArgumentParser() 1105f9996aaSopenharmony_ci parser.add_argument('--branch', default='master', help='OHOS branch name') 1115f9996aaSopenharmony_ci parser.add_argument('--product-name', default='ohos-sdk-full', help='OHOS product name') 1125f9996aaSopenharmony_ci parser.add_argument('--api-version', default='10', help='OHOS sdk api version') 1135f9996aaSopenharmony_ci args = parser.parse_args() 1145f9996aaSopenharmony_ci default_save_path = os.path.join(find_top(), 'prebuilts') 1155f9996aaSopenharmony_ci if not os.path.exists(default_save_path): 1165f9996aaSopenharmony_ci os.makedirs(default_save_path, exist_ok=True) 1175f9996aaSopenharmony_ci print(default_save_path) 1185f9996aaSopenharmony_ci try: 1195f9996aaSopenharmony_ci now_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S') 1205f9996aaSopenharmony_ci last_hour = (datetime.datetime.now() + 1215f9996aaSopenharmony_ci datetime.timedelta(hours=-72)).strftime('%Y%m%d%H%M%S') 1225f9996aaSopenharmony_ci 1235f9996aaSopenharmony_ci url = "http://ci.openharmony.cn/api/daily_build/build/tasks" 1245f9996aaSopenharmony_ci myobj = {"pageNum": 1, 1255f9996aaSopenharmony_ci "pageSize": 1000, 1265f9996aaSopenharmony_ci "startTime": "", 1275f9996aaSopenharmony_ci "endTime": "", 1285f9996aaSopenharmony_ci "projectName": "openharmony", 1295f9996aaSopenharmony_ci "branch": args.branch, 1305f9996aaSopenharmony_ci "component": "", 1315f9996aaSopenharmony_ci "deviceLevel": "", 1325f9996aaSopenharmony_ci "hardwareBoard": "", 1335f9996aaSopenharmony_ci "buildStatus": "success", 1345f9996aaSopenharmony_ci "buildFailReason": "", 1355f9996aaSopenharmony_ci "testResult": ""} 1365f9996aaSopenharmony_ci myobj["startTime"] = str(last_hour) 1375f9996aaSopenharmony_ci myobj["endTime"] = str(now_time) 1385f9996aaSopenharmony_ci x = requests.post(url, json=myobj) 1395f9996aaSopenharmony_ci data = json.loads(x.text) 1405f9996aaSopenharmony_ci except BaseException: 1415f9996aaSopenharmony_ci Exception("Unable to establish connection with ci.openharmony.cn") 1425f9996aaSopenharmony_ci 1435f9996aaSopenharmony_ci products_list = data['data']['dailyBuildVos'] 1445f9996aaSopenharmony_ci for product in products_list: 1455f9996aaSopenharmony_ci product_name = product['component'] 1465f9996aaSopenharmony_ci if product_name == args.product_name: 1475f9996aaSopenharmony_ci if os.path.exists(os.path.join(default_save_path, product_name)): 1485f9996aaSopenharmony_ci print('{} already exists. Please backup or delete it first! Download canceled!'.format( 1495f9996aaSopenharmony_ci os.path.join(default_save_path, product_name))) 1505f9996aaSopenharmony_ci break 1515f9996aaSopenharmony_ci 1525f9996aaSopenharmony_ci if product['obsPath'] and os.path.exists(default_save_path): 1535f9996aaSopenharmony_ci download_url = 'http://download.ci.openharmony.cn/{}'.format(product['obsPath']) 1545f9996aaSopenharmony_ci save_path2 = default_save_path 1555f9996aaSopenharmony_ci 1565f9996aaSopenharmony_ci try: 1575f9996aaSopenharmony_ci download(download_url, savepath=save_path2) 1585f9996aaSopenharmony_ci print(download_url, "done") 1595f9996aaSopenharmony_ci except BaseException: 1605f9996aaSopenharmony_ci 1615f9996aaSopenharmony_ci # remove the incomplete downloaded files 1625f9996aaSopenharmony_ci if os.path.exists(os.path.join(save_path2, os.path.basename(download_url))): 1635f9996aaSopenharmony_ci os.remove(os.path.join( 1645f9996aaSopenharmony_ci save_path2, os.path.basename(download_url))) 1655f9996aaSopenharmony_ci Exception("Unable to download {}".format(download_url)) 1665f9996aaSopenharmony_ci 1675f9996aaSopenharmony_ci extract_file(os.path.join( 1685f9996aaSopenharmony_ci save_path2, os.path.basename(download_url))) 1695f9996aaSopenharmony_ci npm_install(save_path2, args) 1705f9996aaSopenharmony_ci break 1715f9996aaSopenharmony_ci 1725f9996aaSopenharmony_ci 1735f9996aaSopenharmony_ciif __name__ == '__main__': 1745f9996aaSopenharmony_ci sys.exit(main()) 175