Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Nicolas-Richard/46bd07f763a5555ca1dfdf7fc3f0bad6 to your computer and use it in GitHub Desktop.

Select an option

Save Nicolas-Richard/46bd07f763a5555ca1dfdf7fc3f0bad6 to your computer and use it in GitHub Desktop.
import pexpect
services = [
"service-foo",
"service-bar",
]
def send_cmd_wait_and_catch_timeout(child, cmd, timeout=30):
try:
# Send the cmd
child.sendline(cmd)
# Print the output of the cmd
while True:
try:
child.expect('\n', timeout=timeout) # expect a newline character
print(child.before) # print the output before the newline character
except pexpect.EOF:
break # break the loop if End Of File (EOF) is encountered
except pexpect.exceptions.TIMEOUT:
print("Timeout occurred")
if __name__ == "__main__":
for service in services:
print('========== processing %s ==========', service)
# create compass console
child = pexpect.spawn('compass console run {} -e qa'.format(service))
# wait for console to be ready
while True:
try:
child.expect('\n', timeout=120) # expect a newline character
if b'try pressing enter' in child.before:
break
print(child.before) # print the output before the newline character
except pexpect.EOF:
break # break the loop if End Of File (EOF) is encountered
# test cmd
# cmd = "psql $POSTGRES_URI -c 'SELECT table_schema,table_name FROM information_schema.tables ORDER BY table_schema,table_name;' | tail"
cmd = "echo -e 'SELECT NOW();' | bin/rails db -p"
send_cmd_wait_and_catch_timeout(child, cmd)
# send a another cmd
send_cmd_wait_and_catch_timeout(child, cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment