Skip to content

Commit

Permalink
[testing][hexagon] Better subproc errors (apache#11853)
Browse files Browse the repository at this point in the history
When a subprocess completes with a non-zero exit code, include
its stdout and stderr text in the Python exception's error message.
  • Loading branch information
Christian Convey authored and blackkker committed Jul 7, 2022
1 parent 2be03ba commit 81cfdb0
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions python/tvm/contrib/hexagon/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@
ANDROID_BASH_FILE_NAME = "android_bash.sh"


def _check_call_verbose(cmd, **kwargs) -> None:
"""
Similar to subprocess.check_call(cmd), but if the exit code is non-zero
then the raised Exception's message provides more detail, including
the stdout/stderr provided by the subprocess.
"""
try:
subprocess.run(cmd, capture_output=True, check=True, text=True, **kwargs)
except Exception as err:
error_msg = f"{err}\nstdout:\n{err.stdout}\nstderr:\n{err.stderr}"
raise Exception(error_msg)


def _get_hexagon_rpc_lib_dir() -> pathlib.Path:
"""Find the Hexagon API binaries.
Expand Down Expand Up @@ -356,13 +369,11 @@ def _copy_to_remote(
self, local_path: Union[str, pathlib.Path], remote_path: Union[str, pathlib.Path]
):
"""Abstract method implementation. See description in HexagonLauncherRPC."""
subprocess.check_call(
self._adb_device_sub_cmd + ["push", str(local_path), str(remote_path)]
)
_check_call_verbose(self._adb_device_sub_cmd + ["push", str(local_path), str(remote_path)])

def _create_remote_directory(self, remote_path: Union[str, pathlib.Path]) -> pathlib.Path:
"""Abstract method implementation. See description in HexagonLauncherRPC."""
subprocess.check_call(self._adb_device_sub_cmd + ["shell", "mkdir", "-p", str(remote_path)])
_check_call_verbose(self._adb_device_sub_cmd + ["shell", "mkdir", "-p", str(remote_path)])
return pathlib.Path(remote_path)

def _copy_binaries(self):
Expand Down Expand Up @@ -418,14 +429,14 @@ def _forward_ports(self, rpc_server_port, existing_forwards):
port = rpc_server_port
while len(self.forwarded_ports_) < 10:
if port not in existing_forwards and not _is_port_in_use(port):
subprocess.check_call(
_check_call_verbose(
self._adb_device_sub_cmd + ["forward", f"tcp:{port}", f"tcp:{port}"]
)
self.forwarded_ports_.append(port)
port += 1

def _reverse_ports(self, rpc_tracker_port):
subprocess.check_call(
_check_call_verbose(
self._adb_device_sub_cmd
+ ["reverse", f"tcp:{rpc_tracker_port}", f"tcp:{rpc_tracker_port}"]
)
Expand Down Expand Up @@ -455,11 +466,11 @@ def _run_server_script(self):
def _cleanup_port_forwarding(self):
# Removed pre-defined forward/reverse rules
rpc_tracker_port = self._rpc_info["rpc_tracker_port"]
subprocess.check_call(
_check_call_verbose(
self._adb_device_sub_cmd + ["reverse", "--remove", f"tcp:{rpc_tracker_port}"]
)
for port in self.forwarded_ports_:
subprocess.check_call(self._adb_device_sub_cmd + ["forward", "--remove", f"tcp:{port}"])
_check_call_verbose(self._adb_device_sub_cmd + ["forward", "--remove", f"tcp:{port}"])

def _terminate_remote(self):
# Send interupt to main and child processes
Expand Down Expand Up @@ -519,11 +530,11 @@ def _copy_to_remote(
self, local_path: Union[str, pathlib.Path], remote_path: Union[str, pathlib.Path]
):
"""Abstract method implementation. See description in HexagonLauncherRPC."""
subprocess.check_call(["cp", str(local_path), str(remote_path)])
_check_call_verbose(["cp", str(local_path), str(remote_path)])

def _create_remote_directory(self, remote_path: Union[str, pathlib.Path]) -> pathlib.Path:
"""Abstract method implementation. See description in HexagonLauncherRPC."""
subprocess.check_call(["mkdir", "-p", str(remote_path)])
_check_call_verbose(["mkdir", "-p", str(remote_path)])
return pathlib.Path(os.path.abspath(remote_path))

def _copy_libcxx(self, dest_dir: Union[str, pathlib.Path]):
Expand All @@ -547,7 +558,7 @@ def _copy_libcxx(self, dest_dir: Union[str, pathlib.Path]):
# links is to save disk space.
tar_in = f"tar -cf - -C {lib_dir} " + " ".join(libcxx_files)
tar_out = f"tar -xf - -C {str(dest_dir)}"
subprocess.check_call(tar_in + " | " + tar_out, shell=True)
_check_call_verbose(tar_in + " | " + tar_out, shell=True)

def start_server(self):
"""Abstract method implementation. See description in HexagonLauncherRPC."""
Expand Down

0 comments on commit 81cfdb0

Please sign in to comment.