2018-05-06 21:58:06 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
2016-07-26 00:59:09 +00:00
|
|
|
# Copyright (c) 2016 Google, Inc
|
|
|
|
#
|
|
|
|
# Terminal output logging.
|
|
|
|
#
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2020-04-18 00:09:04 +00:00
|
|
|
from patman import terminal
|
2016-07-26 00:59:09 +00:00
|
|
|
|
|
|
|
# Output verbosity levels that we support
|
2019-07-20 18:23:35 +00:00
|
|
|
ERROR, WARNING, NOTICE, INFO, DETAIL, DEBUG = range(6)
|
2016-07-26 00:59:09 +00:00
|
|
|
|
2018-10-02 03:12:45 +00:00
|
|
|
in_progress = False
|
|
|
|
|
2016-07-26 00:59:09 +00:00
|
|
|
"""
|
|
|
|
This class handles output of progress and other useful information
|
|
|
|
to the user. It provides for simple verbosity level control and can
|
|
|
|
output nothing but errors at verbosity zero.
|
|
|
|
|
|
|
|
The idea is that modules set up an Output object early in their years and pass
|
|
|
|
it around to other modules that need it. This keeps the output under control
|
|
|
|
of a single class.
|
|
|
|
|
|
|
|
Public properties:
|
|
|
|
verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug
|
|
|
|
"""
|
|
|
|
def __enter__():
|
|
|
|
return
|
|
|
|
|
|
|
|
def __exit__(unused1, unused2, unused3):
|
|
|
|
"""Clean up and remove any progress message."""
|
|
|
|
ClearProgress()
|
|
|
|
return False
|
|
|
|
|
|
|
|
def UserIsPresent():
|
|
|
|
"""This returns True if it is likely that a user is present.
|
|
|
|
|
|
|
|
Sometimes we want to prompt the user, but if no one is there then this
|
|
|
|
is a waste of time, and may lock a script which should otherwise fail.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if it thinks the user is there, and False otherwise
|
|
|
|
"""
|
|
|
|
return stdout_is_tty and verbose > 0
|
|
|
|
|
|
|
|
def ClearProgress():
|
|
|
|
"""Clear any active progress message on the terminal."""
|
2018-10-02 03:12:45 +00:00
|
|
|
global in_progress
|
|
|
|
if verbose > 0 and stdout_is_tty and in_progress:
|
2016-07-26 00:59:09 +00:00
|
|
|
_stdout.write('\r%s\r' % (" " * len (_progress)))
|
|
|
|
_stdout.flush()
|
2018-10-02 03:12:45 +00:00
|
|
|
in_progress = False
|
2016-07-26 00:59:09 +00:00
|
|
|
|
|
|
|
def Progress(msg, warning=False, trailer='...'):
|
|
|
|
"""Display progress information.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
msg: Message to display.
|
|
|
|
warning: True if this is a warning."""
|
2018-10-02 03:12:45 +00:00
|
|
|
global in_progress
|
2016-07-26 00:59:09 +00:00
|
|
|
ClearProgress()
|
|
|
|
if verbose > 0:
|
|
|
|
_progress = msg + trailer
|
|
|
|
if stdout_is_tty:
|
|
|
|
col = _color.YELLOW if warning else _color.GREEN
|
|
|
|
_stdout.write('\r' + _color.Color(col, _progress))
|
|
|
|
_stdout.flush()
|
2018-10-02 03:12:45 +00:00
|
|
|
in_progress = True
|
2016-07-26 00:59:09 +00:00
|
|
|
else:
|
|
|
|
_stdout.write(_progress + '\n')
|
|
|
|
|
|
|
|
def _Output(level, msg, color=None):
|
|
|
|
"""Output a message to the terminal.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
level: Verbosity level for this message. It will only be displayed if
|
|
|
|
this as high as the currently selected level.
|
|
|
|
msg; Message to display.
|
|
|
|
error: True if this is an error message, else False.
|
|
|
|
"""
|
|
|
|
if verbose >= level:
|
|
|
|
ClearProgress()
|
|
|
|
if color:
|
|
|
|
msg = _color.Color(color, msg)
|
2019-07-20 18:23:26 +00:00
|
|
|
print(msg)
|
2016-07-26 00:59:09 +00:00
|
|
|
|
|
|
|
def DoOutput(level, msg):
|
|
|
|
"""Output a message to the terminal.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
level: Verbosity level for this message. It will only be displayed if
|
|
|
|
this as high as the currently selected level.
|
|
|
|
msg; Message to display.
|
|
|
|
"""
|
|
|
|
_Output(level, msg)
|
|
|
|
|
|
|
|
def Error(msg):
|
|
|
|
"""Display an error message
|
|
|
|
|
|
|
|
Args:
|
|
|
|
msg; Message to display.
|
|
|
|
"""
|
2019-07-20 18:23:35 +00:00
|
|
|
_Output(ERROR, msg, _color.RED)
|
2016-07-26 00:59:09 +00:00
|
|
|
|
|
|
|
def Warning(msg):
|
|
|
|
"""Display a warning message
|
|
|
|
|
|
|
|
Args:
|
|
|
|
msg; Message to display.
|
|
|
|
"""
|
2019-07-20 18:23:35 +00:00
|
|
|
_Output(WARNING, msg, _color.YELLOW)
|
2016-07-26 00:59:09 +00:00
|
|
|
|
|
|
|
def Notice(msg):
|
|
|
|
"""Display an important infomation message
|
|
|
|
|
|
|
|
Args:
|
|
|
|
msg; Message to display.
|
|
|
|
"""
|
2019-07-20 18:23:35 +00:00
|
|
|
_Output(NOTICE, msg)
|
2016-07-26 00:59:09 +00:00
|
|
|
|
|
|
|
def Info(msg):
|
|
|
|
"""Display an infomation message
|
|
|
|
|
|
|
|
Args:
|
|
|
|
msg; Message to display.
|
|
|
|
"""
|
2019-07-20 18:23:35 +00:00
|
|
|
_Output(INFO, msg)
|
2016-07-26 00:59:09 +00:00
|
|
|
|
2019-07-08 20:25:49 +00:00
|
|
|
def Detail(msg):
|
|
|
|
"""Display a detailed message
|
|
|
|
|
|
|
|
Args:
|
|
|
|
msg; Message to display.
|
|
|
|
"""
|
2019-07-20 18:23:35 +00:00
|
|
|
_Output(DETAIL, msg)
|
2019-07-08 20:25:49 +00:00
|
|
|
|
2016-07-26 00:59:09 +00:00
|
|
|
def Debug(msg):
|
|
|
|
"""Display a debug message
|
|
|
|
|
|
|
|
Args:
|
|
|
|
msg; Message to display.
|
|
|
|
"""
|
2019-07-20 18:23:35 +00:00
|
|
|
_Output(DEBUG, msg)
|
2016-07-26 00:59:09 +00:00
|
|
|
|
|
|
|
def UserOutput(msg):
|
|
|
|
"""Display a message regardless of the current output level.
|
|
|
|
|
|
|
|
This is used when the output was specifically requested by the user.
|
|
|
|
Args:
|
|
|
|
msg; Message to display.
|
|
|
|
"""
|
|
|
|
_Output(0, msg)
|
|
|
|
|
|
|
|
def Init(_verbose=WARNING, stdout=sys.stdout):
|
|
|
|
"""Initialize a new output object.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
verbose: Verbosity level (0-4).
|
|
|
|
stdout: File to use for stdout.
|
|
|
|
"""
|
|
|
|
global verbose, _progress, _color, _stdout, stdout_is_tty
|
|
|
|
|
|
|
|
verbose = _verbose
|
|
|
|
_progress = '' # Our last progress message
|
|
|
|
_color = terminal.Color()
|
|
|
|
_stdout = stdout
|
|
|
|
|
|
|
|
# TODO(sjg): Move this into Chromite libraries when we have them
|
|
|
|
stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
|
|
|
|
|
|
|
|
def Uninit():
|
|
|
|
ClearProgress()
|
|
|
|
|
|
|
|
Init()
|