|
1 |
| -#!/usr/bin/env python |
| 1 | +#!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import glob
|
4 | 4 | import hashlib
|
| 5 | +import io |
5 | 6 | import os
|
6 | 7 | import re
|
7 | 8 | import shutil
|
|
12 | 13 | import tempfile
|
13 | 14 | import urllib.request
|
14 | 15 |
|
| 16 | + |
15 | 17 | def create_cmakelists(zoneinfo_dir):
|
16 | 18 | tz_to_hash = {}
|
17 | 19 | hash_to_content = {}
|
@@ -53,62 +55,74 @@ def create_cmakelists(zoneinfo_dir):
|
53 | 55 | with open('ya.make.resources', 'w') as f:
|
54 | 56 | print(yamake_template.format(resources), file=f)
|
55 | 57 |
|
56 |
| -def get_latest_iana_version(): |
| 58 | + |
| 59 | +def get_latest_version(): |
| 60 | + # Temporary here for the purposes of reimport |
| 61 | + return "2024a" |
57 | 62 | index_html = urllib.request.urlopen('http://www.iana.org/time-zones').read()
|
58 | 63 | version_match = re.search('<a href="[^"]*">tzdata(.*).tar.gz</a>', index_html.decode())
|
59 | 64 | if not version_match:
|
60 | 65 | raise Exception('Failed to determine the latest tzdata version')
|
61 | 66 | return version_match.group(1)
|
62 | 67 |
|
| 68 | + |
63 | 69 | def get_current_version():
|
64 | 70 | try:
|
65 | 71 | with open('VERSION') as f:
|
66 | 72 | return f.read()
|
67 | 73 | except:
|
68 | 74 | return 0
|
69 | 75 |
|
| 76 | + |
70 | 77 | def prepare_tzdata(version):
|
71 |
| - temp_dir = tempfile.mkdtemp() |
72 |
| - try: |
73 |
| - for file_type in ('data', 'code'): |
74 |
| - file_name = 'tz{}{}.tar.gz'.format(file_type, version) |
75 |
| - full_url = 'http://www.iana.org/time-zones/repository/releases/{}'.format(file_name) |
76 |
| - print('Downloading {}'.format(full_url)) |
| 78 | + temp_dir = "tmp" |
| 79 | + shutil.rmtree(temp_dir, ignore_errors=True) |
77 | 80 |
|
78 |
| - local_file_name = os.path.join(temp_dir, file_name) |
79 |
| - with open(local_file_name, 'wb') as f: |
80 |
| - f.write(urllib.request.urlopen(full_url).read()) |
| 81 | + EXCLUDE = [ |
| 82 | + "iso3166.tab", |
| 83 | + "zone.tab", |
| 84 | + "zonenow.tab", |
| 85 | + ] |
81 | 86 |
|
82 |
| - print('Extracting {}'.format(local_file_name)) |
83 |
| - with tarfile.open(local_file_name) as f: |
| 87 | + try: |
| 88 | + for type in ('data', 'code'): |
| 89 | + filename = f'tz{type}{version}.tar.gz' |
| 90 | + url = f'http://www.iana.org/time-zones/repository/releases/{filename}' |
| 91 | + print(f'Downloading {url}') |
| 92 | + |
| 93 | + bytestream = io.BytesIO(urllib.request.urlopen(url).read()) |
| 94 | + print(f'Extracting {filename}') |
| 95 | + with tarfile.open(fileobj=bytestream, mode="r:gz") as f: |
84 | 96 | f.extractall(path=temp_dir)
|
85 |
| - |
| 97 | + |
86 | 98 | print('Converting tzdata to binary format')
|
87 |
| - subprocess.check_call(['make', '-s', '-C', temp_dir, 'TOPDIR={}'.format(temp_dir), 'install']) |
| 99 | + subprocess.check_call( |
| 100 | + ['make', "--silent", "TOPDIR=.", 'install'], |
| 101 | + cwd=temp_dir, |
| 102 | + ) |
| 103 | + |
| 104 | + shutil.rmtree(f"{temp_dir}/usr/share/zoneinfo/etc") |
| 105 | + for path in EXCLUDE: |
| 106 | + os.remove(f"{temp_dir}/usr/share/zoneinfo/{path}") |
| 107 | + |
| 108 | + # keep posixrules for now |
| 109 | + shutil.copyfile( |
| 110 | + "generated/58543f30ac34b6510b552b9b3e82b772", |
| 111 | + f"{temp_dir}/usr/share/zoneinfo/posixrules", |
| 112 | + ) |
88 | 113 |
|
89 | 114 | print('Preparing ya.make.resources')
|
90 |
| - zoneinfo_dir = os.path.join(temp_dir, 'usr', 'share', 'zoneinfo') |
91 |
| - create_cmakelists(zoneinfo_dir) |
| 115 | + create_cmakelists(f"{temp_dir}/usr/share/zoneinfo") |
92 | 116 | finally:
|
93 | 117 | shutil.rmtree(temp_dir)
|
94 | 118 |
|
| 119 | + |
95 | 120 | def main():
|
96 |
| - current_version, latest_version = get_current_version(), get_latest_iana_version() |
97 |
| - print('The current version of tzdata is {}'.format(current_version)) |
98 |
| - print('The latest version of tzdata on the IANA site is {}'.format(latest_version)) |
99 |
| - if current_version == latest_version: |
100 |
| - print('You already have the latest version') |
101 |
| - return |
102 |
| - print('Updating from {} to {}'.format(current_version, latest_version)) |
103 |
| - prepare_tzdata(latest_version) |
104 |
| - |
105 |
| - with open('VERSION', 'w') as f: |
106 |
| - f.write(latest_version) |
107 |
| - |
108 |
| - print('All good! Now make sure the tests pass, and run this:') |
109 |
| - print('arc add VERSION update_tzdata.py ya.make.resources') |
110 |
| - print('arc co -b tzdata.{}'.format(latest_version)) |
111 |
| - print('arc ci . -m "Updated tzdata from {} to {}"'.format(current_version, latest_version)) |
| 121 | + version_current = get_current_version() |
| 122 | + version_latest = get_latest_version() |
| 123 | + print(f'Updating from {version_current} to {version_latest}') |
| 124 | + prepare_tzdata(version_latest) |
112 | 125 |
|
113 | 126 | if __name__ == '__main__':
|
| 127 | + |
114 | 128 | main()
|
0 commit comments