Run a command: - on Linux command will be run by the standard shell selected with subprocess.run(shell=True) - on Windows command will be run in a Powershell shell This function returns the exit code, and does not log the result and output of the command.
(cmd_name: str, shell_cmd: str,
env: Optional[dict] = None)
| 135 | |
| 136 | |
| 137 | def execute_command_status(cmd_name: str, shell_cmd: str, |
| 138 | env: Optional[dict] = None) -> tuple[int, str, str]: |
| 139 | """ |
| 140 | Run a command: |
| 141 | - on Linux command will be run by the standard shell selected with |
| 142 | subprocess.run(shell=True) |
| 143 | - on Windows command will be run in a Powershell shell |
| 144 | |
| 145 | This function returns the exit code, and does not log the result and output |
| 146 | of the command. |
| 147 | |
| 148 | :param str cmd_name: the user facing name of the hook being run |
| 149 | :param str shell_cmd: shell command to execute |
| 150 | :param dict env: environ to pass into subprocess.run |
| 151 | |
| 152 | :returns: `tuple` (`int` returncode, `str` stderr, `str` stdout) |
| 153 | """ |
| 154 | logger.info("Running %s command: %s", cmd_name, shell_cmd) |
| 155 | |
| 156 | if POSIX_MODE: |
| 157 | proc = subprocess.run(shell_cmd, shell=True, stdout=subprocess.PIPE, |
| 158 | stderr=subprocess.PIPE, universal_newlines=True, |
| 159 | check=False, env=env) |
| 160 | else: |
| 161 | line = ['powershell.exe', '-Command', shell_cmd] |
| 162 | proc = subprocess.run(line, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 163 | universal_newlines=True, check=False, env=env) |
| 164 | |
| 165 | # universal_newlines causes stdout and stderr to be str objects instead of |
| 166 | # bytes in Python 3 |
| 167 | out, err = proc.stdout, proc.stderr |
| 168 | return proc.returncode, err, out |