1- import os
2- import sys
31import shutil
42import json
53import gzip
4+ import pathlib
65
76
87class App :
98 def __init__ (self ) -> None :
10- self .encoding = sys .getdefaultencoding ()
11- self .cwd = os .path .dirname (__file__ )
12- self .out_dir = os .path .join (self .cwd , 'Release' )
13- self .assets_dir = os .path .join (self .cwd , 'gdl-assets' )
14- if not os .path .isdir (self .assets_dir ):
15- raise FileNotFoundError (f'Could not found gdl-assets at { self .assets_dir } ' )
16- if os .path .isdir (self .out_dir ):
9+ self .cwd = pathlib .Path (__file__ ).parent .resolve ()
10+ self .parent_cwd = self .cwd .parent .resolve ()
11+ self .out_dir = self .parent_cwd .joinpath ('Release' )
12+ self .assets_dir = self .parent_cwd .joinpath ('gdl-assets' )
13+ if not self .assets_dir .is_dir ():
14+ raise FileNotFoundError (
15+ f'Could not find gdl-assets at { self .assets_dir } ' )
16+ if self .out_dir .is_dir ():
1717 shutil .rmtree (self .out_dir )
18- os . mkdir ( self .out_dir )
18+ self .out_dir . mkdir ( )
1919 self .ball_json = {}
2020 self .ball = b''
2121 self .append_gdl_assets ()
@@ -25,53 +25,58 @@ def __init__(self) -> None:
2525 def append_gdl_assets (self ) -> None :
2626 self .ball_json ['gdl-assets' ] = []
2727 self .ball_json ['gdl-assets-size' ] = 0
28- for fn in os .listdir (self .assets_dir ):
29- fp = os .path .join (self .assets_dir , fn )
30- if fn .startswith ('.' ) or not os .path .isfile (fp ):
28+ for fn in self .assets_dir .iterdir ():
29+ if not fn .is_file ():
3130 continue
32- f = open (fp , 'rb' )
33- content = f .read ()
34- f .close ()
31+
32+ with open (fn , 'rb' ) as f :
33+ content = f .read ()
34+
3535 self .ball_json ['gdl-assets' ].append ({
36- 'fn' : fn ,
36+ 'fn' : fn . name ,
3737 'size' : len (content )
3838 })
39+
3940 self .ball_json ['gdl-assets-size' ] += len (content )
4041 self .ball += content
4142
4243 def append_gdl_binaries (self ) -> None :
4344 self .ball_json ['gdl-binaries' ] = []
44- for fn in os .listdir (self .cwd ) + ["../ru_ru.json" ]:
45- fp = os .path .join (self .cwd , fn )
46- fext = fn .split ('.' )[- 1 ].lower ()
47- if not os .path .isfile (fp ) or fext in ('py' , 'md' ):
48- # if fn.startswith('.') or not os.path.isfile(fp) or fext in ('py', 'md'):
45+ for fn in list (self .cwd .iterdir ()) + [self .parent_cwd .joinpath ("ru_ru.json" )]:
46+ if not fn .is_file () or fn .suffix .lower () in ['.py' , '.md' ]:
4947 continue
50- f = open (fp , 'rb' )
51- content = f .read ()
52- f .close ()
48+
49+ with open (fn , 'rb' ) as f :
50+ content = f .read ()
51+
5352 self .ball_json ['gdl-binaries' ].append ({
54- 'fn' : fn .replace ( "../" , "" ) ,
53+ 'fn' : fn .name ,
5554 'size' : len (content )
5655 })
56+
5757 self .ball += content
5858
5959 def write_out (self ) -> None :
6060 compressed = gzip .compress (self .ball )
61+
6162 self .ball_json ['u-size' ] = len (self .ball )
6263 self .ball_json ['size' ] = len (compressed )
63- json_f = open (os .path .join (self .out_dir , 'gdl-binaries.json' ), 'w' , encoding = self .encoding )
64- json_f .write (json .dumps (self .ball_json , ensure_ascii = False , indent = 4 ))
65- json_f .close ()
66- gzip_f = open (os .path .join (self .out_dir , 'gdl-binaries.bin.gzip' ), 'wb' )
67- gzip_f .write (compressed )
68- gzip_f .close ()
64+
65+ with open (self .out_dir .joinpath ('gdl-binaries.json' ), 'w' , encoding = 'utf-8' ) as f :
66+ json .dump (self .ball_json , f , ensure_ascii = False , indent = 4 )
67+
68+ with open (self .out_dir .joinpath ('gdl-binaries.bin.gzip' ), 'wb' ) as f :
69+ f .write (compressed )
70+
6971 self .print_stats (compressed )
7072
7173 def print_stats (self , compressed : bytes ) -> None :
72- print (f'Uncompressed size: { round (len (self .ball ) / 1024 / 1024 * 100 ) / 100 } MB' )
73- print (f'Compressed size: { round (len (compressed ) / 1024 / 1024 * 100 ) / 100 } MB' )
74- print (f'[{ round (len (compressed ) / len (self .ball ) * 100 )} % of 100%] or' , end = ' ' )
74+ print (
75+ f'Uncompressed size: { round (len (self .ball ) / 1024 / 1024 * 100 ) / 100 } MB' )
76+ print (
77+ f'Compressed size: { round (len (compressed ) / 1024 / 1024 * 100 ) / 100 } MB' )
78+ print (
79+ f'[{ round (len (compressed ) / len (self .ball ) * 100 )} % of 100%] or' , end = ' ' )
7580 print (f'[100% of { round (len (self .ball ) / len (compressed ) * 100 )} %]' )
7681
7782
0 commit comments