tout.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2016 Google, Inc
  3. #
  4. # Terminal output logging.
  5. #
  6. import sys
  7. from patman import terminal
  8. # Output verbosity levels that we support
  9. ERROR, WARNING, NOTICE, INFO, DETAIL, DEBUG = range(6)
  10. in_progress = False
  11. """
  12. This class handles output of progress and other useful information
  13. to the user. It provides for simple verbosity level control and can
  14. output nothing but errors at verbosity zero.
  15. The idea is that modules set up an Output object early in their years and pass
  16. it around to other modules that need it. This keeps the output under control
  17. of a single class.
  18. Public properties:
  19. verbose: Verbosity level: 0=silent, 1=progress, 3=full, 4=debug
  20. """
  21. def __enter__():
  22. return
  23. def __exit__(unused1, unused2, unused3):
  24. """Clean up and remove any progress message."""
  25. ClearProgress()
  26. return False
  27. def UserIsPresent():
  28. """This returns True if it is likely that a user is present.
  29. Sometimes we want to prompt the user, but if no one is there then this
  30. is a waste of time, and may lock a script which should otherwise fail.
  31. Returns:
  32. True if it thinks the user is there, and False otherwise
  33. """
  34. return stdout_is_tty and verbose > 0
  35. def ClearProgress():
  36. """Clear any active progress message on the terminal."""
  37. global in_progress
  38. if verbose > 0 and stdout_is_tty and in_progress:
  39. _stdout.write('\r%s\r' % (" " * len (_progress)))
  40. _stdout.flush()
  41. in_progress = False
  42. def Progress(msg, warning=False, trailer='...'):
  43. """Display progress information.
  44. Args:
  45. msg: Message to display.
  46. warning: True if this is a warning."""
  47. global in_progress
  48. ClearProgress()
  49. if verbose > 0:
  50. _progress = msg + trailer
  51. if stdout_is_tty:
  52. col = _color.YELLOW if warning else _color.GREEN
  53. _stdout.write('\r' + _color.Color(col, _progress))
  54. _stdout.flush()
  55. in_progress = True
  56. else:
  57. _stdout.write(_progress + '\n')
  58. def _Output(level, msg, color=None):
  59. """Output a message to the terminal.
  60. Args:
  61. level: Verbosity level for this message. It will only be displayed if
  62. this as high as the currently selected level.
  63. msg; Message to display.
  64. error: True if this is an error message, else False.
  65. """
  66. if verbose >= level:
  67. ClearProgress()
  68. if color:
  69. msg = _color.Color(color, msg)
  70. print(msg)
  71. def DoOutput(level, msg):
  72. """Output a message to the terminal.
  73. Args:
  74. level: Verbosity level for this message. It will only be displayed if
  75. this as high as the currently selected level.
  76. msg; Message to display.
  77. """
  78. _Output(level, msg)
  79. def Error(msg):
  80. """Display an error message
  81. Args:
  82. msg; Message to display.
  83. """
  84. _Output(ERROR, msg, _color.RED)
  85. def Warning(msg):
  86. """Display a warning message
  87. Args:
  88. msg; Message to display.
  89. """
  90. _Output(WARNING, msg, _color.YELLOW)
  91. def Notice(msg):
  92. """Display an important infomation message
  93. Args:
  94. msg; Message to display.
  95. """
  96. _Output(NOTICE, msg)
  97. def Info(msg):
  98. """Display an infomation message
  99. Args:
  100. msg; Message to display.
  101. """
  102. _Output(INFO, msg)
  103. def Detail(msg):
  104. """Display a detailed message
  105. Args:
  106. msg; Message to display.
  107. """
  108. _Output(DETAIL, msg)
  109. def Debug(msg):
  110. """Display a debug message
  111. Args:
  112. msg; Message to display.
  113. """
  114. _Output(DEBUG, msg)
  115. def UserOutput(msg):
  116. """Display a message regardless of the current output level.
  117. This is used when the output was specifically requested by the user.
  118. Args:
  119. msg; Message to display.
  120. """
  121. _Output(0, msg)
  122. def Init(_verbose=WARNING, stdout=sys.stdout):
  123. """Initialize a new output object.
  124. Args:
  125. verbose: Verbosity level (0-4).
  126. stdout: File to use for stdout.
  127. """
  128. global verbose, _progress, _color, _stdout, stdout_is_tty
  129. verbose = _verbose
  130. _progress = '' # Our last progress message
  131. _color = terminal.Color()
  132. _stdout = stdout
  133. # TODO(sjg): Move this into Chromite libraries when we have them
  134. stdout_is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
  135. def Uninit():
  136. ClearProgress()
  137. Init()