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 26import stat 27 28 29def find_current_version(path): 30 flags = os.O_RDONLY 31 modes = stat.S_IWUSR | stat.S_IRUSR 32 with os.fdopen(os.open(path, flags, modes), 'r') as file: 33 version = file.readline().strip() 34 return version 35 36 37def try_download(file_type, try_version, save_path, version): 38 if try_version == version: 39 print('current version is already the lastest version.') 40 return 0 41 parent_url = "https://data.iana.org/time-zones/releases/" 42 context = ssl.SSLContext() 43 try: 44 file_name = file_type + try_version + '.tar.gz' 45 data = request.urlopen(parent_url + file_name, context=context) 46 except error.HTTPError as http_error: 47 return -1 48 print('start to download ' + file_name) 49 content = data.read() 50 data.close() 51 flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL 52 modes = stat.S_IWUSR | stat.S_IRUSR 53 with os.fdopen(os.open(save_path + file_name, flags, modes), 'wb+') as file: 54 file.write(content) 55 print('download finished!') 56 return 1 57 58 59def download(file_type, save_path, version): 60 local_time = time.localtime(time.time()) 61 year = local_time[0] 62 version_suffixes = "zyxwvutsrqponmlkjihgfedcba" 63 version_index = 0 64 65 print('start to find the lastest version of tzdata and tzcode.') 66 status = -1 67 while status < 0: 68 status = try_download(file_type, str(year) + 69 version_suffixes[version_index], save_path, 70 version) 71 if status < 0: 72 if version_index < 25: 73 version_index += 1 74 else: 75 year -= 1 76 version_index = 0 77 if status == 0: 78 return '' 79 file_name = file_type + str(year) + version_suffixes[version_index] + \ 80 '.tar.gz' 81 return file_name 82 83 84def decompress(file_name, save_path): 85 tar = tarfile.open(save_path + file_name, "r:gz") 86 tar.extractall(save_path) 87 tar.close() 88 print('decompress finished!') 89 90 91def main(): 92 file_path = os.path.abspath(__file__) 93 file_dir = os.path.dirname(file_path) 94 95 version_file_path = file_dir + "/../../data/prebuild/posix/version.txt" 96 version = find_current_version(version_file_path) 97 98 print('current version is ' + version) 99 100 download_path = file_dir + "/../../data/iana/" 101 if not os.path.exists(download_path): 102 os.makedirs(download_path) 103 file_type = "tzdata" 104 file_name = download(file_type, download_path, version) 105 106 if file_name != '': 107 decompress(file_name, download_path) 108 file_type = "tzcode" 109 new_version = file_name[6:11] 110 try_download(file_type, new_version, download_path, version) 111 decompress(file_type + new_version + '.tar.gz', download_path) 112 flags = os.O_WRONLY 113 modes = stat.S_IWUSR | stat.S_IRUSR 114 with os.fdopen(os.open(os.path.join(download_path, 'version.txt'), flags, modes), 'w') as file: 115 file.write(new_version + '\n') 116 117 118if __name__ == '__main__': 119 sys.exit(main()) 120