| 42 | |
| 43 | # Execute a command under `sources` that depends on the given script. By default it runs the given script. |
| 44 | def execute(self, script_filename=None, command=None, timeout=10): |
| 45 | try: |
| 46 | if self.container is None: |
| 47 | self.recreate_container() |
| 48 | |
| 49 | # Define a signal handler for the timeout |
| 50 | def handler(signum, frame): |
| 51 | raise TimeoutError("Execution timed out") |
| 52 | |
| 53 | # Set a signal alarm to raise a TimeoutError after 30 seconds |
| 54 | signal.signal(signal.SIGALRM, handler) |
| 55 | signal.alarm(timeout) |
| 56 | |
| 57 | if command is None: |
| 58 | command = f"python {script_filename}" |
| 59 | |
| 60 | # Run the test code in the existing container |
| 61 | exit_code, output = self.container.exec_run( |
| 62 | command, |
| 63 | workdir=f"/{self.sources_dirname}", |
| 64 | ) |
| 65 | |
| 66 | # Disable the signal alarm after the code has finished executing |
| 67 | signal.alarm(0) |
| 68 | |
| 69 | logs = output.decode("utf-8").strip() |
| 70 | |
| 71 | return exit_code, logs |
| 72 | |
| 73 | except Exception as e: |
| 74 | self.shutdown() |
| 75 | return -1, "" |