Debian Codegraph @

scripts/download_import_cldr.py

raw
1 #!/usr/bin/env python3
2
3 import contextlib
4 import hashlib
5 import os
6 import shutil
7 import subprocess
8 import sys
9 import zipfile
10 from urllib.request import Request, urlopen
11
12 URL = 'https://unicode.org/Public/cldr/48.2/cldr-common-48.2.zip'
13 FILENAME = 'cldr-common-48.2.zip'
14 # Via https://unicode.org/Public/cldr/48.2/hashes/SHASUM512.txt (core.zip; see https://unicode-org.atlassian.net/browse/CLDR-19388)
15 FILESUM = 'de8660f5371e0fcfd03a42e3b4fc4c686ec6cd602b402f1e3d227844005a54eb7952873894443523837d5828c42874a1a267a19f91ded207a2d166144791fa62'
16
17
18 def reporthook(bytes_transmitted, total_size):
19 cols = shutil.get_terminal_size().columns
20 buffer = 6
21 percent = float(bytes_transmitted) / (total_size or 1)
22 done = int(percent * (cols - buffer))
23 bar = ('=' * done).ljust(cols - buffer)
24 sys.stdout.write(f'\r{bar}{int(percent * 100): 4d}%')
25 sys.stdout.flush()
26
27
28 def log(message):
29 sys.stderr.write(f'{message}\n')
30
31
32 def download_file(url, dest_path, reporthook=None):
33 request = Request(url, headers={'User-Agent': 'babel-cldr-downloader (https://babel.pocoo.org/)'})
34 with urlopen(request) as response:
35 total_size = int(response.headers.get('Content-Length', 0))
36 log(f"Downloading {url} to {dest_path}: {total_size // 1024} KiB")
37 block_count = 0
38 with open(dest_path, 'wb') as out_file:
39 while True:
40 block = response.read(262144)
41 if not block:
42 break
43 out_file.write(block)
44 block_count += 1
45 if reporthook:
46 reporthook(out_file.tell(), total_size)
47
48
49 def is_good_file(filename):
50 if not os.path.isfile(filename):
51 log(f"Local copy '{filename}' not found")
52 return False
53 h = hashlib.sha512()
54 with open(filename, 'rb') as f:
55 while True:
56 blk = f.read(262144)
57 if not blk:
58 break
59 h.update(blk)
60 digest = h.hexdigest()
61 if digest != FILESUM:
62 raise RuntimeError(f'Checksum mismatch: {digest!r} != {FILESUM!r}')
63 else:
64 return True
65
66
67 def main():
68 scripts_path = os.path.dirname(os.path.abspath(__file__))
69 repo = os.path.dirname(scripts_path)
70 cldr_dl_path = os.path.join(repo, 'cldr')
71 cldr_path = os.path.join(repo, 'cldr', os.path.splitext(FILENAME)[0])
72 zip_path = os.path.join(cldr_dl_path, FILENAME)
73 changed = False
74 show_progress = (False if os.environ.get("BABEL_CLDR_NO_DOWNLOAD_PROGRESS") else sys.stdout.isatty())
75
76 while not is_good_file(zip_path):
77 tmp_path = f"{zip_path}.tmp"
78 download_file(URL, tmp_path, (reporthook if show_progress else None))
79 os.replace(tmp_path, zip_path)
80 changed = True
81 print()
82 common_path = os.path.join(cldr_path, 'common')
83
84 if changed or not os.path.isdir(common_path):
85 if os.path.isdir(common_path):
86 log(f"Deleting old CLDR checkout in '{cldr_path}'")
87 shutil.rmtree(common_path)
88
89 log(f"Extracting CLDR to '{cldr_path}'")
90 with contextlib.closing(zipfile.ZipFile(zip_path)) as z:
91 z.extractall(cldr_path)
92
93 subprocess.check_call([
94 sys.executable,
95 os.path.join(scripts_path, 'import_cldr.py'),
96 common_path,
97 *sys.argv[1:],
98 ])
99
100
101 if __name__ == '__main__':
102 main()