(self, code, owner=None, terminal_input=None, **env)
| 76 | ) |
| 77 | |
| 78 | def shell(self, code, owner=None, terminal_input=None, **env): |
| 79 | settings = self.env.copy() |
| 80 | settings.update(env) |
| 81 | if owner is not None: |
| 82 | settings["TRAIN_TRACKS_GITHUB_OWNER"] = owner |
| 83 | command = ["bash", "-eu", "-c", 'source "$HELPER"\n' + code] |
| 84 | if terminal_input is None: |
| 85 | return subprocess.run( |
| 86 | command, input="", capture_output=True, text=True, env=settings, timeout=10 |
| 87 | ) |
| 88 | master, slave = pty.openpty() |
| 89 | try: |
| 90 | with subprocess.Popen( |
| 91 | command, stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 92 | text=True, env=settings, |
| 93 | ) as child: |
| 94 | os.close(slave) |
| 95 | slave = None |
| 96 | os.write(master, terminal_input.encode()) |
| 97 | stdout, stderr = child.communicate(timeout=10) |
| 98 | return subprocess.CompletedProcess(command, child.returncode, stdout, stderr) |
| 99 | finally: |
| 100 | os.close(master) |
| 101 | if slave is not None: |
| 102 | os.close(slave) |
| 103 | |
| 104 | def read_calls(self): |
| 105 | return [json.loads(line) for line in self.calls.read_text().splitlines()] if self.calls.exists() else [] |
no test coverage detected