From 3cd98a9fccdd8ac7f241e7b64718e8dedd6cd7fd Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Tue, 9 Apr 2019 13:17:59 -0400 Subject: [PATCH] Use ansible.module_utils.six in inventory scripts (#55000) * Use six from ansible.module_utils for inventory scripts Remove skips from sanity test * Change all imports of ConfigParser to use module_utils.six.moves * Remove commented out lines * Fix six imports --- ...module_utils-six-in-contrib-inventory.yaml | 2 ++ contrib/inventory/abiquo.py | 2 +- contrib/inventory/apache-libcloud.py | 4 ++-- contrib/inventory/azure_rm.py | 8 +------- contrib/inventory/brook.py | 5 +---- contrib/inventory/cobbler.py | 4 ++-- contrib/inventory/collins.py | 6 +++--- contrib/inventory/consul_io.py | 7 ++----- contrib/inventory/ec2.py | 4 ++-- contrib/inventory/foreman.py | 7 +------ contrib/inventory/freeipa.py | 2 +- contrib/inventory/gce.py | 5 +---- contrib/inventory/linode.py | 2 +- contrib/inventory/lxd.py | 6 ++---- contrib/inventory/mdt_dynamic_inventory.py | 5 +---- contrib/inventory/nagios_livestatus.py | 6 +----- contrib/inventory/nagios_ndo.py | 6 +----- contrib/inventory/nsot.py | 2 +- contrib/inventory/openshift.py | 2 +- contrib/inventory/ovirt.py | 2 +- contrib/inventory/ovirt4.py | 5 +---- contrib/inventory/packet_net.py | 4 ++-- contrib/inventory/proxmox.py | 4 ++-- contrib/inventory/rax.py | 4 ++-- contrib/inventory/rudder.py | 2 +- contrib/inventory/scaleway.py | 4 ++-- contrib/inventory/spacewalk.py | 4 ++-- contrib/inventory/vmware.py | 5 ++--- contrib/inventory/vmware_inventory.py | 5 +++-- contrib/inventory/zabbix.py | 5 +---- contrib/vault/azure_vault.py | 7 +------ docs/bin/plugin_formatter.py | 2 +- test/sanity/code-smell/use-compat-six.py | 19 ------------------- 33 files changed, 48 insertions(+), 109 deletions(-) create mode 100644 changelogs/fragments/use-module_utils-six-in-contrib-inventory.yaml diff --git a/changelogs/fragments/use-module_utils-six-in-contrib-inventory.yaml b/changelogs/fragments/use-module_utils-six-in-contrib-inventory.yaml new file mode 100644 index 00000000000..10ec89f54f8 --- /dev/null +++ b/changelogs/fragments/use-module_utils-six-in-contrib-inventory.yaml @@ -0,0 +1,2 @@ +minor_changes: + - use ansible.module_utils.six for all scripts in contrib/inventory diff --git a/contrib/inventory/abiquo.py b/contrib/inventory/abiquo.py index 90d52d3c6c9..5a7950bd705 100755 --- a/contrib/inventory/abiquo.py +++ b/contrib/inventory/abiquo.py @@ -44,10 +44,10 @@ imagetemplate: Creates a host group for each image template containing all hosts import os import sys import time -import ConfigParser import json +from ansible.module_utils.six.moves import configparser as ConfigParser from ansible.module_utils.urls import open_url diff --git a/contrib/inventory/apache-libcloud.py b/contrib/inventory/apache-libcloud.py index 5d3fe9a2e08..3857d2f9344 100755 --- a/contrib/inventory/apache-libcloud.py +++ b/contrib/inventory/apache-libcloud.py @@ -35,9 +35,9 @@ import os import argparse import re from time import time -import ConfigParser -from six import iteritems, string_types +from ansible.module_utils.six import iteritems, string_types +from ansible.module_utils.six.moves import configparser as ConfigParser from libcloud.compute.types import Provider from libcloud.compute.providers import get_driver import libcloud.security as sec diff --git a/contrib/inventory/azure_rm.py b/contrib/inventory/azure_rm.py index a83558cd49c..7dc438f6a2f 100755 --- a/contrib/inventory/azure_rm.py +++ b/contrib/inventory/azure_rm.py @@ -201,14 +201,8 @@ import re import sys import inspect -try: - # python2 - import ConfigParser as cp -except ImportError: - # python3 - import configparser as cp - from os.path import expanduser +from ansible.module_utils.six.moves import configparser as cp import ansible.module_utils.six.moves.urllib.parse as urlparse HAS_AZURE = True diff --git a/contrib/inventory/brook.py b/contrib/inventory/brook.py index b8c4bd7341b..236571315bb 100755 --- a/contrib/inventory/brook.py +++ b/contrib/inventory/brook.py @@ -79,10 +79,7 @@ Version: 0.2 import sys import os -try: - from ConfigParser import SafeConfigParser as ConfigParser -except ImportError: - from configparser import ConfigParser +from ansible.module_utils.six.moves.configparser import SafeConfigParser as ConfigParser import json diff --git a/contrib/inventory/cobbler.py b/contrib/inventory/cobbler.py index eff9318967c..a3b94b7a967 100755 --- a/contrib/inventory/cobbler.py +++ b/contrib/inventory/cobbler.py @@ -59,7 +59,6 @@ Changelog: ###################################################################### import argparse -import ConfigParser import os import re from time import time @@ -67,7 +66,8 @@ import xmlrpclib import json -from six import iteritems +from ansible.module_utils.six import iteritems +from ansible.module_utils.six.moves import configparser as ConfigParser # NOTE -- this file assumes Ansible is being accessed FROM the cobbler # server, so it does not attempt to login with a username and password. diff --git a/contrib/inventory/collins.py b/contrib/inventory/collins.py index b768f6b3f9c..39f6c4b4475 100755 --- a/contrib/inventory/collins.py +++ b/contrib/inventory/collins.py @@ -67,7 +67,6 @@ Tested against Ansible 1.8.2 and Collins 1.3.0. import argparse -import ConfigParser import logging import os import re @@ -77,8 +76,9 @@ import traceback import json -from six import iteritems -from six.moves.urllib.parse import urlencode +from ansible.module_utils.six import iteritems +from ansible.module_utils.six.moves import configparser as ConfigParser +from ansible.module_utils.six.moves.urllib.parse import urlencode from ansible.module_utils.urls import open_url diff --git a/contrib/inventory/consul_io.py b/contrib/inventory/consul_io.py index 113f3c99a8b..4dad3eeec13 100755 --- a/contrib/inventory/consul_io.py +++ b/contrib/inventory/consul_io.py @@ -136,10 +136,7 @@ import re import argparse import sys -try: - import configparser -except ImportError: - import ConfigParser as configparser +from ansible.module_utils.six.moves import configparser def get_log_filename(): @@ -201,7 +198,7 @@ except ImportError as e: sys.exit("""failed=True msg='python-consul required for this module. See https://python-consul.readthedocs.io/en/latest/#installation'""") -from six import iteritems +from ansible.module_utils.six import iteritems class ConsulInventory(object): diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index 1e9487d9bbb..3d0ba3a2454 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -166,9 +166,10 @@ from boto import rds from boto import elasticache from boto import route53 from boto import sts -import six +from ansible.module_utils import six from ansible.module_utils import ec2 as ec2_utils +from ansible.module_utils.six.moves import configparser HAS_BOTO3 = False try: @@ -177,7 +178,6 @@ try: except ImportError: pass -from six.moves import configparser from collections import defaultdict import json diff --git a/contrib/inventory/foreman.py b/contrib/inventory/foreman.py index f51d073b17c..e3f98f360a8 100755 --- a/contrib/inventory/foreman.py +++ b/contrib/inventory/foreman.py @@ -22,12 +22,6 @@ # Stdlib imports # __future__ imports must occur at the beginning of file from __future__ import print_function -try: - # Python 2 version - import ConfigParser -except ImportError: - # Python 3 version - import configparser as ConfigParser import json import argparse import copy @@ -47,6 +41,7 @@ if LooseVersion(requests.__version__) < LooseVersion('1.1.0'): from requests.auth import HTTPBasicAuth from ansible.module_utils._text import to_text +from ansible.module_utils.six.moves import configparser as ConfigParser def json_format_dict(data, pretty=False): diff --git a/contrib/inventory/freeipa.py b/contrib/inventory/freeipa.py index 8dac3382a01..cb5ccc071a5 100755 --- a/contrib/inventory/freeipa.py +++ b/contrib/inventory/freeipa.py @@ -8,7 +8,7 @@ import json import os import sys from ipalib import api, errors, __version__ as IPA_VERSION -from six import u +from ansible.module_utils.six import u def initialize(): diff --git a/contrib/inventory/gce.py b/contrib/inventory/gce.py index e030a5fb127..89a08fbd137 100755 --- a/contrib/inventory/gce.py +++ b/contrib/inventory/gce.py @@ -80,10 +80,7 @@ import argparse from time import time -if sys.version_info >= (3, 0): - import configparser -else: - import ConfigParser as configparser +from ansible.module_utils.six.moves import configparser import logging logging.getLogger('libcloud.common.google').addHandler(logging.NullHandler()) diff --git a/contrib/inventory/linode.py b/contrib/inventory/linode.py index 427796be3ca..f4ae302816f 100755 --- a/contrib/inventory/linode.py +++ b/contrib/inventory/linode.py @@ -108,7 +108,7 @@ except Exception: load_chube_config() # Imports for ansible -import ConfigParser +from ansible.module_utils.six.moves import configparser as ConfigParser class LinodeInventory(object): diff --git a/contrib/inventory/lxd.py b/contrib/inventory/lxd.py index 78a545a791e..2cb13542773 100755 --- a/contrib/inventory/lxd.py +++ b/contrib/inventory/lxd.py @@ -27,10 +27,8 @@ from subprocess import Popen, PIPE import distutils.spawn import sys import json -try: - import configparser -except Exception: - from six.moves import configparser + +from ansible.module_utils.six.moves import configparser # Set up defaults resource = 'local:' diff --git a/contrib/inventory/mdt_dynamic_inventory.py b/contrib/inventory/mdt_dynamic_inventory.py index 947091079ae..154aac4f8d4 100755 --- a/contrib/inventory/mdt_dynamic_inventory.py +++ b/contrib/inventory/mdt_dynamic_inventory.py @@ -27,10 +27,7 @@ maintainer: J Barnett (github @jbarnett1981) import argparse import json import pymssql -try: - import configparser -except ImportError: - import ConfigParser as configparser +from ansible.module_utils.six.moves import configparser class MDTInventory(object): diff --git a/contrib/inventory/nagios_livestatus.py b/contrib/inventory/nagios_livestatus.py index 8f428000d8a..25c043b5c06 100755 --- a/contrib/inventory/nagios_livestatus.py +++ b/contrib/inventory/nagios_livestatus.py @@ -36,11 +36,7 @@ import re import argparse import sys -try: - import configparser -except ImportError: - import ConfigParser - configparser = ConfigParser +from ansible.module_utils.six.moves import configparser import json try: diff --git a/contrib/inventory/nagios_ndo.py b/contrib/inventory/nagios_ndo.py index a4477e1e263..0f89ede6596 100755 --- a/contrib/inventory/nagios_ndo.py +++ b/contrib/inventory/nagios_ndo.py @@ -29,11 +29,7 @@ Configuration is read from `nagios_ndo.ini`. import os import argparse import sys -try: - import configparser -except ImportError: - import ConfigParser - configparser = ConfigParser +from ansible.module_utils.six.moves import configparser import json try: diff --git a/contrib/inventory/nsot.py b/contrib/inventory/nsot.py index cf1c1e19037..da5df40449f 100755 --- a/contrib/inventory/nsot.py +++ b/contrib/inventory/nsot.py @@ -148,7 +148,7 @@ from pynsot.client import get_api_client from pynsot.app import HttpServerError from click.exceptions import UsageError -from six import string_types +from ansible.module_utils.six import string_types def warning(*objs): diff --git a/contrib/inventory/openshift.py b/contrib/inventory/openshift.py index e6ba8ecfa29..c0aa4f1b897 100755 --- a/contrib/inventory/openshift.py +++ b/contrib/inventory/openshift.py @@ -32,10 +32,10 @@ import json import os import os.path import sys -import ConfigParser import StringIO from ansible.module_utils.urls import open_url +from ansible.module_utils.six.moves import configparser as ConfigParser configparser = None diff --git a/contrib/inventory/ovirt.py b/contrib/inventory/ovirt.py index 90729558ab2..f97ab7a24a6 100755 --- a/contrib/inventory/ovirt.py +++ b/contrib/inventory/ovirt.py @@ -68,8 +68,8 @@ USER_AGENT_VERSION = "v1" import sys import os import argparse -import ConfigParser from collections import defaultdict +from ansible.module_utils.six.moves import configparser as ConfigParser import json diff --git a/contrib/inventory/ovirt4.py b/contrib/inventory/ovirt4.py index 71ce85582cd..69c3cc3a05f 100755 --- a/contrib/inventory/ovirt4.py +++ b/contrib/inventory/ovirt4.py @@ -65,10 +65,7 @@ import sys from collections import defaultdict -try: - import ConfigParser as configparser -except ImportError: - import configparser +from ansible.module_utils.six.moves import configparser import json diff --git a/contrib/inventory/packet_net.py b/contrib/inventory/packet_net.py index 5368de4eb0c..6672226f2b1 100755 --- a/contrib/inventory/packet_net.py +++ b/contrib/inventory/packet_net.py @@ -43,9 +43,9 @@ import os import argparse import re from time import time -import six -from six.moves import configparser +from ansible.module_utils import six +from ansible.module_utils.six.moves import configparser try: import packet diff --git a/contrib/inventory/proxmox.py b/contrib/inventory/proxmox.py index 351e55da096..0538ca8a9b3 100755 --- a/contrib/inventory/proxmox.py +++ b/contrib/inventory/proxmox.py @@ -30,8 +30,8 @@ import os import sys from optparse import OptionParser -from six import iteritems -from six.moves.urllib.parse import urlencode +from ansible.module_utils.six import iteritems +from ansible.module_utils.six.moves.urllib.parse import urlencode from ansible.module_utils.urls import open_url diff --git a/contrib/inventory/rax.py b/contrib/inventory/rax.py index dca3c80bf49..c6d512cd12b 100755 --- a/contrib/inventory/rax.py +++ b/contrib/inventory/rax.py @@ -151,9 +151,9 @@ import sys import argparse import warnings import collections -import ConfigParser -from six import iteritems +from ansible.module_utils.six import iteritems +from ansible.module_utils.six.moves import configparser as ConfigParser import json diff --git a/contrib/inventory/rudder.py b/contrib/inventory/rudder.py index e361f22de62..4722fcf1e41 100755 --- a/contrib/inventory/rudder.py +++ b/contrib/inventory/rudder.py @@ -53,9 +53,9 @@ import sys import os import re import argparse -import six import httplib2 as http from time import time +from ansible.module_utils import six from ansible.module_utils.six.moves import configparser from ansible.module_utils.six.moves.urllib.parse import urlparse diff --git a/contrib/inventory/scaleway.py b/contrib/inventory/scaleway.py index 756af89931f..32999cc0e7a 100755 --- a/contrib/inventory/scaleway.py +++ b/contrib/inventory/scaleway.py @@ -41,8 +41,8 @@ all: Contains all hosts defined in Scaleway. import copy import os import requests -import six -from six.moves import configparser +from ansible.module_utils import six +from ansible.module_utils.six.moves import configparser import sys import time import traceback diff --git a/contrib/inventory/spacewalk.py b/contrib/inventory/spacewalk.py index 3d6d00b8f5b..e94298c308d 100755 --- a/contrib/inventory/spacewalk.py +++ b/contrib/inventory/spacewalk.py @@ -47,10 +47,10 @@ import os import time from optparse import OptionParser import subprocess -import ConfigParser import json -from six import iteritems +from ansible.module_utils.six import iteritems +from ansible.module_utils.six.moves import configparser as ConfigParser base_dir = os.path.dirname(os.path.realpath(__file__)) diff --git a/contrib/inventory/vmware.py b/contrib/inventory/vmware.py index de52e713d6b..483fd42318c 100755 --- a/contrib/inventory/vmware.py +++ b/contrib/inventory/vmware.py @@ -38,10 +38,9 @@ import ssl import sys import time -from six import integer_types, text_type, string_types -from six.moves import configparser - from ansible.module_utils.common._collections_compat import MutableMapping +from ansible.module_utils.six import integer_types, text_type, string_types +from ansible.module_utils.six.moves import configparser # Disable logging message trigged by pSphere/suds. try: diff --git a/contrib/inventory/vmware_inventory.py b/contrib/inventory/vmware_inventory.py index e6472d1fb62..2f5e4a8d074 100755 --- a/contrib/inventory/vmware_inventory.py +++ b/contrib/inventory/vmware_inventory.py @@ -39,8 +39,9 @@ import uuid from time import time from jinja2 import Environment -from six import integer_types, PY3 -from six.moves import configparser + +from ansible.module_utils.six import integer_types, PY3 +from ansible.module_utils.six.moves import configparser try: import argparse diff --git a/contrib/inventory/zabbix.py b/contrib/inventory/zabbix.py index 43a539a0067..acdf38e7048 100755 --- a/contrib/inventory/zabbix.py +++ b/contrib/inventory/zabbix.py @@ -38,10 +38,7 @@ from __future__ import print_function import os import sys import argparse -try: - import ConfigParser as configparser -except ImportError: - import configparser +from ansible.module_utils.six.moves import configparser try: from zabbix_api import ZabbixAPI diff --git a/contrib/vault/azure_vault.py b/contrib/vault/azure_vault.py index 8c118cc0fc6..5f15b2d40ec 100755 --- a/contrib/vault/azure_vault.py +++ b/contrib/vault/azure_vault.py @@ -122,12 +122,7 @@ import sys import inspect from azure.keyvault import KeyVaultClient -try: - # python2 - import ConfigParser as cp -except ImportError: - # python3 - import configparser as cp +from ansible.module_utils.six.moves import configparser as cp from os.path import expanduser import ansible.module_utils.six.moves.urllib.parse as urlparse diff --git a/docs/bin/plugin_formatter.py b/docs/bin/plugin_formatter.py index c685b7eb17d..3fb093fd9b8 100755 --- a/docs/bin/plugin_formatter.py +++ b/docs/bin/plugin_formatter.py @@ -35,12 +35,12 @@ import jinja2 import yaml from jinja2 import Environment, FileSystemLoader from jinja2.runtime import Undefined -from six import iteritems, string_types from ansible.errors import AnsibleError from ansible.module_utils._text import to_bytes, to_text from ansible.module_utils.common.collections import is_sequence from ansible.module_utils.parsing.convert_bool import boolean +from ansible.module_utils.six import iteritems, string_types from ansible.plugins.loader import fragment_loader from ansible.utils import plugin_docs from ansible.utils.display import Display diff --git a/test/sanity/code-smell/use-compat-six.py b/test/sanity/code-smell/use-compat-six.py index c3da78e8491..fc87ed22841 100755 --- a/test/sanity/code-smell/use-compat-six.py +++ b/test/sanity/code-smell/use-compat-six.py @@ -11,25 +11,6 @@ def main(): # digital_ocean is checking for six because dopy doesn't specify the # requirement on six so it needs to try importing six to give the correct error message 'lib/ansible/modules/cloud/digital_ocean/digital_ocean.py', - # correct imports in the following files and remove them from this list - 'contrib/inventory/apache-libcloud.py', - 'contrib/inventory/cobbler.py', - 'contrib/inventory/collins.py', - 'contrib/inventory/consul_io.py', - 'contrib/inventory/ec2.py', - 'contrib/inventory/freeipa.py', - 'contrib/inventory/lxd.py', - 'contrib/inventory/nova.py', - 'contrib/inventory/nsot.py', - 'contrib/inventory/packet_net.py', - 'contrib/inventory/proxmox.py', - 'contrib/inventory/rax.py', - 'contrib/inventory/rudder.py', - 'contrib/inventory/scaleway.py', - 'contrib/inventory/spacewalk.py', - 'contrib/inventory/vmware.py', - 'contrib/inventory/vmware_inventory.py', - 'docs/bin/plugin_formatter.py', ]) for path in sys.argv[1:] or sys.stdin.read().splitlines():