Run a command by using ``subprocess.run`` with capture_output=True and stderr=subprocess.STDOUT so that the raise exception will have that information. The argument `capture_output` can be set explicitly if desired, but will be overriden with the debug status from the variable. Arg
(cmd_list: list[str], **kwargs: Any)
| 875 | |
| 876 | |
| 877 | def run_cmd(cmd_list: list[str], **kwargs: Any) -> subprocess.CompletedProcess: |
| 878 | """ |
| 879 | Run a command by using ``subprocess.run`` with capture_output=True and stderr=subprocess.STDOUT |
| 880 | so that the raise exception will have that information. The argument `capture_output` can be set explicitly |
| 881 | if desired, but will be overriden with the debug status from the variable. |
| 882 | |
| 883 | Args: |
| 884 | cmd_list: a list of strings describing the command to run. |
| 885 | kwargs: keyword arguments supported by the ``subprocess.run`` method. |
| 886 | |
| 887 | Returns: |
| 888 | a CompletedProcess instance after the command completes. |
| 889 | """ |
| 890 | debug = MONAIEnvVars.debug() |
| 891 | # Always capture output when check=True so that error details are available |
| 892 | # in the CalledProcessError exception for debugging subprocess failures. |
| 893 | if kwargs.get("check", False): |
| 894 | kwargs.setdefault("capture_output", True) |
| 895 | else: |
| 896 | kwargs["capture_output"] = kwargs.get("capture_output", debug) |
| 897 | |
| 898 | if kwargs.pop("run_cmd_verbose", False): |
| 899 | import monai |
| 900 | |
| 901 | monai.apps.utils.get_logger("monai.utils.run_cmd").info(f"{cmd_list}") # type: ignore[attr-defined] |
| 902 | try: |
| 903 | return subprocess.run(cmd_list, **kwargs) |
| 904 | except subprocess.CalledProcessError as e: |
| 905 | output = str(e.stdout.decode(errors="replace")) if e.stdout else "" |
| 906 | errors = str(e.stderr.decode(errors="replace")) if e.stderr else "" |
| 907 | raise RuntimeError(f"subprocess call error {e.returncode}: {errors}, {output}") from e |
| 908 | |
| 909 | |
| 910 | def is_sqrt(num: Sequence[int] | int) -> bool: |
searching dependent graphs…