Utils¶
Internal subroutines for e.g. aborting execution with an error message, or performing indenting on multiline output.
-
fabric.utils.
abort
(msg)¶ Abort execution, print
msg
to stderr and exit with error status (1.)This function currently makes use of SystemExit in a manner that is similar to sys.exit (but which skips the automatic printing to stderr, allowing us to more tightly control it via settings).
Therefore, it’s possible to detect and recover from inner calls to
abort
by usingexcept SystemExit
or similar.
-
fabric.utils.
error
(message, func=None, exception=None, stdout=None, stderr=None)¶ Call
func
with given errormessage
.If
func
is None (the default), the value ofenv.warn_only
determines whether to callabort
orwarn
.If
exception
is given, it is inspected to get a string message, which is printed alongside the user-generatedmessage
.If
stdout
and/orstderr
are given, they are assumed to be strings to be printed.
-
fabric.utils.
fastprint
(text, show_prefix=False, end='', flush=True)¶ Print
text
immediately, without any prefix or line ending.This function is simply an alias of
puts
with different default argument values, such that thetext
is printed without any embellishment and immediately flushed.It is useful for any situation where you wish to print text which might otherwise get buffered by Python’s output buffering (such as within a processor intensive
for
loop). Since such use cases typically also require a lack of line endings (such as printing a series of dots to signify progress) it also omits the traditional newline by default.Note
Since
fastprint
callsputs
, it is likewise subject to theuser
output level.See also
-
fabric.utils.
indent
(text, spaces=4, strip=False)¶ Return
text
indented by the given number of spaces.If text is not a string, it is assumed to be a list of lines and will be joined by
\n
prior to indenting.When
strip
isTrue
, a minimum amount of whitespace is removed from the left-hand side of the given string (so that relative indents are preserved, but otherwise things are left-stripped). This allows you to effectively “normalize” any previous indentation for some inputs.
-
fabric.utils.
isatty
(stream)¶ Check if a stream is a tty.
Not all file-like objects implement the
isatty
method.
-
fabric.utils.
puts
(text, show_prefix=None, end='\n', flush=False)¶ An alias for
print
whose output is managed by Fabric’s output controls.In other words, this function simply prints to
sys.stdout
, but will hide its output if theuser
output level is set toFalse
.If
show_prefix=False
,puts
will omit the leading[hostname]
which it tacks on by default. (It will also omit this prefix ifenv.host_string
is empty.)Newlines may be disabled by setting
end
to the empty string (''
). (This intentionally mirrors Python 3’sprint
syntax.)You may force output flushing (e.g. to bypass output buffering) by setting
flush=True
.See also
-
fabric.utils.
warn
(msg)¶ Print warning message, but do not abort execution.
This function honors Fabric’s output controls and will print the given
msg
to stderr, provided that thewarnings
output level (which is active by default) is turned on.