4242from tools import colored_logger , diagnostics , building
4343from tools .shared import unsuffixed , unsuffixed_basename , WINDOWS , safe_copy
4444from tools .shared import run_process , read_and_preprocess , exit_with_error , DEBUG
45+ from tools .shared import read_file , write_file , read_binary
4546from tools .shared import do_replace , strip_prefix
4647from tools .response_file import substitute_response_files
4748from tools .minimal_runtime_shell import generate_minimal_runtime_html
@@ -357,7 +358,7 @@ def standardize_setting_change(key, value):
357358 filename = strip_prefix (value , '@' )
358359 if not os .path .exists (filename ):
359360 exit_with_error ('%s: file not found parsing argument: %s=%s' % (filename , key , value ))
360- value = open (filename ). read ( ).strip ()
361+ value = read_file (filename ).strip ()
361362 else :
362363 value = value .replace ('\\ ' , '\\ \\ ' )
363364
@@ -589,7 +590,7 @@ def check_human_readable_list(items):
589590
590591
591592def make_js_executable (script ):
592- src = open (script ). read ( )
593+ src = read_file (script )
593594 cmd = shared .shlex_join (config .JS_ENGINE )
594595 if not os .path .isabs (config .JS_ENGINE [0 ]):
595596 # TODO: use whereis etc. And how about non-*NIX?
@@ -1350,19 +1351,19 @@ def phase_linker_setup(options, state, newargs, settings_map):
13501351 add_link_flag (state , sys .maxsize , f )
13511352
13521353 if options .emrun :
1353- options .pre_js += open (shared .path_from_root ('src' , 'emrun_prejs.js' )). read ( ) + '\n '
1354- options .post_js += open (shared .path_from_root ('src' , 'emrun_postjs.js' )). read ( ) + '\n '
1354+ options .pre_js += read_file (shared .path_from_root ('src' , 'emrun_prejs.js' )) + '\n '
1355+ options .post_js += read_file (shared .path_from_root ('src' , 'emrun_postjs.js' )) + '\n '
13551356 # emrun mode waits on program exit
13561357 settings .EXIT_RUNTIME = 1
13571358
13581359 if options .cpu_profiler :
1359- options .post_js += open (shared .path_from_root ('src' , 'cpuprofiler.js' )). read ( ) + '\n '
1360+ options .post_js += read_file (shared .path_from_root ('src' , 'cpuprofiler.js' )) + '\n '
13601361
13611362 if options .memory_profiler :
13621363 settings .MEMORYPROFILER = 1
13631364
13641365 if options .thread_profiler :
1365- options .post_js += open (shared .path_from_root ('src' , 'threadprofiler.js' )). read ( ) + '\n '
1366+ options .post_js += read_file (shared .path_from_root ('src' , 'threadprofiler.js' )) + '\n '
13661367
13671368 if options .memory_init_file is None :
13681369 options .memory_init_file = settings .OPT_LEVEL >= 2
@@ -2527,7 +2528,7 @@ def phase_source_transforms(options, target):
25272528 # Apply pre and postjs files
25282529 if final_js and (options .pre_js or options .post_js ):
25292530 logger .debug ('applying pre/postjses' )
2530- src = open (final_js ). read ( )
2531+ src = read_file (final_js )
25312532 final_js += '.pp.js'
25322533 with open (final_js , 'w' ) as f :
25332534 # pre-js code goes right after the Module integration code (so it
@@ -2553,9 +2554,9 @@ def phase_memory_initializer(memfile):
25532554 # is set the memory initializer url.
25542555 global final_js
25552556
2556- src = open (final_js ). read ( )
2557+ src = read_file (final_js )
25572558 src = do_replace (src , '// {{MEM_INITIALIZER}}' , 'var memoryInitializer = "%s";' % os .path .basename (memfile ))
2558- open (final_js + '.mem.js' , 'w' ). write ( src )
2559+ write_file (final_js + '.mem.js' , src )
25592560 final_js += '.mem.js'
25602561
25612562
@@ -2565,9 +2566,9 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
25652566
25662567 # Remove some trivial whitespace
25672568 # TODO: do not run when compress has already been done on all parts of the code
2568- # src = open (final_js).read( )
2569+ # src = read_file (final_js)
25692570 # src = re.sub(r'\n+[ \n]*\n+', '\n', src)
2570- # open (final_js, 'w').write( src)
2571+ # write_file (final_js, src)
25712572
25722573 if settings .USE_PTHREADS :
25732574 target_dir = os .path .dirname (os .path .abspath (target ))
@@ -2578,7 +2579,7 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
25782579 # Minify the worker.js file in optimized builds
25792580 if (settings .OPT_LEVEL >= 1 or settings .SHRINK_LEVEL >= 1 ) and not settings .DEBUG_LEVEL :
25802581 minified_worker = building .acorn_optimizer (worker_output , ['minifyWhitespace' ], return_output = True )
2581- open (worker_output , 'w' ). write ( minified_worker )
2582+ write_file (worker_output , minified_worker )
25822583
25832584 # track files that will need native eols
25842585 generated_text_files_with_native_eols = []
@@ -2599,16 +2600,15 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
25992600 # Unmangle previously mangled `import.meta` references in both main code and libraries.
26002601 # See also: `preprocess` in parseTools.js.
26012602 if settings .EXPORT_ES6 and settings .USE_ES6_IMPORT_META :
2602- src = open (final_js ). read ( )
2603+ src = read_file (final_js )
26032604 final_js += '.esmeta.js'
2604- with open (final_js , 'w' ) as f :
2605- f .write (src .replace ('EMSCRIPTEN$IMPORT$META' , 'import.meta' ))
2605+ write_file (final_js , src .replace ('EMSCRIPTEN$IMPORT$META' , 'import.meta' ))
26062606 save_intermediate ('es6-import-meta' )
26072607
26082608 # Apply pre and postjs files
26092609 if options .extern_pre_js or options .extern_post_js :
26102610 logger .debug ('applying extern pre/postjses' )
2611- src = open (final_js ). read ( )
2611+ src = read_file (final_js )
26122612 final_js += '.epp.js'
26132613 with open (final_js , 'w' ) as f :
26142614 f .write (fix_windows_newlines (options .extern_pre_js ))
@@ -2753,13 +2753,13 @@ def consume_arg_file():
27532753 elif check_arg ('--js-transform' ):
27542754 options .js_transform = consume_arg ()
27552755 elif check_arg ('--pre-js' ):
2756- options .pre_js += open (consume_arg_file ()). read ( ) + '\n '
2756+ options .pre_js += read_file (consume_arg_file ()) + '\n '
27572757 elif check_arg ('--post-js' ):
2758- options .post_js += open (consume_arg_file ()). read ( ) + '\n '
2758+ options .post_js += read_file (consume_arg_file ()) + '\n '
27592759 elif check_arg ('--extern-pre-js' ):
2760- options .extern_pre_js += open (consume_arg_file ()). read ( ) + '\n '
2760+ options .extern_pre_js += read_file (consume_arg_file ()) + '\n '
27612761 elif check_arg ('--extern-post-js' ):
2762- options .extern_post_js += open (consume_arg_file ()). read ( ) + '\n '
2762+ options .extern_post_js += read_file (consume_arg_file ()) + '\n '
27632763 elif check_arg ('--compiler-wrapper' ):
27642764 config .COMPILER_WRAPPER = consume_arg ()
27652765 elif check_flag ('--post-link' ):
@@ -3177,10 +3177,10 @@ def run_closure_compiler():
31773177
31783178 # replace placeholder strings with correct subresource locations
31793179 if final_js and settings .SINGLE_FILE and not settings .WASM2JS :
3180- js = open (final_js ). read ( )
3180+ js = read_file (final_js )
31813181
31823182 if settings .MINIMAL_RUNTIME :
3183- js = do_replace (js , '<<< WASM_BINARY_DATA >>>' , base64_encode (open (wasm_target , 'rb' ). read ( )))
3183+ js = do_replace (js , '<<< WASM_BINARY_DATA >>>' , base64_encode (read_binary (wasm_target )))
31843184 else :
31853185 js = do_replace (js , '<<< WASM_BINARY_FILE >>>' , shared .JS .get_subresource_location (wasm_target ))
31863186 shared .try_delete (wasm_target )
@@ -3191,7 +3191,7 @@ def run_closure_compiler():
31913191def modularize ():
31923192 global final_js
31933193 logger .debug ('Modularizing, assigning to var ' + settings .EXPORT_NAME )
3194- src = open (final_js ). read ( )
3194+ src = read_file (final_js )
31953195
31963196 return_value = settings .EXPORT_NAME
31973197 if settings .WASM_ASYNC_COMPILATION :
@@ -3386,7 +3386,7 @@ def generate_traditional_runtime_html(target, options, js_target, target_basenam
33863386 if settings .SINGLE_FILE :
33873387 js_contents = script .inline or ''
33883388 if script .src :
3389- js_contents += open (js_target ). read ( )
3389+ js_contents += read_file (js_target )
33903390 shared .try_delete (js_target )
33913391 script .src = None
33923392 script .inline = js_contents
@@ -3479,13 +3479,13 @@ def generate_worker_js(target, js_target, target_basename):
34793479 proxy_worker_filename = (settings .PROXY_TO_WORKER_FILENAME or worker_target_basename ) + '.js'
34803480
34813481 target_contents = worker_js_script (proxy_worker_filename )
3482- open (target , 'w' ). write ( target_contents )
3482+ write_file (target , target_contents )
34833483
34843484
34853485def worker_js_script (proxy_worker_filename ):
3486- web_gl_client_src = open (shared .path_from_root ('src' , 'webGLClient.js' )). read ( )
3487- idb_store_src = open (shared .path_from_root ('src' , 'IDBStore.js' )). read ( )
3488- proxy_client_src = open (shared .path_from_root ('src' , 'proxyClient.js' )). read ( )
3486+ web_gl_client_src = read_file (shared .path_from_root ('src' , 'webGLClient.js' ))
3487+ idb_store_src = read_file (shared .path_from_root ('src' , 'IDBStore.js' ))
3488+ proxy_client_src = read_file (shared .path_from_root ('src' , 'proxyClient.js' ))
34893489 proxy_client_src = do_replace (proxy_client_src , '{{{ filename }}}' , proxy_worker_filename )
34903490 proxy_client_src = do_replace (proxy_client_src , '{{{ IDBStore.js }}}' , idb_store_src )
34913491 return web_gl_client_src + '\n ' + proxy_client_src
0 commit comments