12
12
See emrun --help for more information
13
13
"""
14
14
15
+ # N.B. Do not introduce external dependencies to this file. It is often used
16
+ # standalone outside Emscripten directory tree.
15
17
import argparse
16
18
import atexit
17
19
import cgi
20
22
import platform
21
23
import re
22
24
import shlex
25
+ import shutil
23
26
import socket
27
+ import stat
24
28
import struct
25
29
import subprocess
26
30
import sys
29
33
import time
30
34
from operator import itemgetter
31
35
32
- from tools import shared
33
-
34
36
if sys .version_info .major == 2 :
35
37
import SocketServer as socketserver
36
38
from BaseHTTPServer import HTTPServer
37
39
from SimpleHTTPServer import SimpleHTTPRequestHandler
38
40
from urllib import unquote
39
41
from urlparse import urlsplit
42
+
43
+ def print_to_handle (handle , line ):
44
+ print >> handle , line # noqa: F633
40
45
else :
41
46
import socketserver
42
47
from http .server import HTTPServer , SimpleHTTPRequestHandler
43
48
from urllib .parse import unquote , urlsplit
44
49
50
+ def print_to_handle (handle , line ):
51
+ handle .write (line + '\n ' )
52
+
45
53
# Populated from cmdline params
46
54
emrun_options = None
47
55
@@ -151,7 +159,7 @@ def logi(msg):
151
159
if emrun_options .log_html :
152
160
sys .stdout .write (format_html (msg ))
153
161
else :
154
- print ( msg , file = sys .stdout )
162
+ print_to_handle ( sys .stdout , msg )
155
163
sys .stdout .flush ()
156
164
last_message_time = tick ()
157
165
@@ -166,7 +174,7 @@ def logv(msg):
166
174
if emrun_options .log_html :
167
175
sys .stdout .write (format_html (msg ))
168
176
else :
169
- print ( msg , file = sys .stdout )
177
+ print_to_handle ( sys .stdout , msg )
170
178
sys .stdout .flush ()
171
179
last_message_time = tick ()
172
180
@@ -179,7 +187,7 @@ def loge(msg):
179
187
if emrun_options .log_html :
180
188
sys .stderr .write (format_html (msg ))
181
189
else :
182
- print ( msg , file = sys .stderr )
190
+ print_to_handle ( sys .stderr , msg )
183
191
sys .stderr .flush ()
184
192
last_message_time = tick ()
185
193
@@ -195,7 +203,7 @@ def browser_logi(msg):
195
203
"""
196
204
global last_message_time
197
205
msg = format_eol (msg )
198
- print ( msg , file = browser_stdout_handle )
206
+ print_to_handle ( browser_stdout_handle , msg )
199
207
browser_stdout_handle .flush ()
200
208
last_message_time = tick ()
201
209
@@ -205,7 +213,7 @@ def browser_loge(msg):
205
213
"""
206
214
global last_message_time
207
215
msg = format_eol (msg )
208
- print ( msg , file = browser_stderr_handle )
216
+ print_to_handle ( browser_stderr_handle , msg )
209
217
browser_stderr_handle .flush ()
210
218
last_message_time = tick ()
211
219
@@ -228,7 +236,7 @@ def delete_emrun_safe_firefox_profile():
228
236
global temp_firefox_profile_dir
229
237
if temp_firefox_profile_dir is not None :
230
238
logv ('remove_tree("' + temp_firefox_profile_dir + '")' )
231
- shared . try_delete (temp_firefox_profile_dir )
239
+ remove_tree (temp_firefox_profile_dir )
232
240
temp_firefox_profile_dir = None
233
241
234
242
@@ -684,7 +692,7 @@ def do_POST(self):
684
692
pass
685
693
filename = os .path .join (dump_out_directory , os .path .normpath (filename ))
686
694
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 + '".' )
688
696
have_received_messages = True
689
697
elif path == '/system_info' :
690
698
system_info = json .loads (get_system_info (format_json = True ))
@@ -788,7 +796,7 @@ def get_cpu_info():
788
796
logical_cores = physical_cores * int (re .search (r'Thread\(s\) per core: (.*)' , lscpu ).group (1 ).strip ())
789
797
except Exception as e :
790
798
import traceback
791
- print (traceback .format_exc ())
799
+ loge (traceback .format_exc ())
792
800
return {'model' : 'Unknown ("' + str (e ) + '")' ,
793
801
'physicalCores' : 1 ,
794
802
'logicalCores' : 1 ,
@@ -1347,6 +1355,21 @@ def subprocess_env():
1347
1355
return e
1348
1356
1349
1357
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
+
1350
1373
def get_system_info (format_json ):
1351
1374
if emrun_options .android :
1352
1375
if format_json :
@@ -1658,7 +1681,6 @@ def run():
1658
1681
1659
1682
url = url .replace ('&' , '\\ &' )
1660
1683
browser = [ADB , 'shell' , 'am' , 'start' , '-a' , 'android.intent.action.VIEW' , '-n' , browser_app , '-d' , url ]
1661
- print (str (browser ))
1662
1684
processname_killed_atexit = browser_app [:browser_app .find ('/' )]
1663
1685
else : # Launching a web page on local system.
1664
1686
if options .browser :
@@ -1714,7 +1736,7 @@ def run():
1714
1736
profile_dir = create_emrun_safe_firefox_profile ()
1715
1737
1716
1738
def run (cmd ):
1717
- print (str (cmd ))
1739
+ logi (str (cmd ))
1718
1740
subprocess .call (cmd )
1719
1741
1720
1742
run (['adb' , 'shell' , 'rm' , '-rf' , '/mnt/sdcard/safe_firefox_profile' ])
@@ -1732,16 +1754,16 @@ def run(cmd):
1732
1754
1733
1755
if options .system_info :
1734
1756
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 ))
1736
1758
1737
1759
if options .browser_info :
1738
1760
if options .android :
1739
1761
if options .json :
1740
- print (json .dumps ({'browser' : 'Android ' + browser_app }, indent = 2 ))
1762
+ logi (json .dumps ({'browser' : 'Android ' + browser_app }, indent = 2 ))
1741
1763
else :
1742
1764
logi ('Browser: Android ' + browser_app )
1743
1765
else :
1744
- print (get_browser_info (browser_exe , format_json = options .json ))
1766
+ logi (get_browser_info (browser_exe , format_json = options .json ))
1745
1767
1746
1768
# Suppress run warning if requested.
1747
1769
if options .no_emrun_detect :
0 commit comments