forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emrun.py
1879 lines (1611 loc) · 76.6 KB
/
emrun.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Copyright 2017 The Emscripten Authors. All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License. Both these licenses can be
# found in the LICENSE file.
"""emrun: Implements machinery that allows running a .html page as if it was a
standard executable file.
Usage: emrun <options> filename.html <args to program>
See emrun --help for more information
"""
# N.B. Do not introduce external dependencies to this file. It is often used
# standalone outside Emscripten directory tree.
import argparse
import atexit
import cgi
import json
import os
import platform
import re
import shlex
import shutil
import socket
import stat
import struct
import subprocess
import sys
import tempfile
import threading
import time
from operator import itemgetter
if sys.version_info.major == 2:
import SocketServer as socketserver
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
from urllib import unquote
from urlparse import urlsplit
def print_to_handle(handle, line):
print >> handle, line # noqa: F633
else:
import socketserver
from http.server import HTTPServer, SimpleHTTPRequestHandler
from urllib.parse import unquote, urlsplit
def print_to_handle(handle, line):
handle.write(line + '\n')
# Populated from cmdline params
emrun_options = None
# Represents the process object handle to the browser we opened to run the html
# page.
browser_process = None
previous_browser_processes = None
current_browser_processes = None
navigation_has_occurred = False
# Stores the browser executable that was run with --browser= parameter.
browser_exe = None
# If we have routed browser output to file with --log_stdout and/or
# --log_stderr, these track the handles.
browser_stdout_handle = sys.stdout
browser_stderr_handle = sys.stderr
# This flag tracks whether the html page has sent any stdout messages back to
# us. Used to detect whether we might have gotten detached from the browser
# process we spawned, in which case we are not able to detect when user closes
# the browser with the close button.
have_received_messages = False
# At startup print a warning message once if user did not build with --emrun.
emrun_not_enabled_nag_printed = False
# Stores the exit() code of the html page when/if it quits.
page_exit_code = None
# If this is set to a non-empty string, all processes by this name will be
# killed at exit. This is used to clean up after browsers that spawn
# subprocesses to handle the actual browser launch. For example opera has a
# launcher.exe that runs the actual opera browser. So killing browser_process
# would just kill launcher.exe and not the opera
# browser itself.
processname_killed_atexit = ""
# Using "0.0.0.0" means "all interfaces", which should allow connecting to this
# server via LAN addresses. Using "localhost" should allow only connecting from
# local computer.
default_webserver_hostname = '0.0.0.0'
# If user does not specify a --port parameter, this port is used to launch the
# server.
default_webserver_port = 6931
# Location of Android Debug Bridge executable
ADB = None
# Host OS detection to autolocate browsers and other OS-specific support needs.
WINDOWS = False
LINUX = False
MACOS = False
if os.name == 'nt':
WINDOWS = True
import winreg
elif platform.system() == 'Linux':
LINUX = True
elif platform.mac_ver()[0] != '':
MACOS = True
import plistlib
# If you are running on an OS that is not any of these, must add explicit support for it.
if not WINDOWS and not LINUX and not MACOS:
raise Exception("Unknown OS!")
# Returns wallclock time in seconds.
def tick():
# Would like to return time.clock() since it's apparently better for
# precision, but it is broken on macOS 10.10 and Python 2.7.8.
return time.time()
# Absolute wallclock time in seconds specifying when the previous HTTP stdout
# message from the page was received.
last_message_time = tick()
# Absolute wallclock time in seconds telling when we launched emrun.
page_start_time = tick()
# Stores the time of most recent http page serve.
page_last_served_time = None
def format_html(msg):
"""Returns given log message formatted to be outputted on a HTML page."""
if not msg.endswith('\n'):
msg += '\n'
msg = cgi.escape(msg)
msg = msg.replace('\r\n', '<br />').replace('\n', '<br />')
return msg
# HTTP requests are handled from separate threads - synchronize them to avoid race conditions
http_mutex = threading.RLock()
def logi(msg):
"""Prints a log message to 'info' stdout channel. Always printed.
"""
global last_message_time
with http_mutex:
if emrun_options.log_html:
sys.stdout.write(format_html(msg))
else:
print_to_handle(sys.stdout, msg)
sys.stdout.flush()
last_message_time = tick()
def logv(msg):
"""Prints a verbose log message to stdout channel.
Only shown if run with --verbose.
"""
global last_message_time
if emrun_options.verbose:
with http_mutex:
if emrun_options.log_html:
sys.stdout.write(format_html(msg))
else:
print_to_handle(sys.stdout, msg)
sys.stdout.flush()
last_message_time = tick()
def loge(msg):
"""Prints an error message to stderr channel.
"""
global last_message_time
with http_mutex:
if emrun_options.log_html:
sys.stderr.write(format_html(msg))
else:
print_to_handle(sys.stderr, msg)
sys.stderr.flush()
last_message_time = tick()
def format_eol(msg):
if WINDOWS:
msg = msg.replace('\r\n', '\n').replace('\n', '\r\n')
return msg
def browser_logi(msg):
"""Prints a message to the browser stdout output stream.
"""
global last_message_time
msg = format_eol(msg)
print_to_handle(browser_stdout_handle, msg)
browser_stdout_handle.flush()
last_message_time = tick()
def browser_loge(msg):
"""Prints a message to the browser stderr output stream.
"""
global last_message_time
msg = format_eol(msg)
print_to_handle(browser_stderr_handle, msg)
browser_stderr_handle.flush()
last_message_time = tick()
def unquote_u(source):
"""Unquotes a unicode string.
(translates ascii-encoded utf string back to utf)
"""
result = unquote(source)
if '%u' in result:
result = result.replace('%u', '\\u').decode('unicode_escape')
return result
temp_firefox_profile_dir = None
def delete_emrun_safe_firefox_profile():
"""Deletes the temporary created Firefox profile (if one exists)"""
global temp_firefox_profile_dir
if temp_firefox_profile_dir is not None:
logv('remove_tree("' + temp_firefox_profile_dir + '")')
remove_tree(temp_firefox_profile_dir)
temp_firefox_profile_dir = None
# Firefox has a lot of default behavior that makes it unsuitable for
# automated/unattended run.
# This function creates a temporary profile directory that customized Firefox
# with various flags that enable automated runs.
def create_emrun_safe_firefox_profile():
global temp_firefox_profile_dir
temp_firefox_profile_dir = tempfile.mkdtemp(prefix='temp_emrun_firefox_profile_')
with open(os.path.join(temp_firefox_profile_dir, 'prefs.js'), 'w') as f:
f.write('''
// Lift the default max 20 workers limit to something higher to avoid hangs when page needs to spawn a lot of threads.
user_pref("dom.workers.maxPerDomain", 100);
// Always allow opening popups
user_pref("browser.popups.showPopupBlocker", false);
user_pref("dom.disable_open_during_load", false);
// Don't ask user if they want to set Firefox as the default system browser
user_pref("browser.shell.checkDefaultBrowser", false);
user_pref("browser.shell.skipDefaultBrowserCheck", true);
// If automated runs crash, don't resume old tabs on the next run or show safe mode dialogs or anything else extra.
user_pref("browser.sessionstore.resume_from_crash", false);
user_pref("services.sync.prefs.sync.browser.sessionstore.restore_on_demand", false);
user_pref("browser.sessionstore.restore_on_demand", false);
user_pref("browser.sessionstore.max_resumed_crashes", -1);
user_pref("toolkit.startup.max_resumed_crashes", -1);
// Don't show the slow script dialog popup
user_pref("dom.max_script_run_time", 0);
user_pref("dom.max_chrome_script_run_time", 0);
// Don't open a home page at startup
user_pref("startup.homepage_override_url", "about:blank");
user_pref("startup.homepage_welcome_url", "about:blank");
user_pref("browser.startup.homepage", "about:blank");
// Don't try to perform browser (auto)update on the background
user_pref("app.update.auto", false);
user_pref("app.update.enabled", false);
user_pref("app.update.silent", false);
user_pref("app.update.mode", 0);
user_pref("app.update.service.enabled", false);
// Don't check compatibility with add-ons, or (auto)update them
user_pref("extensions.lastAppVersion", '');
user_pref("plugins.hide_infobar_for_outdated_plugin", true);
user_pref("plugins.update.url", '');
// Disable health reporter
user_pref("datareporting.healthreport.service.enabled", false);
// Disable crash reporter
user_pref("toolkit.crashreporter.enabled", false);
// Don't show WhatsNew on first run after every update
user_pref("browser.startup.homepage_override.mstone","ignore");
// Don't show 'know your rights' and a bunch of other nag windows at startup
user_pref("browser.rights.3.shown", true);
user_pref('devtools.devedition.promo.shown', true);
user_pref('extensions.shownSelectionUI', true);
user_pref('browser.newtabpage.introShown', true);
user_pref('browser.download.panel.shown', true);
user_pref('browser.customizemode.tip0.shown', true);
user_pref("browser.toolbarbuttons.introduced.pocket-button", true);
// Don't ask the user if they want to close the browser when there are multiple tabs.
user_pref("browser.tabs.warnOnClose", false);
// Allow the launched script window to close itself, so that we don't need to kill the browser process in order to move on.
user_pref("dom.allow_scripts_to_close_windows", true);
// Set various update timers to a large value in the future in order to not
// trigger a large mass of update HTTP traffic on each Firefox run on the clean profile.
// 2147483647 seconds since Unix epoch is sometime in the year 2038, and this is the max integer accepted by Firefox.
user_pref("app.update.lastUpdateTime.addon-background-update-timer", 2147483647);
user_pref("app.update.lastUpdateTime.background-update-timer", 2147483647);
user_pref("app.update.lastUpdateTime.blocklist-background-update-timer", 2147483647);
user_pref("app.update.lastUpdateTime.browser-cleanup-thumbnails", 2147483647);
user_pref("app.update.lastUpdateTime.experiments-update-timer", 2147483647);
user_pref("app.update.lastUpdateTime.search-engine-update-timer", 2147483647);
user_pref("app.update.lastUpdateTime.xpi-signature-verification", 2147483647);
user_pref("extensions.getAddons.cache.lastUpdate", 2147483647);
user_pref("media.gmp-eme-adobe.lastUpdate", 2147483647);
user_pref("media.gmp-gmpopenh264.lastUpdate", 2147483647);
user_pref("datareporting.healthreport.nextDataSubmissionTime", "2147483647000");
// Sending Firefox Health Report Telemetry data is not desirable, since these are automated runs.
user_pref("datareporting.healthreport.uploadEnabled", false);
user_pref("datareporting.healthreport.service.enabled", false);
user_pref("datareporting.healthreport.service.firstRun", false);
user_pref("toolkit.telemetry.enabled", false);
user_pref("toolkit.telemetry.unified", false);
user_pref("datareporting.policy.dataSubmissionEnabled", false);
user_pref("datareporting.policy.dataSubmissionPolicyBypassNotification", true);
// Allow window.dump() to print directly to console
user_pref("browser.dom.window.dump.enabled", true);
// Disable background add-ons related update & information check pings
user_pref("extensions.update.enabled", false);
user_pref("extensions.getAddons.cache.enabled", false);
// Enable wasm
user_pref("javascript.options.wasm", true);
// Enable SharedArrayBuffer (this profile is for a testing environment, so Spectre/Meltdown don't apply)
user_pref("javascript.options.shared_memory", true);
''')
if emrun_options.private_browsing:
f.write('''
// Start in private browsing mode to not cache anything to disk (everything will be wiped anyway after this run)
user_pref("browser.privatebrowsing.autostart", true);
''')
logv('create_emrun_safe_firefox_profile: Created new Firefox profile "' + temp_firefox_profile_dir + '"')
return temp_firefox_profile_dir
def is_browser_process_alive():
"""Returns whether the browser page we spawned is still running.
(note, not perfect atm, in case we are running in detached mode)
"""
# If navigation to the web page has not yet occurred, we behave as if the
# browser has not yet even loaded the page, and treat it as if the browser
# is running (as it is just starting up)
if not navigation_has_occurred:
return True
if browser_process and browser_process.poll() is None:
return True
if current_browser_processes:
try:
import psutil
for p in current_browser_processes:
if psutil.pid_exists(p['pid']):
return True
return False
except Exception:
# Fail gracefully if psutil not available
logv('psutil is not available, emrun may not be able to accurately track whether the browser process is alive or not')
# We do not have a track of the browser process ID that we spawned.
# Make an assumption that the browser process is open as long until
# the C program calls exit().
return page_exit_code is None
def kill_browser_process():
"""Kills browser_process and processname_killed_atexit. Also removes the
temporary Firefox profile that was created, if one exists.
"""
global browser_process, processname_killed_atexit, current_browser_processes
if browser_process and browser_process.poll() is None:
try:
logv('Terminating browser process pid=' + str(browser_process.pid) + '..')
browser_process.kill()
except Exception as e:
logv('Failed with error ' + str(e) + '!')
browser_process = None
# We have a hold of the target browser process explicitly, no need to resort to killall,
# so clear that record out.
processname_killed_atexit = ''
if current_browser_processes:
for pid in current_browser_processes:
try:
logv('Terminating browser process pid=' + str(pid['pid']) + '..')
os.kill(pid['pid'], 9)
except Exception as e:
logv('Failed with error ' + str(e) + '!')
current_browser_processes = None
# We have a hold of the target browser process explicitly, no need to resort to killall,
# so clear that record out.
processname_killed_atexit = ''
if len(processname_killed_atexit):
if emrun_options.android:
logv("Terminating Android app '" + processname_killed_atexit + "'.")
subprocess.call([ADB, 'shell', 'am', 'force-stop', processname_killed_atexit])
else:
logv("Terminating all processes that have string '" + processname_killed_atexit + "' in their name.")
if WINDOWS:
process_image = processname_killed_atexit if '.exe' in processname_killed_atexit else (processname_killed_atexit + '.exe')
process = subprocess.Popen(['taskkill', '/F', '/IM', process_image, '/T'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.communicate()
else:
try:
subprocess.call(['pkill', processname_killed_atexit])
except OSError:
try:
subprocess.call(['killall', processname_killed_atexit])
except OSError:
loge('Both commands pkill and killall failed to clean up the spawned browser process. Perhaps neither of these utilities is available on your system?')
delete_emrun_safe_firefox_profile()
# Clear the process name to represent that the browser is now dead.
processname_killed_atexit = ''
delete_emrun_safe_firefox_profile()
# Heuristic that attempts to search for the browser process IDs that emrun spawned.
# This depends on the assumption that no other browser process IDs have been spawned
# during the short time perioed between the time that emrun started, and the browser
# process navigated to the page.
# This heuristic is needed because all modern browsers are multiprocess systems -
# starting a browser process from command line generally launches just a "stub" spawner
# process that immediately exits.
def detect_browser_processes():
if not browser_exe:
return # Running with --no_browser, we are not binding to a spawned browser.
global current_browser_processes
logv('First navigation occurred. Identifying currently running browser processes')
running_browser_processes = list_processes_by_name(browser_exe)
def pid_existed(pid):
for proc in previous_browser_processes:
if proc['pid'] == pid:
return True
return False
for p in running_browser_processes:
logv('Detected running browser process id: ' + str(p['pid']) + ', existed already at emrun startup? ' + str(pid_existed(p['pid'])))
current_browser_processes = [p for p in running_browser_processes if not pid_existed(p['pid'])]
if len(current_browser_processes) == 0:
logv('Was unable to detect the browser process that was spawned by emrun. This may occur if the target page was opened in a tab on a browser process that already existed before emrun started up.')
# Our custom HTTP web server that will server the target page to run via .html.
# This is used so that we can load the page via a http:// URL instead of a
# file:// URL, since those wouldn't work too well unless user allowed XHR
# without CORS rules. Also, the target page will route its stdout and stderr
# back to here via HTTP requests.
class HTTPWebServer(socketserver.ThreadingMixIn, HTTPServer):
"""Log messaging arriving via HTTP can come in out of sequence. Implement a
sequencing mechanism to enforce ordered transmission."""
expected_http_seq_num = 1
# Stores messages that have arrived out of order, pending for a send as soon
# as the missing message arrives. Kept in sorted order, first element is the
# oldest message received.
http_message_queue = []
def handle_incoming_message(self, seq_num, log, data):
global have_received_messages
with http_mutex:
have_received_messages = True
if seq_num == -1:
# Message arrived without a sequence number? Just log immediately
log(data)
elif seq_num == self.expected_http_seq_num:
log(data)
self.expected_http_seq_num += 1
self.print_messages_due()
elif seq_num < self.expected_http_seq_num:
log(data)
else:
self.http_message_queue += [(seq_num, data, log)]
self.http_message_queue.sort(key=itemgetter(0))
if len(self.http_message_queue) > 16:
self.print_next_message()
# If it's been too long since we we got a message, prints out the oldest
# queued message, ignoring the proper order. This ensures that if any
# messages are actually lost, that the message queue will be orderly flushed.
def print_timed_out_messages(self):
global last_message_time
with http_mutex:
now = tick()
max_message_queue_time = 5
if len(self.http_message_queue) and now - last_message_time > max_message_queue_time:
self.print_next_message()
# Skips to printing the next message in queue now, independent of whether
# there was missed messages in the sequence numbering.
def print_next_message(self):
with http_mutex:
if len(self.http_message_queue):
self.expected_http_seq_num = self.http_message_queue[0][0]
self.print_messages_due()
# Completely flushes all out-of-order messages in the queue.
def print_all_messages(self):
with http_mutex:
while len(self.http_message_queue):
self.print_next_message()
# Prints any messages that are now due after we logged some other previous
# messages.
def print_messages_due(self):
with http_mutex:
while len(self.http_message_queue):
msg = self.http_message_queue[0]
if msg[0] == self.expected_http_seq_num:
msg[2](msg[1])
self.expected_http_seq_num += 1
self.http_message_queue.pop(0)
else:
return
def serve_forever(self, timeout=0.5):
global last_message_time, page_exit_code, emrun_not_enabled_nag_printed
self.is_running = True
self.timeout = timeout
logi('Now listening at http://%s/' % ':'.join(map(str, self.socket.getsockname())))
logv("Entering web server loop.")
while self.is_running:
now = tick()
# Did user close browser?
if emrun_options.run_browser and not is_browser_process_alive():
logv("Shutting down because browser is no longer alive")
delete_emrun_safe_firefox_profile()
if not emrun_options.serve_after_close:
logv("Browser process has shut down, quitting web server.")
self.is_running = False
# Serve HTTP
self.handle_request()
# Process message log queue
self.print_timed_out_messages()
# If web page was silent for too long without printing anything, kill process.
time_since_message = now - last_message_time
if emrun_options.silence_timeout != 0 and time_since_message > emrun_options.silence_timeout:
self.shutdown()
logi('No activity in ' + str(emrun_options.silence_timeout) + ' seconds. Quitting web server with return code ' + str(emrun_options.timeout_returncode) + '. (--silence_timeout option)')
page_exit_code = emrun_options.timeout_returncode
emrun_options.kill_exit = True
# If the page has been running too long as a whole, kill process.
time_since_start = now - page_start_time
if emrun_options.timeout != 0 and time_since_start > emrun_options.timeout:
self.shutdown()
logi('Page has not finished in ' + str(emrun_options.timeout) + ' seconds. Quitting web server with return code ' + str(emrun_options.timeout_returncode) + '. (--timeout option)')
emrun_options.kill_exit = True
page_exit_code = emrun_options.timeout_returncode
# If we detect that the page is not running with emrun enabled, print a warning message.
if not emrun_not_enabled_nag_printed and page_last_served_time is not None:
time_since_page_serve = now - page_last_served_time
if not have_received_messages and time_since_page_serve > 10:
logv('The html page you are running is not emrun-capable. Stdout, stderr and exit(returncode) capture will not work. Recompile the application with the --emrun linker flag to enable this, or pass --no_emrun_detect to emrun to hide this check.')
emrun_not_enabled_nag_printed = True
# Clean up at quit, print any leftover messages in queue.
self.print_all_messages()
logv("Web server loop done.")
def handle_error(self, request, client_address):
err = sys.exc_info()[1].args[0]
# Filter out the useless '[Errno 10054] An existing connection was forcibly
# closed by the remote host' errors that occur when we forcibly kill the
# client.
if err != 10054:
socketserver.BaseServer.handle_error(self, request, client_address)
def shutdown(self):
self.is_running = False
self.print_all_messages()
return 1
# Processes HTTP request back to the browser.
class HTTPHandler(SimpleHTTPRequestHandler):
def send_head(self):
self.protocol_version = 'HTTP/1.1'
global page_last_served_time
path = self.translate_path(self.path)
f = None
# A browser has navigated to this page - check which PID got spawned for
# the browser
global navigation_has_occurred
if not navigation_has_occurred and current_browser_processes is None:
detect_browser_processes()
navigation_has_occurred = True
if os.path.isdir(path):
if not self.path.endswith('/'):
self.send_response(301)
self.send_header("Location", self.path + "/")
self.end_headers()
return None
for index in "index.html", "index.htm":
index = os.path.join(path, index)
if os.path.isfile(index):
path = index
break
else:
# Manually implement directory listing support.
return self.list_directory(path)
try:
f = open(path, 'rb')
except IOError:
self.send_error(404, "File not found: " + path)
return None
self.send_response(200)
guess_file_type = path
# All files of type x.gz are served as gzip-compressed, which means the
# browser will transparently decode the file before passing the
# uncompressed bytes to the JS page.
# Note: In a slightly silly manner, detect files ending with "gz" and not
# ".gz", since both Unity and UE4 generate multiple files with .jsgz,
# .datagz, .memgz, .symbolsgz suffixes and so on, so everything goes.
# Note 2: If the JS application would like to receive the actual bits of a
# gzipped file, instead of having the browser decompress it immediately,
# then it can't use the suffix .gz when using emrun.
# To work around, one can use the suffix .gzip instead.
if path.lower().endswith('gz'):
self.send_header('Content-Encoding', 'gzip')
logv('Serving ' + path + ' as gzip-compressed.')
guess_file_type = guess_file_type[:-2]
if guess_file_type.endswith('.'):
guess_file_type = guess_file_type[:-1]
elif path.lower().endswith('br'):
self.send_header('Content-Encoding', 'br')
logv('Serving ' + path + ' as brotli-compressed.')
guess_file_type = guess_file_type[:-2]
if guess_file_type.endswith('.'):
guess_file_type = guess_file_type[:-1]
ctype = self.guess_type(guess_file_type)
if guess_file_type.lower().endswith('.wasm'):
ctype = 'application/wasm'
if guess_file_type.lower().endswith('.js'):
ctype = 'application/javascript'
self.send_header('Content-type', ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs.st_size))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.send_header('Cache-Control', 'no-cache, must-revalidate')
self.send_header('Connection', 'close')
self.send_header('Expires', '-1')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Cross-Origin-Opener-Policy', 'same-origin')
self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
self.send_header('Cross-Origin-Resource-Policy', 'cross-origin')
self.end_headers()
page_last_served_time = tick()
return f
def log_request(self, code):
# Filter out 200 OK messages to remove noise.
if code != 200:
SimpleHTTPRequestHandler.log_request(self, code)
def log_message(self, format, *args):
msg = '%s - - [%s] %s\n' % (self.address_string(), self.log_date_time_string(), format % args)
# Filter out 404 messages on favicon.ico not being found to remove noise.
if 'favicon.ico' not in msg:
sys.stderr.write(msg)
def do_POST(self):
self.protocol_version = 'HTTP/1.1'
global page_exit_code, have_received_messages
(_, _, path, query, _) = urlsplit(self.path)
logv('POST: "' + self.path + '" (path: "' + path + '", query: "' + query + '")')
if query.startswith('file='):
# Binary file dump/upload handling. Requests to
# "stdio.html?file=filename" will write binary data to the given file.
data = self.rfile.read(int(self.headers['Content-Length']))
filename = unquote_u(query[len('file='):])
filename = os.path.join(emrun_options.dump_out_directory, os.path.normpath(filename))
try:
os.makedirs(os.path.dirname(filename))
except OSError:
pass
with open(filename, 'wb') as fh:
fh.write(data)
logi('Wrote ' + str(len(data)) + ' bytes to file "' + filename + '".')
have_received_messages = True
elif path == '/system_info':
system_info = json.loads(get_system_info(format_json=True))
try:
browser_info = json.loads(get_browser_info(browser_exe, format_json=True))
except ValueError:
browser_info = ''
data = {'system': system_info, 'browser': browser_info}
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Cache-Control', 'no-cache, must-revalidate')
self.send_header('Connection', 'close')
self.send_header('Expires', '-1')
self.end_headers()
self.wfile.write(json.dumps(data))
return
else:
data = self.rfile.read(int(self.headers['Content-Length']))
if str is not bytes and isinstance(data, bytes):
data = data.decode('utf-8')
data = data.replace("+", " ")
data = unquote_u(data)
if data == '^pageload^': # Browser is just notifying that it has successfully launched the page.
have_received_messages = True
elif data.startswith('^exit^'):
if not emrun_options.serve_after_exit:
page_exit_code = int(data[6:])
logv('Web page has quit with a call to exit() with return code ' + str(page_exit_code) + '. Shutting down web server. Pass --serve_after_exit to keep serving even after the page terminates with exit().')
self.server.shutdown()
return
else:
# The user page sent a message with POST. Parse the message and log it to stdout/stderr.
is_stdout = False
is_stderr = False
seq_num = -1
# The html shell is expected to send messages of form ^out^(number)^(message) or ^err^(number)^(message).
if data.startswith('^err^'):
is_stderr = True
elif data.startswith('^out^'):
is_stdout = True
if is_stderr or is_stdout:
try:
i = data.index('^', 5)
seq_num = int(data[5:i])
data = data[i + 1:]
except ValueError:
pass
log = browser_loge if is_stderr else browser_logi
self.server.handle_incoming_message(seq_num, log, data)
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.send_header('Cache-Control', 'no-cache, must-revalidate')
self.send_header('Connection', 'close')
self.send_header('Expires', '-1')
self.end_headers()
self.wfile.write(b'OK')
# Returns stdout by running command with universal_newlines=True
def check_output(cmd, universal_newlines=True, *args, **kwargs):
if hasattr(subprocess, "run"):
return subprocess.run(cmd, universal_newlines=universal_newlines, stdout=subprocess.PIPE, check=True, *args, **kwargs).stdout
else:
# check_output is considered as an old API so prefer subprocess.run if possible
return subprocess.check_output(cmd, universal_newlines=universal_newlines, *args, **kwargs)
# From http://stackoverflow.com/questions/4842448/getting-processor-information-in-python
# Returns a string with something like "AMD64, Intel(R) Core(TM) i5-2557M CPU @
# 1.70GHz, Intel64 Family 6 Model 42 Stepping 7, GenuineIntel"
def get_cpu_info():
physical_cores = 1
logical_cores = 1
frequency = 0
try:
if WINDOWS:
from win32com.client import GetObject
root_winmgmts = GetObject('winmgmts:root\\cimv2')
cpus = root_winmgmts.ExecQuery('Select * from Win32_Processor')
cpu_name = cpus[0].Name + ', ' + platform.processor()
physical_cores = int(check_output(['wmic', 'cpu', 'get', 'NumberOfCores']).split('\n')[1].strip())
logical_cores = int(check_output(['wmic', 'cpu', 'get', 'NumberOfLogicalProcessors']).split('\n')[1].strip())
frequency = int(check_output(['wmic', 'cpu', 'get', 'MaxClockSpeed']).split('\n')[1].strip())
elif MACOS:
cpu_name = check_output(['sysctl', '-n', 'machdep.cpu.brand_string']).strip()
physical_cores = int(check_output(['sysctl', '-n', 'machdep.cpu.core_count']).strip())
logical_cores = int(check_output(['sysctl', '-n', 'machdep.cpu.thread_count']).strip())
frequency = int(check_output(['sysctl', '-n', 'hw.cpufrequency']).strip()) // 1000000
elif LINUX:
all_info = check_output(['cat', '/proc/cpuinfo']).strip()
for line in all_info.split("\n"):
if 'model name' in line:
cpu_name = re.sub('.*model name.*:', '', line, 1).strip()
lscpu = check_output(['lscpu'])
frequency = int(float(re.search('CPU MHz: (.*)', lscpu).group(1).strip()) + 0.5)
sockets = int(re.search(r'Socket\(s\): (.*)', lscpu).group(1).strip())
physical_cores = sockets * int(re.search(r'Core\(s\) per socket: (.*)', lscpu).group(1).strip())
logical_cores = physical_cores * int(re.search(r'Thread\(s\) per core: (.*)', lscpu).group(1).strip())
except Exception as e:
import traceback
loge(traceback.format_exc())
return {'model': 'Unknown ("' + str(e) + '")',
'physicalCores': 1,
'logicalCores': 1,
'frequency': 0
}
return {'model': platform.machine() + ', ' + cpu_name,
'physicalCores': physical_cores,
'logicalCores': logical_cores,
'frequency': frequency
}
def get_android_cpu_infoline():
lines = check_output([ADB, 'shell', 'cat', '/proc/cpuinfo']).split('\n')
processor = ''
hardware = ''
for line in lines:
if line.startswith('Processor'):
processor = line[line.find(':') + 1:].strip()
elif line.startswith('Hardware'):
hardware = line[line.find(':') + 1:].strip()
freq = int(check_output([ADB, 'shell', 'cat', '/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq']).strip()) // 1000
return 'CPU: ' + processor + ', ' + hardware + ' @ ' + str(freq) + ' MHz'
def win_get_gpu_info():
gpus = []
def find_gpu_model(model):
for gpu in gpus:
if gpu['model'] == model:
return gpu
return None
for i in range(0, 16):
try:
hHardwareReg = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'HARDWARE')
hDeviceMapReg = winreg.OpenKey(hHardwareReg, 'DEVICEMAP')
hVideoReg = winreg.OpenKey(hDeviceMapReg, 'VIDEO')
VideoCardString = winreg.QueryValueEx(hVideoReg, '\\Device\\Video' + str(i))[0]
# Get Rid of Registry/Machine from the string
VideoCardStringSplit = VideoCardString.split('\\')
ClearnVideoCardString = "\\".join(VideoCardStringSplit[3:])
# Go up one level for detailed
# VideoCardStringRoot = "\\".join(VideoCardStringSplit[3:len(VideoCardStringSplit)-1])
# Get the graphics card information
hVideoCardReg = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, ClearnVideoCardString)
try:
VideoCardDescription = winreg.QueryValueEx(hVideoCardReg, 'Device Description')[0]
except WindowsError:
VideoCardDescription = winreg.QueryValueEx(hVideoCardReg, 'DriverDesc')[0]
try:
driverVersion = winreg.QueryValueEx(hVideoCardReg, 'DriverVersion')[0]
VideoCardDescription += ', driver version ' + driverVersion
except WindowsError:
pass
try:
driverDate = winreg.QueryValueEx(hVideoCardReg, 'DriverDate')[0]
VideoCardDescription += ' (' + driverDate + ')'
except WindowsError:
pass
VideoCardMemorySize = winreg.QueryValueEx(hVideoCardReg, 'HardwareInformation.MemorySize')[0]
try:
vram = struct.unpack('l', bytes(VideoCardMemorySize))[0]
except struct.error:
vram = int(VideoCardMemorySize)
if not find_gpu_model(VideoCardDescription):
gpus += [{'model': VideoCardDescription, 'ram': vram}]
except WindowsError:
pass
return gpus
def linux_get_gpu_info():
glinfo = ''
try:
glxinfo = check_output('glxinfo')
for line in glxinfo.split("\n"):
if "OpenGL vendor string:" in line:
gl_vendor = line[len("OpenGL vendor string:"):].strip()
if "OpenGL version string:" in line:
gl_version = line[len("OpenGL version string:"):].strip()
if "OpenGL renderer string:" in line:
gl_renderer = line[len("OpenGL renderer string:"):].strip()
glinfo = gl_vendor + ' ' + gl_renderer + ', GL version ' + gl_version
except Exception as e:
logv(e)
adapterinfo = ''
try:
vgainfo = check_output(['lshw', '-C', 'display'], stderr=subprocess.PIPE)
vendor = re.search("vendor: (.*)", vgainfo).group(1).strip()
product = re.search("product: (.*)", vgainfo).group(1).strip()
description = re.search("description: (.*)", vgainfo).group(1).strip()
clock = re.search("clock: (.*)", vgainfo).group(1).strip()
adapterinfo = vendor + ' ' + product + ', ' + description + ' (' + clock + ')'
except Exception as e:
logv(e)
ram = 0
try:
vgainfo = check_output('lspci -v -s $(lspci | grep VGA | cut -d " " -f 1)', shell=True, stderr=subprocess.PIPE)
ram = int(re.search(r"\[size=([0-9]*)M\]", vgainfo).group(1)) * 1024 * 1024
except Exception as e:
logv(e)
model = (adapterinfo + ' ' + glinfo).strip()
if not model:
model = 'Unknown'
return [{'model': model, 'ram': ram}]
def macos_get_gpu_info():
gpus = []
try:
info = check_output(['system_profiler', 'SPDisplaysDataType'])
info = info.split("Chipset Model:")[1:]
for gpu in info:
model_name = gpu.split('\n')[0].strip()
bus = re.search("Bus: (.*)", gpu).group(1).strip()
memory = int(re.search("VRAM (.*?): (.*) MB", gpu).group(2).strip())
gpus += [{'model': model_name + ' (' + bus + ')', 'ram': memory * 1024 * 1024}]
return gpus
except Exception:
pass
def get_gpu_info():
if WINDOWS:
return win_get_gpu_info()
elif LINUX:
return linux_get_gpu_info()
elif MACOS:
return macos_get_gpu_info()
else:
return []
def get_executable_version(filename):
try:
if WINDOWS:
import win32api
info = win32api.GetFileVersionInfo(filename, "\\")
ms = info['FileVersionMS']
ls = info['FileVersionLS']
version = win32api.HIWORD(ms), win32api.LOWORD(ms), win32api.HIWORD(ls), win32api.LOWORD(ls)
return '.'.join(map(str, version))
elif MACOS:
plistfile = filename[0:filename.find('MacOS')] + 'Info.plist'
info = plistlib.readPlist(plistfile)
# Data in Info.plists is a bit odd, this check combo gives best information on each browser.
if 'firefox' in filename.lower():
return info['CFBundleShortVersionString']
if 'opera' in filename.lower():
return info['CFBundleVersion']
else:
return info['CFBundleShortVersionString']
elif LINUX:
if 'firefox' in filename.lower():
version = check_output([filename, '-v'])
version = version.replace('Mozilla Firefox ', '')
return version.strip()
else:
return ""
except Exception as e:
logv(e)
return ""
def get_browser_build_date(filename):
try:
if MACOS:
plistfile = filename[0:filename.find('MacOS')] + 'Info.plist'
info = plistlib.readPlist(plistfile)
# Data in Info.plists is a bit odd, this check combo gives best information on each browser.
if 'firefox' in filename.lower():
return '20' + '-'.join(map((lambda x: x.zfill(2)), info['CFBundleVersion'][2:].split('.')))
except Exception as e:
logv(e)
# No exact information about the build date, so take the last modified date of the file.
# This is not right, but assuming that one installed the browser shortly after the update was
# available, it's shooting close.
try:
return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(os.path.getmtime(filename)))
except Exception as e:
logv(e)
return '(unknown)'