Attempt to make skydb more user-friendly
Automatically shut-down the sky_server when quitting. Always start the skyserver at the mojo-root instead of the parent directory to the path. Log when starting a sky_server outside the mojo root. Re-use already running sky_servers when possible. R=esprehn@chromium.org, abarth@chromium.org Review URL: https://codereview.chromium.org/679333005
This commit is contained in:
parent
3687a4515b
commit
3d7beea154
@ -8,13 +8,16 @@ import os
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import urlparse
|
import urlparse
|
||||||
|
import logging
|
||||||
|
import socket;
|
||||||
|
|
||||||
|
|
||||||
BUILD_DIRECTORY = 'out'
|
OUT_DIR = 'out'
|
||||||
CONFIG_DIRECTORY = 'Debug'
|
CONFIG_NAME = 'Debug'
|
||||||
SKY_TOOLS_DIRECTORY = os.path.abspath(os.path.join(__file__, os.pardir))
|
SKY_TOOLS_DIRECTORY = os.path.abspath(os.path.join(__file__, os.pardir))
|
||||||
MOJO_SHELL_PATH = os.path.abspath(os.path.join(SKY_TOOLS_DIRECTORY, os.pardir,
|
MOJO_ROOT = os.path.abspath(os.path.join(SKY_TOOLS_DIRECTORY, os.pardir,
|
||||||
os.pardir, BUILD_DIRECTORY, CONFIG_DIRECTORY, 'mojo_shell'))
|
os.pardir))
|
||||||
|
MOJO_SHELL_PATH = os.path.join(MOJO_ROOT, OUT_DIR, CONFIG_NAME, 'mojo_shell')
|
||||||
|
|
||||||
SUPPORTED_MIME_TYPES = [
|
SUPPORTED_MIME_TYPES = [
|
||||||
'text/html',
|
'text/html',
|
||||||
@ -22,18 +25,40 @@ SUPPORTED_MIME_TYPES = [
|
|||||||
'text/plain',
|
'text/plain',
|
||||||
]
|
]
|
||||||
|
|
||||||
def start_http_server_for_file(path):
|
class SkyDebugger(object):
|
||||||
|
@staticmethod
|
||||||
|
def _port_in_use(port):
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
return sock.connect_ex(('localhost', port)) == 0
|
||||||
|
|
||||||
|
def _start_http_server_for_file(self, path):
|
||||||
HTTP_PORT = 9999
|
HTTP_PORT = 9999
|
||||||
|
|
||||||
|
path = os.path.abspath(path)
|
||||||
|
if os.path.commonprefix([path, MOJO_ROOT]) == MOJO_ROOT:
|
||||||
|
server_root = MOJO_ROOT
|
||||||
|
else:
|
||||||
|
server_root = os.path.dirname(path)
|
||||||
|
logging.warn(
|
||||||
|
'%s is outside of mojo root, using %s as server root' %
|
||||||
|
(path, server_root))
|
||||||
|
relative_path = os.path.relpath(path, server_root)
|
||||||
|
|
||||||
|
if self._port_in_use(HTTP_PORT):
|
||||||
|
logging.warn(
|
||||||
|
'Port %s already in use, assuming custom sky_server started.')
|
||||||
|
else:
|
||||||
server_command = [
|
server_command = [
|
||||||
os.path.join(SKY_TOOLS_DIRECTORY, 'sky_server'),
|
os.path.join(SKY_TOOLS_DIRECTORY, 'sky_server'),
|
||||||
os.path.dirname(os.path.abspath(path)),
|
server_root,
|
||||||
str(HTTP_PORT),
|
str(HTTP_PORT),
|
||||||
]
|
]
|
||||||
subprocess.Popen(server_command)
|
self._sky_server = subprocess.Popen(server_command)
|
||||||
return 'http://localhost:%s/%s' % (HTTP_PORT, os.path.basename(path))
|
return 'http://localhost:%s/%s' % (HTTP_PORT, relative_path)
|
||||||
|
|
||||||
|
def main(self):
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
def main():
|
|
||||||
parser = argparse.ArgumentParser(description='Sky launcher/debugger')
|
parser = argparse.ArgumentParser(description='Sky launcher/debugger')
|
||||||
parser.add_argument('--gdb', action='store_true')
|
parser.add_argument('--gdb', action='store_true')
|
||||||
parser.add_argument('url', nargs='?', type=str)
|
parser.add_argument('url', nargs='?', type=str)
|
||||||
@ -51,7 +76,7 @@ def main():
|
|||||||
if args.url:
|
if args.url:
|
||||||
url = args.url
|
url = args.url
|
||||||
if not urlparse.urlparse(url).scheme:
|
if not urlparse.urlparse(url).scheme:
|
||||||
url = start_http_server_for_file(url)
|
url = self._start_http_server_for_file(url)
|
||||||
|
|
||||||
prompt_args = '--args-for=mojo://sky_debugger_prompt/ %s' % url
|
prompt_args = '--args-for=mojo://sky_debugger_prompt/ %s' % url
|
||||||
shell_command.append(prompt_args)
|
shell_command.append(prompt_args)
|
||||||
@ -60,9 +85,17 @@ def main():
|
|||||||
|
|
||||||
subprocess.check_call(shell_command)
|
subprocess.check_call(shell_command)
|
||||||
|
|
||||||
|
def shutdown(self):
|
||||||
|
print "Quitting"
|
||||||
|
if self._sky_server:
|
||||||
|
self._sky_server.terminate()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
skydb = SkyDebugger()
|
||||||
try:
|
try:
|
||||||
main()
|
skydb.main()
|
||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
print "Quitting"
|
pass
|
||||||
|
finally:
|
||||||
|
skydb.shutdown()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user