LGTM.

Thanks,
Jose

On Mon, Nov 18, 2013 at 10:34:49AM +0100, Petr Pudlak wrote:
> It uses `tput` to check if the current terminal has enough colors.
> 
> Signed-off-by: Petr Pudlak <[email protected]>
> ---
>  qa/colors.py    | 21 +++++++++++++++++++++
>  qa/ganeti-qa.py |  2 ++
>  2 files changed, 23 insertions(+)
> 
> diff --git a/qa/colors.py b/qa/colors.py
> index 1ca58b0..77cee42 100644
> --- a/qa/colors.py
> +++ b/qa/colors.py
> @@ -22,10 +22,12 @@
>  """Script for adding colorized output to Ganeti.
>  
>  Colors are enabled only if the standard output is a proper terminal.
> +(Or call check_for_colors() to make a thorough test using "tput".)
>  
>  """
>  
>  import os
> +import subprocess
>  import sys
>  
>  DEFAULT = '\033[0m'
> @@ -47,3 +49,22 @@ def colorize(line, color=None):
>      return color + line + DEFAULT
>    else:
>      return line
> +
> +
> +def check_for_colors():
> +  """Tries to call 'tput' to properly determine, if the terminal has colors.
> +
> +  This functions is meant to be run once at the program's start. If not
> +  invoked, colors are enabled iff standard output is a terminal.
> +  """
> +  colors = 0
> +  if sys.stdout.isatty():
> +    try:
> +      p = subprocess.Popen(["tput", "colors"], stdout=subprocess.PIPE)
> +      output = p.communicate()[0]
> +      if p.returncode == 0:
> +        colors = int(output)
> +    except (OSError, ValueError):
> +      pass
> +  global _enabled
> +  _enabled = (colors >= 2)
> diff --git a/qa/ganeti-qa.py b/qa/ganeti-qa.py
> index 425a026..54b821e 100755
> --- a/qa/ganeti-qa.py
> +++ b/qa/ganeti-qa.py
> @@ -953,6 +953,8 @@ def main():
>    """Main program.
>  
>    """
> +  colors.check_for_colors()
> +
>    parser = optparse.OptionParser(usage="%prog [options] <config-file>")
>    parser.add_option("--yes-do-it", dest="yes_do_it",
>                      action="store_true",
> -- 
> 1.8.4.1
> 

-- 
Jose Antonio Lopes
Ganeti Engineering
Google Germany GmbH
Dienerstr. 12, 80331, München

Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Graham Law, Christine Elizabeth Flores
Steuernummer: 48/725/00206
Umsatzsteueridentifikationsnummer: DE813741370

Reply via email to