• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
107def main():
108    parser = argparse.ArgumentParser()
109    parser.add_argument('--branch', default='master', help='OHOS branch name')
110    parser.add_argument('--product-name', default='ohos-sdk-full', help='OHOS product name')
111    parser.add_argument('--api-version', default='10', help='OHOS sdk api version')
112    args = parser.parse_args()
113    default_save_path = os.path.join(find_top(), 'prebuilts')
114    if not os.path.exists(default_save_path):
115        os.makedirs(default_save_path, exist_ok=True)
116    print(default_save_path)
117    try:
118        now_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
119        last_hour = (datetime.datetime.now() +
120                     datetime.timedelta(hours=-72)).strftime('%Y%m%d%H%M%S')
121
122        url = "http://ci.openharmony.cn/api/daily_build/build/tasks"
123        myobj = {"pageNum": 1,
124                 "pageSize": 1000,
125                 "startTime": "",
126                 "endTime": "",
127                 "projectName": "openharmony",
128                 "branch": args.branch,
129                 "component": "",
130                 "deviceLevel": "",
131                 "hardwareBoard": "",
132                 "buildStatus": "success",
133                 "buildFailReason": "",
134                 "testResult": ""}
135        myobj["startTime"] = str(last_hour)
136        myobj["endTime"] = str(now_time)
137        x = requests.post(url, json=myobj)
138        data = json.loads(x.text)
139    except BaseException:
140        Exception("Unable to establish connection with ci.openharmony.cn")
141
142    products_list = data['data']['dailyBuildVos']
143    for product in products_list:
144        product_name = product['component']
145        if product_name == args.product_name:
146            if os.path.exists(os.path.join(default_save_path, product_name)):
147                print('{} already exists. Please backup or delete it first! Download canceled!'.format(
148                    os.path.join(default_save_path, product_name)))
149                break
150
151            if product['obsPath'] and os.path.exists(default_save_path):
152                download_url = 'http://download.ci.openharmony.cn/{}'.format(product['obsPath'])
153                save_path2 = default_save_path
154
155            try:
156                download(download_url, savepath=save_path2)
157                print(download_url, "done")
158            except BaseException:
159
160                # remove the incomplete downloaded files
161                if os.path.exists(os.path.join(save_path2, os.path.basename(download_url))):
162                    os.remove(os.path.join(
163                        save_path2, os.path.basename(download_url)))
164                Exception("Unable to download {}".format(download_url))
165
166            extract_file(os.path.join(
167                save_path2, os.path.basename(download_url)))
168            npm_install(save_path2, args)
169            break
170
171
172if __name__ == '__main__':
173    sys.exit(main())
174