Skip to content

Commit 4778850

Browse files
authored
Partially revert commit that broke using emrun.py outside the tree (emscripten-core#13412)
* Partially revert commit that broke using emrun.py outside the tree: commit 4fc6505 Author: Sam Clegg <[email protected]> Date: Sun Sep 13 19:05:11 2020 -0700 Use shared wrappers for shutils. NFC. (emscripten-core#12200) We we not being consistent in our use of safe_copy and safe_move and try_delete. * Add comment. * Make emrun.py python 2 compatible again * flake
1 parent 229e9ea commit 4778850

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

emrun.py

+37-15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
See emrun --help for more information
1313
"""
1414

15+
# N.B. Do not introduce external dependencies to this file. It is often used
16+
# standalone outside Emscripten directory tree.
1517
import argparse
1618
import atexit
1719
import cgi
@@ -20,7 +22,9 @@
2022
import platform
2123
import re
2224
import shlex
25+
import shutil
2326
import socket
27+
import stat
2428
import struct
2529
import subprocess
2630
import sys
@@ -29,19 +33,23 @@
2933
import time
3034
from operator import itemgetter
3135

32-
from tools import shared
33-
3436
if sys.version_info.major == 2:
3537
import SocketServer as socketserver
3638
from BaseHTTPServer import HTTPServer
3739
from SimpleHTTPServer import SimpleHTTPRequestHandler
3840
from urllib import unquote
3941
from urlparse import urlsplit
42+
43+
def print_to_handle(handle, line):
44+
print >> handle, line # noqa: F633
4045
else:
4146
import socketserver
4247
from http.server import HTTPServer, SimpleHTTPRequestHandler
4348
from urllib.parse import unquote, urlsplit
4449

50+
def print_to_handle(handle, line):
51+
handle.write(line + '\n')
52+
4553
# Populated from cmdline params
4654
emrun_options = None
4755

@@ -151,7 +159,7 @@ def logi(msg):
151159
if emrun_options.log_html:
152160
sys.stdout.write(format_html(msg))
153161
else:
154-
print(msg, file=sys.stdout)
162+
print_to_handle(sys.stdout, msg)
155163
sys.stdout.flush()
156164
last_message_time = tick()
157165

@@ -166,7 +174,7 @@ def logv(msg):
166174
if emrun_options.log_html:
167175
sys.stdout.write(format_html(msg))
168176
else:
169-
print(msg, file=sys.stdout)
177+
print_to_handle(sys.stdout, msg)
170178
sys.stdout.flush()
171179
last_message_time = tick()
172180

@@ -179,7 +187,7 @@ def loge(msg):
179187
if emrun_options.log_html:
180188
sys.stderr.write(format_html(msg))
181189
else:
182-
print(msg, file=sys.stderr)
190+
print_to_handle(sys.stderr, msg)
183191
sys.stderr.flush()
184192
last_message_time = tick()
185193

@@ -195,7 +203,7 @@ def browser_logi(msg):
195203
"""
196204
global last_message_time
197205
msg = format_eol(msg)
198-
print(msg, file=browser_stdout_handle)
206+
print_to_handle(browser_stdout_handle, msg)
199207
browser_stdout_handle.flush()
200208
last_message_time = tick()
201209

@@ -205,7 +213,7 @@ def browser_loge(msg):
205213
"""
206214
global last_message_time
207215
msg = format_eol(msg)
208-
print(msg, file=browser_stderr_handle)
216+
print_to_handle(browser_stderr_handle, msg)
209217
browser_stderr_handle.flush()
210218
last_message_time = tick()
211219

@@ -228,7 +236,7 @@ def delete_emrun_safe_firefox_profile():
228236
global temp_firefox_profile_dir
229237
if temp_firefox_profile_dir is not None:
230238
logv('remove_tree("' + temp_firefox_profile_dir + '")')
231-
shared.try_delete(temp_firefox_profile_dir)
239+
remove_tree(temp_firefox_profile_dir)
232240
temp_firefox_profile_dir = None
233241

234242

@@ -684,7 +692,7 @@ def do_POST(self):
684692
pass
685693
filename = os.path.join(dump_out_directory, os.path.normpath(filename))
686694
open(filename, 'wb').write(data)
687-
print('Wrote ' + str(len(data)) + ' bytes to file "' + filename + '".')
695+
logi('Wrote ' + str(len(data)) + ' bytes to file "' + filename + '".')
688696
have_received_messages = True
689697
elif path == '/system_info':
690698
system_info = json.loads(get_system_info(format_json=True))
@@ -788,7 +796,7 @@ def get_cpu_info():
788796
logical_cores = physical_cores * int(re.search(r'Thread\(s\) per core: (.*)', lscpu).group(1).strip())
789797
except Exception as e:
790798
import traceback
791-
print(traceback.format_exc())
799+
loge(traceback.format_exc())
792800
return {'model': 'Unknown ("' + str(e) + '")',
793801
'physicalCores': 1,
794802
'logicalCores': 1,
@@ -1347,6 +1355,21 @@ def subprocess_env():
13471355
return e
13481356

13491357

1358+
# Removes a directory tree even if it was readonly, and doesn't throw exception on failure.
1359+
def remove_tree(d):
1360+
os.chmod(d, stat.S_IWRITE)
1361+
try:
1362+
def remove_readonly_and_try_again(func, path, exc_info):
1363+
if not (os.stat(path).st_mode & stat.S_IWRITE):
1364+
os.chmod(path, stat.S_IWRITE)
1365+
func(path)
1366+
else:
1367+
raise
1368+
shutil.rmtree(d, onerror=remove_readonly_and_try_again)
1369+
except Exception:
1370+
pass
1371+
1372+
13501373
def get_system_info(format_json):
13511374
if emrun_options.android:
13521375
if format_json:
@@ -1658,7 +1681,6 @@ def run():
16581681

16591682
url = url.replace('&', '\\&')
16601683
browser = [ADB, 'shell', 'am', 'start', '-a', 'android.intent.action.VIEW', '-n', browser_app, '-d', url]
1661-
print(str(browser))
16621684
processname_killed_atexit = browser_app[:browser_app.find('/')]
16631685
else: # Launching a web page on local system.
16641686
if options.browser:
@@ -1714,7 +1736,7 @@ def run():
17141736
profile_dir = create_emrun_safe_firefox_profile()
17151737

17161738
def run(cmd):
1717-
print(str(cmd))
1739+
logi(str(cmd))
17181740
subprocess.call(cmd)
17191741

17201742
run(['adb', 'shell', 'rm', '-rf', '/mnt/sdcard/safe_firefox_profile'])
@@ -1732,16 +1754,16 @@ def run(cmd):
17321754

17331755
if options.system_info:
17341756
logi('Time of run: ' + time.strftime("%x %X"))
1735-
print(get_system_info(format_json=options.json))
1757+
logi(get_system_info(format_json=options.json))
17361758

17371759
if options.browser_info:
17381760
if options.android:
17391761
if options.json:
1740-
print(json.dumps({'browser': 'Android ' + browser_app}, indent=2))
1762+
logi(json.dumps({'browser': 'Android ' + browser_app}, indent=2))
17411763
else:
17421764
logi('Browser: Android ' + browser_app)
17431765
else:
1744-
print(get_browser_info(browser_exe, format_json=options.json))
1766+
logi(get_browser_info(browser_exe, format_json=options.json))
17451767

17461768
# Suppress run warning if requested.
17471769
if options.no_emrun_detect:

0 commit comments

Comments
 (0)