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