| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
""" |
| 22 |
CLI results display class |
| 23 |
""" |
| 24 |
|
| 25 |
from __future__ import print_function |
| 26 |
|
| 27 |
import difflib |
| 28 |
import io |
| 29 |
import sys |
| 30 |
import os |
| 31 |
|
| 32 |
from ClusterShell.NodeSet import NodeSet |
| 33 |
|
| 34 |
|
| 35 |
VERB_QUIET = 0 |
| 36 |
VERB_STD = 1 |
| 37 |
VERB_VERB = 2 |
| 38 |
VERB_DEBUG = 3 |
| 39 |
THREE_CHOICES = ["", "never", "always", "auto"] |
| 40 |
WHENCOLOR_CHOICES = THREE_CHOICES |
| 41 |
|
| 42 |
if sys.getdefaultencoding() == 'ascii': |
| 43 |
STRING_ENCODING = 'utf-8' |
| 44 |
else: |
| 45 |
STRING_ENCODING = sys.getdefaultencoding() |
| 46 |
|
| 47 |
|
| 48 |
def sys_stdin(): |
| 49 |
return getattr(sys.stdin, 'buffer', sys.stdin) |
| 50 |
|
| 51 |
|
| 52 |
class Display(object): |
| 53 |
""" |
| 54 |
Output display class for command line scripts. |
| 55 |
""" |
| 56 |
COLOR_RESULT_FMT = "\033[92m%s\033[0m" |
| 57 |
COLOR_STDOUT_FMT = "\033[94m%s\033[0m" |
| 58 |
COLOR_STDERR_FMT = "\033[91m%s\033[0m" |
| 59 |
COLOR_DIFFHDR_FMT = "\033[1m%s\033[0m" |
| 60 |
COLOR_DIFFHNK_FMT = "\033[96m%s\033[0m" |
| 61 |
COLOR_DIFFADD_FMT = "\033[92m%s\033[0m" |
| 62 |
COLOR_DIFFDEL_FMT = "\033[91m%s\033[0m" |
| 63 |
SEP = "-" * 15 |
| 64 |
|
| 65 |
class _KeySet(set): |
| 66 |
"""Private NodeSet substitution to display raw keys""" |
| 67 |
def __str__(self): |
| 68 |
return ",".join(self) |
| 69 |
|
| 70 |
def __init__(self, options, config=None, color=None): |
| 71 |
"""Initialize a Display object from CLI.OptionParser options |
| 72 |
and optional CLI.ClushConfig. |
| 73 |
|
| 74 |
If `color' boolean flag is not specified, it is auto detected |
| 75 |
according to options.whencolor. |
| 76 |
""" |
| 77 |
if options.diff: |
| 78 |
self._print_buffer = self._print_diff |
| 79 |
else: |
| 80 |
self._print_buffer = self._print_content |
| 81 |
self._display = self._print_buffer |
| 82 |
self._diffref = None |
| 83 |
|
| 84 |
self.gather = options.gatherall or options.gather or options.diff |
| 85 |
self.progress = getattr(options, 'progress', False) |
| 86 |
|
| 87 |
if options.diff and options.line_mode: |
| 88 |
raise ValueError("diff not supported in line_mode") |
| 89 |
self.line_mode = options.line_mode |
| 90 |
self.label = options.label |
| 91 |
self.regroup = options.regroup |
| 92 |
self.groupsource = options.groupsource |
| 93 |
self.noprefix = options.groupbase |
| 94 |
self.outdir = options.outdir |
| 95 |
self.errdir = options.errdir |
| 96 |
|
| 97 |
self.maxrc = getattr(options, 'maxrc', False) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if options.whencolor is None and color is not False: |
| 105 |
if (config is None) or (config.color == '' or config.color == 'auto'): |
| 106 |
if 'NO_COLOR' not in os.environ: |
| 107 |
color = self._has_cli_color() |
| 108 |
else: |
| 109 |
color = False |
| 110 |
|
| 111 |
if color is None: |
| 112 |
|
| 113 |
color = False |
| 114 |
if not options.whencolor or options.whencolor == "auto": |
| 115 |
color = sys.stdout.isatty() |
| 116 |
elif options.whencolor == "always": |
| 117 |
color = True |
| 118 |
|
| 119 |
self._color = color |
| 120 |
|
| 121 |
self.out = sys.stdout |
| 122 |
self.err = sys.stderr |
| 123 |
if hasattr(self.out, 'reconfigure'): |
| 124 |
if not self.out.line_buffering: |
| 125 |
self.out.reconfigure(line_buffering=True) |
| 126 |
if not self.err.line_buffering: |
| 127 |
self.err.reconfigure(line_buffering=True) |
| 128 |
elif hasattr(self.out, 'buffer'): |
| 129 |
self.out = io.TextIOWrapper(self.out.buffer, encoding=self.out.encoding, |
| 130 |
errors=self.out.errors, line_buffering=True) |
| 131 |
self.err = io.TextIOWrapper(self.err.buffer, encoding=self.err.encoding, |
| 132 |
errors=self.err.errors, line_buffering=True) |
| 133 |
|
| 134 |
if self._color: |
| 135 |
self.color_stdout_fmt = self.COLOR_STDOUT_FMT |
| 136 |
self.color_stderr_fmt = self.COLOR_STDERR_FMT |
| 137 |
self.color_diffhdr_fmt = self.COLOR_DIFFHDR_FMT |
| 138 |
self.color_diffctx_fmt = self.COLOR_DIFFHNK_FMT |
| 139 |
self.color_diffadd_fmt = self.COLOR_DIFFADD_FMT |
| 140 |
self.color_diffdel_fmt = self.COLOR_DIFFDEL_FMT |
| 141 |
else: |
| 142 |
self.color_stdout_fmt = self.color_stderr_fmt = \ |
| 143 |
self.color_diffhdr_fmt = self.color_diffctx_fmt = \ |
| 144 |
self.color_diffadd_fmt = self.color_diffdel_fmt = "%s" |
| 145 |
|
| 146 |
|
| 147 |
if config: |
| 148 |
|
| 149 |
self.node_count = config.node_count |
| 150 |
self.verbosity = config.verbosity |
| 151 |
else: |
| 152 |
self.node_count = True |
| 153 |
self.verbosity = VERB_STD |
| 154 |
if hasattr(options, 'quiet') and options.quiet: |
| 155 |
self.verbosity = VERB_QUIET |
| 156 |
if hasattr(options, 'verbose') and options.verbose: |
| 157 |
self.verbosity = VERB_VERB |
| 158 |
if hasattr(options, 'debug') and options.debug: |
| 159 |
self.verbosity = VERB_DEBUG |
| 160 |
|
| 161 |
def _has_cli_color(self): |
| 162 |
"""Tests CLICOLOR environment variable to determine whether to |
| 163 |
use color or not on output.""" |
| 164 |
|
| 165 |
|
| 166 |
if os.getenv("CLICOLOR_FORCE", "0") != "0": |
| 167 |
return True |
| 168 |
|
| 169 |
cli_color = os.getenv("CLICOLOR") |
| 170 |
|
| 171 |
if cli_color is None: |
| 172 |
return None |
| 173 |
elif cli_color != "0": |
| 174 |
|
| 175 |
|
| 176 |
return sys.stdout.isatty() |
| 177 |
else: |
| 178 |
|
| 179 |
return False |
| 180 |
|
| 181 |
def flush(self): |
| 182 |
"""flush display object buffers""" |
| 183 |
|
| 184 |
self._diffref = None |
| 185 |
|
| 186 |
def _getlmode(self): |
| 187 |
"""line_mode getter""" |
| 188 |
return self._display == self._print_lines |
| 189 |
|
| 190 |
def _setlmode(self, value): |
| 191 |
"""line_mode setter""" |
| 192 |
if value: |
| 193 |
self._display = self._print_lines |
| 194 |
else: |
| 195 |
self._display = self._print_buffer |
| 196 |
line_mode = property(_getlmode, _setlmode) |
| 197 |
|
| 198 |
def _format_nodeset(self, nodeset): |
| 199 |
"""Sub-routine to format nodeset string.""" |
| 200 |
if self.regroup: |
| 201 |
return nodeset.regroup(self.groupsource, noprefix=self.noprefix) |
| 202 |
return str(nodeset) |
| 203 |
|
| 204 |
def format_header(self, nodeset, indent=0): |
| 205 |
"""Format nodeset-based header.""" |
| 206 |
if not self.label: |
| 207 |
return "" |
| 208 |
indstr = " " * indent |
| 209 |
nodecntstr = "" |
| 210 |
if self.verbosity >= VERB_STD and self.node_count and len(nodeset) > 1: |
| 211 |
nodecntstr = " (%d)" % len(nodeset) |
| 212 |
hdr = self.color_stdout_fmt % ("%s%s\n%s%s%s\n%s%s" % \ |
| 213 |
(indstr, self.SEP, |
| 214 |
indstr, self._format_nodeset(nodeset), nodecntstr, |
| 215 |
indstr, self.SEP)) |
| 216 |
return hdr + '\n' |
| 217 |
|
| 218 |
def print_line(self, nodeset, line): |
| 219 |
"""Display a line with optional label.""" |
| 220 |
linestr = line.decode(STRING_ENCODING, errors='replace') + '\n' |
| 221 |
if self.label: |
| 222 |
prefix = self.color_stdout_fmt % ("%s: " % nodeset) |
| 223 |
self.out.write(prefix + linestr) |
| 224 |
else: |
| 225 |
self.out.write(linestr) |
| 226 |
|
| 227 |
def print_line_error(self, nodeset, line): |
| 228 |
"""Display an error line with optional label.""" |
| 229 |
linestr = line.decode(STRING_ENCODING, errors='replace') + '\n' |
| 230 |
if self.label: |
| 231 |
prefix = self.color_stderr_fmt % ("%s: " % nodeset) |
| 232 |
self.err.write(prefix + linestr) |
| 233 |
else: |
| 234 |
self.err.write(linestr) |
| 235 |
|
| 236 |
def print_gather(self, nodeset, obj): |
| 237 |
"""Generic method for displaying nodeset/content according to current |
| 238 |
object settings.""" |
| 239 |
return self._display(NodeSet(nodeset), obj) |
| 240 |
|
| 241 |
def print_gather_finalize(self, nodeset): |
| 242 |
"""Finalize display of diff-like gathered contents.""" |
| 243 |
if self._display == self._print_diff and self._diffref: |
| 244 |
return self._display(nodeset, '') |
| 245 |
|
| 246 |
def print_gather_keys(self, keys, obj): |
| 247 |
"""Generic method for displaying raw keys/content according to current |
| 248 |
object settings (used by clubak).""" |
| 249 |
return self._display(self.__class__._KeySet(keys), obj) |
| 250 |
|
| 251 |
def _print_content(self, nodeset, content): |
| 252 |
"""Display a dshbak-like header block and content.""" |
| 253 |
s = bytes(content).decode(STRING_ENCODING, errors='replace') |
| 254 |
self.out.write(self.format_header(nodeset) + s + '\n') |
| 255 |
|
| 256 |
def _print_diff(self, nodeset, content): |
| 257 |
"""Display unified diff between remote gathered outputs.""" |
| 258 |
if self._diffref is None: |
| 259 |
self._diffref = (nodeset, content) |
| 260 |
else: |
| 261 |
nodeset_ref, content_ref = self._diffref |
| 262 |
nsstr_ref = self._format_nodeset(nodeset_ref) |
| 263 |
nsstr = self._format_nodeset(nodeset) |
| 264 |
if self.verbosity >= VERB_STD and self.node_count: |
| 265 |
if len(nodeset_ref) > 1: |
| 266 |
nsstr_ref += " (%d)" % len(nodeset_ref) |
| 267 |
if len(nodeset) > 1: |
| 268 |
nsstr += " (%d)" % len(nodeset) |
| 269 |
|
| 270 |
alist = [aline.decode('utf-8', 'ignore') for aline in content_ref] |
| 271 |
blist = [bline.decode('utf-8', 'ignore') for bline in content] |
| 272 |
udiff = difflib.unified_diff(alist, blist, fromfile=nsstr_ref, |
| 273 |
tofile=nsstr, lineterm='') |
| 274 |
output = '' |
| 275 |
for line in udiff: |
| 276 |
if line.startswith('---') or line.startswith('+++'): |
| 277 |
output += self.color_diffhdr_fmt % line.rstrip() |
| 278 |
elif line.startswith('@@'): |
| 279 |
output += self.color_diffctx_fmt % line |
| 280 |
elif line.startswith('+'): |
| 281 |
output += self.color_diffadd_fmt % line |
| 282 |
elif line.startswith('-'): |
| 283 |
output += self.color_diffdel_fmt % line |
| 284 |
else: |
| 285 |
output += line |
| 286 |
output += '\n' |
| 287 |
self.out.write(output) |
| 288 |
|
| 289 |
def _print_lines(self, nodeset, msg): |
| 290 |
"""Display a MsgTree buffer by line with prefixed header.""" |
| 291 |
out = self.out |
| 292 |
if self.label: |
| 293 |
header = self.color_stdout_fmt % \ |
| 294 |
("%s: " % self._format_nodeset(nodeset)) |
| 295 |
for line in msg: |
| 296 |
out.write(header + line.decode(STRING_ENCODING, |
| 297 |
errors='replace') + '\n') |
| 298 |
else: |
| 299 |
for line in msg: |
| 300 |
out.write(line.decode(STRING_ENCODING, |
| 301 |
errors='replace') + '\n') |
| 302 |
|
| 303 |
def vprint(self, level, message): |
| 304 |
"""Utility method to print a message if verbose level is high |
| 305 |
enough.""" |
| 306 |
if self.verbosity >= level: |
| 307 |
print(message) |
| 308 |
|
| 309 |
def vprint_err(self, level, message): |
| 310 |
"""Utility method to print a message on stderr if verbose level |
| 311 |
is high enough.""" |
| 312 |
if self.verbosity >= level: |
| 313 |
print(message, file=sys.stderr) |
| 314 |
|