1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Changes to convert to unicode at the borders

The module docs and vault changes solve issues where tracebacks can
happen.  The galaxy changes are mostly refactoring to be more pythonic
with a small chance that a unicode traceback could have occurred there
without the changes.  The change in __init__.py when we actually call
the pager makes things more robust but could hide places where we had
bytes coming in already so I didn't want to change that without auditing
where the text was coming from.

Fixes #14178
This commit is contained in:
Toshio Kuratomi 2016-01-28 10:50:20 -08:00
parent e7a9031d61
commit fa9822df0f
4 changed files with 31 additions and 20 deletions

View file

@ -459,7 +459,7 @@ class CLI(object):
os.environ['LESS'] = CLI.LESS_OPTS os.environ['LESS'] = CLI.LESS_OPTS
try: try:
cmd = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=sys.stdout) cmd = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=sys.stdout)
cmd.communicate(input=text.encode(sys.stdout.encoding)) cmd.communicate(input=to_bytes(text))
except IOError: except IOError:
pass pass
except KeyboardInterrupt: except KeyboardInterrupt:

View file

@ -39,6 +39,7 @@ from ansible.galaxy.role import GalaxyRole
from ansible.galaxy.login import GalaxyLogin from ansible.galaxy.login import GalaxyLogin
from ansible.galaxy.token import GalaxyToken from ansible.galaxy.token import GalaxyToken
from ansible.playbook.role.requirement import RoleRequirement from ansible.playbook.role.requirement import RoleRequirement
from ansible.utils.unicode import to_unicode
try: try:
from __main__ import display from __main__ import display
@ -161,8 +162,8 @@ class GalaxyCLI(CLI):
def _display_role_info(self, role_info): def _display_role_info(self, role_info):
text = "\nRole: %s \n" % role_info['name'] text = [u"", u"Role: %s" % to_unicode(role_info['name'])]
text += "\tdescription: %s \n" % role_info.get('description', '') text.append(u"\tdescription: %s" % role_info.get('description', ''))
for k in sorted(role_info.keys()): for k in sorted(role_info.keys()):
@ -171,14 +172,15 @@ class GalaxyCLI(CLI):
if isinstance(role_info[k], dict): if isinstance(role_info[k], dict):
text += "\t%s: \n" % (k) text += "\t%s: \n" % (k)
text.append(u"\t%s:" % (k))
for key in sorted(role_info[k].keys()): for key in sorted(role_info[k].keys()):
if key in self.SKIP_INFO_KEYS: if key in self.SKIP_INFO_KEYS:
continue continue
text += "\t\t%s: %s\n" % (key, role_info[k][key]) text.append(u"\t\t%s: %s" % (key, role_info[k][key]))
else: else:
text += "\t%s: %s\n" % (k, role_info[k]) text.append(u"\t%s: %s" % (k, role_info[k]))
return text return u'\n'.join(text)
############################ ############################
# execute actions # execute actions
@ -322,9 +324,11 @@ class GalaxyCLI(CLI):
if role_spec: if role_spec:
role_info.update(role_spec) role_info.update(role_spec)
data += self._display_role_info(role_info) data = self._display_role_info(role_info)
### FIXME: This is broken in both 1.9 and 2.0 as
# _display_role_info() always returns something
if not data: if not data:
data += "\n- the role %s was not found" % role data = u"\n- the role %s was not found" % role
self.pager(data) self.pager(data)
@ -518,24 +522,25 @@ class GalaxyCLI(CLI):
display.display("No roles match your search.", color=C.COLOR_ERROR) display.display("No roles match your search.", color=C.COLOR_ERROR)
return True return True
data = '' data = [u'']
if response['count'] > page_size: if response['count'] > page_size:
data += ("\nFound %d roles matching your search. Showing first %s.\n" % (response['count'], page_size)) data.append(u"Found %d roles matching your search. Showing first %s." % (response['count'], page_size))
else: else:
data += ("\nFound %d roles matching your search:\n" % response['count']) data.append(u"Found %d roles matching your search:" % response['count'])
max_len = [] max_len = []
for role in response['results']: for role in response['results']:
max_len.append(len(role['username'] + '.' + role['name'])) max_len.append(len(role['username'] + '.' + role['name']))
name_len = max(max_len) name_len = max(max_len)
format_str = " %%-%ds %%s\n" % name_len format_str = u" %%-%ds %%s" % name_len
data +='\n' data.append(u'')
data += (format_str % ("Name", "Description")) data.append(format_str % (u"Name", u"Description"))
data += (format_str % ("----", "-----------")) data.append(format_str % (u"----", u"-----------"))
for role in response['results']: for role in response['results']:
data += (format_str % (role['username'] + '.' + role['name'],role['description'])) data.append(format_str % (u'%s.%s' % (role['username'], role['name']), role['description']))
data = u'\n'.join(data)
self.pager(data) self.pager(data)
return True return True

View file

@ -26,6 +26,7 @@ from ansible.errors import AnsibleError, AnsibleOptionsError
from ansible.parsing.dataloader import DataLoader from ansible.parsing.dataloader import DataLoader
from ansible.parsing.vault import VaultEditor from ansible.parsing.vault import VaultEditor
from ansible.cli import CLI from ansible.cli import CLI
from ansible.utils.unicode import to_unicode
try: try:
from __main__ import display from __main__ import display
@ -157,7 +158,12 @@ class VaultCLI(CLI):
def execute_view(self): def execute_view(self):
for f in self.args: for f in self.args:
self.pager(self.editor.plaintext(f)) # Note: vault should return byte strings because it could encrypt
# and decrypt binary files. We are responsible for changing it to
# unicode here because we are displaying it and therefore can make
# the decision that the display doesn't have to be precisely what
# the input was (leave that to decrypt instead)
self.pager(to_unicode(self.editor.plaintext(f)))
def execute_rekey(self): def execute_rekey(self):
for f in self.args: for f in self.args:

View file

@ -24,7 +24,7 @@ __metaclass__ = type
import os import os
import sys import sys
import ast import ast
import yaml from ansible.parsing.yaml.loader import AnsibleLoader
import traceback import traceback
from collections import MutableMapping, MutableSet, MutableSequence from collections import MutableMapping, MutableSet, MutableSequence
@ -71,7 +71,7 @@ def get_docstring(filename, verbose=False):
continue continue
if 'DOCUMENTATION' in theid: if 'DOCUMENTATION' in theid:
doc = yaml.safe_load(child.value.s) doc = AnsibleLoader(child.value.s, file_name=filename).get_single_data()
fragments = doc.get('extends_documentation_fragment', []) fragments = doc.get('extends_documentation_fragment', [])
if isinstance(fragments, basestring): if isinstance(fragments, basestring):
@ -91,7 +91,7 @@ def get_docstring(filename, verbose=False):
assert fragment_class is not None assert fragment_class is not None
fragment_yaml = getattr(fragment_class, fragment_var, '{}') fragment_yaml = getattr(fragment_class, fragment_var, '{}')
fragment = yaml.safe_load(fragment_yaml) fragment = AnsibleLoader(fragment_yaml, file_name=filename).get_single_data()
if fragment.has_key('notes'): if fragment.has_key('notes'):
notes = fragment.pop('notes') notes = fragment.pop('notes')