Execute the given command. Raises error if command returns non-zero code. cmd: command to run. cmdArgs: arguments of the command. Can be a single string containing all args or multiple strings comma separated (one arg each). stdin: stdin to pass the command during runtime. Useful to
(cmd, *cmdArgs, stdin=None, cstdout=False, cstderr=False, cwd=None, env=None, ignoreFail=False, dryrun=False)
| 169 | print(msg) |
| 170 | |
| 171 | def run(cmd, *cmdArgs, stdin=None, cstdout=False, cstderr=False, cwd=None, env=None, ignoreFail=False, dryrun=False): |
| 172 | """ |
| 173 | Execute the given command. Raises error if command returns non-zero code. |
| 174 | cmd: command to run. |
| 175 | cmdArgs: arguments of the command. Can be a single string containing all args or multiple strings comma separated (one arg each). |
| 176 | stdin: stdin to pass the command during runtime. Useful to pipe the output of one command to another. |
| 177 | cstdout: set to True to capture stdout of the cmd. If set to False, stdout is printed to console. |
| 178 | cstderr: set to True to capture stderr of the cmd. If set to False, stderr is printed to console. |
| 179 | cwd: change working directory to this path during runtime. |
| 180 | env: dictionary containing environment variables to pass during runtime. |
| 181 | ignoreFail: if True, do not raise an exception if the cmd fails. |
| 182 | return: success, stdout, stderr. stdout and stderr are only populated if cstdout and cstderr are True. |
| 183 | dryrun: print the command without running it |
| 184 | """ |
| 185 | runcmd = f'{cmd} {" ".join(cmdArgs)}' |
| 186 | stde_pipe = subprocess.PIPE if cstderr else None |
| 187 | stdo_pipe = subprocess.PIPE if cstdout else None |
| 188 | log.debug(f'Running: "{runcmd}"' + (f' in CWD="{os.path.abspath(cwd)}"' if cwd else '')) |
| 189 | if dryrun: |
| 190 | print(runcmd) |
| 191 | else: |
| 192 | try: |
| 193 | _r = subprocess.run(shlex.split(runcmd), stdout=stdo_pipe, stderr=stde_pipe, |
| 194 | check=True, input=stdin, cwd=cwd, env=env) |
| 195 | return _r.returncode == 0, _r.stdout, _r.stderr |
| 196 | except Exception as e: |
| 197 | if ignoreFail: |
| 198 | return False, None, None |
| 199 | raise(e) |
| 200 | |
| 201 | def run_condfail(cmd, *cmdArgs, stdin=None, cstdout=False, cstderr=False, cwd=None, env=None, ignoreFail=False): |
| 202 | """Wrapper function for run() that ignores failures if selected.""" |
no outgoing calls
no test coverage detected