fancy-subprocess 1.1__tar.gz → 2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,8 @@
1
+ root = true
2
+
1
3
  [*]
4
+ charset = utf-8
2
5
  trim_trailing_whitespace = true
3
6
  insert_final_newline = true
4
- indent_style = "space"
7
+ indent_style = space
5
8
  indent_size = 4
6
-
@@ -0,0 +1,287 @@
1
+ Metadata-Version: 2.4
2
+ Name: fancy-subprocess
3
+ Version: 2.2
4
+ Summary: subprocess.run() with formatted output, detailed error messages and retry capabilities
5
+ Project-URL: Homepage, https://github.com/petamas/python-fancy-subprocess
6
+ Project-URL: Bug Tracker, https://github.com/petamas/python-fancy-subprocess/issues
7
+ Author-email: Tamás PEREGI <petamas@gmail.com>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: MacOS
12
+ Classifier: Operating System :: Microsoft :: Windows
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Operating System :: POSIX
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Requires-Python: >=3.10
21
+ Requires-Dist: ntstatus<2,>=1.1
22
+ Requires-Dist: oslex<2,>=0.1.3
23
+ Requires-Dist: pathext<2,>=1.5
24
+ Requires-Dist: typeguard<5,>=4.4.2
25
+ Requires-Dist: typing-extensions<5,>=4.14
26
+ Description-Content-Type: text/markdown
27
+
28
+ # fancy-subprocess
29
+
30
+ `fancy-subprocess` provides variants of `subprocess.run()` with formatted output, detailed error messages and retry capabilities.
31
+
32
+ ## `fancy_subprocess.run()` and related functionality
33
+
34
+ An extended (and in some aspects, constrained) version of `subprocess.run()`. It runs a command and prints its output line-by-line using a customizable `print_output` function, while printing informational messages (eg. which command it is running) using a customizable `print_message` function.
35
+
36
+ Key differences compared to `subprocess.run()`:
37
+ - The command must be specified as a list, simply specifying a string is not allowed.
38
+ - The command's stdout and stderr is always combined into a single stream. (Like `subprocess.run(stderr=STDOUT)`.)
39
+ - The output of the command is always assumed to be textual, not binary. (Like `subprocess.run(text=True)`.)
40
+ - The output of the command is always captured, but it is also immediately printed using `print_output`.
41
+ - The exit code of the command is checked, and an exception is raised on failure, like `subprocess.run(check=True)`, but the list of exit codes treated as success is customizable, and the raised exception is `RunError` instead of `CalledProcessError`.
42
+ - `OSError` is never raised, it gets converted to `RunError`.
43
+ - `RunResult` is returned instead of `CompletedProcess` on success.
44
+
45
+ Arguments (all of them except `cmd` are optional):
46
+ - `cmd: Sequence[str | Path]` - Command to run. See `subprocess.run()`'s documentation for the interpretation of `cmd[0]`. It is recommended to use `fancy_subprocess.which()` to produce `cmd[0]`.
47
+ - `print_message: Callable[[str], None]` - Function used to print informational messages. If unspecified or set to `None`, defaults to `fancy_subprocess.default_print`. Use `message_quiet=True` to disable printing informational messages.
48
+ - The type of this argument is also aliased as `fancy_subprocess.PrintFunction`.
49
+ - `print_output: Callable[[str], None]` - Function used to print a line of the output of the command. If unspecified or set to `None`, defaults to `fancy_subprocess.default_print`. Use `output_quiet=True` to disable printing the command's output.
50
+ - The type of this argument is also aliased as `fancy_subprocess.PrintFunction`.
51
+ - `message_quiet: bool` - If `True`, `print_message` is ignored, and no informational messages are printed. If unspecified or set to `None`, defaults to `False`.
52
+ - `output_quiet: bool` - If `True`, `print_output` is ignored, and the command's output it not printed. If unspecified or set to `None`, defaults to `False`. Note that this parameter also affects the default value of `description`.
53
+ - `description: str` - Description printed before running the command. If unspecified or set to `None`, defaults to `Running command: ...` when `output_quiet` is `False`, and `Running command (output silenced): ...` when `output_quiet` is `True`.
54
+ - `success: Sequence[int] | AnyExitCode` - List of exit codes that should be considered successful. If set to `fancy_subprocess.ANY_EXIT_CODE`, then all exit codes are considered successful. If unspecified or set to `None`, defaults to `[0]`. Note that 0 is not automatically included in the list of successful exit codes, so if a list without 0 is specified, then the function will consider 0 a failure.
55
+ - The type of this argument is also aliased as `fancy_subprocess.Success`.
56
+ - `flush_before_subprocess: bool` - If `True`, flushes both the standard output and error streams before running the command. If unspecified or set to `None`, defaults to `True`.
57
+ - `trim_output_lines: bool` - If `True`, remove trailing whitespace from the lines of the output of the command before calling `print_output` and adding them to the `output` field of `RunResult`. If unspecified or set to `None`, defaults to `True`.
58
+ - `max_output_size: int` - Maximum number of characters to be recorded in the `output` field of `RunResult`. If the command produces more than `max_output_size` characters, only the last `max_output_size` will be recorded. If unspecified or set to `None`, defaults to 10,000,000.
59
+ - `retry: int` - Number of times to retry running the command on failure. Note that the total number of attempts is one greater than what's specified. (I.e. `retry=2` attempts to run the command 3 times.) If unspecified or set to `None`, defaults to 0.
60
+ - `retry_initial_sleep_seconds: float` - Number of seconds to wait before retrying for the first time. If unspecified or set to `None`, defaults to 10.
61
+ - `retry_backoff: float` - Factor used to increase wait times before subsequent retries. If unspecified or set to `None`, defaults to 2.
62
+ - `env_overrides: Mapping[str, str]` - Dictionary used to set environment variables. Note that unline the `env` argument of `subprocess.run()`, `env_overrides` does not need to contain all environment variables, only the ones you want to add/modify compared to os.environ. If unspecified or set to `None`, defaults to empty dictionary, i.e. no change to the environment.
63
+ - The type of this argument is also aliased as `fancy_subprocess.EnvOverrides`.
64
+ - `cwd: str | Path` - If not `None`, change current working directory to `cwd` before running the command.
65
+ - `encoding: str` - This encoding will be used to open stdout and stderr of the command. If unspecified or set to `None`, see default behaviour in `io.TextIOWrapper`'s documentation.
66
+ - `errors: str` - This specifies how text decoding errors will be handled. (See possible options in `io.TextIOWrapper`'s documentation.) If unspecified or set to `None`, defaults to `replace`. Note that this differs from `io.TextIOWrapper`'s default behaviour, which is to use `strict`.
67
+ - `replace_fffd_with_question_mark: bool` - Replace Unicode Replacement Character U+FFFD (`�`, usually introduced by `errors='replace'`) with ASCII question mark (`?`) in lines of the output of the command before calling `print_output` and adding them to the `output` field of `RunResult`. If unspecified or set to `None`, defaults to `True`.
68
+
69
+ ### Return value: `fancy_subprocess.RunResult`
70
+
71
+ `fancy_subprocess.run()` and similar functions return a `RunResult` instance on success.
72
+
73
+ `RunResult` has the following properties:
74
+ - `exit_code: int` - Exit code of the finished process. (On Windows, this is a signed `int32` value, i.e. in the range of \[-2<sup>31</sup>, 2<sup>31</sup>-1\].)
75
+ - `output: str` - Combination of the process's output on stdout and stderr.
76
+
77
+ ### Exception: `fancy_subprocess.RunError`
78
+
79
+ `fancy_subprocess.run()` and similar functions raise `RunError` on error. There are two kinds of errors that result in a `RunError`:
80
+ - If the requested command has failed, the `completed` property will be `True`, and the `exit_code` and `output` properties will be set.
81
+ - If the command couldn't be run (eg. because the executable wasn't found), the `completed` property will be `False`, and the `oserror` property will be set to the `OSError` exception instance originally raised by the underlying `subprocess.Popen()` call.
82
+
83
+ Calling `str()` on a `RunError` object returns a detailed one-line description of the error:
84
+ - The failed command is included in the message.
85
+ - If an `OSError` happened, its message is included in the message.
86
+ - On Windows, if the exit code of the process is recognized as a known `NTSTATUS` error value, its name is included in the message, otherwise its hexadecimal representation is included (to make searching it on the internet easier).
87
+ - On Unix systems, if the exit code represents a signal, its name is included in the message.
88
+
89
+ `RunError` has the following properties:
90
+ - `cmd: Sequence[str | Path]` - Original command passed to `fancy_subprocess.run()`.
91
+ - `completed: bool` - `True` if the process completed (with an error), `False` if the underlying `subprocess.Popen()` call raised an OSError (eg. because it could not start the process).
92
+ - `exit_code: int` - Exit code of the completed process. Raises `ValueError` if `completed` is `False`.
93
+ - `output: str` - Combination of the process's output on stdout and stderr. Raises `ValueError` if `completed` is `False`.
94
+ - `oserror: OSError` - The `OSError` raised by `subprocess.Popen()`. Raises `ValueError` if `completed` is `True`.
95
+
96
+ ### `fancy_subprocess.run_silenced()`
97
+
98
+ Specialized version of `fancy_subprocess.run()`, primarily used to run a command and later process its output.
99
+
100
+ Differences compared to `fancy_subprocess.run()`:
101
+ - `output_quiet` cannot be set from the calling side, it is always set to `True`. Note that this affects `description`'s default value.
102
+ - `print_output` cannot be set from the calling side (because it wouldn't matter anyway because of `output_quiet=True`).
103
+
104
+ All other `fancy_subprocess.run()` arguments are available and behave the same.
105
+
106
+ ### `fancy_subprocess.run_indented()`
107
+
108
+ Specialized version of `fancy_subprocess.run()` which prints the command's output indented by a user-defined amount.
109
+
110
+ The `print_output` argument is replaced by `indent`, which can be set to either the number of spaces to use for indentation or any custom indentation string (eg. `\t`).
111
+
112
+ All other `fancy_subprocess.run()` arguments are available and behave the same.
113
+
114
+ ### Writing your own wrapper
115
+
116
+ Most projects will likely use `fancy_subprocess.run()` through their own wrapper around it, customizing the behaviour of the function using its various arguments. To simplify writing wrappers that specify some of the arguments, and expose the rest to callers, `fancy_subprocess` provides a couple helpers, allowing you to write type-safe wrappers like this:
117
+
118
+ ```
119
+ import fancy_subprocess
120
+ from typing import Unpack
121
+
122
+ def grab_output(cmd: list[str], **kwargs: Unpack[fancy_subprocess.RunParams]) -> str:
123
+ # Raises ValueError if there are unknown parameters in kwargs or if a keyword argument's type is incorrect
124
+ fancy_subprocess.check_run_params(**kwargs)
125
+
126
+ # Make a copy of keyword arguments to be edited
127
+ forwarded_args = kwargs.copy()
128
+ # Make sure nothing's printed, raise ValueError if caller tries to specify "output_quiet" or "message_quiet"
129
+ fancy_subprocess.force_run_params(forwarded_args, message_quiet=True, output_quiet=True)
130
+ # Handle encoding/decoding errors by replacing them with placeholder character by default, but allow callers to still customize behaviour
131
+ fancy_subprocess.change_default_run_params(forwarded_args, errors='backslashreplace')
132
+
133
+ # Run command, raise fancy_subprocess.RunError on failure
134
+ result = fancy_subprocess.run(cmd, **forwarded_args)
135
+
136
+ # Return combined stdout and stderr
137
+ return result.output
138
+ ```
139
+
140
+ The `grab_output()` function supports all `fancy_subprocess.run()` arguments (eg. `retry`), except for `message_quiet` and `output_quiet`. If `errors` is unspecified or set to `None`, it uses `errors='backslashreplace'` instead of the default `errors='replace'` behaviour. It also passes `mypy --strict`.
141
+
142
+ (Using `typing.Unpack` requires Python 3.11 or later. In Python 3.10, use the `typing_extensions` module from PyPI.)
143
+
144
+ ### Predefined printing functions
145
+
146
+ There are various predefined functions projects can use as the `print_message` and `print_output` parameters of `fancy_subprocess.run()`:
147
+
148
+ - `fancy_subprocess.default_print` prints the line to `sys.stdout`, then flushes it.
149
+ - `fancy_subprocess.errors_print` prints the line to `sys.stderr`, then flushes it.
150
+ - `fancy_subprocess.silenced_print` does not print anything. It can be used as an alternative to `output_quiet=True` if the caller does not want to change the default `description`.
151
+ - `fancy_subprocess.indented_print` prints the line to `sys.stdout` indented by 4 spaces, then flushes the file.
152
+ - `fancy_subprocess.indented_print_factory(indent)` returns a function that calls `fancy_subprocess.indented_print` with its parameter and the specified indent instead of the default 4 spaces.
153
+ - `logging.error(line)`, `logging.info(line)`, etc. can be used to redirect the messages (or even the output) to Python's builtin logging subsystem.
154
+ - The builtin `print` function can also be used to print without flushing.
155
+
156
+ ### Example outputs
157
+
158
+ #### Success
159
+
160
+ Take this script:
161
+
162
+ ```
163
+ import fancy_subprocess
164
+ import sys
165
+
166
+ fancy_subprocess.run_indented(
167
+ [sys.executable, '-m', 'venv', '--help'],
168
+ print_message=lambda msg: print(f'[script-name] {msg}'),
169
+ success=fancy_subprocess.ANY_EXIT_CODE)
170
+ ```
171
+
172
+ Running the script will produce the following output (on Windows):
173
+
174
+ ```
175
+ [script-name] Running command: d:\projects\python-libs\fancy_subprocess\.venv\Scripts\python.exe -m venv --help
176
+ usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
177
+ [--upgrade] [--without-pip] [--prompt PROMPT] [--upgrade-deps]
178
+ ENV_DIR [ENV_DIR ...]
179
+
180
+ Creates virtual Python environments in one or more target directories.
181
+
182
+ positional arguments:
183
+ ENV_DIR A directory to create the environment in.
184
+
185
+ options:
186
+ -h, --help show this help message and exit
187
+ --system-site-packages
188
+ Give the virtual environment access to the system
189
+ site-packages dir.
190
+ --symlinks Try to use symlinks rather than copies, when symlinks
191
+ are not the default for the platform.
192
+ --copies Try to use copies rather than symlinks, even when
193
+ symlinks are the default for the platform.
194
+ --clear Delete the contents of the environment directory if it
195
+ already exists, before environment creation.
196
+ --upgrade Upgrade the environment directory to use this version
197
+ of Python, assuming Python has been upgraded in-place.
198
+ --without-pip Skips installing or upgrading pip in the virtual
199
+ environment (pip is bootstrapped by default)
200
+ --prompt PROMPT Provides an alternative prompt prefix for this
201
+ environment.
202
+ --upgrade-deps Upgrade core dependencies: pip setuptools to the
203
+ latest version in PyPI
204
+
205
+ Once an environment has been created, you may wish to activate it, e.g. by
206
+ sourcing an activate script in its bin directory.
207
+ ```
208
+
209
+
210
+ #### Failed command on Windows
211
+
212
+ Take this script:
213
+
214
+ ```
215
+ import fancy_subprocess
216
+ import sys
217
+
218
+ try:
219
+ fancy_subprocess.run(
220
+ [sys.executable, '-c', 'import sys; print("Noooooo!"); sys.exit(-1072103376)'],
221
+ description='Demonstrating failure...',
222
+ )
223
+ except fancy_subprocess.RunError as e:
224
+ print(e)
225
+ ```
226
+
227
+ Running the script on Windows will produce the following output (-1072103376 is the signed integer interpretation of 0xC0190030, i.e. `STATUS_LOG_CORRUPTION_DETECTED`):
228
+
229
+ ```
230
+ Demonstrating failure...
231
+ Noooooo!
232
+ Command failed with exit code -1072103376 (STATUS_LOG_CORRUPTION_DETECTED): d:\projects\python-libs\fancy_subprocess\.venv\Scripts\python.exe -c "import sys; print("\^"Noooooo^!\^""); sys.exit(-1072103376)"
233
+ ```
234
+
235
+ #### Killed command on Linux
236
+
237
+ Take this script:
238
+
239
+ ```
240
+ import fancy_subprocess
241
+ import sys
242
+
243
+ try:
244
+ fancy_subprocess.run_silenced(
245
+ [sys.executable, '-c', 'import time; time.sleep(60)'],
246
+ description='Sweet dreams!',
247
+ )
248
+ except fancy_subprocess.RunError as e:
249
+ print(e)
250
+ ```
251
+
252
+ Running the script on Linux and killing the subprocess using `kill -9` before the 60 seconds are up will result in the following output:
253
+
254
+ ```
255
+ Sweet dreams!
256
+ Command failed with exit code -9 (SIGKILL): /home/petamas/.venv/bin/python -c 'import time; time.sleep(60)'
257
+ ```
258
+
259
+ #### Failure to find executable
260
+
261
+ Take this script:
262
+
263
+ ```
264
+ import fancy_subprocess
265
+
266
+ try:
267
+ fancy_subprocess.run(['foo', '--bar', 'baz'])
268
+ except fancy_subprocess.RunError as e:
269
+ print(e)
270
+ ```
271
+
272
+ Running the script will produce the following output (exact error message may depend on OS):
273
+
274
+ ```
275
+ Running command: foo --bar baz
276
+ Exception FileNotFoundError with message "[Errno 2] No such file or directory: 'foo'" was raised while trying to run command: foo --bar baz
277
+ ```
278
+
279
+ ## Other utilities
280
+
281
+ ### `fancy_subprocess.reconfigure_standard_output_streams()`
282
+
283
+ Calls `sys.stdout.reconfigure()` and `sys.stderr.reconfigure()` with the provided parameters. Raises `TypeError` if either `sys.stdout` or `sys.stderr` is not an instance of `io.TextIOWrapper`.
284
+
285
+ ## Licensing
286
+
287
+ This library is licensed under the MIT license.
@@ -0,0 +1,260 @@
1
+ # fancy-subprocess
2
+
3
+ `fancy-subprocess` provides variants of `subprocess.run()` with formatted output, detailed error messages and retry capabilities.
4
+
5
+ ## `fancy_subprocess.run()` and related functionality
6
+
7
+ An extended (and in some aspects, constrained) version of `subprocess.run()`. It runs a command and prints its output line-by-line using a customizable `print_output` function, while printing informational messages (eg. which command it is running) using a customizable `print_message` function.
8
+
9
+ Key differences compared to `subprocess.run()`:
10
+ - The command must be specified as a list, simply specifying a string is not allowed.
11
+ - The command's stdout and stderr is always combined into a single stream. (Like `subprocess.run(stderr=STDOUT)`.)
12
+ - The output of the command is always assumed to be textual, not binary. (Like `subprocess.run(text=True)`.)
13
+ - The output of the command is always captured, but it is also immediately printed using `print_output`.
14
+ - The exit code of the command is checked, and an exception is raised on failure, like `subprocess.run(check=True)`, but the list of exit codes treated as success is customizable, and the raised exception is `RunError` instead of `CalledProcessError`.
15
+ - `OSError` is never raised, it gets converted to `RunError`.
16
+ - `RunResult` is returned instead of `CompletedProcess` on success.
17
+
18
+ Arguments (all of them except `cmd` are optional):
19
+ - `cmd: Sequence[str | Path]` - Command to run. See `subprocess.run()`'s documentation for the interpretation of `cmd[0]`. It is recommended to use `fancy_subprocess.which()` to produce `cmd[0]`.
20
+ - `print_message: Callable[[str], None]` - Function used to print informational messages. If unspecified or set to `None`, defaults to `fancy_subprocess.default_print`. Use `message_quiet=True` to disable printing informational messages.
21
+ - The type of this argument is also aliased as `fancy_subprocess.PrintFunction`.
22
+ - `print_output: Callable[[str], None]` - Function used to print a line of the output of the command. If unspecified or set to `None`, defaults to `fancy_subprocess.default_print`. Use `output_quiet=True` to disable printing the command's output.
23
+ - The type of this argument is also aliased as `fancy_subprocess.PrintFunction`.
24
+ - `message_quiet: bool` - If `True`, `print_message` is ignored, and no informational messages are printed. If unspecified or set to `None`, defaults to `False`.
25
+ - `output_quiet: bool` - If `True`, `print_output` is ignored, and the command's output it not printed. If unspecified or set to `None`, defaults to `False`. Note that this parameter also affects the default value of `description`.
26
+ - `description: str` - Description printed before running the command. If unspecified or set to `None`, defaults to `Running command: ...` when `output_quiet` is `False`, and `Running command (output silenced): ...` when `output_quiet` is `True`.
27
+ - `success: Sequence[int] | AnyExitCode` - List of exit codes that should be considered successful. If set to `fancy_subprocess.ANY_EXIT_CODE`, then all exit codes are considered successful. If unspecified or set to `None`, defaults to `[0]`. Note that 0 is not automatically included in the list of successful exit codes, so if a list without 0 is specified, then the function will consider 0 a failure.
28
+ - The type of this argument is also aliased as `fancy_subprocess.Success`.
29
+ - `flush_before_subprocess: bool` - If `True`, flushes both the standard output and error streams before running the command. If unspecified or set to `None`, defaults to `True`.
30
+ - `trim_output_lines: bool` - If `True`, remove trailing whitespace from the lines of the output of the command before calling `print_output` and adding them to the `output` field of `RunResult`. If unspecified or set to `None`, defaults to `True`.
31
+ - `max_output_size: int` - Maximum number of characters to be recorded in the `output` field of `RunResult`. If the command produces more than `max_output_size` characters, only the last `max_output_size` will be recorded. If unspecified or set to `None`, defaults to 10,000,000.
32
+ - `retry: int` - Number of times to retry running the command on failure. Note that the total number of attempts is one greater than what's specified. (I.e. `retry=2` attempts to run the command 3 times.) If unspecified or set to `None`, defaults to 0.
33
+ - `retry_initial_sleep_seconds: float` - Number of seconds to wait before retrying for the first time. If unspecified or set to `None`, defaults to 10.
34
+ - `retry_backoff: float` - Factor used to increase wait times before subsequent retries. If unspecified or set to `None`, defaults to 2.
35
+ - `env_overrides: Mapping[str, str]` - Dictionary used to set environment variables. Note that unline the `env` argument of `subprocess.run()`, `env_overrides` does not need to contain all environment variables, only the ones you want to add/modify compared to os.environ. If unspecified or set to `None`, defaults to empty dictionary, i.e. no change to the environment.
36
+ - The type of this argument is also aliased as `fancy_subprocess.EnvOverrides`.
37
+ - `cwd: str | Path` - If not `None`, change current working directory to `cwd` before running the command.
38
+ - `encoding: str` - This encoding will be used to open stdout and stderr of the command. If unspecified or set to `None`, see default behaviour in `io.TextIOWrapper`'s documentation.
39
+ - `errors: str` - This specifies how text decoding errors will be handled. (See possible options in `io.TextIOWrapper`'s documentation.) If unspecified or set to `None`, defaults to `replace`. Note that this differs from `io.TextIOWrapper`'s default behaviour, which is to use `strict`.
40
+ - `replace_fffd_with_question_mark: bool` - Replace Unicode Replacement Character U+FFFD (`�`, usually introduced by `errors='replace'`) with ASCII question mark (`?`) in lines of the output of the command before calling `print_output` and adding them to the `output` field of `RunResult`. If unspecified or set to `None`, defaults to `True`.
41
+
42
+ ### Return value: `fancy_subprocess.RunResult`
43
+
44
+ `fancy_subprocess.run()` and similar functions return a `RunResult` instance on success.
45
+
46
+ `RunResult` has the following properties:
47
+ - `exit_code: int` - Exit code of the finished process. (On Windows, this is a signed `int32` value, i.e. in the range of \[-2<sup>31</sup>, 2<sup>31</sup>-1\].)
48
+ - `output: str` - Combination of the process's output on stdout and stderr.
49
+
50
+ ### Exception: `fancy_subprocess.RunError`
51
+
52
+ `fancy_subprocess.run()` and similar functions raise `RunError` on error. There are two kinds of errors that result in a `RunError`:
53
+ - If the requested command has failed, the `completed` property will be `True`, and the `exit_code` and `output` properties will be set.
54
+ - If the command couldn't be run (eg. because the executable wasn't found), the `completed` property will be `False`, and the `oserror` property will be set to the `OSError` exception instance originally raised by the underlying `subprocess.Popen()` call.
55
+
56
+ Calling `str()` on a `RunError` object returns a detailed one-line description of the error:
57
+ - The failed command is included in the message.
58
+ - If an `OSError` happened, its message is included in the message.
59
+ - On Windows, if the exit code of the process is recognized as a known `NTSTATUS` error value, its name is included in the message, otherwise its hexadecimal representation is included (to make searching it on the internet easier).
60
+ - On Unix systems, if the exit code represents a signal, its name is included in the message.
61
+
62
+ `RunError` has the following properties:
63
+ - `cmd: Sequence[str | Path]` - Original command passed to `fancy_subprocess.run()`.
64
+ - `completed: bool` - `True` if the process completed (with an error), `False` if the underlying `subprocess.Popen()` call raised an OSError (eg. because it could not start the process).
65
+ - `exit_code: int` - Exit code of the completed process. Raises `ValueError` if `completed` is `False`.
66
+ - `output: str` - Combination of the process's output on stdout and stderr. Raises `ValueError` if `completed` is `False`.
67
+ - `oserror: OSError` - The `OSError` raised by `subprocess.Popen()`. Raises `ValueError` if `completed` is `True`.
68
+
69
+ ### `fancy_subprocess.run_silenced()`
70
+
71
+ Specialized version of `fancy_subprocess.run()`, primarily used to run a command and later process its output.
72
+
73
+ Differences compared to `fancy_subprocess.run()`:
74
+ - `output_quiet` cannot be set from the calling side, it is always set to `True`. Note that this affects `description`'s default value.
75
+ - `print_output` cannot be set from the calling side (because it wouldn't matter anyway because of `output_quiet=True`).
76
+
77
+ All other `fancy_subprocess.run()` arguments are available and behave the same.
78
+
79
+ ### `fancy_subprocess.run_indented()`
80
+
81
+ Specialized version of `fancy_subprocess.run()` which prints the command's output indented by a user-defined amount.
82
+
83
+ The `print_output` argument is replaced by `indent`, which can be set to either the number of spaces to use for indentation or any custom indentation string (eg. `\t`).
84
+
85
+ All other `fancy_subprocess.run()` arguments are available and behave the same.
86
+
87
+ ### Writing your own wrapper
88
+
89
+ Most projects will likely use `fancy_subprocess.run()` through their own wrapper around it, customizing the behaviour of the function using its various arguments. To simplify writing wrappers that specify some of the arguments, and expose the rest to callers, `fancy_subprocess` provides a couple helpers, allowing you to write type-safe wrappers like this:
90
+
91
+ ```
92
+ import fancy_subprocess
93
+ from typing import Unpack
94
+
95
+ def grab_output(cmd: list[str], **kwargs: Unpack[fancy_subprocess.RunParams]) -> str:
96
+ # Raises ValueError if there are unknown parameters in kwargs or if a keyword argument's type is incorrect
97
+ fancy_subprocess.check_run_params(**kwargs)
98
+
99
+ # Make a copy of keyword arguments to be edited
100
+ forwarded_args = kwargs.copy()
101
+ # Make sure nothing's printed, raise ValueError if caller tries to specify "output_quiet" or "message_quiet"
102
+ fancy_subprocess.force_run_params(forwarded_args, message_quiet=True, output_quiet=True)
103
+ # Handle encoding/decoding errors by replacing them with placeholder character by default, but allow callers to still customize behaviour
104
+ fancy_subprocess.change_default_run_params(forwarded_args, errors='backslashreplace')
105
+
106
+ # Run command, raise fancy_subprocess.RunError on failure
107
+ result = fancy_subprocess.run(cmd, **forwarded_args)
108
+
109
+ # Return combined stdout and stderr
110
+ return result.output
111
+ ```
112
+
113
+ The `grab_output()` function supports all `fancy_subprocess.run()` arguments (eg. `retry`), except for `message_quiet` and `output_quiet`. If `errors` is unspecified or set to `None`, it uses `errors='backslashreplace'` instead of the default `errors='replace'` behaviour. It also passes `mypy --strict`.
114
+
115
+ (Using `typing.Unpack` requires Python 3.11 or later. In Python 3.10, use the `typing_extensions` module from PyPI.)
116
+
117
+ ### Predefined printing functions
118
+
119
+ There are various predefined functions projects can use as the `print_message` and `print_output` parameters of `fancy_subprocess.run()`:
120
+
121
+ - `fancy_subprocess.default_print` prints the line to `sys.stdout`, then flushes it.
122
+ - `fancy_subprocess.errors_print` prints the line to `sys.stderr`, then flushes it.
123
+ - `fancy_subprocess.silenced_print` does not print anything. It can be used as an alternative to `output_quiet=True` if the caller does not want to change the default `description`.
124
+ - `fancy_subprocess.indented_print` prints the line to `sys.stdout` indented by 4 spaces, then flushes the file.
125
+ - `fancy_subprocess.indented_print_factory(indent)` returns a function that calls `fancy_subprocess.indented_print` with its parameter and the specified indent instead of the default 4 spaces.
126
+ - `logging.error(line)`, `logging.info(line)`, etc. can be used to redirect the messages (or even the output) to Python's builtin logging subsystem.
127
+ - The builtin `print` function can also be used to print without flushing.
128
+
129
+ ### Example outputs
130
+
131
+ #### Success
132
+
133
+ Take this script:
134
+
135
+ ```
136
+ import fancy_subprocess
137
+ import sys
138
+
139
+ fancy_subprocess.run_indented(
140
+ [sys.executable, '-m', 'venv', '--help'],
141
+ print_message=lambda msg: print(f'[script-name] {msg}'),
142
+ success=fancy_subprocess.ANY_EXIT_CODE)
143
+ ```
144
+
145
+ Running the script will produce the following output (on Windows):
146
+
147
+ ```
148
+ [script-name] Running command: d:\projects\python-libs\fancy_subprocess\.venv\Scripts\python.exe -m venv --help
149
+ usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
150
+ [--upgrade] [--without-pip] [--prompt PROMPT] [--upgrade-deps]
151
+ ENV_DIR [ENV_DIR ...]
152
+
153
+ Creates virtual Python environments in one or more target directories.
154
+
155
+ positional arguments:
156
+ ENV_DIR A directory to create the environment in.
157
+
158
+ options:
159
+ -h, --help show this help message and exit
160
+ --system-site-packages
161
+ Give the virtual environment access to the system
162
+ site-packages dir.
163
+ --symlinks Try to use symlinks rather than copies, when symlinks
164
+ are not the default for the platform.
165
+ --copies Try to use copies rather than symlinks, even when
166
+ symlinks are the default for the platform.
167
+ --clear Delete the contents of the environment directory if it
168
+ already exists, before environment creation.
169
+ --upgrade Upgrade the environment directory to use this version
170
+ of Python, assuming Python has been upgraded in-place.
171
+ --without-pip Skips installing or upgrading pip in the virtual
172
+ environment (pip is bootstrapped by default)
173
+ --prompt PROMPT Provides an alternative prompt prefix for this
174
+ environment.
175
+ --upgrade-deps Upgrade core dependencies: pip setuptools to the
176
+ latest version in PyPI
177
+
178
+ Once an environment has been created, you may wish to activate it, e.g. by
179
+ sourcing an activate script in its bin directory.
180
+ ```
181
+
182
+
183
+ #### Failed command on Windows
184
+
185
+ Take this script:
186
+
187
+ ```
188
+ import fancy_subprocess
189
+ import sys
190
+
191
+ try:
192
+ fancy_subprocess.run(
193
+ [sys.executable, '-c', 'import sys; print("Noooooo!"); sys.exit(-1072103376)'],
194
+ description='Demonstrating failure...',
195
+ )
196
+ except fancy_subprocess.RunError as e:
197
+ print(e)
198
+ ```
199
+
200
+ Running the script on Windows will produce the following output (-1072103376 is the signed integer interpretation of 0xC0190030, i.e. `STATUS_LOG_CORRUPTION_DETECTED`):
201
+
202
+ ```
203
+ Demonstrating failure...
204
+ Noooooo!
205
+ Command failed with exit code -1072103376 (STATUS_LOG_CORRUPTION_DETECTED): d:\projects\python-libs\fancy_subprocess\.venv\Scripts\python.exe -c "import sys; print("\^"Noooooo^!\^""); sys.exit(-1072103376)"
206
+ ```
207
+
208
+ #### Killed command on Linux
209
+
210
+ Take this script:
211
+
212
+ ```
213
+ import fancy_subprocess
214
+ import sys
215
+
216
+ try:
217
+ fancy_subprocess.run_silenced(
218
+ [sys.executable, '-c', 'import time; time.sleep(60)'],
219
+ description='Sweet dreams!',
220
+ )
221
+ except fancy_subprocess.RunError as e:
222
+ print(e)
223
+ ```
224
+
225
+ Running the script on Linux and killing the subprocess using `kill -9` before the 60 seconds are up will result in the following output:
226
+
227
+ ```
228
+ Sweet dreams!
229
+ Command failed with exit code -9 (SIGKILL): /home/petamas/.venv/bin/python -c 'import time; time.sleep(60)'
230
+ ```
231
+
232
+ #### Failure to find executable
233
+
234
+ Take this script:
235
+
236
+ ```
237
+ import fancy_subprocess
238
+
239
+ try:
240
+ fancy_subprocess.run(['foo', '--bar', 'baz'])
241
+ except fancy_subprocess.RunError as e:
242
+ print(e)
243
+ ```
244
+
245
+ Running the script will produce the following output (exact error message may depend on OS):
246
+
247
+ ```
248
+ Running command: foo --bar baz
249
+ Exception FileNotFoundError with message "[Errno 2] No such file or directory: 'foo'" was raised while trying to run command: foo --bar baz
250
+ ```
251
+
252
+ ## Other utilities
253
+
254
+ ### `fancy_subprocess.reconfigure_standard_output_streams()`
255
+
256
+ Calls `sys.stdout.reconfigure()` and `sys.stderr.reconfigure()` with the provided parameters. Raises `TypeError` if either `sys.stdout` or `sys.stderr` is not an instance of `io.TextIOWrapper`.
257
+
258
+ ## Licensing
259
+
260
+ This library is licensed under the MIT license.
@@ -0,0 +1,6 @@
1
+ from fancy_subprocess._compat import *
2
+ from fancy_subprocess._print import *
3
+ from fancy_subprocess._reconfigure import *
4
+ from fancy_subprocess._run_core import *
5
+ from fancy_subprocess._run_param import *
6
+ from fancy_subprocess._run_wrappers import *
@@ -0,0 +1,21 @@
1
+ __all__ = [
2
+ 'checked_which',
3
+ 'RunProcessError',
4
+ 'RunProcessResult',
5
+ 'SILENCE',
6
+ 'which',
7
+ ]
8
+
9
+ from pathext import checked_which, which
10
+
11
+ from fancy_subprocess._run_core import RunError, RunResult
12
+
13
+ RunProcessError = RunError
14
+ RunProcessResult = RunResult
15
+
16
+ def SILENCE(msg: str) -> None:
17
+ """
18
+ Helper function that takes a string, and does nothing with it. Meant to be passed as the print_message or print_output argument of run() and related functions to silence the corresponding output stream.
19
+ """
20
+
21
+ pass
@@ -0,0 +1,39 @@
1
+ __all__ = [
2
+ 'default_print',
3
+ 'error_print',
4
+ 'Indent',
5
+ 'indented_print',
6
+ 'indented_print_factory',
7
+ 'PrintFunction',
8
+ 'silenced_print',
9
+ ]
10
+
11
+ import sys
12
+ from collections.abc import Callable
13
+ from typing import Optional
14
+
15
+ PrintFunction = Callable[[str], None]
16
+
17
+ Indent = int | str
18
+
19
+ def silenced_print(line: str) -> None:
20
+ pass
21
+
22
+ def indented_print(line: str, indent: Optional[Indent] = None) -> None:
23
+ if indent is None:
24
+ real_indent = 4*' '
25
+ elif isinstance(indent, int):
26
+ real_indent = indent*' '
27
+ else:
28
+ real_indent = indent
29
+
30
+ print(f'{real_indent}{line}', flush=True)
31
+
32
+ def indented_print_factory(indent: Optional[Indent] = None) -> PrintFunction:
33
+ return lambda line: indented_print(line, indent)
34
+
35
+ def default_print(line: str) -> None:
36
+ indented_print(line, indent='')
37
+
38
+ def error_print(line: str) -> None:
39
+ print(line, file=sys.stderr, flush=True)
@@ -0,0 +1,33 @@
1
+ __all__ = [
2
+ 'reconfigure_standard_output_streams',
3
+ ]
4
+
5
+ import io
6
+ import sys
7
+ from typing import Optional, TypedDict
8
+
9
+ from typing_extensions import Unpack
10
+
11
+ class ReconfigureParams(TypedDict, total=False):
12
+ encoding: Optional[str]
13
+ errors: Optional[str]
14
+ newline: Optional[str]
15
+ line_buffering: Optional[bool]
16
+ write_through: Optional[bool]
17
+
18
+ def _reconfigure_standard_stream(stream: object, name: str, **kwargs: Unpack[ReconfigureParams]) -> None:
19
+ if stream is None:
20
+ raise TypeError(f'{name} is None')
21
+
22
+ if not isinstance(stream, io.TextIOWrapper):
23
+ raise TypeError(f'{name} is not a TextIOWrapper: {repr(stream)}')
24
+
25
+ stream.reconfigure(**kwargs)
26
+
27
+ def reconfigure_standard_output_streams(**kwargs: Unpack[ReconfigureParams]) -> None:
28
+ """
29
+ Calls `sys.stdout.reconfigure()` and `sys.stderr.reconfigure()` with the provided parameters. Raises `TypeError` if either `sys.stdout` or `sys.stderr` is not an instance of `io.TextIOWrapper`.
30
+ """
31
+
32
+ _reconfigure_standard_stream(sys.stdout, 'sys.stdout', **kwargs)
33
+ _reconfigure_standard_stream(sys.stderr, 'sys.stderr', **kwargs)