in ebcli/operations/commonops.py [0:0]
def open_webpage_in_browser(url, ssl=False):
io.log_info('Opening webpage with default browser.')
if not url.startswith('http'):
if ssl:
url = 'https://' + url
else:
url = 'http://' + url
LOG.debug('url={}'.format(url))
if utils.is_ssh() or platform.system().startswith('Win'):
# Preferred way for ssh or windows
# Windows can't do a fork so we have to do inline
LOG.debug('Running webbrowser inline.')
import webbrowser
webbrowser.open_new_tab(url)
else:
# This is the preferred way to open a web browser on *nix.
# It squashes all output which can be typical on *nix.
LOG.debug('Running webbrowser as subprocess.')
from subprocess import Popen, PIPE
p = Popen(
[
'{python} -m webbrowser \'{url}\''.format(
python=sys.executable,
url=url)
],
stderr=PIPE,
stdout=PIPE,
shell=True
)
'''
We need to fork the process for various reasons
1. Calling p.communicate waits for the thread. Some browsers
(if opening a new window) dont return to the thread until
the browser closes. We dont want the terminal to hang in
this case
2. If we dont call p.communicate, there is a race condition. If
the main process terminates before the browser call is made,
the call never gets made and the browser doesn't open.
Therefor the solution is to fork, then wait for the child
in the backround.
'''
pid = os.fork()
if pid == 0: # Is child
p.communicate()