-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathrunners.py
More file actions
1678 lines (1415 loc) · 64.1 KB
/
runners.py
File metadata and controls
1678 lines (1415 loc) · 64.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import errno
import locale
import io
import os
import struct
import sys
import threading
import time
import signal
from subprocess import Popen, PIPE
from types import TracebackType
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Generator,
IO,
List,
Optional,
Tuple,
Type,
)
# Import some platform-specific things at top level so they can be mocked for
# tests.
try:
import pty
except ImportError:
pty = None # type: ignore[assignment]
try:
import fcntl
except ImportError:
fcntl = None # type: ignore[assignment]
try:
import termios
except ImportError:
termios = None # type: ignore[assignment]
from .exceptions import (
UnexpectedExit,
Failure,
ThreadException,
WatcherError,
SubprocessPipeError,
CommandTimedOut,
)
from .terminals import (
WINDOWS,
pty_size,
character_buffered,
ready_for_reading,
bytes_to_read,
)
from .util import has_fileno, isatty, ExceptionHandlingThread
if TYPE_CHECKING:
from .context import Context
from .watchers import StreamWatcher
class Runner:
"""
Partially-abstract core command-running API.
This class is not usable by itself and must be subclassed, implementing a
number of methods such as `start`, `wait` and `returncode`. For a subclass
implementation example, see the source code for `.Local`.
.. versionadded:: 1.0
"""
opts: Dict[str, Any]
using_pty: bool
read_chunk_size = io.DEFAULT_BUFFER_SIZE
input_sleep = 0.01
def __init__(self, context: "Context") -> None:
"""
Create a new runner with a handle on some `.Context`.
:param context:
a `.Context` instance, used to transmit default options and provide
access to other contextualized information (e.g. a remote-oriented
`.Runner` might want a `.Context` subclass holding info about
hostnames and ports.)
.. note::
The `.Context` given to `.Runner` instances **must** contain
default config values for the `.Runner` class in question. At a
minimum, this means values for each of the default
`.Runner.run` keyword arguments such as ``echo`` and ``warn``.
:raises exceptions.ValueError:
if not all expected default values are found in ``context``.
"""
#: The `.Context` given to the same-named argument of `__init__`.
self.context = context
#: A `threading.Event` signaling program completion.
#:
#: Typically set after `wait` returns. Some IO mechanisms rely on this
#: to know when to exit an infinite read loop.
self.program_finished = threading.Event()
# I wish Sphinx would organize all class/instance attrs in the same
# place. If I don't do this here, it goes 'class vars -> __init__
# docstring -> instance vars' :( TODO: consider just merging class and
# __init__ docstrings, though that's annoying too.
#: How many bytes (at maximum) to read per iteration of stream reads.
self.read_chunk_size = self.__class__.read_chunk_size
# Ditto re: declaring this in 2 places for doc reasons.
#: How many seconds to sleep on each iteration of the stdin read loop
#: and other otherwise-fast loops.
self.input_sleep = self.__class__.input_sleep
#: Whether pty fallback warning has been emitted.
self.warned_about_pty_fallback = False
#: A list of `.StreamWatcher` instances for use by `respond`. Is filled
#: in at runtime by `run`.
self.watchers: List["StreamWatcher"] = []
# Optional timeout timer placeholder
self._timer: Optional[threading.Timer] = None
# Async flags (initialized for 'finally' referencing in case something
# goes REAL bad during options parsing)
self._asynchronous = False
self._disowned = False
def run(self, command: str, **kwargs: Any) -> Optional["Result"]:
"""
Execute ``command``, returning an instance of `Result` once complete.
By default, this method is synchronous (it only returns once the
subprocess has completed), and allows interactive keyboard
communication with the subprocess.
It can instead behave asynchronously (returning early & requiring
interaction with the resulting object to manage subprocess lifecycle)
if you specify ``asynchronous=True``. Furthermore, you can completely
disassociate the subprocess from Invoke's control (allowing it to
persist on its own after Python exits) by saying ``disown=True``. See
the per-kwarg docs below for details on both of these.
.. note::
All kwargs will default to the values found in this instance's
`~.Runner.context` attribute, specifically in its configuration's
``run`` subtree (e.g. ``run.echo`` provides the default value for
the ``echo`` keyword, etc). The base default values are described
in the parameter list below.
:param str command: The shell command to execute.
:param bool asynchronous:
When set to ``True`` (default ``False``), enables asynchronous
behavior, as follows:
- Connections to the controlling terminal are disabled, meaning you
will not see the subprocess output and it will not respond to
your keyboard input - similar to ``hide=True`` and
``in_stream=False`` (though explicitly given
``(out|err|in)_stream`` file-like objects will still be honored
as normal).
- `.run` returns immediately after starting the subprocess, and its
return value becomes an instance of `Promise` instead of
`Result`.
- `Promise` objects are primarily useful for their `~Promise.join`
method, which blocks until the subprocess exits (similar to
threading APIs) and either returns a final `~Result` or raises an
exception, just as a synchronous ``run`` would.
- As with threading and similar APIs, users of
``asynchronous=True`` should make sure to ``join`` their
`Promise` objects to prevent issues with interpreter
shutdown.
- One easy way to handle such cleanup is to use the `Promise`
as a context manager - it will automatically ``join`` at the
exit of the context block.
.. versionadded:: 1.4
:param bool disown:
When set to ``True`` (default ``False``), returns immediately like
``asynchronous=True``, but does not perform any background work
related to that subprocess (it is completely ignored). This allows
subprocesses using shell backgrounding or similar techniques (e.g.
trailing ``&``, ``nohup``) to persist beyond the lifetime of the
Python process running Invoke.
.. note::
If you're unsure whether you want this or ``asynchronous``, you
probably want ``asynchronous``!
Specifically, ``disown=True`` has the following behaviors:
- The return value is ``None`` instead of a `Result` or subclass.
- No I/O worker threads are spun up, so you will have no access to
the subprocess' stdout/stderr, your stdin will not be forwarded,
``(out|err|in)_stream`` will be ignored, and features like
``watchers`` will not function.
- No exit code is checked for, so you will not receive any errors
if the subprocess fails to exit cleanly.
- ``pty=True`` may not function correctly (subprocesses may not run
at all; this seems to be a potential bug in Python's
``pty.fork``) unless your command line includes tools such as
``nohup`` or (the shell builtin) ``disown``.
.. versionadded:: 1.4
:param bool dry:
Whether to dry-run instead of truly invoking the given command. See
:option:`--dry` (which flips this on globally) for details on this
behavior.
.. versionadded:: 1.3
:param bool echo:
Controls whether `.run` prints the command string to local stdout
prior to executing it. Default: ``False``.
.. note::
``hide=True`` will override ``echo=True`` if both are given.
:param echo_format:
A string, which when passed to Python's inbuilt ``.format`` method,
will change the format of the output when ``run.echo`` is set to
true.
Currently, only ``{command}`` is supported as a parameter.
Defaults to printing the full command string in ANSI-escaped bold.
:param bool echo_stdin:
Whether to write data from ``in_stream`` back to ``out_stream``.
In other words, in normal interactive usage, this parameter
controls whether Invoke mirrors what you type back to your
terminal.
By default (when ``None``), this behavior is triggered by the
following:
* Not using a pty to run the subcommand (i.e. ``pty=False``),
as ptys natively echo stdin to stdout on their own;
* And when the controlling terminal of Invoke itself (as per
``in_stream``) appears to be a valid terminal device or TTY.
(Specifically, when `~invoke.util.isatty` yields a ``True``
result when given ``in_stream``.)
.. note::
This property tends to be ``False`` when piping another
program's output into an Invoke session, or when running
Invoke within another program (e.g. running Invoke from
itself).
If both of those properties are true, echoing will occur; if either
is false, no echoing will be performed.
When not ``None``, this parameter will override that auto-detection
and force, or disable, echoing.
:param str encoding:
Override auto-detection of which encoding the subprocess is using
for its stdout/stderr streams (which defaults to the return value
of `default_encoding`).
:param err_stream:
Same as ``out_stream``, except for standard error, and defaulting
to ``sys.stderr``.
:param dict env:
By default, subprocesses receive a copy of Invoke's own environment
(i.e. ``os.environ``). Supply a dict here to update that child
environment.
For example, ``run('command', env={'PYTHONPATH':
'/some/virtual/env/maybe'})`` would modify the ``PYTHONPATH`` env
var, with the rest of the child's env looking identical to the
parent.
.. seealso:: ``replace_env`` for changing 'update' to 'replace'.
:param bool fallback:
Controls auto-fallback behavior re: problems offering a pty when
``pty=True``. Whether this has any effect depends on the specific
`Runner` subclass being invoked. Default: ``True``.
:param hide:
Allows the caller to disable ``run``'s default behavior of copying
the subprocess' stdout and stderr to the controlling terminal.
Specify ``hide='out'`` (or ``'stdout'``) to hide only the stdout
stream, ``hide='err'`` (or ``'stderr'``) to hide only stderr, or
``hide='both'`` (or ``True``) to hide both streams.
The default value is ``None``, meaning to print everything;
``False`` will also disable hiding.
.. note::
Stdout and stderr are always captured and stored in the
``Result`` object, regardless of ``hide``'s value.
.. note::
``hide=True`` will also override ``echo=True`` if both are
given (either as kwargs or via config/CLI).
:param in_stream:
A file-like stream object to used as the subprocess' standard
input. If ``None`` (the default), ``sys.stdin`` will be used.
If ``False``, will disable stdin mirroring entirely (though other
functionality which writes to the subprocess' stdin, such as
autoresponding, will still function.) Disabling stdin mirroring can
help when ``sys.stdin`` is a misbehaving non-stream object, such as
under test harnesses or headless command runners.
:param out_stream:
A file-like stream object to which the subprocess' standard output
should be written. If ``None`` (the default), ``sys.stdout`` will
be used.
:param bool pty:
By default, ``run`` connects directly to the invoked process and
reads its stdout/stderr streams. Some programs will buffer (or even
behave) differently in this situation compared to using an actual
terminal or pseudoterminal (pty). To use a pty instead of the
default behavior, specify ``pty=True``.
.. warning::
Due to their nature, ptys have a single output stream, so the
ability to tell stdout apart from stderr is **not possible**
when ``pty=True``. As such, all output will appear on
``out_stream`` (see below) and be captured into the ``stdout``
result attribute. ``err_stream`` and ``stderr`` will always be
empty when ``pty=True``.
:param bool replace_env:
When ``True``, causes the subprocess to receive the dictionary
given to ``env`` as its entire shell environment, instead of
updating a copy of ``os.environ`` (which is the default behavior).
Default: ``False``.
:param str shell:
Which shell binary to use. Default: ``/bin/bash`` (on Unix;
``COMSPEC`` or ``cmd.exe`` on Windows.)
:param timeout:
Cause the runner to submit an interrupt to the subprocess and raise
`.CommandTimedOut`, if the command takes longer than ``timeout``
seconds to execute. Defaults to ``None``, meaning no timeout.
.. versionadded:: 1.3
:param bool warn:
Whether to warn and continue, instead of raising
`.UnexpectedExit`, when the executed command exits with a
nonzero status. Default: ``False``.
.. note::
This setting has no effect on exceptions, which will still be
raised, typically bundled in `.ThreadException` objects if they
were raised by the IO worker threads.
Similarly, `.WatcherError` exceptions raised by
`.StreamWatcher` instances will also ignore this setting, and
will usually be bundled inside `.Failure` objects (in order to
preserve the execution context).
Ditto `.CommandTimedOut` - basically, anything that prevents a
command from actually getting to "exited with an exit code"
ignores this flag.
:param watchers:
A list of `.StreamWatcher` instances which will be used to scan the
program's ``stdout`` or ``stderr`` and may write into its ``stdin``
(typically ``bytes`` objects) in response to patterns or other
heuristics.
See :doc:`/concepts/watchers` for details on this functionality.
Default: ``[]``.
:returns:
`Result`, or a subclass thereof.
:raises:
`.UnexpectedExit`, if the command exited nonzero and
``warn`` was ``False``.
:raises:
`.Failure`, if the command didn't even exit cleanly, e.g. if a
`.StreamWatcher` raised `.WatcherError`.
:raises:
`.ThreadException` (if the background I/O threads encountered
exceptions other than `.WatcherError`).
.. versionadded:: 1.0
"""
try:
return self._run_body(command, **kwargs)
finally:
if not (self._asynchronous or self._disowned):
self.stop()
def echo(self, command: str) -> None:
print(self.opts["echo_format"].format(command=command))
def _setup(self, command: str, kwargs: Any) -> None:
"""
Prepare data on ``self`` so we're ready to start running.
"""
# Normalize kwargs w/ config; sets self.opts, self.streams
self._unify_kwargs_with_config(kwargs)
# Environment setup
self.env = self.generate_env(
self.opts["env"], self.opts["replace_env"]
)
# Arrive at final encoding if neither config nor kwargs had one
self.encoding = self.opts["encoding"] or self.default_encoding()
# Echo running command (wants to be early to be included in dry-run)
if self.opts["echo"]:
self.echo(command)
# Prepare common result args.
# TODO: I hate this. Needs a deeper separate think about tweaking
# Runner.generate_result in a way that isn't literally just this same
# two-step process, and which also works w/ downstream.
self.result_kwargs = dict(
command=command,
shell=self.opts["shell"],
env=self.env,
pty=self.using_pty,
hide=self.opts["hide"],
encoding=self.encoding,
)
def _run_body(self, command: str, **kwargs: Any) -> Optional["Result"]:
# Prepare all the bits n bobs.
self._setup(command, kwargs)
# If dry-run, stop here.
if self.opts["dry"]:
return self.generate_result(
**dict(self.result_kwargs, stdout="", stderr="", exited=0)
)
# Start executing the actual command (runs in background)
self.start(command, self.opts["shell"], self.env)
# If disowned, we just stop here - no threads, no timer, no error
# checking, nada.
if self._disowned:
return None
# Stand up & kick off IO, timer threads
self.start_timer(self.opts["timeout"])
self.threads, self.stdout, self.stderr = self.create_io_threads()
for thread in self.threads.values():
thread.start()
# Wrap up or promise that we will, depending
return self.make_promise() if self._asynchronous else self._finish()
def make_promise(self) -> "Promise":
"""
Return a `Promise` allowing async control of the rest of lifecycle.
.. versionadded:: 1.4
"""
return Promise(self)
def _finish(self) -> "Result":
# Wait for subprocess to run, forwarding signals as we get them.
try:
while True:
try:
self.wait()
break # done waiting!
# Don't locally stop on ^C, only forward it:
# - if remote end really stops, we'll naturally stop after
# - if remote end does not stop (eg REPL, editor) we don't want
# to stop prematurely
except KeyboardInterrupt as e:
self.send_interrupt(e)
# TODO: honor other signals sent to our own process and
# transmit them to the subprocess before handling 'normally'.
# Make sure we tie off our worker threads, even if something exploded.
# Any exceptions that raised during self.wait() above will appear after
# this block.
finally:
# Inform stdin-mirroring worker to stop its eternal looping
self.program_finished.set()
# Join threads, storing inner exceptions, & set a timeout if
# necessary. (Segregate WatcherErrors as they are "anticipated
# errors" that want to show up at the end during creation of
# Failure objects.)
watcher_errors = []
thread_exceptions = []
for target, thread in self.threads.items():
thread.join(self._thread_join_timeout(target))
exception = thread.exception()
if exception is not None:
real = exception.value
if isinstance(real, WatcherError):
watcher_errors.append(real)
else:
thread_exceptions.append(exception)
# If any exceptions appeared inside the threads, raise them now as an
# aggregate exception object.
# NOTE: this is kept outside the 'finally' so that main-thread
# exceptions are raised before worker-thread exceptions; they're more
# likely to be Big Serious Problems.
if thread_exceptions:
raise ThreadException(thread_exceptions)
# Collate stdout/err, calculate exited, and get final result obj
result = self._collate_result(watcher_errors)
# Any presence of WatcherError from the threads indicates a watcher was
# upset and aborted execution; make a generic Failure out of it and
# raise that.
if watcher_errors:
# TODO: ambiguity exists if we somehow get WatcherError in *both*
# threads...as unlikely as that would normally be.
raise Failure(result, reason=watcher_errors[0])
# If a timeout was requested and the subprocess did time out, shout.
timeout = self.opts["timeout"]
if timeout is not None and self.timed_out:
raise CommandTimedOut(result, timeout=timeout)
if not (result or self.opts["warn"]):
raise UnexpectedExit(result)
return result
def _unify_kwargs_with_config(self, kwargs: Any) -> None:
"""
Unify `run` kwargs with config options to arrive at local options.
Sets:
- ``self.opts`` - opts dict
- ``self.streams`` - map of stream names to stream target values
"""
opts = {}
for key, value in self.context.config.run.items():
runtime = kwargs.pop(key, None)
opts[key] = value if runtime is None else runtime
# Pull in command execution timeout, which stores config elsewhere,
# but only use it if it's actually set (backwards compat)
config_timeout = self.context.config.timeouts.command
opts["timeout"] = kwargs.pop("timeout", config_timeout)
# Handle invalid kwarg keys (anything left in kwargs).
# Act like a normal function would, i.e. TypeError
if kwargs:
err = "run() got an unexpected keyword argument '{}'"
raise TypeError(err.format(list(kwargs.keys())[0]))
# Update disowned, async flags
self._asynchronous = opts["asynchronous"]
self._disowned = opts["disown"]
if self._asynchronous and self._disowned:
err = "Cannot give both 'asynchronous' and 'disown' at the same time!" # noqa
raise ValueError(err)
# If hide was True, turn off echoing
if opts["hide"] is True:
opts["echo"] = False
# Conversely, ensure echoing is always on when dry-running
if opts["dry"] is True:
opts["echo"] = True
# Always hide if async
if self._asynchronous:
opts["hide"] = True
# Then normalize 'hide' from one of the various valid input values,
# into a stream-names tuple. Also account for the streams.
out_stream, err_stream = opts["out_stream"], opts["err_stream"]
opts["hide"] = normalize_hide(opts["hide"], out_stream, err_stream)
# Derive stream objects
if out_stream is None:
out_stream = sys.stdout
if err_stream is None:
err_stream = sys.stderr
in_stream = opts["in_stream"]
if in_stream is None:
# If in_stream hasn't been overridden, and we're async, we don't
# want to read from sys.stdin (otherwise the default) - so set
# False instead.
in_stream = False if self._asynchronous else sys.stdin
# Determine pty or no
self.using_pty = self.should_use_pty(opts["pty"], opts["fallback"])
if opts["watchers"]:
self.watchers = opts["watchers"]
# Set data
self.opts = opts
self.streams = {"out": out_stream, "err": err_stream, "in": in_stream}
def _collate_result(self, watcher_errors: List[WatcherError]) -> "Result":
# At this point, we had enough success that we want to be returning or
# raising detailed info about our execution; so we generate a Result.
stdout = "".join(self.stdout)
stderr = "".join(self.stderr)
if WINDOWS:
# "Universal newlines" - replace all standard forms of
# newline with \n. This is not technically Windows related
# (\r as newline is an old Mac convention) but we only apply
# the translation for Windows as that's the only platform
# it is likely to matter for these days.
stdout = stdout.replace("\r\n", "\n").replace("\r", "\n")
stderr = stderr.replace("\r\n", "\n").replace("\r", "\n")
# Get return/exit code, unless there were WatcherErrors to handle.
# NOTE: In that case, returncode() may block waiting on the process
# (which may be waiting for user input). Since most WatcherError
# situations lack a useful exit code anyways, skipping this doesn't
# really hurt any.
exited = None if watcher_errors else self.returncode()
# TODO: as noted elsewhere, I kinda hate this. Consider changing
# generate_result()'s API in next major rev so we can tidy up.
result = self.generate_result(
**dict(
self.result_kwargs, stdout=stdout, stderr=stderr, exited=exited
)
)
return result
def _thread_join_timeout(self, target: Callable) -> Optional[int]:
# Add a timeout to out/err thread joins when it looks like they're not
# dead but their counterpart is dead; this indicates issue #351 (fixed
# by #432) where the subproc may hang because its stdout (or stderr) is
# no longer being consumed by the dead thread (and a pipe is filling
# up.) In that case, the non-dead thread is likely to block forever on
# a `recv` unless we add this timeout.
if target == self.handle_stdin:
return None
opposite = self.handle_stderr
if target == self.handle_stderr:
opposite = self.handle_stdout
if opposite in self.threads and self.threads[opposite].is_dead:
return 1
return None
def create_io_threads(
self,
) -> Tuple[Dict[Callable, ExceptionHandlingThread], List[str], List[str]]:
"""
Create and return a dictionary of IO thread worker objects.
Caller is expected to handle persisting and/or starting the wrapped
threads.
"""
stdout: List[str] = []
stderr: List[str] = []
# Set up IO thread parameters (format - body_func: {kwargs})
thread_args: Dict[Callable, Any] = {
self.handle_stdout: {
"buffer_": stdout,
"hide": "stdout" in self.opts["hide"],
"output": self.streams["out"],
}
}
# After opt processing above, in_stream will be a real stream obj or
# False, so we can truth-test it. We don't even create a stdin-handling
# thread if it's False, meaning user indicated stdin is nonexistent or
# problematic.
if self.streams["in"]:
thread_args[self.handle_stdin] = {
"input_": self.streams["in"],
"output": self.streams["out"],
"echo": self.opts["echo_stdin"],
}
if not self.using_pty:
thread_args[self.handle_stderr] = {
"buffer_": stderr,
"hide": "stderr" in self.opts["hide"],
"output": self.streams["err"],
}
# Kick off IO threads
threads = {}
for target, kwargs in thread_args.items():
t = ExceptionHandlingThread(target=target, kwargs=kwargs)
threads[target] = t
return threads, stdout, stderr
def generate_result(self, **kwargs: Any) -> "Result":
"""
Create & return a suitable `Result` instance from the given ``kwargs``.
Subclasses may wish to override this in order to manipulate things or
generate a `Result` subclass (e.g. ones containing additional metadata
besides the default).
.. versionadded:: 1.0
"""
return Result(**kwargs)
def read_proc_output(self, reader: Callable) -> Generator[str, None, None]:
"""
Iteratively read & decode bytes from a subprocess' out/err stream.
:param reader:
A literal reader function/partial, wrapping the actual stream
object in question, which takes a number of bytes to read, and
returns that many bytes (or ``None``).
``reader`` should be a reference to either `read_proc_stdout` or
`read_proc_stderr`, which perform the actual, platform/library
specific read calls.
:returns:
A generator yielding strings.
Specifically, each resulting string is the result of decoding
`read_chunk_size` bytes read from the subprocess' out/err stream.
.. versionadded:: 1.0
"""
# NOTE: Typically, reading from any stdout/err (local, remote or
# otherwise) can be thought of as "read until you get nothing back".
# This is preferable over "wait until an out-of-band signal claims the
# process is done running" because sometimes that signal will appear
# before we've actually read all the data in the stream (i.e.: a race
# condition).
while True:
data = reader(self.read_chunk_size)
if not data:
break
yield self.decode(data)
def write_our_output(self, stream: IO, string: str) -> None:
"""
Write ``string`` to ``stream``.
Also calls ``.flush()`` on ``stream`` to ensure that real terminal
streams don't buffer.
:param stream:
A file-like stream object, mapping to the ``out_stream`` or
``err_stream`` parameters of `run`.
:param string: A Unicode string object.
:returns: ``None``.
.. versionadded:: 1.0
"""
stream.write(string)
stream.flush()
def _handle_output(
self,
buffer_: List[str],
hide: bool,
output: IO,
reader: Callable,
) -> None:
# TODO: store un-decoded/raw bytes somewhere as well...
for data in self.read_proc_output(reader):
# Echo to local stdout if necessary
# TODO: should we rephrase this as "if you want to hide, give me a
# dummy output stream, e.g. something like /dev/null"? Otherwise, a
# combo of 'hide=stdout' + 'here is an explicit out_stream' means
# out_stream is never written to, and that seems...odd.
if not hide:
self.write_our_output(stream=output, string=data)
# Store in shared buffer so main thread can do things with the
# result after execution completes.
# NOTE: this is threadsafe insofar as no reading occurs until after
# the thread is join()'d.
buffer_.append(data)
# Run our specific buffer through the autoresponder framework
self.respond(buffer_)
def handle_stdout(
self, buffer_: List[str], hide: bool, output: IO
) -> None:
"""
Read process' stdout, storing into a buffer & printing/parsing.
Intended for use as a thread target. Only terminates when all stdout
from the subprocess has been read.
:param buffer_: The capture buffer shared with the main thread.
:param bool hide: Whether or not to replay data into ``output``.
:param output:
Output stream (file-like object) to write data into when not
hiding.
:returns: ``None``.
.. versionadded:: 1.0
"""
self._handle_output(
buffer_, hide, output, reader=self.read_proc_stdout
)
def handle_stderr(
self, buffer_: List[str], hide: bool, output: IO
) -> None:
"""
Read process' stderr, storing into a buffer & printing/parsing.
Identical to `handle_stdout` except for the stream read from; see its
docstring for API details.
.. versionadded:: 1.0
"""
self._handle_output(
buffer_, hide, output, reader=self.read_proc_stderr
)
def read_our_stdin(self, input_: IO) -> Optional[str]:
"""
Read & decode bytes from a local stdin stream.
:param input_:
Actual stream object to read from. Maps to ``in_stream`` in `run`,
so will often be ``sys.stdin``, but might be any stream-like
object.
:returns:
A Unicode string, the result of decoding the read bytes (this might
be the empty string if the pipe has closed/reached EOF); or
``None`` if stdin wasn't ready for reading yet.
.. versionadded:: 1.0
"""
# TODO: consider moving the character_buffered contextmanager call in
# here? Downside is it would be flipping those switches for every byte
# read instead of once per session, which could be costly (?).
bytes_ = None
if ready_for_reading(input_):
try:
bytes_ = input_.read(bytes_to_read(input_))
except OSError as e:
# Assume EBADF in this situation implies running under nohup or
# similar, where:
# - we cannot reliably detect a bad FD up front
# - trying to read it would explode
# - user almost surely doesn't care about stdin anyways
# and ignore it (but not other OSErrors!)
if e.errno != errno.EBADF:
raise
# Decode if it appears to be binary-type. (From real terminal
# streams, usually yes; from file-like objects, often no.)
if bytes_ and isinstance(bytes_, bytes):
# TODO: will decoding 1 byte at a time break multibyte
# character encodings? How to square interactivity with that?
bytes_ = self.decode(bytes_)
return bytes_
def handle_stdin(
self,
input_: IO,
output: IO,
echo: bool = False,
) -> None:
"""
Read local stdin, copying into process' stdin as necessary.
Intended for use as a thread target.
.. note::
Because real terminal stdin streams have no well-defined "end", if
such a stream is detected (based on existence of a callable
``.fileno()``) this method will wait until `program_finished` is
set, before terminating.
When the stream doesn't appear to be from a terminal, the same
semantics as `handle_stdout` are used - the stream is simply
``read()`` from until it returns an empty value.
:param input_: Stream (file-like object) from which to read.
:param output: Stream (file-like object) to which echoing may occur.
:param bool echo: User override option for stdin-stdout echoing.
:returns: ``None``.
.. versionadded:: 1.0
"""
# TODO: reinstate lock/whatever thread logic from fab v1 which prevents
# reading from stdin while other parts of the code are prompting for
# runtime passwords? (search for 'input_enabled')
# TODO: fabric#1339 is strongly related to this, if it's not literally
# exposing some regression in Fabric 1.x itself.
closed_stdin = False
with character_buffered(input_):
while True:
data = self.read_our_stdin(input_)
if data:
# Mirror what we just read to process' stdin.
# We encode to ensure bytes, but skip the decode step since
# there's presumably no need (nobody's interacting with
# this data programmatically).
self.write_proc_stdin(data)
# Also echo it back to local stdout (or whatever
# out_stream is set to) when necessary.
if echo is None:
echo = self.should_echo_stdin(input_, output)
if echo:
self.write_our_output(stream=output, string=data)
# Empty string/char/byte != None. Can't just use 'else' here.
elif data is not None:
# When reading from file-like objects that aren't "real"
# terminal streams, an empty byte signals EOF.
if not self.using_pty and not closed_stdin:
self.close_proc_stdin()
closed_stdin = True
# Dual all-done signals: program being executed is done
# running, *and* we don't seem to be reading anything out of
# stdin. (NOTE: If we only test the former, we may encounter
# race conditions re: unread stdin.)
if self.program_finished.is_set() and not data:
break
# When data is None, we're waiting for input on stdin.
# Take a nap so we're not chewing CPU.
if data is None:
time.sleep(self.input_sleep)
def should_echo_stdin(self, input_: IO, output: IO) -> bool:
"""
Determine whether data read from ``input_`` should echo to ``output``.
Used by `handle_stdin`; tests attributes of ``input_`` and ``output``.
:param input_: Input stream (file-like object).
:param output: Output stream (file-like object).
:returns: A ``bool``.
.. versionadded:: 1.0
"""
return (not self.using_pty) and isatty(input_)
def respond(self, buffer_: List[str]) -> None:
"""
Write to the program's stdin in response to patterns in ``buffer_``.
The patterns and responses are driven by the `.StreamWatcher` instances
from the ``watchers`` kwarg of `run` - see :doc:`/concepts/watchers`
for a conceptual overview.
:param buffer:
The capture buffer for this thread's particular IO stream.
:returns: ``None``.
.. versionadded:: 1.0
"""
# Join buffer contents into a single string; without this,
# StreamWatcher subclasses can't do things like iteratively scan for
# pattern matches.
# NOTE: using string.join should be "efficient enough" for now, re:
# speed and memory use. Should that become false, consider using
# StringIO or cStringIO (tho the latter doesn't do Unicode well?) which
# is apparently even more efficient.
stream = "".join(buffer_)
for watcher in self.watchers:
for response in watcher.submit(stream):
self.write_proc_stdin(response)
def generate_env(
self, env: Dict[str, Any], replace_env: bool
) -> Dict[str, Any]:
"""
Return a suitable environment dict based on user input & behavior.
:param dict env: Dict supplying overrides or full env, depending.
:param bool replace_env:
Whether ``env`` updates, or is used in place of, the value of
`os.environ`.
:returns: A dictionary of shell environment vars.
.. versionadded:: 1.0
"""
return env if replace_env else dict(os.environ, **env)
def should_use_pty(self, pty: bool, fallback: bool) -> bool:
"""
Should execution attempt to use a pseudo-terminal?
:param bool pty:
Whether the user explicitly asked for a pty.
:param bool fallback:
Whether falling back to non-pty execution should be allowed, in
situations where ``pty=True`` but a pty could not be allocated.
.. versionadded:: 1.0
"""
# NOTE: fallback not used: no falling back implemented by default.
return pty
@property
def has_dead_threads(self) -> bool:
"""
Detect whether any IO threads appear to have terminated unexpectedly.
Used during process-completion waiting (in `wait`) to ensure we don't
deadlock our child process if our IO processing threads have
errored/died.
:returns:
``True`` if any threads appear to have terminated with an
exception, ``False`` otherwise.
.. versionadded:: 1.0
"""
return any(x.is_dead for x in self.threads.values())
def wait(self) -> None:
"""
Block until the running command appears to have exited.
:returns: ``None``.
.. versionadded:: 1.0
"""