Created
January 9, 2024 17:51
-
-
Save Nicolas-Richard/46bd07f763a5555ca1dfdf7fc3f0bad6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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