OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Common python commands used by various build scripts.""" |
| 6 |
| 7 import subprocess |
| 8 import sys |
| 9 |
| 10 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() |
| 11 |
| 12 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, |
| 13 exit_code=False, redirect_stdout=False, redirect_stderr=False, |
| 14 cwd=None, input=None, enter_chroot=False): |
| 15 """Runs a shell command. |
| 16 |
| 17 Keyword arguments: |
| 18 cmd - cmd to run. Should be input to subprocess.POpen. If a string, |
| 19 converted to an array using split(). |
| 20 print_cmd -- prints the command before running it. |
| 21 error_ok -- does not raise an exception on error. |
| 22 error_message -- prints out this message when an error occurrs. |
| 23 exit_code -- returns the return code of the shell command. |
| 24 redirect_stdout -- returns the stdout. |
| 25 redirect_stderr -- holds stderr output until input is communicated. |
| 26 cwd -- the working directory to run this cmd. |
| 27 input -- input to pipe into this command through stdin. |
| 28 enter_chroot -- this command should be run from within the chroot. If set, |
| 29 cwd must point to the scripts directory. |
| 30 Raises: |
| 31 Exception: Raises generic exception on error with optional error_message. |
| 32 """ |
| 33 # Set default for variables. |
| 34 stdout = None |
| 35 stderr = None |
| 36 stdin = None |
| 37 |
| 38 # Modify defaults based on parameters. |
| 39 if redirect_stdout: stdout = subprocess.PIPE |
| 40 if redirect_stderr: stderr = subprocess.PIPE |
| 41 if input: stdin = subprocess.PIPE |
| 42 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd |
| 43 |
| 44 # Print out the command before running. |
| 45 if print_cmd: |
| 46 Info('RunCommand: %s' % ' '.join(cmd)) |
| 47 |
| 48 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, |
| 49 stdout=stdout, stderr=stderr) |
| 50 (output, error) = proc.communicate(input) |
| 51 if exit_code: |
| 52 return proc.returncode |
| 53 |
| 54 if not error_ok and proc.returncode: |
| 55 raise Exception('Command "%s" failed.\n' % (' '.join(cmd)) + |
| 56 (error_message or error or output or '')) |
| 57 |
| 58 return output |
| 59 |
| 60 |
| 61 class Color(object): |
| 62 """Conditionally wraps text in ANSI color escape sequences.""" |
| 63 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |
| 64 BOLD = -1 |
| 65 COLOR_START = '\033[1;%dm' |
| 66 BOLD_START = '\033[1m' |
| 67 RESET = '\033[0m' |
| 68 |
| 69 def __init__(self, enabled=True): |
| 70 self._enabled = enabled |
| 71 |
| 72 def Color(self, color, text): |
| 73 """Returns text with conditionally added color escape sequences. |
| 74 |
| 75 Keyword arguments: |
| 76 color: Text color -- one of the color constants defined in this class. |
| 77 text: The text to color. |
| 78 |
| 79 Returns: |
| 80 If self._enabled is False, returns the original text. If it's True, |
| 81 returns text with color escape sequences based on the value of color. |
| 82 """ |
| 83 if not self._enabled: |
| 84 return text |
| 85 if color == self.BOLD: |
| 86 start = self.BOLD_START |
| 87 else: |
| 88 start = self.COLOR_START % (color + 30) |
| 89 return start + text + self.RESET |
| 90 |
| 91 |
| 92 def Die(message): |
| 93 """Emits a red error message and halts execution. |
| 94 |
| 95 Keyword arguments: |
| 96 message: The message to be emitted before exiting. |
| 97 """ |
| 98 print >> sys.stderr, ( |
| 99 Color(_STDOUT_IS_TTY).Color(Color.RED, '\nERROR: ' + message)) |
| 100 sys.exit(1) |
| 101 |
| 102 |
| 103 def Warning(message): |
| 104 """Emits a yellow warning message and continues execution. |
| 105 |
| 106 Keyword arguments: |
| 107 message: The message to be emitted. |
| 108 """ |
| 109 print >> sys.stderr, ( |
| 110 Color(_STDOUT_IS_TTY).Color(Color.YELLOW, '\nWARNING: ' + message)) |
| 111 |
| 112 |
| 113 def Info(message): |
| 114 """Emits a blue informational message and continues execution. |
| 115 |
| 116 Keyword arguments: |
| 117 message: The message to be emitted. |
| 118 """ |
| 119 print >> sys.stderr, ( |
| 120 Color(_STDOUT_IS_TTY).Color(Color.BLUE, '\nINFO: ' + message)) |
OLD | NEW |