Skip to content

Commit de5805c

Browse files
authored
Merge pull request #238 from easybuilders/bartoldeman-transition-to-run-shell-cmd
Document how to transition from `run_cmd` to `run_shell_cmd`
2 parents 677d472 + 4144b7a commit de5805c

File tree

1 file changed

+66
-4
lines changed

1 file changed

+66
-4
lines changed

docs/easybuild-v5/run_shell_cmd.md

+66-4
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,82 @@ This function replaces both the `run_cmd` and `run_cmd_qa` functions, which will
88

99
## Motivation
1010

11-
...
11+
Over the years `run_cmd` and `run_cmd_qa` accumulated a lot of arguments which sometimes have misleading names.
12+
Moreover they have two different kinds of return values (depending on whether called with `simple=True` or `simple=False`).
13+
So a new command `run_shell_cmd` is introduced to replace both, using more direct and natural arguments, and as a bonus,
14+
better error reporting. In line with `--trace` being set by default, `run_shell_cmd` is now also more verbose by default.
1215

1316
## High-level overview
1417

15-
...
18+
First of all `run_shell_cmd` has been designed so the defaults can be used for most situations.
19+
In that case the main thing to watch out for is the return code, which changed from a tuple `(output, exit_code)`
20+
to a named tuple with multiple fields, the most prominent ones being:
21+
22+
- `output`: command output, `stdout`+`stderr` combined if `split_stderr` is disabled, only `stdout` otherwise
23+
- `exit_code`: exit code of command (integer)
24+
- `stderr`: stderr output if `split_stderr` is enabled, `None` otherwise
25+
26+
A typical transition will then change
27+
28+
```python
29+
(out, _) = run_cmd(cmd, log_all=True, simple=False)
30+
```
31+
32+
to
33+
34+
```python
35+
res = run_shell_cmd(cmd)
36+
out = res.output
37+
```
1638

1739
## Use cases
1840

19-
...
41+
Examples:
42+
43+
- Basic usage:
44+
45+
```python
46+
cmd = ' '.join([self.cfg['preinstallopts'], install_cmd, self.cfg['installopts']])
47+
run_shell_cmd(cmd)
48+
```
49+
50+
- Get error code for both failure and non-failure of the command, as otherwise `run_shell_cmd` will raise `RunShellCmdError`. Additionally, don't display this command in terminal output:
51+
52+
```python
53+
cmd = "cmake --version"
54+
res = run_shell_cmd(cmd, hidden=True, fail_on_error=False)
55+
out = res.output
56+
ec = res.code
57+
```
2058

2159
## Transitioning from `run_cmd` to `run_shell_cmd`
2260

23-
...
61+
For parameters in general, the following translation table can be used, where the default values are shown:
62+
63+
| run_cmd parameter |run_shell_cmd parameter| meaning |
64+
| ---------------------|-----------------------|---------|
65+
| `cmd` | `cmd` | command to run |
66+
| `log_all=False` | (removed) | always log command output and exit code *(now always `True`)* |
67+
| `simple=False` | (removed) | if `True`, just return `True`/`False` to indicate success *(obsolete)* |
68+
| `regexp=True` | (removed) | regex used to check the output for errors *(obsolete)* |
69+
| `log_ok=True` | `fail_on_error=True` | fail on non-zero exit code |
70+
| | `split_error=False` | split of stderr from stdout output *(new feature)*|
71+
| `inp=None` | `stdin=None` | the input given to the command via `stdin` |
72+
| | `env=None` | environment to use to run command (if `None`, inherit current process environment) *(new feature)* |
73+
| `trace=True` | `hidden=False` | don't show command in terminal output with `--trace`, or `--extended-dry-run` / `-x`) |
74+
| `force_in_dry_run=False`| `in_dry_run=False` | also run command in dry run mode |
75+
| `verbose=True` | `verbose_dry_run=False` | show that command is run in dry run mode (overrules `--hidden`) |
76+
| `path=None` | `work_dir=None` | working directory to run command in (current working directory if `None`) |
77+
| `shell=None` | `use_bash=True` | execute command through bash shell (`run_cmd` enables this for `None`)|
78+
| `log_output=False` | `output_file=True` | collect command output in temporary output file *(changed default)* |
79+
| `stream_output=None` | `stream_output=None` | stream command output to stdout (auto-enabled with `--logtostdout` if `None`) |
80+
| `asynchronous=False` | `asynchronous=False` | run command asynchronously (not yet implemented for `run_shell_cmd`)|
81+
| `with_hooks=True` | `with_hooks=True` | trigger pre/post `run_cmd` or `run_shell_cmd` hooks |
82+
| | `qa_patterns=None` | list of 2-tuples with patterns for questions + corresponding answers (not yet implemented for `run_shell_cmd`) |
83+
| | `qa_wait_patterns=None`| list of 2-tuples with patterns for non-questions and number of iterations to allow these patterns to match with end out command output (not yet implemented for `run_shell_cmd`)|
2484

2585
## Transitioning from `run_cmd_qa` to `run_shell_cmd`
2686

87+
This is still to be implemented in `run_shell_cmd`.
88+
2789
...

0 commit comments

Comments
 (0)