Improve compatibiliity with Python 3

This patchset introduces some changes to improve compability with Python
3. It does _not_ make the codebase 100% Python 3 compatible, however.

Changes made:

* Fix import of the Queue module to look for 'queue' on ImportError
* Rename `iteritems` calls to `items`.

Some remaining compatibility issues:

* Bytes vs string objects - this compromises most of the errors we
  currently have, across all files and tests
* Treating `dict_keys` objects as lists and calling `append`
  (generate._parse_global_variables)
* Altering dictionary keys during iteration
  (test_inventory.testing_deleting_elements)

Change-Id: I6ad9601c5835703f44cc0752cef15c682298a40e
This commit is contained in:
Nolan Brubaker 2017-06-06 10:39:10 -04:00
parent 3bea9d3404
commit a1d51d58e7
5 changed files with 25 additions and 19 deletions

View File

@ -27,7 +27,7 @@ def merge_dict(base_items, new_items):
:param new_items: ``dict``
:return dictionary:
"""
for key, value in new_items.iteritems():
for key, value in new_items.items():
if isinstance(value, dict) and value:
base_merge = merge_dict(base_items.get(key, {}), value)
base_items[key] = base_merge
@ -79,9 +79,9 @@ def recursive_dict_removal(inventory, purge_list):
:param inventory: ``dict`` Dictionary representing the inventory
:param purge_list: ``list`` List of items to remove
"""
for key, value in inventory.iteritems():
for key, value in inventory.items():
if isinstance(value, dict):
for child_key, child_value in value.iteritems():
for child_key, child_value in value.items():
if isinstance(child_value, dict):
for item in purge_list:
if item in child_value:

View File

@ -261,7 +261,7 @@ def _append_to_host_groups(inventory, container_type, assignment, host_type,
iph = inventory[physical_group_type]['hosts']
iah = inventory[assignment]['hosts']
for hname, hdata in inventory['_meta']['hostvars'].iteritems():
for hname, hdata in inventory['_meta']['hostvars'].items():
is_metal = False
properties = hdata.get('properties')
if properties:
@ -413,7 +413,7 @@ def user_defined_setup(config, inventory):
:param inventory: ``dict`` Living dictionary of inventory
"""
hvs = inventory['_meta']['hostvars']
for key, value in config.iteritems():
for key, value in config.items():
if key.endswith('hosts'):
if key not in inventory:
logger.debug("Key %s was added to inventory", key)
@ -423,7 +423,7 @@ def user_defined_setup(config, inventory):
logger.debug("Key %s had no value", key)
return
for _key, _value in value.iteritems():
for _key, _value in value.items():
if _key not in hvs:
hvs[_key] = {}
@ -460,10 +460,10 @@ def skel_setup(environment, inventory):
:param environment: ``dict`` Known environment information
:param inventory: ``dict`` Living dictionary of inventory
"""
for key, value in environment.iteritems():
for key, value in environment.items():
if key == 'version':
continue
for _key, _value in value.iteritems():
for _key, _value in value.items():
if _key not in inventory:
logger.debug("Key %s added to inventory", _key)
inventory[_key] = {}
@ -496,7 +496,7 @@ def skel_load(skeleton, inventory):
memberships for the inventory.
:param inventory: ``dict`` Living dictionary of inventory
"""
for key, value in skeleton.iteritems():
for key, value in skeleton.items():
_parse_belongs_to(
key,
belongs_to=value['belongs_to'],
@ -670,7 +670,7 @@ def container_skel_load(container_skel, inventory, config):
"""
logger.debug("Loading container skeleton")
for key, value in container_skel.iteritems():
for key, value in container_skel.items():
contains_in = value.get('contains', False)
belongs_to_in = value.get('belongs_to', False)
if contains_in or belongs_to_in:
@ -846,9 +846,9 @@ def _check_same_ip_to_multiple_host(config):
"""
ips_to_hostnames_mapping = dict()
for key, value in config.iteritems():
for key, value in config.items():
if key.endswith('hosts'):
for _key, _value in value.iteritems():
for _key, _value in value.items():
hostname = _key
ip = _value['ip']
if not (ip in ips_to_hostnames_mapping):
@ -931,7 +931,7 @@ def _check_config_settings(cidr_networks, config, container_skel):
# search for any container that doesn't have is_metal flag set to true
is_provider_networks_needed = False
for key, value in container_skel.iteritems():
for key, value in container_skel.items():
properties = value.get('properties', {})
is_metal = properties.get('is_metal', False)
if not is_metal:
@ -1010,9 +1010,9 @@ def _collect_hostnames(inventory):
# Generate a list of all hosts and their used IP addresses
hostnames_ips = {}
for _host, _vars in inventory['_meta']['hostvars'].iteritems():
for _host, _vars in inventory['_meta']['hostvars'].items():
host_hash = hostnames_ips[_host] = {}
for _key, _value in _vars.iteritems():
for _key, _value in _vars.items():
if _key.endswith('address') or _key == 'ansible_host':
host_hash[_key] = _value

View File

@ -19,7 +19,10 @@
import copy
import logging
import netaddr
import Queue
try:
import Queue
except ImportError:
import queue as Queue
import random
logger = logging.getLogger('osa-inventory')

View File

@ -169,7 +169,7 @@ def print_groups_per_container(inventory):
]
table = prettytable.PrettyTable(required_list)
for container_name, groups in containers.iteritems():
for container_name, groups in containers.items():
row = [container_name, ', '.join(sorted(groups))]
table.add_row(row)
@ -230,7 +230,7 @@ def print_inventory(inventory, sort_key):
'container_types'
]
table = prettytable.PrettyTable(required_list)
for key, values in _meta_data.iteritems():
for key, values in _meta_data.items():
for rl in required_list:
if rl not in values:
values[rl] = None

View File

@ -16,7 +16,10 @@ import json
import mock
import os
from os import path
import Queue
try:
import Queue
except ImportError:
import queue as Queue
import sys
import unittest
import warnings