• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    with open(save_path + file_name, 'wb+') as file:
48        file.write(content)
49    print('download finished!')
50    return 1
51
52
53def download(file_type, save_path, version):
54    local_time = time.localtime(time.time())
55    year = local_time[0]
56    version_suffixes = "zyxwvutsrqponmlkjihgfedcba"
57    version_index = 0
58
59    print('start to find the lastest version of tzdata and tzcode.')
60    status = -1
61    while status < 0:
62        status = try_download(file_type, str(year) +
63                              version_suffixes[version_index], save_path,
64                              version)
65        if status < 0:
66            if version_index < 25:
67                version_index += 1
68            else:
69                year -= 1
70                version_index = 0
71    if status == 0:
72        return ''
73    file_name = file_type + str(year) + version_suffixes[version_index] + \
74                '.tar.gz'
75    return file_name
76
77
78def decompress(file_name, save_path):
79    tar = tarfile.open(save_path + file_name, "r:gz")
80    tar.extractall(save_path)
81    tar.close()
82    print('decompress finished!')
83
84
85def main():
86    file_path = os.path.abspath(__file__)
87    file_dir = os.path.dirname(file_path)
88
89    version_file_path = file_dir + "/../../data/prebuild/posix/version.txt"
90    version = find_current_version(version_file_path)
91
92    print('current version is ' + version)
93
94    download_path = file_dir + "/../../data/iana/"
95    if not os.path.exists(download_path):
96        os.makedirs(download_path)
97    file_type = "tzdata"
98    file_name = download(file_type, download_path, version)
99
100    if file_name != '':
101        decompress(file_name, download_path)
102        file_type = "tzcode"
103        new_version = file_name[6:11]
104        try_download(file_type, new_version, download_path, version)
105        decompress(file_type + new_version + '.tar.gz', download_path)
106        with open(os.path.join(download_path, 'version.txt'), 'w') as file:
107            file.write(new_version + '\n')
108
109
110if __name__ == '__main__':
111    sys.exit(main())
112