1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2022 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import time 20import urllib.request as request 21import urllib.error as error 22import ssl 23import tarfile 24import sys 25import os 26 27 28def find_current_version(path): 29 with open(path, 'r') as file: 30 version = file.readline().strip() 31 return version 32 33 34def try_download(file_type, try_version, save_path, version): 35 if try_version == version: 36 print('current version is already the lastest version.') 37 return 0 38 parent_url = "https://data.iana.org/time-zones/releases/" 39 context = ssl.SSLContext() 40 try: 41 file_name = file_type + try_version + '.tar.gz' 42 data = request.urlopen(parent_url + file_name, context=context) 43 except error.HTTPError as http_error: 44 return -1 45 print('start to download ' + file_name) 46 content = data.read() 47 data.close() 48 with open(save_path + file_name, 'wb+') as file: 49 file.write(content) 50 print('download finished!') 51 return 1 52 53 54def download(file_type, save_path, version): 55 local_time = time.localtime(time.time()) 56 year = local_time[0] 57 version_suffixes = "zyxwvutsrqponmlkjihgfedcba" 58 version_index = 0 59 60 print('start to find the lastest version of tzdata and tzcode.') 61 status = -1 62 while status < 0: 63 status = try_download(file_type, str(year) + 64 version_suffixes[version_index], save_path, 65 version) 66 if status < 0: 67 if version_index < 25: 68 version_index += 1 69 else: 70 year -= 1 71 version_index = 0 72 if status == 0: 73 return '' 74 file_name = file_type + str(year) + version_suffixes[version_index] + \ 75 '.tar.gz' 76 return file_name 77 78 79def decompress(file_name, save_path): 80 tar = tarfile.open(save_path + file_name, "r:gz") 81 tar.extractall(save_path) 82 tar.close() 83 print('decompress finished!') 84 85 86def main(): 87 file_path = os.path.abspath(__file__) 88 file_dir = os.path.dirname(file_path) 89 90 version_file_path = file_dir + "/../../data/prebuild/posix/version.txt" 91 version = find_current_version(version_file_path) 92 93 print('current version is ' + version) 94 95 download_path = file_dir + "/../../data/iana/" 96 if not os.path.exists(download_path): 97 os.makedirs(download_path) 98 file_type = "tzdata" 99 file_name = download(file_type, download_path, version) 100 101 if file_name != '': 102 decompress(file_name, download_path) 103 file_type = "tzcode" 104 new_version = file_name[6:11] 105 try_download(file_type, new_version, download_path, version) 106 decompress(file_type + new_version + '.tar.gz', download_path) 107 with open(os.path.join(download_path, 'version.txt'), 'w') as file: 108 file.write(new_version + '\n') 109 110 111if __name__ == '__main__': 112 sys.exit(main()) 113