2 # vim: ai ts=4 sts=4 et sw=4
4 # Copyright 2009, 2010, 2011 Intel, Inc.
6 # This copyrighted material is made available to anyone wishing to use, modify,
7 # copy, or redistribute it subject to the terms and conditions of the GNU
8 # General Public License v.2. This program is distributed in the hope that it
9 # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
10 # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat
16 # trademarks that are incorporated in the source code or documentation are not
17 # subject to the GNU General Public License and may only be used or replicated
18 # with the express permission of Red Hat, Inc.
24 __ALL__ = ['set_mode', 'get_loglevel', 'set_loglevel', 'raw' 'debug', 'verbose', 'info', 'warning', 'error', 'ask', 'pause', 'run']
27 INFO_COLOR = 32 # green
28 WARN_COLOR = 33 # yellow
33 PREFIX_RE = re.compile('^<(.*?)>\s*(.*)')
46 def _color_print(head, color, msg = None, stream = sys.stdout, level = 'normal'):
47 if LOG_LEVELS[level] > LOG_LEVEL:
52 if color == NO_COLOR or \
53 not stream.isatty() or \
54 os.getenv('ANSI_COLORS_DISABLED') is not None:
57 if head.startswith('\r'):
64 head = '\033[%dm%s:\033[0m ' %(color, head)
66 # ESC cmd to clear line
67 head = '\033[2K' + head
71 if head.startswith('\r'):
76 stream.write('%s%s' % (head, msg))
82 def _color_perror(head, color, msg, level = 'normal'):
83 _color_print(head, color, msg, sys.stderr, level)
85 def _split_msg(head, msg):
86 if isinstance(msg, list):
87 msg = '\n'.join(map(str, msg))
89 if msg.startswith('\n'):
90 # means print \n at first
94 elif msg.startswith('\r'):
95 # means print \r at first
99 m = PREFIX_RE.match(msg)
101 head += ' <%s>' % m.group(1)
107 return (k for k,v in LOG_LEVELS.items() if v==LOG_LEVEL).next()
109 def set_loglevel(level):
111 if level not in LOG_LEVELS:
115 LOG_LEVEL = LOG_LEVELS[level]
117 def set_mode(interactive):
126 sys.stdout.write(msg)
127 sys.stdout.write('\n')
131 head, msg = _split_msg('Info', msg)
132 _color_print(head, INFO_COLOR, msg)
135 head, msg = _split_msg('Verbose', msg)
136 _color_print(head, INFO_COLOR, msg, level = 'verbose')
139 head, msg = _split_msg('Warning', msg)
140 _color_perror(head, WARN_COLOR, msg)
143 head, msg = _split_msg('Debug', msg)
144 _color_perror(head, ERR_COLOR, msg, level = 'debug')
147 head, msg = _split_msg('Error', msg)
148 _color_perror(head, ERR_COLOR, msg)
151 def ask(msg, default=True):
152 _color_print('\rQ', ASK_COLOR, '')
160 repl = raw_input(msg)
161 if repl.lower() == 'y':
163 elif repl.lower() == 'n':
165 elif not repl.strip():
171 sys.stdout.write('%s ' % msg)
173 sys.stdout.write('Y\n')
175 sys.stdout.write('N\n')
177 except KeyboardInterrupt:
178 sys.stdout.write('\n')
183 _color_print('\rQ', ASK_COLOR, '')
185 msg = 'press <ENTER> to continue ...'
188 def run(cmdln_or_args, quiet=False):
189 from subprocess import *
190 if isinstance(cmdln_or_args, list):
194 args = shlex.split(cmdln_or_args)
196 p = Popen(args, stdout=PIPE, stderr=PIPE)
197 out = p.communicate()[0].strip()
200 msg = 'running command: "%s"' % ' '.join(args)
202 msg += ', with output::'
203 msg += '\n +----------------'
204 for line in out.splitlines():
205 msg += '\n | %s' % line
206 msg += '\n +----------------'