From e45d5b7e8ebc05fadb0f66f115722c5ae29246af Mon Sep 17 00:00:00 2001 From: Achraf Cherti Date: Fri, 18 Aug 2017 00:27:03 -0400 Subject: [PATCH] Compatibility of gce.py (Google Cloud Ansible inventory) with Python 3 (#26032) * Compatibility of gce.py (inventory) with Python 3 * Revert './secrets.py' file check (will import 'secrets' from PYTHONPATH) Instead of checking if secrets.py exists in the current directory, this commit will make gce import 'secrets' from one of PYTHONPATH's paths. There are 2 possibilities: 1. secrets.py will be used if secrets.GCE_PARAMS and secrets.GCE_KEYWORD_PARAMS are declared. 2. secrets.py will be ignored if secrets.GCE_PARAMS and secrets.GCE_KEYWORD_PARAMS aren't declared. This could happen in Python >=3.6 where a module named 'secrets' could be imported if a custom secrets.py doesn't exist in PYTHONPATH. Check out https://www.python.org/dev/peps/pep-0506/ and https://docs.python.org/3/library/secrets.html for more information. --- contrib/inventory/gce.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/contrib/inventory/gce.py b/contrib/inventory/gce.py index a73c8b7d023..a1e72dac7f3 100755 --- a/contrib/inventory/gce.py +++ b/contrib/inventory/gce.py @@ -92,7 +92,10 @@ import argparse from time import time -import ConfigParser +if sys.version_info >= (3, 0): + import configparser +else: + import ConfigParser as configparser import logging logging.getLogger('libcloud.common.google').addHandler(logging.NullHandler()) @@ -213,7 +216,7 @@ class GceInventory(object): # This provides empty defaults to each key, so that environment # variable configuration (as opposed to INI configuration) is able # to work. - config = ConfigParser.SafeConfigParser(defaults={ + config = configparser.SafeConfigParser(defaults={ 'gce_service_account_email_address': '', 'gce_service_account_pem_file_path': '', 'gce_project_id': '', @@ -271,10 +274,11 @@ class GceInventory(object): # exists. secrets_path = self.config.get('gce', 'libcloud_secrets') secrets_found = False + try: import secrets - args = list(getattr(secrets, 'GCE_PARAMS', [])) - kwargs = getattr(secrets, 'GCE_KEYWORD_PARAMS', {}) + args = list(secrets.GCE_PARAMS) + kwargs = secrets.GCE_KEYWORD_PARAMS secrets_found = True except: pass