2019-08-25 01:38:50 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
CURDIR = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.join(CURDIR))
|
|
|
|
|
|
|
|
import uexpect
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
prompt = ":\) "
|
|
|
|
end_of_block = r".*\r\n.*\r\n"
|
|
|
|
|
2019-08-25 01:38:50 +00:00
|
|
|
|
|
|
|
class client(object):
|
2022-03-22 16:39:58 +00:00
|
|
|
def __init__(self, command=None, name="", log=None):
|
|
|
|
self.client = uexpect.spawn(["/bin/bash", "--noediting"])
|
2019-08-25 01:38:50 +00:00
|
|
|
if command is None:
|
2022-03-22 16:39:58 +00:00
|
|
|
command = os.environ.get("CLICKHOUSE_BINARY", "clickhouse") + " client"
|
2019-08-25 01:38:50 +00:00
|
|
|
self.client.command = command
|
2022-03-22 16:39:58 +00:00
|
|
|
self.client.eol("\r")
|
2019-08-25 01:38:50 +00:00
|
|
|
self.client.logger(log, prefix=name)
|
2020-07-11 02:30:53 +00:00
|
|
|
self.client.timeout(120)
|
2022-03-22 16:39:58 +00:00
|
|
|
self.client.expect("[#\$] ", timeout=60)
|
2019-08-25 01:38:50 +00:00
|
|
|
self.client.send(command)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self.client.__enter__()
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
2022-03-22 16:39:58 +00:00
|
|
|
self.client.reader["kill_event"].set()
|
2019-08-25 01:38:50 +00:00
|
|
|
# send Ctrl-C
|
2022-03-22 16:39:58 +00:00
|
|
|
self.client.send("\x03", eol="")
|
2019-08-25 01:38:50 +00:00
|
|
|
time.sleep(0.3)
|
2022-03-22 16:39:58 +00:00
|
|
|
self.client.send("quit", eol="\r")
|
|
|
|
self.client.send("\x03", eol="")
|
2019-08-25 01:38:50 +00:00
|
|
|
return self.client.__exit__(type, value, traceback)
|