Skip to content

Commit 03ebecf

Browse files
committed
[emrun] Use GNU command line flag names
For example `--no-browser` vs `--no_browser`. Continue to support the old style but don't advertise these.
1 parent e6be4f5 commit 03ebecf

File tree

2 files changed

+57
-48
lines changed

2 files changed

+57
-48
lines changed

emrun.py

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
# Stores the browser executable that was run with --browser= parameter.
5959
browser_exe = None
6060

61-
# If we have routed browser output to file with --log_stdout and/or
62-
# --log_stderr, these track the handles.
61+
# If we have routed browser output to file with --log-stdout and/or
62+
# --log-stderr, these track the handles.
6363
browser_stdout_handle = sys.stdout
6464
browser_stderr_handle = sys.stderr
6565

@@ -408,7 +408,7 @@ def kill_browser_process():
408408
# process that immediately exits.
409409
def detect_browser_processes():
410410
if not browser_exe:
411-
return # Running with --no_browser, we are not binding to a spawned browser.
411+
return # Running with --no-browser, we are not binding to a spawned browser.
412412

413413
global current_browser_processes
414414
logv('First navigation occurred. Identifying currently running browser processes')
@@ -526,7 +526,7 @@ def serve_forever(self, timeout=0.5):
526526
time_since_message = now - last_message_time
527527
if emrun_options.silence_timeout != 0 and time_since_message > emrun_options.silence_timeout:
528528
self.shutdown()
529-
logi('No activity in ' + str(emrun_options.silence_timeout) + ' seconds. Quitting web server with return code ' + str(emrun_options.timeout_returncode) + '. (--silence_timeout option)')
529+
logi('No activity in ' + str(emrun_options.silence_timeout) + ' seconds. Quitting web server with return code ' + str(emrun_options.timeout_returncode) + '. (--silence-timeout option)')
530530
page_exit_code = emrun_options.timeout_returncode
531531
emrun_options.kill_exit = True
532532

@@ -542,7 +542,7 @@ def serve_forever(self, timeout=0.5):
542542
if not emrun_not_enabled_nag_printed and page_last_served_time is not None:
543543
time_since_page_serve = now - page_last_served_time
544544
if not have_received_messages and time_since_page_serve > 10:
545-
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.')
545+
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.')
546546
emrun_not_enabled_nag_printed = True
547547

548548
# Clean up at quit, print any leftover messages in queue.
@@ -703,7 +703,7 @@ def do_POST(self):
703703
elif data.startswith('^exit^'):
704704
if not emrun_options.serve_after_exit:
705705
page_exit_code = int(data[6:])
706-
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().')
706+
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().')
707707
self.server.shutdown()
708708
return
709709
else:
@@ -1155,7 +1155,7 @@ def win_get_default_browser():
11551155
except WindowsError:
11561156
logv("Unable to find default browser key in Windows registry. Trying fallback.")
11571157

1158-
# Fall back to 'start "" %1', which we have to treat as if user passed --serve_forever, since
1158+
# Fall back to 'start "" %1', which we have to treat as if user passed --serve-forever, since
11591159
# for some reason, we are not able to detect when the browser closes when this is passed.
11601160
#
11611161
# If the first argument to 'start' is quoted, then 'start' will create a new cmd.exe window with
@@ -1444,44 +1444,44 @@ def list_processes_by_name(exe_full_path):
14441444
"""
14451445

14461446

1447-
def parse_args():
1447+
def parse_args(args):
14481448
parser = argparse.ArgumentParser(usage=usage_str)
14491449

1450-
parser.add_argument('--kill_start', action='store_true',
1450+
parser.add_argument('--kill-start', action='store_true',
14511451
help='If true, any previously running instances of '
14521452
'the target browser are killed before starting.')
14531453

1454-
parser.add_argument('--kill_exit', action='store_true',
1454+
parser.add_argument('--kill-exit', action='store_true',
14551455
help='If true, the spawned browser process is forcibly '
14561456
'killed when it calls exit(). Note: Using this '
14571457
'option may require explicitly passing the option '
14581458
'--browser=/path/to/browser, to avoid emrun being '
14591459
'detached from the browser process it spawns.')
14601460

1461-
parser.add_argument('--no_server', dest='run_server', action='store_false',
1461+
parser.add_argument('--no-server', dest='run_server', action='store_false',
14621462
default=True,
14631463
help='If specified, a HTTP web server is not launched '
14641464
'to host the page to run.')
14651465

1466-
parser.add_argument('--no_browser', dest='run_browser', action='store_false',
1466+
parser.add_argument('--no-browser', dest='run_browser', action='store_false',
14671467
default=True,
14681468
help='If specified, emrun will not launch a web browser '
14691469
'to run the page.')
14701470

1471-
parser.add_argument('--no_emrun_detect', action='store_true',
1471+
parser.add_argument('--no-emrun-detect', action='store_true',
14721472
help='If specified, skips printing the warning message '
14731473
'if html page is detected to not have been built '
14741474
'with --emrun linker flag.')
14751475

1476-
parser.add_argument('--serve_after_close', action='store_true',
1476+
parser.add_argument('--serve-after-close', action='store_true',
14771477
help='If true, serves the web page even after the '
14781478
'application quits by user closing the web page.')
14791479

1480-
parser.add_argument('--serve_after_exit', action='store_true',
1480+
parser.add_argument('--serve-after-exit', action='store_true',
14811481
help='If true, serves the web page even after the '
14821482
'application quits by a call to exit().')
14831483

1484-
parser.add_argument('--serve_root',
1484+
parser.add_argument('--serve-root',
14851485
help='If set, specifies the root path that the emrun '
14861486
'web server serves. If not specified, the directory '
14871487
'where the target .html page lives in is served.')
@@ -1495,14 +1495,14 @@ def parse_args():
14951495
parser.add_argument('--port', default=default_webserver_port, type=int,
14961496
help='Specifies the port the server runs in.')
14971497

1498-
parser.add_argument('--log_stdout',
1498+
parser.add_argument('--log-stdout',
14991499
help='Specifies a log filename where the browser process '
15001500
'stdout data will be appended to.')
15011501

1502-
parser.add_argument('--log_stderr',
1502+
parser.add_argument('--log-stderr',
15031503
help='Specifies a log filename where the browser process stderr data will be appended to.')
15041504

1505-
parser.add_argument('--silence_timeout', type=int, default=0,
1505+
parser.add_argument('--silence-timeout', type=int, default=0,
15061506
help='If no activity is received in this many seconds, '
15071507
'the browser process is assumed to be hung, and the web '
15081508
'server is shut down and the target browser killed. '
@@ -1514,66 +1514,75 @@ def parse_args():
15141514
'to be hung, and the web server is shut down and the '
15151515
'target browser killed. Disabled by default.')
15161516

1517-
parser.add_argument('--timeout_returncode', type=int, default=99999,
1517+
parser.add_argument('--timeout-returncode', type=int, default=99999,
15181518
help='Sets the exit code that emrun reports back to '
15191519
'caller in the case that a page timeout occurs. '
15201520
'Default: 99999.')
15211521

1522-
parser.add_argument('--list_browsers', action='store_true',
1522+
parser.add_argument('--list-browsers', action='store_true',
15231523
help='Prints out all detected browser that emrun is able '
15241524
'to use with the --browser command and exits.')
15251525

15261526
parser.add_argument('--browser',
15271527
help='Specifies the browser executable to run the web page in.')
15281528

1529-
parser.add_argument('--browser_args', default='',
1529+
parser.add_argument('--browser-args', default='',
15301530
help='Specifies the arguments to the browser executable.')
15311531

15321532
parser.add_argument('--android', action='store_true',
15331533
help='Launches the page in a browser of an Android '
15341534
'device connected to an USB on the local system. (via adb)')
15351535

1536-
parser.add_argument('--android_tunnel', action='store_true',
1536+
parser.add_argument('--android-tunnel', action='store_true',
15371537
help='Expose the port directly to the Android device '
15381538
'and connect to it as localhost, establishing '
15391539
'cross origin isolation. Implies --android. A '
15401540
'reverse socket connection is created by adb '
15411541
'reverse, and remains after emrun terminates (it '
15421542
'can be removed by adb reverse --remove).')
15431543

1544-
parser.add_argument('--system_info', action='store_true',
1544+
parser.add_argument('--system-info', action='store_true',
15451545
help='Prints information about the current system at startup.')
15461546

1547-
parser.add_argument('--browser_info', action='store_true',
1547+
parser.add_argument('--browser-info', action='store_true',
15481548
help='Prints information about the target browser to launch at startup.')
15491549

15501550
parser.add_argument('--json', action='store_true',
1551-
help='If specified, --system_info and --browser_info are '
1551+
help='If specified, --system-info and --browser-info are '
15521552
'outputted in JSON format.')
15531553

1554-
parser.add_argument('--safe_firefox_profile', action='store_true',
1554+
parser.add_argument('--safe-firefox-profile', action='store_true',
15551555
help='If true, the browser is launched into a new clean '
15561556
'Firefox profile that is suitable for unattended '
15571557
'automated runs. (If target browser != Firefox, '
15581558
'this parameter is ignored)')
15591559

1560-
parser.add_argument('--private_browsing', action='store_true',
1560+
parser.add_argument('--private-browsing', action='store_true',
15611561
help='If specified, opens browser in private/incognito mode.')
15621562

1563-
parser.add_argument('--dump_out_directory', default='dump_out', type=str,
1563+
parser.add_argument('--dump-out-directory', default='dump_out', type=str,
15641564
help='If specified, overrides the directory for dump files using emrun_file_dump method.')
15651565

15661566
parser.add_argument('serve', nargs='?', default='')
15671567

15681568
parser.add_argument('cmdlineparams', nargs='*')
15691569

1570-
return parser.parse_args()
1570+
# Support legacy argument names with `_` in them (but don't
1571+
# advertize these in the --help message.
1572+
newargs = []
1573+
for a in args:
1574+
if a.startswith('--') and '_' in a:
1575+
newargs.append(a.replace('_', '-'))
1576+
else:
1577+
newargs.append(a)
1578+
1579+
return parser.parse_args(newargs)
15711580

15721581

1573-
def run():
1582+
def run(args):
15741583
global browser_process, browser_exe, processname_killed_atexit, emrun_options, emrun_not_enabled_nag_printed
15751584

1576-
options = emrun_options = parse_args()
1585+
options = emrun_options = parse_args(args)
15771586

15781587
if options.android_tunnel:
15791588
options.android = True
@@ -1603,7 +1612,7 @@ def run():
16031612
return
16041613

16051614
if not options.serve and (options.system_info or options.browser_info):
1606-
# Don't run if only --system_info or --browser_info was passed.
1615+
# Don't run if only --system-info or --browser-info was passed.
16071616
options.run_server = options.run_browser = False
16081617

16091618
if not options.serve and (options.run_server or options.run_browser):
@@ -1659,7 +1668,7 @@ def run():
16591668
if options.android:
16601669
if options.run_browser or options.browser_info:
16611670
if not options.browser:
1662-
loge("Running on Android requires that you explicitly specify the browser to run with --browser <id>. Run emrun --android --list_browsers to obtain a list of installed browsers you can use.")
1671+
loge("Running on Android requires that you explicitly specify the browser to run with --browser <id>. Run emrun --android --list-browsers to obtain a list of installed browsers you can use.")
16631672
return 1
16641673
elif options.browser == 'firefox':
16651674
browser_app = 'org.mozilla.firefox/org.mozilla.gecko.BrowserApp'
@@ -1757,7 +1766,7 @@ def run(cmd):
17571766
run(['adb', 'push', os.path.join(profile_dir, 'prefs.js'), '/mnt/sdcard/safe_firefox_profile/prefs.js'])
17581767
except Exception as e:
17591768
loge('Creating Firefox profile prefs.js file to internal storage in /mnt/sdcard failed with error ' + str(e) + '!')
1760-
loge('Try running without --safe_firefox_profile flag if unattended execution mode is not important, or')
1769+
loge('Try running without --safe-firefox-profile flag if unattended execution mode is not important, or')
17611770
loge('enable rooted debugging on the Android device to allow adb to write files to /mnt/sdcard.')
17621771
browser += ['--es', 'args', '"--profile /mnt/sdcard/safe_firefox_profile"']
17631772

@@ -1820,7 +1829,7 @@ def run(cmd):
18201829
premature_quit_code = browser_process.poll()
18211830
if premature_quit_code is not None:
18221831
options.serve_after_close = True
1823-
logv('Warning: emrun got immediately detached from the target browser process (the process quit with exit code ' + str(premature_quit_code) + '). Cannot detect when user closes the browser. Behaving as if --serve_after_close was passed in.')
1832+
logv('Warning: emrun got immediately detached from the target browser process (the process quit with exit code ' + str(premature_quit_code) + '). Cannot detect when user closes the browser. Behaving as if --serve-after-close was passed in.')
18241833
if not options.browser:
18251834
logv('Try passing the --browser=/path/to/browser option to avoid this from occurring. See https://github.com/emscripten-core/emscripten/issues/3234 for more discussion.')
18261835

@@ -1838,7 +1847,7 @@ def run(cmd):
18381847
kill_browser_process()
18391848
else:
18401849
if is_browser_process_alive():
1841-
logv('Not terminating browser process, pass --kill_exit to terminate the browser when it calls exit().')
1850+
logv('Not terminating browser process, pass --kill-exit to terminate the browser when it calls exit().')
18421851
# If we have created a temporary Firefox profile, we would really really
18431852
# like to wait until the browser closes, or otherwise we'll just have to
18441853
# litter temp files and keep the temporary profile alive. It is possible
@@ -1855,13 +1864,13 @@ def run(cmd):
18551864
return page_exit_code
18561865

18571866

1858-
def main():
1859-
returncode = run()
1867+
def main(args):
1868+
returncode = run(args)
18601869
logv('emrun quitting with process exit code ' + str(returncode))
18611870
if temp_firefox_profile_dir is not None:
1862-
logi('Warning: Had to leave behind a temporary Firefox profile directory ' + temp_firefox_profile_dir + ' because --safe_firefox_profile was set and the browser did not quit before emrun did.')
1871+
logi('Warning: Had to leave behind a temporary Firefox profile directory ' + temp_firefox_profile_dir + ' because --safe-firefox-profile was set and the browser did not quit before emrun did.')
18631872
return returncode
18641873

18651874

18661875
if __name__ == '__main__':
1867-
sys.exit(main())
1876+
sys.exit(main(sys.argv[1:]))

test/test_browser.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5844,22 +5844,22 @@ class emrun(RunnerCore):
58445844
def test_emrun_info(self):
58455845
if not has_browser():
58465846
self.skipTest('need a browser')
5847-
result = self.run_process([EMRUN, '--system_info', '--browser_info'], stdout=PIPE).stdout
5847+
result = self.run_process([EMRUN, '--system-info', '--browser_info'], stdout=PIPE).stdout
58485848
assert 'CPU' in result
58495849
assert 'Browser' in result
58505850
assert 'Traceback' not in result
58515851

5852-
result = self.run_process([EMRUN, '--list_browsers'], stdout=PIPE).stdout
5852+
result = self.run_process([EMRUN, '--list-browsers'], stdout=PIPE).stdout
58535853
assert 'Traceback' not in result
58545854

58555855
def test_no_browser(self):
5856-
# Test --no_browser mode where we have to take care of launching the browser ourselves
5856+
# Test --no-browser mode where we have to take care of launching the browser ourselves
58575857
# and then killing emrun when we are done.
58585858
if not has_browser():
58595859
self.skipTest('need a browser')
58605860

58615861
self.run_process([EMCC, test_file('test_emrun.c'), '--emrun', '-o', 'hello_world.html'])
5862-
proc = subprocess.Popen([EMRUN, '--no_browser', '.', '--port=3333'], stdout=PIPE)
5862+
proc = subprocess.Popen([EMRUN, '--no-browser', '.', '--port=3333'], stdout=PIPE)
58635863
try:
58645864
if EMTEST_BROWSER:
58655865
print('Starting browser')
@@ -5891,9 +5891,9 @@ def test_emrun(self):
58915891

58925892
os.chdir(path_from_root())
58935893
args_base = [EMRUN, '--timeout', '30', '--safe_firefox_profile',
5894-
'--kill_exit', '--port', '6939', '--verbose',
5895-
'--log_stdout', self.in_dir('stdout.txt'),
5896-
'--log_stderr', self.in_dir('stderr.txt')]
5894+
'--kill-exit', '--port', '6939', '--verbose',
5895+
'--log-stdout', self.in_dir('stdout.txt'),
5896+
'--log-stderr', self.in_dir('stderr.txt')]
58975897

58985898
# Verify that trying to pass argument to the page without the `--` separator will
58995899
# generate an actionable error message

0 commit comments

Comments
 (0)