Fix cow mode to work with unicode task and play names (#17576)

Fixes #17560
pull/16741/head
Toshio Kuratomi 8 years ago committed by GitHub
parent 7b3cbafbe9
commit 70e63ddf6c

@ -147,14 +147,14 @@ class CallbackModule(CallbackBase):
# (shoulder surfing, logging stdout straight to a file, etc). # (shoulder surfing, logging stdout straight to a file, etc).
args = '' args = ''
if not task.no_log and C.DISPLAY_ARGS_TO_STDOUT: if not task.no_log and C.DISPLAY_ARGS_TO_STDOUT:
args = ', '.join(('%s=%s' % a for a in task.args.items())) args = u', '.join(u'%s=%s' % a for a in task.args.items())
args = ' %s' % args args = u' %s' % args
self._display.banner("TASK [%s%s]" % (task.get_name().strip(), args)) self._display.banner(u"TASK [%s%s]" % (task.get_name().strip(), args))
if self._display.verbosity >= 2: if self._display.verbosity >= 2:
path = task.get_path() path = task.get_path()
if path: if path:
self._display.display("task path: %s" % path, color=C.COLOR_DEBUG) self._display.display(u"task path: %s" % path, color=C.COLOR_DEBUG)
self._last_task_banner = task._uuid self._last_task_banner = task._uuid
@ -167,9 +167,9 @@ class CallbackModule(CallbackBase):
def v2_playbook_on_play_start(self, play): def v2_playbook_on_play_start(self, play):
name = play.get_name().strip() name = play.get_name().strip()
if not name: if not name:
msg = "PLAY" msg = u"PLAY"
else: else:
msg = "PLAY [%s]" % name msg = u"PLAY [%s]" % name
self._play = play self._play = play

@ -58,7 +58,11 @@ if C.DEFAULT_LOG_PATH:
else: else:
print("[WARNING]: log file at %s is not writeable and we cannot create it, aborting\n" % path, file=sys.stderr) print("[WARNING]: log file at %s is not writeable and we cannot create it, aborting\n" % path, file=sys.stderr)
b_COW_PATHS = (b"/usr/bin/cowsay",
b"/usr/games/cowsay",
b"/usr/local/bin/cowsay", # BSD path for cowsay
b"/opt/local/bin/cowsay", # MacPorts path for cowsay
)
class Display: class Display:
def __init__(self, verbosity=0): def __init__(self, verbosity=0):
@ -71,35 +75,27 @@ class Display:
self._warns = {} self._warns = {}
self._errors = {} self._errors = {}
self.cowsay = None self.b_cowsay = None
self.noncow = C.ANSIBLE_COW_SELECTION self.noncow = C.ANSIBLE_COW_SELECTION
self.set_cowsay_info() self.set_cowsay_info()
if self.cowsay: if self.b_cowsay:
try: try:
cmd = subprocess.Popen([self.cowsay, "-l"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) cmd = subprocess.Popen([self.b_cowsay, "-l"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = cmd.communicate() (out, err) = cmd.communicate()
self.cows_available = list(set(C.ANSIBLE_COW_WHITELIST).intersection(out.split())) self.cows_available = [ to_bytes(c) for c in set(C.ANSIBLE_COW_WHITELIST).intersection(out.split())]
except: except:
# could not execute cowsay for some reason # could not execute cowsay for some reason
self.cowsay = False self.b_cowsay = False
self._set_column_width() self._set_column_width()
def set_cowsay_info(self): def set_cowsay_info(self):
if not C.ANSIBLE_NOCOWS: if not C.ANSIBLE_NOCOWS:
if os.path.exists("/usr/bin/cowsay"): for b_cow_path in b_COW_PATHS:
self.cowsay = "/usr/bin/cowsay" if os.path.exists(b_cow_path):
elif os.path.exists("/usr/games/cowsay"): self.b_cowsay = b_cow_path
self.cowsay = "/usr/games/cowsay"
elif os.path.exists("/usr/local/bin/cowsay"):
# BSD path for cowsay
self.cowsay = "/usr/local/bin/cowsay"
elif os.path.exists("/opt/local/bin/cowsay"):
# MacPorts path for cowsay
self.cowsay = "/opt/local/bin/cowsay"
def display(self, msg, color=None, stderr=False, screen_only=False, log_only=False): def display(self, msg, color=None, stderr=False, screen_only=False, log_only=False):
""" Display a message to the user """ Display a message to the user
@ -231,7 +227,7 @@ class Display:
Prints a header-looking line with stars taking up to 80 columns Prints a header-looking line with stars taking up to 80 columns
of width (3 columns, minimum) of width (3 columns, minimum)
''' '''
if self.cowsay: if self.b_cowsay:
try: try:
self.banner_cowsay(msg) self.banner_cowsay(msg)
return return
@ -242,25 +238,25 @@ class Display:
star_len = (79 - len(msg)) star_len = (79 - len(msg))
if star_len < 0: if star_len < 0:
star_len = 3 star_len = 3
stars = "*" * star_len stars = u"*" * star_len
self.display("\n%s %s" % (msg, stars), color=color) self.display(u"\n%s %s" % (msg, stars), color=color)
def banner_cowsay(self, msg, color=None): def banner_cowsay(self, msg, color=None):
if ": [" in msg: if u": [" in msg:
msg = msg.replace("[","") msg = msg.replace(u"[", u"")
if msg.endswith("]"): if msg.endswith(u"]"):
msg = msg[:-1] msg = msg[:-1]
runcmd = [self.cowsay,"-W", "60"] runcmd = [self.b_cowsay, b"-W", b"60"]
if self.noncow: if self.noncow:
thecow = self.noncow thecow = self.noncow
if thecow == 'random': if thecow == b'random':
thecow = random.choice(self.cows_available) thecow = random.choice(self.cows_available)
runcmd.append('-f') runcmd.append(b'-f')
runcmd.append(thecow) runcmd.append(thecow)
runcmd.append(msg) runcmd.append(to_bytes(msg))
cmd = subprocess.Popen(runcmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) cmd = subprocess.Popen(runcmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = cmd.communicate() (out, err) = cmd.communicate()
self.display("%s\n" % out, color=color) self.display(u"%s\n" % to_text(out), color=color)
def error(self, msg, wrap_text=True): def error(self, msg, wrap_text=True):
if wrap_text: if wrap_text:

Loading…
Cancel
Save