Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 1 | import re |
| 2 | import subprocess |
| 3 | import os |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 4 | import shlex |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 5 | import shutil |
| 6 | import sys |
| 7 | |
| 8 | from .terminal import terminals |
| 9 | |
| 10 | |
| 11 | def cli_from_config(config, terminal_choice): |
| 12 | cli = [] |
| 13 | if config["fvp-bindir"]: |
| 14 | cli.append(os.path.join(config["fvp-bindir"], config["exe"])) |
| 15 | else: |
| 16 | cli.append(config["exe"]) |
| 17 | |
| 18 | for param, value in config["parameters"].items(): |
| 19 | cli.extend(["--parameter", f"{param}={value}"]) |
| 20 | |
| 21 | for value in config["data"]: |
| 22 | cli.extend(["--data", value]) |
| 23 | |
| 24 | for param, value in config["applications"].items(): |
| 25 | cli.extend(["--application", f"{param}={value}"]) |
| 26 | |
| 27 | for terminal, name in config["terminals"].items(): |
| 28 | # If terminals are enabled and this terminal has been named |
| 29 | if terminal_choice != "none" and name: |
| 30 | # TODO if raw mode |
| 31 | # cli.extend(["--parameter", f"{terminal}.mode=raw"]) |
| 32 | # TODO put name into terminal title |
| 33 | cli.extend(["--parameter", f"{terminal}.terminal_command={terminals[terminal_choice].command}"]) |
| 34 | else: |
| 35 | # Disable terminal |
| 36 | cli.extend(["--parameter", f"{terminal}.start_telnet=0"]) |
| 37 | |
| 38 | cli.extend(config["args"]) |
| 39 | |
| 40 | return cli |
| 41 | |
| 42 | def check_telnet(): |
| 43 | # Check that telnet is present |
| 44 | if not bool(shutil.which("telnet")): |
| 45 | raise RuntimeError("Cannot find telnet, this is needed to connect to the FVP.") |
| 46 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 47 | |
| 48 | class ConsolePortParser: |
| 49 | def __init__(self, lines): |
| 50 | self._lines = lines |
| 51 | self._console_ports = {} |
| 52 | |
| 53 | def parse_port(self, console): |
| 54 | if console in self._console_ports: |
| 55 | return self._console_ports[console] |
| 56 | |
| 57 | while True: |
| 58 | try: |
| 59 | line = next(self._lines).strip().decode(errors='ignore') |
| 60 | m = re.match(r"^(\S+): Listening for serial connection on port (\d+)$", line) |
| 61 | if m: |
| 62 | matched_console = m.group(1) |
| 63 | matched_port = int(m.group(2)) |
| 64 | if matched_console == console: |
| 65 | return matched_port |
| 66 | else: |
| 67 | self._console_ports[matched_console] = matched_port |
| 68 | except StopIteration: |
| 69 | # self._lines might be a growing log file |
| 70 | pass |
| 71 | |
| 72 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 73 | # This function is backported from Python 3.8. Remove it and replace call sites |
| 74 | # with shlex.join once OE-core support for earlier Python versions is dropped. |
| 75 | def shlex_join(split_command): |
| 76 | """Return a shell-escaped string from *split_command*.""" |
| 77 | return ' '.join(shlex.quote(arg) for arg in split_command) |
| 78 | |
| 79 | |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 80 | class FVPRunner: |
| 81 | def __init__(self, logger): |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 82 | self._logger = logger |
| 83 | self._fvp_process = None |
| 84 | self._telnets = [] |
| 85 | self._pexpects = [] |
| 86 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 87 | def start(self, config, extra_args=[], terminal_choice="none", stdout=subprocess.PIPE): |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 88 | cli = cli_from_config(config, terminal_choice) |
| 89 | cli += extra_args |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 90 | |
| 91 | # Pass through environment variables needed for GUI applications, such |
| 92 | # as xterm, to work. |
| 93 | env = config['env'] |
Andrew Geissler | ea144b03 | 2023-01-27 16:03:57 -0600 | [diff] [blame^] | 94 | for name in ('DISPLAY', 'WAYLAND_DISPLAY', 'XAUTHORITY'): |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 95 | if name in os.environ: |
| 96 | env[name] = os.environ[name] |
| 97 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 98 | self._logger.debug(f"Constructed FVP call: {shlex_join(cli)}") |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 99 | self._fvp_process = subprocess.Popen( |
| 100 | cli, |
| 101 | stdin=subprocess.DEVNULL, stdout=stdout, stderr=subprocess.STDOUT, |
Patrick Williams | 8dd6848 | 2022-10-04 07:57:18 -0500 | [diff] [blame] | 102 | env=env) |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 103 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 104 | def stop(self): |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 105 | if self._fvp_process: |
| 106 | self._logger.debug(f"Terminating FVP PID {self._fvp_process.pid}") |
| 107 | try: |
| 108 | self._fvp_process.terminate() |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 109 | self._fvp_process.wait(10.0) |
| 110 | except subprocess.TimeoutExpired: |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 111 | self._logger.debug(f"Killing FVP PID {self._fvp_process.pid}") |
| 112 | self._fvp_process.kill() |
| 113 | except ProcessLookupError: |
| 114 | pass |
| 115 | |
| 116 | for telnet in self._telnets: |
| 117 | try: |
| 118 | telnet.terminate() |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 119 | telnet.wait(10.0) |
| 120 | except subprocess.TimeoutExpired: |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 121 | telnet.kill() |
| 122 | except ProcessLookupError: |
| 123 | pass |
| 124 | |
| 125 | for console in self._pexpects: |
| 126 | import pexpect |
| 127 | # Ensure pexpect logs all remaining output to the logfile |
| 128 | console.expect(pexpect.EOF, timeout=5.0) |
| 129 | console.close() |
| 130 | |
Patrick Williams | 2194f50 | 2022-10-16 14:26:09 -0500 | [diff] [blame] | 131 | if self._fvp_process and self._fvp_process.returncode and \ |
| 132 | self._fvp_process.returncode > 0: |
| 133 | # Return codes < 0 indicate that the process was explicitly |
| 134 | # terminated above. |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 135 | self._logger.info(f"FVP quit with code {self._fvp_process.returncode}") |
| 136 | return self._fvp_process.returncode |
| 137 | else: |
| 138 | return 0 |
| 139 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 140 | def wait(self, timeout): |
| 141 | self._fvp_process.wait(timeout) |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 142 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 143 | @property |
| 144 | def stdout(self): |
| 145 | return self._fvp_process.stdout |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 146 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 147 | def create_telnet(self, port): |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 148 | check_telnet() |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 149 | telnet = subprocess.Popen(["telnet", "localhost", str(port)], stdin=sys.stdin, stdout=sys.stdout) |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 150 | self._telnets.append(telnet) |
| 151 | return telnet |
| 152 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame] | 153 | def create_pexpect(self, port, **kwargs): |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 154 | import pexpect |
Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 155 | instance = pexpect.spawn(f"telnet localhost {port}", **kwargs) |
| 156 | self._pexpects.append(instance) |
| 157 | return instance |
| 158 | |
| 159 | def pid(self): |
| 160 | return self._fvp_process.pid |