77import subprocess
88import sys
99import zipfile
10- from urllib .request import urlretrieve
10+ from urllib .request import Request , urlopen
1111
1212URL = 'https://unicode.org/Public/cldr/47/cldr-common-47.zip'
1313FILENAME = 'cldr-common-47.0.zip'
1414# Via https://unicode.org/Public/cldr/45/hashes/SHASUM512.txt
1515FILESUM = '3b1eb2a046dae23cf16f611f452833e2a95affb1aa2ae3fa599753d229d152577114c2ff44ca98a7f369fa41dc6f45b0d7a6647653ca79694aacfd3f3be59801'
16- BLKSIZE = 131072
1716
1817
19- def reporthook (block_count , block_size , total_size ):
20- bytes_transmitted = block_count * block_size
18+ def reporthook (bytes_transmitted , total_size ):
2119 cols = shutil .get_terminal_size ().columns
2220 buffer = 6
2321 percent = float (bytes_transmitted ) / (total_size or 1 )
@@ -31,14 +29,31 @@ def log(message):
3129 sys .stderr .write (f'{ message } \n ' )
3230
3331
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+
3449def is_good_file (filename ):
3550 if not os .path .isfile (filename ):
3651 log (f"Local copy '{ filename } ' not found" )
3752 return False
3853 h = hashlib .sha512 ()
3954 with open (filename , 'rb' ) as f :
4055 while True :
41- blk = f .read (BLKSIZE )
56+ blk = f .read (262144 )
4257 if not blk :
4358 break
4459 h .update (blk )
@@ -59,9 +74,8 @@ def main():
5974 show_progress = (False if os .environ .get ("BABEL_CLDR_NO_DOWNLOAD_PROGRESS" ) else sys .stdout .isatty ())
6075
6176 while not is_good_file (zip_path ):
62- log (f"Downloading '{ FILENAME } ' from { URL } " )
6377 tmp_path = f"{ zip_path } .tmp"
64- urlretrieve (URL , tmp_path , (reporthook if show_progress else None ))
78+ download_file (URL , tmp_path , (reporthook if show_progress else None ))
6579 os .replace (tmp_path , zip_path )
6680 changed = True
6781 print ()
0 commit comments