1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# 4# Copyright (c) 2023 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17 18import json 19import logging 20import os 21import stat 22import tarfile 23import xml.etree.ElementTree as ET 24 25import requests 26from tqdm import tqdm 27import yaml 28 29 30def get_images_and_testcases(url, download_path, extract_path): 31 print(f"Get new image from {url},please wait!") 32 r = requests.get(url, stream=True) 33 total = int(r.headers.get('content-length'), 0) 34 flags = os.O_WRONLY | os.O_CREAT 35 modes = stat.S_IWUSR | stat.S_IRUSR 36 37 with os.fdopen(os.open(download_path, flags, modes), "wb") as f, tqdm( 38 desc="dayu200_xts.tar.gz", 39 total=total, 40 unit='iB', 41 unit_scale=True, 42 unit_divisor=1024, 43 ) as bar: 44 for byte in r.iter_content(chunk_size=1024): 45 size = f.write(byte) 46 bar.update(size) 47 48 print("extracrting file") 49 with tarfile.open(download_path, "r") as tar: 50 for member in tqdm(desc='dayu200_xts', iterable=tar.getmembers(), total=len(tar.getmembers())): 51 tar.extract(path=extract_path, member=member) 52 logging.basicConfig(filename="log.log", level='INFO') 53 logging.info(f'Downloading Success, url:{url}') 54 55 56def get_url(url, headers, json_data, url_2): 57 response = requests.post(url, json=json_data, headers=headers) 58 json_obj = json.loads(response.text) 59 start_time = json_obj['result']['dailyBuildVos'][0]['buildStartTime'] 60 start_time = start_time[:8] + "_" + start_time[8:] 61 return url_2[0] + start_time + url_2[1] + start_time + url_2[2] 62 63 64def change_port(xml_path, xml_dw="./environment/device/port"): 65 doc = ET.parse(xml_path) 66 root = doc.getroot() 67 port = root.find(xml_dw) 68 port.text = "8710" 69 doc.write(xml_path) 70 71 72if __name__ == '__main__': 73 with open(r".\get_resource\config.yaml", 'r') as config_file: 74 data = yaml.safe_load(config_file.read()) 75 dest_url = get_url(data['url_dailybuilds'], data['headers'], data['data'], data['url_dayu200']) 76 get_images_and_testcases(dest_url, data['path_xts_pack'], data['path_xts_dir']) 77 change_port(data['path_configfile']) 78