Use readline in the remote debugger client and also have it try to connect repeatedly for two seconds before giving up

This commit is contained in:
Kovid Goyal 2014-03-30 15:47:50 +05:30
parent 469cc85255
commit 22fdc186f6

View File

@ -6,10 +6,11 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
import pdb, socket, inspect, sys, select import pdb, socket, inspect, sys, select, os, atexit, time
from calibre import prints from calibre import prints
from calibre.utils.ipc import eintr_retry_call from calibre.utils.ipc import eintr_retry_call
from calibre.constants import cache_dir
PROMPT = b'(debug) ' PROMPT = b'(debug) '
QUESTION = b'\x00\x01\x02' QUESTION = b'\x00\x01\x02'
@ -92,10 +93,27 @@ def set_trace(port=4444, skip=None):
def cli(port=4444): def cli(port=4444):
prints('Connecting to remote process on port %d...' % port) prints('Connecting to remote process on port %d...' % port)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(120) for i in xrange(20):
sock.connect(('127.0.0.1', port)) try:
sock.connect(('127.0.0.1', port))
break
except socket.error:
pass
time.sleep(0.1)
else:
try:
sock.connect(('127.0.0.1', port))
except socket.error as err:
prints(err, file=sys.stderr)
raise SystemExit(1)
prints('Connected to remote process') prints('Connected to remote process')
sock.setblocking(True) import readline
histfile = os.path.join(cache_dir(), 'calibre-rpdb.history')
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
try: try:
while True: while True:
recvd = b'' recvd = b''
@ -104,18 +122,20 @@ def cli(port=4444):
if not buf: if not buf:
return return
recvd += buf recvd += buf
if recvd: recvd = recvd[:-len(PROMPT)]
if recvd.startswith(QUESTION): if recvd.startswith(QUESTION):
recvd = recvd[len(QUESTION):-len(PROMPT)] recvd = recvd[len(QUESTION):]
sys.stdout.write(recvd) sys.stdout.write(recvd)
buf = [] raw = sys.stdin.readline() or b'n'
raw = b'' else:
try: sys.stdout.write(recvd)
raw = raw_input() + b'\n' raw = b''
except (EOFError, KeyboardInterrupt): try:
pass raw = raw_input(PROMPT) + b'\n'
if not raw: except (EOFError, KeyboardInterrupt):
raw = b'quit\n' pass
if not raw:
raw = b'quit\n'
eintr_retry_call(sock.send, raw) eintr_retry_call(sock.send, raw)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass