77import hashlib
88import zipfile
99import subprocess
10- try :
11- from urllib .request import urlretrieve
12- except ImportError :
13- from urllib import urlretrieve
10+ from urllib .request import urlretrieve
1411
1512
1613URL = 'http://unicode.org/Public/cldr/41/cldr-common-41.0.zip'
2017BLKSIZE = 131072
2118
2219
23- def get_terminal_width ():
24- try :
25- import fcntl
26- import termios
27- import struct
28- fd = sys .stdin .fileno ()
29- cr = struct .unpack ('hh' , fcntl .ioctl (fd , termios .TIOCGWINSZ , '1234' ))
30- return cr [1 ]
31- except Exception :
32- return 80
33-
34-
3520def reporthook (block_count , block_size , total_size ):
3621 bytes_transmitted = block_count * block_size
37- cols = get_terminal_width ()
22+ cols = shutil . get_terminal_size (). columns
3823 buffer = 6
3924 percent = float (bytes_transmitted ) / (total_size or 1 )
4025 done = int (percent * (cols - buffer ))
41- sys .stdout .write ('\r ' )
42- sys .stdout .write (' ' + '=' * done + ' ' * (cols - done - buffer ))
43- sys .stdout .write ('% 4d%%' % (percent * 100 ))
26+ bar = ('=' * done ).ljust (cols - buffer )
27+ sys .stdout .write (f'\r { bar } { int (percent * 100 ): 4d} %' )
4428 sys .stdout .flush ()
4529
4630
47- def log (message , * args ):
48- if args :
49- message = message % args
50- sys .stderr .write (message + '\n ' )
31+ def log (message ):
32+ sys .stderr .write (f'{ message } \n ' )
5133
5234
5335def is_good_file (filename ):
5436 if not os .path .isfile (filename ):
55- log (' Local copy \' %s \ ' not found' , filename )
37+ log (f" Local copy ' { filename } ' not found" )
5638 return False
5739 h = hashlib .sha512 ()
5840 with open (filename , 'rb' ) as f :
@@ -63,8 +45,7 @@ def is_good_file(filename):
6345 h .update (blk )
6446 digest = h .hexdigest ()
6547 if digest != FILESUM :
66- raise RuntimeError ('Checksum mismatch: %r != %r'
67- % (digest , FILESUM ))
48+ raise RuntimeError (f'Checksum mismatch: { digest !r} != { FILESUM !r} ' )
6849 else :
6950 return True
7051
@@ -79,20 +60,20 @@ def main():
7960 show_progress = (False if os .environ .get ("BABEL_CLDR_NO_DOWNLOAD_PROGRESS" ) else sys .stdout .isatty ())
8061
8162 while not is_good_file (zip_path ):
82- log ("Downloading '%s ' from %s" , FILENAME , URL )
83- if os . path . isfile ( zip_path ):
84- os . remove ( zip_path )
85- urlretrieve ( URL , zip_path , ( reporthook if show_progress else None ) )
63+ log (f "Downloading '{ FILENAME } ' from { URL } " )
64+ tmp_path = f" { zip_path } .tmp"
65+ urlretrieve ( URL , tmp_path , ( reporthook if show_progress else None ) )
66+ os . replace ( tmp_path , zip_path )
8667 changed = True
8768 print ()
8869 common_path = os .path .join (cldr_path , 'common' )
8970
9071 if changed or not os .path .isdir (common_path ):
9172 if os .path .isdir (common_path ):
92- log (' Deleting old CLDR checkout in \' %s \' ' , cldr_path )
73+ log (f" Deleting old CLDR checkout in ' { cldr_path } '" )
9374 shutil .rmtree (common_path )
9475
95- log (' Extracting CLDR to \' %s \' ' , cldr_path )
76+ log (f" Extracting CLDR to ' { cldr_path } '" )
9677 with contextlib .closing (zipfile .ZipFile (zip_path )) as z :
9778 z .extractall (cldr_path )
9879
0 commit comments