From 27dcf8aaab58b94d94feba96e1c23e78e7a01f5a Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 24 May 2019 14:49:04 -0400 Subject: [PATCH] Not native, but text (#55676) * use to_text instead of to_native * cleaned up some imports and other pyflakisms * fix missing lib messages --- .../fragments/fix_incorrect_nativisim.yml | 2 ++ lib/ansible/executor/playbook_executor.py | 7 +++---- lib/ansible/parsing/dataloader.py | 2 +- lib/ansible/plugins/action/reboot.py | 8 ++++---- lib/ansible/plugins/connection/ssh.py | 6 +++--- .../plugins/inventory/advanced_host_list.py | 4 ++-- lib/ansible/plugins/inventory/foreman.py | 6 +++--- lib/ansible/plugins/inventory/gcp_compute.py | 17 ++++++++--------- lib/ansible/plugins/inventory/host_list.py | 4 ++-- lib/ansible/plugins/loader.py | 5 ++--- lib/ansible/template/__init__.py | 3 +-- lib/ansible/vars/manager.py | 4 ++-- 12 files changed, 33 insertions(+), 35 deletions(-) create mode 100644 changelogs/fragments/fix_incorrect_nativisim.yml diff --git a/changelogs/fragments/fix_incorrect_nativisim.yml b/changelogs/fragments/fix_incorrect_nativisim.yml new file mode 100644 index 00000000000..4847e151fe8 --- /dev/null +++ b/changelogs/fragments/fix_incorrect_nativisim.yml @@ -0,0 +1,2 @@ +bugfixes: + - fix incorrect uses of to_native that should be to_text instead. diff --git a/lib/ansible/executor/playbook_executor.py b/lib/ansible/executor/playbook_executor.py index 7b2c7e07ea8..f8efee052c0 100644 --- a/lib/ansible/executor/playbook_executor.py +++ b/lib/ansible/executor/playbook_executor.py @@ -24,13 +24,12 @@ import os from ansible import constants as C from ansible import context from ansible.executor.task_queue_manager import TaskQueueManager -from ansible.module_utils._text import to_native, to_text +from ansible.module_utils._text import to_text +from ansible.module_utils.parsing.convert_bool import boolean from ansible.plugins.loader import become_loader, connection_loader, shell_loader from ansible.playbook import Playbook from ansible.template import Templar -from ansible.plugins.loader import connection_loader, shell_loader from ansible.utils.helpers import pct_to_int -from ansible.module_utils.parsing.convert_bool import boolean from ansible.utils.path import makedirs_safe from ansible.utils.ssh_functions import check_for_controlpersist from ansible.utils.display import Display @@ -306,7 +305,7 @@ class PlaybookExecutor: for x in replay_hosts: fd.write("%s\n" % x) except Exception as e: - display.warning("Could not create retry file '%s'.\n\t%s" % (retry_path, to_native(e))) + display.warning("Could not create retry file '%s'.\n\t%s" % (retry_path, to_text(e))) return False return True diff --git a/lib/ansible/parsing/dataloader.py b/lib/ansible/parsing/dataloader.py index be61c55a749..1e15584d4c2 100644 --- a/lib/ansible/parsing/dataloader.py +++ b/lib/ansible/parsing/dataloader.py @@ -389,7 +389,7 @@ class DataLoader: try: self.cleanup_tmp_file(f) except Exception as e: - display.warning("Unable to cleanup temp files: %s" % to_native(e)) + display.warning("Unable to cleanup temp files: %s" % to_text(e)) def find_vars_files(self, path, name, extensions=None, allow_dir=True): """ diff --git a/lib/ansible/plugins/action/reboot.py b/lib/ansible/plugins/action/reboot.py index 2d829fad766..4578997c0e3 100644 --- a/lib/ansible/plugins/action/reboot.py +++ b/lib/ansible/plugins/action/reboot.py @@ -143,9 +143,9 @@ class ActionModule(ActionBase): err_msg = "'search_paths' must be a string or flat list of strings, got {0}" try: incorrect_type = any(not is_string(x) for x in search_paths) - except TypeError as te: - raise AnsibleError(err_msg.format(search_paths)) - if not isinstance(search_paths, list) or incorrect_type: + if not isinstance(search_paths, list) or incorrect_type: + raise TypeError + except TypeError: raise AnsibleError(err_msg.format(search_paths)) display.debug('{action}: running find module looking in {paths} to get path for "{command}"'.format( @@ -293,7 +293,7 @@ class ActionModule(ActionBase): reboot_result = self._low_level_execute_command(reboot_command, sudoable=self.DEFAULT_SUDOABLE) except AnsibleConnectionFailure as e: # If the connection is closed too quickly due to the system being shutdown, carry on - display.debug('{action}: AnsibleConnectionFailure caught and handled: {error}'.format(action=self._task.action, error=to_native(e))) + display.debug('{action}: AnsibleConnectionFailure caught and handled: {error}'.format(action=self._task.action, error=to_text(e))) reboot_result['rc'] = 0 result['start'] = datetime.utcnow() diff --git a/lib/ansible/plugins/connection/ssh.py b/lib/ansible/plugins/connection/ssh.py index 59c2a8d5cca..61b0ba9eccc 100644 --- a/lib/ansible/plugins/connection/ssh.py +++ b/lib/ansible/plugins/connection/ssh.py @@ -1126,9 +1126,9 @@ class Connection(ConnectionBase): else: # If not in smart mode, the data will be printed by the raise below if len(methods) > 1: - display.warning(msg='%s transfer mechanism failed on %s. Use ANSIBLE_DEBUG=1 to see detailed information' % (method, host)) - display.debug(msg='%s' % to_native(stdout)) - display.debug(msg='%s' % to_native(stderr)) + display.warning('%s transfer mechanism failed on %s. Use ANSIBLE_DEBUG=1 to see detailed information' % (method, host)) + display.debug('%s' % to_text(stdout)) + display.debug('%s' % to_text(stderr)) if returncode == 255: raise AnsibleConnectionFailure("Failed to connect to the host via %s: %s" % (method, to_native(stderr))) diff --git a/lib/ansible/plugins/inventory/advanced_host_list.py b/lib/ansible/plugins/inventory/advanced_host_list.py index 270394776d6..dae0242780a 100644 --- a/lib/ansible/plugins/inventory/advanced_host_list.py +++ b/lib/ansible/plugins/inventory/advanced_host_list.py @@ -24,7 +24,7 @@ EXAMPLES = ''' import os from ansible.errors import AnsibleError, AnsibleParserError -from ansible.module_utils._text import to_bytes, to_native +from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.plugins.inventory import BaseInventoryPlugin @@ -52,7 +52,7 @@ class InventoryModule(BaseInventoryPlugin): try: (hostnames, port) = self._expand_hostpattern(h) except AnsibleError as e: - self.display.vvv("Unable to parse address from hostname, leaving unchanged: %s" % to_native(e)) + self.display.vvv("Unable to parse address from hostname, leaving unchanged: %s" % to_text(e)) host = [h] port = None diff --git a/lib/ansible/plugins/inventory/foreman.py b/lib/ansible/plugins/inventory/foreman.py index 996f7d88359..ebcdff93862 100644 --- a/lib/ansible/plugins/inventory/foreman.py +++ b/lib/ansible/plugins/inventory/foreman.py @@ -72,7 +72,7 @@ validate_certs: False from distutils.version import LooseVersion from ansible.errors import AnsibleError -from ansible.module_utils._text import to_bytes, to_native +from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.module_utils.common._collections_compat import MutableMapping from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable, to_safe_group_name @@ -215,9 +215,9 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): try: self.inventory.set_variable(host['name'], self.get_option('vars_prefix') + k, v) except ValueError as e: - self.display.warning("Could not set host info hostvar for %s, skipping %s: %s" % (host, k, to_native(e))) + self.display.warning("Could not set host info hostvar for %s, skipping %s: %s" % (host, k, to_text(e))) except ValueError as e: - self.display.warning("Could not get host info for %s, skipping: %s" % (host['name'], to_native(e))) + self.display.warning("Could not get host info for %s, skipping: %s" % (host['name'], to_text(e))) # set host vars from params if self.get_option('want_params'): diff --git a/lib/ansible/plugins/inventory/gcp_compute.py b/lib/ansible/plugins/inventory/gcp_compute.py index ff1e753354c..84fc69d36ad 100644 --- a/lib/ansible/plugins/inventory/gcp_compute.py +++ b/lib/ansible/plugins/inventory/gcp_compute.py @@ -120,16 +120,11 @@ compose: import json from ansible.errors import AnsibleError, AnsibleParserError -from ansible.module_utils._text import to_native -from ansible.module_utils.gcp_utils import GcpSession, navigate_hash, GcpRequestException +from ansible.module_utils._text import to_text +from ansible.module_utils.basic import missing_required_lib +from ansible.module_utils.gcp_utils import GcpSession, navigate_hash, GcpRequestException, HAS_GOOGLE_LIBRARIES from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable -try: - import google.auth - import requests -except ImportError: - raise AnsibleError('The gcp dynamic inventory plugin requires the requests and google-auth libraries') - # Mocking a module to reuse module_utils class GcpMockModule(object): @@ -161,7 +156,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): try: self.inventory.set_variable(hostname, self.get_option('vars_prefix') + key, item[key]) except (ValueError, TypeError) as e: - self.display.warning("Could not set host info hostvar for %s, skipping %s: %s" % (hostname, key, to_native(e))) + self.display.warning("Could not set host info hostvar for %s, skipping %s: %s" % (hostname, key, to_text(e))) self.inventory.add_child('all', hostname) def verify_file(self, path): @@ -421,6 +416,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): return interface[u'networkIP'] def parse(self, inventory, loader, path, cache=True): + + if not HAS_GOOGLE_LIBRARIES: + raise AnsibleParserError('gce inventory plugin cannot start: %s' % missing_required_lib('google-auth')) + super(InventoryModule, self).parse(inventory, loader, path) config_data = {} diff --git a/lib/ansible/plugins/inventory/host_list.py b/lib/ansible/plugins/inventory/host_list.py index 6c166349167..4a7a98d8ce3 100644 --- a/lib/ansible/plugins/inventory/host_list.py +++ b/lib/ansible/plugins/inventory/host_list.py @@ -27,7 +27,7 @@ EXAMPLES = r''' import os from ansible.errors import AnsibleError, AnsibleParserError -from ansible.module_utils._text import to_bytes, to_native +from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.parsing.utils.addresses import parse_address from ansible.plugins.inventory import BaseInventoryPlugin @@ -56,7 +56,7 @@ class InventoryModule(BaseInventoryPlugin): try: (host, port) = parse_address(h, allow_ranges=False) except AnsibleError as e: - self.display.vvv("Unable to parse address from hostname, leaving unchanged: %s" % to_native(e)) + self.display.vvv("Unable to parse address from hostname, leaving unchanged: %s" % to_text(e)) host = h port = None diff --git a/lib/ansible/plugins/loader.py b/lib/ansible/plugins/loader.py index c3f832a6782..922263a4b52 100644 --- a/lib/ansible/plugins/loader.py +++ b/lib/ansible/plugins/loader.py @@ -10,7 +10,6 @@ __metaclass__ = type import glob import os import os.path -import pkgutil import sys import warnings @@ -56,7 +55,7 @@ def add_all_plugin_dirs(path): if os.path.isdir(plugin_path): obj.add_directory(to_text(plugin_path)) else: - display.warning("Ignoring invalid path provided to plugin path: '%s' is not a directory" % to_native(path)) + display.warning("Ignoring invalid path provided to plugin path: '%s' is not a directory" % to_text(path)) def get_shell_plugin(shell_type=None, executable=None): @@ -522,7 +521,7 @@ class PluginLoader: if isinstance(ex, AnsibleError): raise # log and continue, likely an innocuous type/package loading failure in collections import - display.debug('has_plugin error: {0}'.format(to_native(ex))) + display.debug('has_plugin error: {0}'.format(to_text(ex))) __contains__ = has_plugin diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 3dfce04d68c..2d168e48f3f 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -27,7 +27,6 @@ import pwd import re import time -from functools import wraps from numbers import Number try: @@ -809,7 +808,7 @@ class Templar: errmsg += "Make sure your variable name does not contain invalid characters like '-': %s" % to_native(te) raise AnsibleUndefinedVariable(errmsg) else: - display.debug("failing because of a type error, template data is: %s" % to_native(data)) + display.debug("failing because of a type error, template data is: %s" % to_text(data)) raise AnsibleError("Unexpected templating type error occurred on (%s): %s" % (to_native(data), to_native(te))) if USE_JINJA2_NATIVE and not isinstance(res, string_types): diff --git a/lib/ansible/vars/manager.py b/lib/ansible/vars/manager.py index 3d2733282f2..9a2b6f4845a 100644 --- a/lib/ansible/vars/manager.py +++ b/lib/ansible/vars/manager.py @@ -35,7 +35,7 @@ from ansible import constants as C from ansible.errors import AnsibleError, AnsibleParserError, AnsibleUndefinedVariable, AnsibleFileNotFound, AnsibleAssertionError, AnsibleTemplateError from ansible.inventory.host import Host from ansible.inventory.helpers import sort_groups, get_group_vars -from ansible.module_utils._text import to_bytes, to_native +from ansible.module_utils._text import to_bytes, to_text from ansible.module_utils.common._collections_compat import Mapping, MutableMapping, Sequence from ansible.module_utils.six import iteritems, text_type, string_types from ansible.plugins.loader import lookup_loader, vars_loader @@ -104,7 +104,7 @@ class VariableManager: except AnsibleError as e: # bad cache plugin is not fatal error # fallback to a dict as in memory cache - display.warning(to_native(e)) + display.warning(to_text(e)) self._fact_cache = {} def __getstate__(self):