capturer-ng 4.0__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.
- capturer_ng-4.0/.gitignore +12 -0
- capturer_ng-4.0/CHANGELOG.rst +131 -0
- capturer_ng-4.0/LICENSE.txt +20 -0
- capturer_ng-4.0/PKG-INFO +271 -0
- capturer_ng-4.0/README.rst +226 -0
- capturer_ng-4.0/capturer/__init__.py +719 -0
- capturer_ng-4.0/capturer/tests.py +198 -0
- capturer_ng-4.0/docs/api.rst +14 -0
- capturer_ng-4.0/docs/changelog.rst +1 -0
- capturer_ng-4.0/docs/conf.py +79 -0
- capturer_ng-4.0/docs/index.rst +32 -0
- capturer_ng-4.0/docs/readme.rst +1 -0
- capturer_ng-4.0/pyproject.toml +67 -0
- capturer_ng-4.0/requirements-checks.txt +4 -0
- capturer_ng-4.0/requirements-tests.txt +8 -0
- capturer_ng-4.0/tox.ini +20 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
Changelog
|
|
2
|
+
=========
|
|
3
|
+
|
|
4
|
+
The purpose of this document is to list all of the notable changes to this
|
|
5
|
+
project. The format was inspired by `Keep a Changelog`_. This project adheres
|
|
6
|
+
to `semantic versioning`_.
|
|
7
|
+
|
|
8
|
+
.. contents::
|
|
9
|
+
:local:
|
|
10
|
+
|
|
11
|
+
.. _Keep a Changelog: http://keepachangelog.com/
|
|
12
|
+
.. _semantic versioning: http://semver.org/
|
|
13
|
+
|
|
14
|
+
`Release 4.0`_ (2026-11-23)
|
|
15
|
+
---------------------------
|
|
16
|
+
|
|
17
|
+
This is the first release of the continuation of :pypi:`capturer`, published
|
|
18
|
+
on PyPI as :pypi:`capturer-ng`. The import name is still ``capturer`` and the
|
|
19
|
+
API and behavior are unchanged, so it works as a drop-in replacement. Don't
|
|
20
|
+
install it next to the original distribution: both ship the same files.
|
|
21
|
+
|
|
22
|
+
The original author Peter Odding is not involved in this continuation. License
|
|
23
|
+
(MIT) and copyright are unchanged. The continuation is maintained by one person
|
|
24
|
+
with AI agents, without independent review (see the readme).
|
|
25
|
+
|
|
26
|
+
**Noteworthy changes:**
|
|
27
|
+
|
|
28
|
+
- Restored compatibility with Python 3.14 and newer, where the default start
|
|
29
|
+
method of ``multiprocessing`` on Linux is no longer ``fork``.
|
|
30
|
+
Capturing failed with a ``TypeError`` there and could leave the process
|
|
31
|
+
hanging. The ``fork`` start method is now requested explicitly (pull request
|
|
32
|
+
`#16`_ by Scott K Logan).
|
|
33
|
+
|
|
34
|
+
- Python 3.10 is now the oldest supported version (this is backwards
|
|
35
|
+
incompatible, which is why the major version number was bumped). Python 3.10
|
|
36
|
+
up to 3.15 are tested.
|
|
37
|
+
|
|
38
|
+
- The requirement on :pypi:`humanfriendly` changed to :pypi:`humanfriendly-ng`.
|
|
39
|
+
|
|
40
|
+
**Internal changes:**
|
|
41
|
+
|
|
42
|
+
- The package is built with ``pyproject.toml``, tested with GitHub Actions on
|
|
43
|
+
Linux and macOS and released through PyPI Trusted Publishing.
|
|
44
|
+
|
|
45
|
+
.. _#16: https://github.com/xolox/python-capturer/pull/16
|
|
46
|
+
.. _Release 4.0: https://github.com/marco8154/python-capturer-ng/compare/3.0...4.0
|
|
47
|
+
|
|
48
|
+
`Release 3.0`_ (2020-03-07)
|
|
49
|
+
---------------------------
|
|
50
|
+
|
|
51
|
+
This is a maintenance release that updates the supported Python
|
|
52
|
+
versions, adds a changelog and makes some minor internal changes:
|
|
53
|
+
|
|
54
|
+
- Added support for Python 3.7 and 3.8.
|
|
55
|
+
- Dropped support for Python 2.6 and 3.4.
|
|
56
|
+
- Actively deprecate ``interpret_carriage_returns()``.
|
|
57
|
+
- Moved test helpers to :mod:`humanfriendly.testing`.
|
|
58
|
+
- Include documentation in source distributions.
|
|
59
|
+
- Use Python 3 for local development (``Makefile``).
|
|
60
|
+
- Restructured the online documentation.
|
|
61
|
+
- Updated PyPI domain in documentation.
|
|
62
|
+
- Added this changelog.
|
|
63
|
+
|
|
64
|
+
.. _Release 3.0: https://github.com/xolox/python-capturer/compare/2.4...3.0
|
|
65
|
+
|
|
66
|
+
`Release 2.4`_ (2017-05-17)
|
|
67
|
+
---------------------------
|
|
68
|
+
|
|
69
|
+
- Allow capturing output without relaying it.
|
|
70
|
+
- Make ``OutputBuffer.flush()`` more robust.
|
|
71
|
+
- Add Python 3.6 to supported versions.
|
|
72
|
+
|
|
73
|
+
.. _Release 2.4: https://github.com/xolox/python-capturer/compare/2.3...2.4
|
|
74
|
+
|
|
75
|
+
`Release 2.3`_ (2016-11-12)
|
|
76
|
+
---------------------------
|
|
77
|
+
|
|
78
|
+
- Clearly document supported operating systems (`#4`_).
|
|
79
|
+
- Start testing Python 3.5 and Mac OS X on Travis CI.
|
|
80
|
+
- Start publishing wheel distributions.
|
|
81
|
+
- PEP-8 and PEP-257 checks.
|
|
82
|
+
|
|
83
|
+
.. _Release 2.3: https://github.com/xolox/python-capturer/compare/2.2...2.3
|
|
84
|
+
.. _#4: https://github.com/xolox/python-capturer/issues/4
|
|
85
|
+
|
|
86
|
+
`Release 2.2`_ (2016-10-09)
|
|
87
|
+
---------------------------
|
|
88
|
+
|
|
89
|
+
Switch to :func:`humanfriendly.terminal.clean_terminal_output()`.
|
|
90
|
+
|
|
91
|
+
.. _Release 2.2: https://github.com/xolox/python-capturer/compare/2.1.1...2.2
|
|
92
|
+
|
|
93
|
+
`Release 2.1.1`_ (2015-10-24)
|
|
94
|
+
-----------------------------
|
|
95
|
+
|
|
96
|
+
Make it easier to run test suite from PyPI release (fixes `#3`_).
|
|
97
|
+
|
|
98
|
+
.. _Release 2.1.1: https://github.com/xolox/python-capturer/compare/2.1...2.1.1
|
|
99
|
+
.. _#3: https://github.com/xolox/python-capturer/issues/3
|
|
100
|
+
|
|
101
|
+
`Release 2.1`_ (2015-06-21)
|
|
102
|
+
---------------------------
|
|
103
|
+
|
|
104
|
+
Make "nested" output capturing work as expected (issue `#2`_).
|
|
105
|
+
|
|
106
|
+
.. _Release 2.1: https://github.com/xolox/python-capturer/compare/2.0...2.1
|
|
107
|
+
.. _#2: https://github.com/xolox/python-capturer/issues/2
|
|
108
|
+
|
|
109
|
+
`Release 2.0`_ (2015-06-18)
|
|
110
|
+
---------------------------
|
|
111
|
+
|
|
112
|
+
Experimental support for capturing stdout/stderr separately (issue `#2`_).
|
|
113
|
+
|
|
114
|
+
.. _Release 2.0: https://github.com/xolox/python-capturer/compare/1.1...2.0
|
|
115
|
+
.. _#2: https://github.com/xolox/python-capturer/issues/2
|
|
116
|
+
|
|
117
|
+
`Release 1.1`_ (2015-06-16)
|
|
118
|
+
---------------------------
|
|
119
|
+
|
|
120
|
+
- Expose captured output as file handle (wiht shortcuts for saving to files).
|
|
121
|
+
- Improve documentation of ``interpret_carriage_returns()``.
|
|
122
|
+
- Clearly document drawbacks of emulating a terminal.
|
|
123
|
+
|
|
124
|
+
.. _Release 1.1: https://github.com/xolox/python-capturer/compare/1.0...1.1
|
|
125
|
+
|
|
126
|
+
`Release 1.0`_ (2015-06-14)
|
|
127
|
+
---------------------------
|
|
128
|
+
|
|
129
|
+
This was the initial release.
|
|
130
|
+
|
|
131
|
+
.. _Release 1.0: https://github.com/xolox/python-capturer/tree/1.0
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2020 Peter Odding
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
capturer_ng-4.0/PKG-INFO
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: capturer-ng
|
|
3
|
+
Version: 4.0
|
|
4
|
+
Summary: Easily capture stdout/stderr of the current process and subprocesses
|
|
5
|
+
Project-URL: Homepage, https://github.com/marco8154/python-capturer-ng
|
|
6
|
+
Project-URL: Documentation, https://capturer.readthedocs.io
|
|
7
|
+
Project-URL: Changelog, https://github.com/marco8154/python-capturer-ng/blob/master/CHANGELOG.rst
|
|
8
|
+
Project-URL: Original project, https://github.com/xolox/python-capturer
|
|
9
|
+
Author-email: Peter Odding <peter@peterodding.com>
|
|
10
|
+
Maintainer: marco8154
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE.txt
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Environment :: Console
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Intended Audience :: Information Technology
|
|
17
|
+
Classifier: Intended Audience :: System Administrators
|
|
18
|
+
Classifier: Natural Language :: English
|
|
19
|
+
Classifier: Operating System :: POSIX
|
|
20
|
+
Classifier: Operating System :: Unix
|
|
21
|
+
Classifier: Programming Language :: Python
|
|
22
|
+
Classifier: Programming Language :: Python :: 3
|
|
23
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
28
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
29
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
30
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
31
|
+
Classifier: Topic :: Communications
|
|
32
|
+
Classifier: Topic :: Scientific/Engineering :: Human Machine Interfaces
|
|
33
|
+
Classifier: Topic :: Software Development
|
|
34
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
35
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
36
|
+
Classifier: Topic :: Software Development :: User Interfaces
|
|
37
|
+
Classifier: Topic :: System :: Shells
|
|
38
|
+
Classifier: Topic :: System :: System Shells
|
|
39
|
+
Classifier: Topic :: System :: Systems Administration
|
|
40
|
+
Classifier: Topic :: Terminals
|
|
41
|
+
Classifier: Topic :: Text Processing :: General
|
|
42
|
+
Requires-Python: >=3.10
|
|
43
|
+
Requires-Dist: humanfriendly-ng
|
|
44
|
+
Description-Content-Type: text/x-rst
|
|
45
|
+
|
|
46
|
+
capturer: Easily capture stdout/stderr of the current process and subprocesses
|
|
47
|
+
==============================================================================
|
|
48
|
+
|
|
49
|
+
.. image:: https://github.com/marco8154/python-capturer-ng/actions/workflows/test.yml/badge.svg?branch=master
|
|
50
|
+
:target: https://github.com/marco8154/python-capturer-ng/actions
|
|
51
|
+
|
|
52
|
+
The capturer package makes it easy to capture the stdout_ and stderr_ streams
|
|
53
|
+
of the current process *and subprocesses*. Output can be relayed to the
|
|
54
|
+
terminal in real time but is also available to the Python program for
|
|
55
|
+
additional processing. It's currently tested on Python 3.10 and newer. It's
|
|
56
|
+
tested on Linux and Mac OS X and may work on other unixes but
|
|
57
|
+
definitely won't work on Windows (due to the use of the platform dependent pty_
|
|
58
|
+
module). For usage instructions please refer to the documentation_.
|
|
59
|
+
|
|
60
|
+
.. contents::
|
|
61
|
+
:local:
|
|
62
|
+
|
|
63
|
+
Maintenance status
|
|
64
|
+
------------------
|
|
65
|
+
|
|
66
|
+
This is a continuation of ``capturer``, originally written by Peter Odding
|
|
67
|
+
(`@xolox <https://github.com/xolox>`_), whose last release was 3.0 in 2020.
|
|
68
|
+
The original author is not involved in this continuation. Should he return or
|
|
69
|
+
want the project back, it will be handed over.
|
|
70
|
+
|
|
71
|
+
This continuation is **maintained by one person with AI agents, without
|
|
72
|
+
independent review**. In practice that means:
|
|
73
|
+
|
|
74
|
+
- Changes are written and checked with the help of AI coding agents. A second
|
|
75
|
+
AI model is used as an additional check. That is a tool, not an independent
|
|
76
|
+
human review.
|
|
77
|
+
- There is no second maintainer who reads every change before a release.
|
|
78
|
+
- The goal is to keep the package working on current Python versions. The API
|
|
79
|
+
and behaviour are meant to stay the same. Licence (MIT) and copyright are
|
|
80
|
+
unchanged.
|
|
81
|
+
- Releases are built by GitHub Actions and published to PyPI through Trusted
|
|
82
|
+
Publishing, so every release can be traced to a tagged commit.
|
|
83
|
+
|
|
84
|
+
Install it with ``pip install capturer-ng``. The import name is unchanged
|
|
85
|
+
(``import capturer``), so do not install it next to the original
|
|
86
|
+
``capturer`` distribution: both ship the same files and would overwrite
|
|
87
|
+
each other.
|
|
88
|
+
|
|
89
|
+
If you depend on this package in production, please pin a version and read the
|
|
90
|
+
changelog before upgrading. Reviews, bug reports and co-maintainers are welcome.
|
|
91
|
+
|
|
92
|
+
Status
|
|
93
|
+
------
|
|
94
|
+
|
|
95
|
+
The `capturer` package was developed as a proof of concept over the course of a
|
|
96
|
+
weekend, because I was curious to see if it could be done (reliably). After a
|
|
97
|
+
weekend of extensive testing it seems to work fairly well so I'm publishing the
|
|
98
|
+
initial release as version 1.0, however I still consider this a proof of
|
|
99
|
+
concept because I don't have extensive "production" experience using it yet.
|
|
100
|
+
Here's hoping it works as well in practice as it did during my testing :-).
|
|
101
|
+
|
|
102
|
+
Installation
|
|
103
|
+
------------
|
|
104
|
+
|
|
105
|
+
The `capturer` package is available on PyPI_ which means installation should be
|
|
106
|
+
as simple as:
|
|
107
|
+
|
|
108
|
+
.. code-block:: console
|
|
109
|
+
|
|
110
|
+
$ pip install capturer-ng
|
|
111
|
+
|
|
112
|
+
There's actually a multitude of ways to install Python packages (e.g. the `per
|
|
113
|
+
user site-packages directory`_, `virtual environments`_ or just installing
|
|
114
|
+
system wide) and I have no intention of getting into that discussion here, so
|
|
115
|
+
if this intimidates you then read up on your options before returning to these
|
|
116
|
+
instructions ;-).
|
|
117
|
+
|
|
118
|
+
Getting started
|
|
119
|
+
---------------
|
|
120
|
+
|
|
121
|
+
The easiest way to capture output is to use a context manager:
|
|
122
|
+
|
|
123
|
+
.. code-block:: python
|
|
124
|
+
|
|
125
|
+
import subprocess
|
|
126
|
+
from capturer import CaptureOutput
|
|
127
|
+
|
|
128
|
+
with CaptureOutput() as capturer:
|
|
129
|
+
# Generate some output from Python.
|
|
130
|
+
print "Output from Python"
|
|
131
|
+
# Generate output from a subprocess.
|
|
132
|
+
subprocess.call(["echo", "Output from a subprocess"])
|
|
133
|
+
# Get the output in each of the supported formats.
|
|
134
|
+
assert capturer.get_bytes() == b'Output from Python\r\nOutput from a subprocess\r\n'
|
|
135
|
+
assert capturer.get_lines() == [u'Output from Python', u'Output from a subprocess']
|
|
136
|
+
assert capturer.get_text() == u'Output from Python\nOutput from a subprocess'
|
|
137
|
+
|
|
138
|
+
The use of a context manager (`the with statement`_) ensures that output
|
|
139
|
+
capturing is enabled and disabled at the appropriate time, regardless of
|
|
140
|
+
whether exceptions interrupt the normal flow of processing.
|
|
141
|
+
|
|
142
|
+
Note that the first call to `get_bytes()`_, `get_lines()`_ or `get_text()`_
|
|
143
|
+
will stop the capturing of output by default. This is intended as a sane
|
|
144
|
+
default to prevent partial reads (which can be confusing as hell when you don't
|
|
145
|
+
have experience with them). So we could have simply used ``print`` to show
|
|
146
|
+
the results without causing a recursive "captured output is printed and then
|
|
147
|
+
captured again" loop. There's an optional ``partial=True`` keyword argument
|
|
148
|
+
that can be used to disable this behavior (please refer to the documentation_
|
|
149
|
+
for details).
|
|
150
|
+
|
|
151
|
+
Design choices
|
|
152
|
+
--------------
|
|
153
|
+
|
|
154
|
+
There are existing solutions out there to capture the stdout_ and stderr_
|
|
155
|
+
streams of (Python) processes. The `capturer` package was created for a very
|
|
156
|
+
specific use case that wasn't catered for by existing solutions (that I could
|
|
157
|
+
find). This section documents the design choices that guided the development of
|
|
158
|
+
the `capturer` package:
|
|
159
|
+
|
|
160
|
+
.. contents::
|
|
161
|
+
:local:
|
|
162
|
+
|
|
163
|
+
Intercepts writes to low level file descriptors
|
|
164
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
165
|
+
|
|
166
|
+
Libraries like capture_ and iocapture_ change Python's sys.stdout_ and
|
|
167
|
+
sys.stderr_ file objects to fake file objects (using StringIO_). This enables
|
|
168
|
+
capturing of (most) output written to the stdout_ and stderr_ streams from the
|
|
169
|
+
same Python process, however any output from subprocesses is unaffected by the
|
|
170
|
+
redirection and not captured.
|
|
171
|
+
|
|
172
|
+
The `capturer` package instead intercepts writes to low level file descriptors
|
|
173
|
+
(similar to and inspired by `how pytest does it`_). This enables capturing of
|
|
174
|
+
output written to the standard output and error streams from the same Python
|
|
175
|
+
process as well as any subprocesses.
|
|
176
|
+
|
|
177
|
+
Uses a pseudo terminal to emulate a real terminal
|
|
178
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
179
|
+
|
|
180
|
+
The `capturer` package uses a pseudo terminal created using `pty.openpty()`_ to
|
|
181
|
+
capture output. This means subprocesses will use ANSI escape sequences because
|
|
182
|
+
they think they're connected to a terminal. In the current implementation you
|
|
183
|
+
can't opt out of this, but feel free to submit a feature request to change this
|
|
184
|
+
:-). This does have some drawbacks:
|
|
185
|
+
|
|
186
|
+
- The use of `pty.openpty()`_ means you need to be running in a UNIX like
|
|
187
|
+
environment for `capturer` to work (Windows definitely isn't supported).
|
|
188
|
+
|
|
189
|
+
- All output captured is relayed on the stderr_ stream by default, so capturing
|
|
190
|
+
changes the semantics of your programs. How much this matters obviously
|
|
191
|
+
depends on your use case. For the use cases that triggered me to create
|
|
192
|
+
`capturer` it doesn't matter, which explains why this is the default mode.
|
|
193
|
+
|
|
194
|
+
There is experimental support for capturing stdout_ and stderr_ separately
|
|
195
|
+
and relaying captured output to the appropriate original stream. Basically
|
|
196
|
+
you call ``CaptureOutput(merged=False)`` and then you use the ``stdout`` and
|
|
197
|
+
``stderr`` attributes of the ``CaptureOutput`` object to get at the output
|
|
198
|
+
captured on each stream.
|
|
199
|
+
|
|
200
|
+
I say experimental because this method of capturing can unintentionally
|
|
201
|
+
change the order in which captured output is emitted, in order to avoid
|
|
202
|
+
interleaving output emitted on the stdout_ and stderr_ streams (which would
|
|
203
|
+
most likely result in incomprehensible output). Basically output is relayed
|
|
204
|
+
on each stream separately after each line break. This means interactive
|
|
205
|
+
prompts that block on reading from standard input without emitting a line
|
|
206
|
+
break won't show up (until it's too late ;-).
|
|
207
|
+
|
|
208
|
+
Relays output to the terminal in real time
|
|
209
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
210
|
+
|
|
211
|
+
The main use case of `capturer` is to capture all output of a snippet of Python
|
|
212
|
+
code (including any output by subprocesses) but also relay the output to the
|
|
213
|
+
terminal in real time. This has a couple of useful properties:
|
|
214
|
+
|
|
215
|
+
- Long running operations can provide the operator with real time feedback by
|
|
216
|
+
emitting output on the terminal. This sounds obvious (and it is!) but it is
|
|
217
|
+
non-trivial to implement (an understatement :-) when you *also* want to
|
|
218
|
+
capture the output.
|
|
219
|
+
|
|
220
|
+
- Programs like gpg_ and ssh_ that use interactive password prompts will render
|
|
221
|
+
their password prompt on the terminal in real time. This avoids the awkward
|
|
222
|
+
interaction where a password prompt is silenced but the program still hangs,
|
|
223
|
+
waiting for input on stdin_.
|
|
224
|
+
|
|
225
|
+
Contact
|
|
226
|
+
-------
|
|
227
|
+
|
|
228
|
+
The latest version of `capturer` is available on PyPI_ and GitHub_. The
|
|
229
|
+
documentation is hosted on `Read the Docs`_ and includes a changelog_. For bug
|
|
230
|
+
reports please create an issue on GitHub_. Please do not contact the
|
|
231
|
+
original author about problems with this continuation.
|
|
232
|
+
|
|
233
|
+
License
|
|
234
|
+
-------
|
|
235
|
+
|
|
236
|
+
This software is licensed under the `MIT license`_.
|
|
237
|
+
|
|
238
|
+
© 2020 Peter Odding.
|
|
239
|
+
|
|
240
|
+
A big thanks goes out to the pytest_ developers because pytest's mechanism for
|
|
241
|
+
capturing the output of subprocesses provided inspiration for the `capturer`
|
|
242
|
+
package. No code was copied, but both projects are MIT licensed anyway, so it's
|
|
243
|
+
not like it's very relevant :-).
|
|
244
|
+
|
|
245
|
+
.. External references:
|
|
246
|
+
.. _capture: https://pypi.org/project/capture
|
|
247
|
+
.. _changelog: https://capturer.readthedocs.io/en/latest/changelog.html
|
|
248
|
+
.. _documentation: https://capturer.readthedocs.io
|
|
249
|
+
.. _get_bytes(): https://capturer.readthedocs.io/en/latest/api.html#capturer.CaptureOutput.get_bytes
|
|
250
|
+
.. _get_lines(): https://capturer.readthedocs.io/en/latest/api.html#capturer.CaptureOutput.get_lines
|
|
251
|
+
.. _get_text(): https://capturer.readthedocs.io/en/latest/api.html#capturer.CaptureOutput.get_text
|
|
252
|
+
.. _GitHub: https://github.com/marco8154/python-capturer-ng
|
|
253
|
+
.. _gpg: https://en.wikipedia.org/wiki/GNU_Privacy_Guard
|
|
254
|
+
.. _how pytest does it: https://pytest.org/latest/capture.html
|
|
255
|
+
.. _iocapture: https://pypi.org/project/iocapture
|
|
256
|
+
.. _MIT license: http://en.wikipedia.org/wiki/MIT_License
|
|
257
|
+
.. _per user site-packages directory: https://www.python.org/dev/peps/pep-0370/
|
|
258
|
+
.. _pty.openpty(): https://docs.python.org/2/library/pty.html#pty.openpty
|
|
259
|
+
.. _pty: https://docs.python.org/2/library/pty.html
|
|
260
|
+
.. _PyPI: https://pypi.org/project/capturer-ng
|
|
261
|
+
.. _pytest: https://pypi.org/project/pytest
|
|
262
|
+
.. _Read the Docs: https://capturer.readthedocs.io
|
|
263
|
+
.. _ssh: https://en.wikipedia.org/wiki/Secure_Shell
|
|
264
|
+
.. _stderr: https://en.wikipedia.org/wiki/Standard_streams#Standard_error_.28stderr.29
|
|
265
|
+
.. _stdin: https://en.wikipedia.org/wiki/Standard_streams#Standard_input_.28stdin.29
|
|
266
|
+
.. _stdout: https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29
|
|
267
|
+
.. _StringIO: https://docs.python.org/2/library/stringio.html
|
|
268
|
+
.. _sys.stderr: https://docs.python.org/2/library/sys.html#sys.stderr
|
|
269
|
+
.. _sys.stdout: https://docs.python.org/2/library/sys.html#sys.stdout
|
|
270
|
+
.. _the with statement: https://docs.python.org/2/reference/compound_stmts.html#the-with-statement
|
|
271
|
+
.. _virtual environments: http://docs.python-guide.org/en/latest/dev/virtualenvs/
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
capturer: Easily capture stdout/stderr of the current process and subprocesses
|
|
2
|
+
==============================================================================
|
|
3
|
+
|
|
4
|
+
.. image:: https://github.com/marco8154/python-capturer-ng/actions/workflows/test.yml/badge.svg?branch=master
|
|
5
|
+
:target: https://github.com/marco8154/python-capturer-ng/actions
|
|
6
|
+
|
|
7
|
+
The capturer package makes it easy to capture the stdout_ and stderr_ streams
|
|
8
|
+
of the current process *and subprocesses*. Output can be relayed to the
|
|
9
|
+
terminal in real time but is also available to the Python program for
|
|
10
|
+
additional processing. It's currently tested on Python 3.10 and newer. It's
|
|
11
|
+
tested on Linux and Mac OS X and may work on other unixes but
|
|
12
|
+
definitely won't work on Windows (due to the use of the platform dependent pty_
|
|
13
|
+
module). For usage instructions please refer to the documentation_.
|
|
14
|
+
|
|
15
|
+
.. contents::
|
|
16
|
+
:local:
|
|
17
|
+
|
|
18
|
+
Maintenance status
|
|
19
|
+
------------------
|
|
20
|
+
|
|
21
|
+
This is a continuation of ``capturer``, originally written by Peter Odding
|
|
22
|
+
(`@xolox <https://github.com/xolox>`_), whose last release was 3.0 in 2020.
|
|
23
|
+
The original author is not involved in this continuation. Should he return or
|
|
24
|
+
want the project back, it will be handed over.
|
|
25
|
+
|
|
26
|
+
This continuation is **maintained by one person with AI agents, without
|
|
27
|
+
independent review**. In practice that means:
|
|
28
|
+
|
|
29
|
+
- Changes are written and checked with the help of AI coding agents. A second
|
|
30
|
+
AI model is used as an additional check. That is a tool, not an independent
|
|
31
|
+
human review.
|
|
32
|
+
- There is no second maintainer who reads every change before a release.
|
|
33
|
+
- The goal is to keep the package working on current Python versions. The API
|
|
34
|
+
and behaviour are meant to stay the same. Licence (MIT) and copyright are
|
|
35
|
+
unchanged.
|
|
36
|
+
- Releases are built by GitHub Actions and published to PyPI through Trusted
|
|
37
|
+
Publishing, so every release can be traced to a tagged commit.
|
|
38
|
+
|
|
39
|
+
Install it with ``pip install capturer-ng``. The import name is unchanged
|
|
40
|
+
(``import capturer``), so do not install it next to the original
|
|
41
|
+
``capturer`` distribution: both ship the same files and would overwrite
|
|
42
|
+
each other.
|
|
43
|
+
|
|
44
|
+
If you depend on this package in production, please pin a version and read the
|
|
45
|
+
changelog before upgrading. Reviews, bug reports and co-maintainers are welcome.
|
|
46
|
+
|
|
47
|
+
Status
|
|
48
|
+
------
|
|
49
|
+
|
|
50
|
+
The `capturer` package was developed as a proof of concept over the course of a
|
|
51
|
+
weekend, because I was curious to see if it could be done (reliably). After a
|
|
52
|
+
weekend of extensive testing it seems to work fairly well so I'm publishing the
|
|
53
|
+
initial release as version 1.0, however I still consider this a proof of
|
|
54
|
+
concept because I don't have extensive "production" experience using it yet.
|
|
55
|
+
Here's hoping it works as well in practice as it did during my testing :-).
|
|
56
|
+
|
|
57
|
+
Installation
|
|
58
|
+
------------
|
|
59
|
+
|
|
60
|
+
The `capturer` package is available on PyPI_ which means installation should be
|
|
61
|
+
as simple as:
|
|
62
|
+
|
|
63
|
+
.. code-block:: console
|
|
64
|
+
|
|
65
|
+
$ pip install capturer-ng
|
|
66
|
+
|
|
67
|
+
There's actually a multitude of ways to install Python packages (e.g. the `per
|
|
68
|
+
user site-packages directory`_, `virtual environments`_ or just installing
|
|
69
|
+
system wide) and I have no intention of getting into that discussion here, so
|
|
70
|
+
if this intimidates you then read up on your options before returning to these
|
|
71
|
+
instructions ;-).
|
|
72
|
+
|
|
73
|
+
Getting started
|
|
74
|
+
---------------
|
|
75
|
+
|
|
76
|
+
The easiest way to capture output is to use a context manager:
|
|
77
|
+
|
|
78
|
+
.. code-block:: python
|
|
79
|
+
|
|
80
|
+
import subprocess
|
|
81
|
+
from capturer import CaptureOutput
|
|
82
|
+
|
|
83
|
+
with CaptureOutput() as capturer:
|
|
84
|
+
# Generate some output from Python.
|
|
85
|
+
print "Output from Python"
|
|
86
|
+
# Generate output from a subprocess.
|
|
87
|
+
subprocess.call(["echo", "Output from a subprocess"])
|
|
88
|
+
# Get the output in each of the supported formats.
|
|
89
|
+
assert capturer.get_bytes() == b'Output from Python\r\nOutput from a subprocess\r\n'
|
|
90
|
+
assert capturer.get_lines() == [u'Output from Python', u'Output from a subprocess']
|
|
91
|
+
assert capturer.get_text() == u'Output from Python\nOutput from a subprocess'
|
|
92
|
+
|
|
93
|
+
The use of a context manager (`the with statement`_) ensures that output
|
|
94
|
+
capturing is enabled and disabled at the appropriate time, regardless of
|
|
95
|
+
whether exceptions interrupt the normal flow of processing.
|
|
96
|
+
|
|
97
|
+
Note that the first call to `get_bytes()`_, `get_lines()`_ or `get_text()`_
|
|
98
|
+
will stop the capturing of output by default. This is intended as a sane
|
|
99
|
+
default to prevent partial reads (which can be confusing as hell when you don't
|
|
100
|
+
have experience with them). So we could have simply used ``print`` to show
|
|
101
|
+
the results without causing a recursive "captured output is printed and then
|
|
102
|
+
captured again" loop. There's an optional ``partial=True`` keyword argument
|
|
103
|
+
that can be used to disable this behavior (please refer to the documentation_
|
|
104
|
+
for details).
|
|
105
|
+
|
|
106
|
+
Design choices
|
|
107
|
+
--------------
|
|
108
|
+
|
|
109
|
+
There are existing solutions out there to capture the stdout_ and stderr_
|
|
110
|
+
streams of (Python) processes. The `capturer` package was created for a very
|
|
111
|
+
specific use case that wasn't catered for by existing solutions (that I could
|
|
112
|
+
find). This section documents the design choices that guided the development of
|
|
113
|
+
the `capturer` package:
|
|
114
|
+
|
|
115
|
+
.. contents::
|
|
116
|
+
:local:
|
|
117
|
+
|
|
118
|
+
Intercepts writes to low level file descriptors
|
|
119
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
120
|
+
|
|
121
|
+
Libraries like capture_ and iocapture_ change Python's sys.stdout_ and
|
|
122
|
+
sys.stderr_ file objects to fake file objects (using StringIO_). This enables
|
|
123
|
+
capturing of (most) output written to the stdout_ and stderr_ streams from the
|
|
124
|
+
same Python process, however any output from subprocesses is unaffected by the
|
|
125
|
+
redirection and not captured.
|
|
126
|
+
|
|
127
|
+
The `capturer` package instead intercepts writes to low level file descriptors
|
|
128
|
+
(similar to and inspired by `how pytest does it`_). This enables capturing of
|
|
129
|
+
output written to the standard output and error streams from the same Python
|
|
130
|
+
process as well as any subprocesses.
|
|
131
|
+
|
|
132
|
+
Uses a pseudo terminal to emulate a real terminal
|
|
133
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
134
|
+
|
|
135
|
+
The `capturer` package uses a pseudo terminal created using `pty.openpty()`_ to
|
|
136
|
+
capture output. This means subprocesses will use ANSI escape sequences because
|
|
137
|
+
they think they're connected to a terminal. In the current implementation you
|
|
138
|
+
can't opt out of this, but feel free to submit a feature request to change this
|
|
139
|
+
:-). This does have some drawbacks:
|
|
140
|
+
|
|
141
|
+
- The use of `pty.openpty()`_ means you need to be running in a UNIX like
|
|
142
|
+
environment for `capturer` to work (Windows definitely isn't supported).
|
|
143
|
+
|
|
144
|
+
- All output captured is relayed on the stderr_ stream by default, so capturing
|
|
145
|
+
changes the semantics of your programs. How much this matters obviously
|
|
146
|
+
depends on your use case. For the use cases that triggered me to create
|
|
147
|
+
`capturer` it doesn't matter, which explains why this is the default mode.
|
|
148
|
+
|
|
149
|
+
There is experimental support for capturing stdout_ and stderr_ separately
|
|
150
|
+
and relaying captured output to the appropriate original stream. Basically
|
|
151
|
+
you call ``CaptureOutput(merged=False)`` and then you use the ``stdout`` and
|
|
152
|
+
``stderr`` attributes of the ``CaptureOutput`` object to get at the output
|
|
153
|
+
captured on each stream.
|
|
154
|
+
|
|
155
|
+
I say experimental because this method of capturing can unintentionally
|
|
156
|
+
change the order in which captured output is emitted, in order to avoid
|
|
157
|
+
interleaving output emitted on the stdout_ and stderr_ streams (which would
|
|
158
|
+
most likely result in incomprehensible output). Basically output is relayed
|
|
159
|
+
on each stream separately after each line break. This means interactive
|
|
160
|
+
prompts that block on reading from standard input without emitting a line
|
|
161
|
+
break won't show up (until it's too late ;-).
|
|
162
|
+
|
|
163
|
+
Relays output to the terminal in real time
|
|
164
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
165
|
+
|
|
166
|
+
The main use case of `capturer` is to capture all output of a snippet of Python
|
|
167
|
+
code (including any output by subprocesses) but also relay the output to the
|
|
168
|
+
terminal in real time. This has a couple of useful properties:
|
|
169
|
+
|
|
170
|
+
- Long running operations can provide the operator with real time feedback by
|
|
171
|
+
emitting output on the terminal. This sounds obvious (and it is!) but it is
|
|
172
|
+
non-trivial to implement (an understatement :-) when you *also* want to
|
|
173
|
+
capture the output.
|
|
174
|
+
|
|
175
|
+
- Programs like gpg_ and ssh_ that use interactive password prompts will render
|
|
176
|
+
their password prompt on the terminal in real time. This avoids the awkward
|
|
177
|
+
interaction where a password prompt is silenced but the program still hangs,
|
|
178
|
+
waiting for input on stdin_.
|
|
179
|
+
|
|
180
|
+
Contact
|
|
181
|
+
-------
|
|
182
|
+
|
|
183
|
+
The latest version of `capturer` is available on PyPI_ and GitHub_. The
|
|
184
|
+
documentation is hosted on `Read the Docs`_ and includes a changelog_. For bug
|
|
185
|
+
reports please create an issue on GitHub_. Please do not contact the
|
|
186
|
+
original author about problems with this continuation.
|
|
187
|
+
|
|
188
|
+
License
|
|
189
|
+
-------
|
|
190
|
+
|
|
191
|
+
This software is licensed under the `MIT license`_.
|
|
192
|
+
|
|
193
|
+
© 2020 Peter Odding.
|
|
194
|
+
|
|
195
|
+
A big thanks goes out to the pytest_ developers because pytest's mechanism for
|
|
196
|
+
capturing the output of subprocesses provided inspiration for the `capturer`
|
|
197
|
+
package. No code was copied, but both projects are MIT licensed anyway, so it's
|
|
198
|
+
not like it's very relevant :-).
|
|
199
|
+
|
|
200
|
+
.. External references:
|
|
201
|
+
.. _capture: https://pypi.org/project/capture
|
|
202
|
+
.. _changelog: https://capturer.readthedocs.io/en/latest/changelog.html
|
|
203
|
+
.. _documentation: https://capturer.readthedocs.io
|
|
204
|
+
.. _get_bytes(): https://capturer.readthedocs.io/en/latest/api.html#capturer.CaptureOutput.get_bytes
|
|
205
|
+
.. _get_lines(): https://capturer.readthedocs.io/en/latest/api.html#capturer.CaptureOutput.get_lines
|
|
206
|
+
.. _get_text(): https://capturer.readthedocs.io/en/latest/api.html#capturer.CaptureOutput.get_text
|
|
207
|
+
.. _GitHub: https://github.com/marco8154/python-capturer-ng
|
|
208
|
+
.. _gpg: https://en.wikipedia.org/wiki/GNU_Privacy_Guard
|
|
209
|
+
.. _how pytest does it: https://pytest.org/latest/capture.html
|
|
210
|
+
.. _iocapture: https://pypi.org/project/iocapture
|
|
211
|
+
.. _MIT license: http://en.wikipedia.org/wiki/MIT_License
|
|
212
|
+
.. _per user site-packages directory: https://www.python.org/dev/peps/pep-0370/
|
|
213
|
+
.. _pty.openpty(): https://docs.python.org/2/library/pty.html#pty.openpty
|
|
214
|
+
.. _pty: https://docs.python.org/2/library/pty.html
|
|
215
|
+
.. _PyPI: https://pypi.org/project/capturer-ng
|
|
216
|
+
.. _pytest: https://pypi.org/project/pytest
|
|
217
|
+
.. _Read the Docs: https://capturer.readthedocs.io
|
|
218
|
+
.. _ssh: https://en.wikipedia.org/wiki/Secure_Shell
|
|
219
|
+
.. _stderr: https://en.wikipedia.org/wiki/Standard_streams#Standard_error_.28stderr.29
|
|
220
|
+
.. _stdin: https://en.wikipedia.org/wiki/Standard_streams#Standard_input_.28stdin.29
|
|
221
|
+
.. _stdout: https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29
|
|
222
|
+
.. _StringIO: https://docs.python.org/2/library/stringio.html
|
|
223
|
+
.. _sys.stderr: https://docs.python.org/2/library/sys.html#sys.stderr
|
|
224
|
+
.. _sys.stdout: https://docs.python.org/2/library/sys.html#sys.stdout
|
|
225
|
+
.. _the with statement: https://docs.python.org/2/reference/compound_stmts.html#the-with-statement
|
|
226
|
+
.. _virtual environments: http://docs.python-guide.org/en/latest/dev/virtualenvs/
|