Skip to content

Commit

Permalink
Merge pull request ansible#12119 from mgedmin/py3k
Browse files Browse the repository at this point in the history
Some steps towards Python 3 support
  • Loading branch information
abadger committed Aug 27, 2015
2 parents 2ac931d + c846bab commit 6907166
Show file tree
Hide file tree
Showing 48 changed files with 114 additions and 120 deletions.
10 changes: 5 additions & 5 deletions contrib/inventory/abiquo.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def save_cache(data, config):
cache = open('/'.join([dpath,'inventory']), 'w')
cache.write(json.dumps(data))
cache.close()
except IOError, e:
except IOError as e:
pass # not really sure what to do here


Expand All @@ -88,7 +88,7 @@ def get_cache(cache_item, config):
cache = open('/'.join([dpath,'inventory']), 'r')
inv = cache.read()
cache.close()
except IOError, e:
except IOError as e:
pass # not really sure what to do here

return inv
Expand Down Expand Up @@ -172,7 +172,7 @@ def generate_inv_from_api(enterprise_entity,config):
else:
vm_metadata = metadata['metadata']['metadata']
inventory['_meta']['hostvars'][vm_nic] = vm_metadata
except Exception, e:
except Exception as e:
pass

inventory[vm_vapp]['children'].append(vmcollection['name'])
Expand All @@ -183,7 +183,7 @@ def generate_inv_from_api(enterprise_entity,config):
inventory[vmcollection['name']].append(vm_nic)

return inventory
except Exception, e:
except Exception as e:
# Return empty hosts output
return { 'all': {'hosts': []}, '_meta': { 'hostvars': {} } }

Expand Down Expand Up @@ -214,7 +214,7 @@ def get_inventory(enterprise, config):
try:
login = api_get(None,config)
enterprise = next(link for link in (login['links']) if (link['rel']=='enterprise'))
except Exception, e:
except Exception as e:
enterprise = None

if cache_available(config):
Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/cloudstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(self):
options = parser.parse_args()
try:
self.cs = CloudStack(**read_config())
except CloudStackException, e:
except CloudStackException as e:
print >> sys.stderr, "Error: Could not connect to CloudStack API"

project_id = ''
Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/consul_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@

try:
import consul
except ImportError, e:
except ImportError as e:
print """failed=True msg='python-consul required for this module. see
http://python-consul.readthedocs.org/en/latest/#installation'"""
sys.exit(1)
Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/digital_ocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@

try:
from dopy.manager import DoError, DoManager
except ImportError, e:
except ImportError as e:
print "failed=True msg='`dopy` library required for this script'"
sys.exit(1)

Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def get_instance(self, instance_name):
'''Gets details about a specific instance '''
try:
return self.driver.ex_get_node(instance_name)
except Exception, e:
except Exception as e:
return None

def group_instances(self):
Expand Down
6 changes: 3 additions & 3 deletions contrib/inventory/linode.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
from chube.linode_obj import Linode

sys.path = old_path
except Exception, e:
except Exception as e:
raise Exception("could not import chube")

load_chube_config()
Expand Down Expand Up @@ -184,7 +184,7 @@ def get_nodes(self):
try:
for node in Linode.search(status=Linode.STATUS_RUNNING):
self.add_node(node)
except chube_api.linode_api.ApiError, e:
except chube_api.linode_api.ApiError as e:
print "Looks like Linode's API is down:"
print
print e
Expand All @@ -194,7 +194,7 @@ def get_node(self, linode_id):
"""Gets details about a specific node."""
try:
return Linode.find(api_id=linode_id)
except chube_api.linode_api.ApiError, e:
except chube_api.linode_api.ApiError as e:
print "Looks like Linode's API is down:"
print
print e
Expand Down
4 changes: 2 additions & 2 deletions contrib/inventory/rax.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def _list_into_cache(regions):
if cs is None:
warnings.warn(
'Connecting to Rackspace region "%s" has caused Pyrax to '
'return a NoneType. Is this a valid region?' % region,
'return None. Is this a valid region?' % region,
RuntimeWarning)
continue
for server in cs.servers.list():
Expand Down Expand Up @@ -412,7 +412,7 @@ def setup():
pyrax.keyring_auth(keyring_username, region=region)
else:
pyrax.set_credential_file(creds_file, region=region)
except Exception, e:
except Exception as e:
sys.stderr.write("%s: %s\n" % (e, e.message))
sys.exit(1)

Expand Down
8 changes: 4 additions & 4 deletions contrib/inventory/spacewalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def spacewalk_report(name):
for group in spacewalk_report('system-groups'):
org_groups[group['spacewalk_group_id']] = group['spacewalk_org_id']

except (OSError), e:
except (OSError) as e:
print >> sys.stderr, 'Problem executing the command "%s system-groups": %s' % \
(SW_REPORT, str(e))
sys.exit(2)
Expand All @@ -148,7 +148,7 @@ def spacewalk_report(name):
for item in spacewalk_report('inventory'):
host_vars[ item['spacewalk_profile_name'] ] = dict( ( key, ( value.split(';') if ';' in value else value) ) for key, value in item.items() )

except (OSError), e:
except (OSError) as e:
print >> sys.stderr, 'Problem executing the command "%s inventory": %s' % \
(SW_REPORT, str(e))
sys.exit(2)
Expand Down Expand Up @@ -185,7 +185,7 @@ def spacewalk_report(name):
if system['spacewalk_server_name'] in host_vars and not system['spacewalk_server_name'] in meta[ "hostvars" ]:
meta[ "hostvars" ][ system['spacewalk_server_name'] ] = host_vars[ system['spacewalk_server_name'] ]

except (OSError), e:
except (OSError) as e:
print >> sys.stderr, 'Problem executing the command "%s system-groups-systems": %s' % \
(SW_REPORT, str(e))
sys.exit(2)
Expand All @@ -212,7 +212,7 @@ def spacewalk_report(name):
host_details = system
break

except (OSError), e:
except (OSError) as e:
print >> sys.stderr, 'Problem executing the command "%s inventory": %s' % \
(SW_REPORT, str(e))
sys.exit(2)
Expand Down
4 changes: 2 additions & 2 deletions contrib/inventory/vmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def _get_obj_info(self, obj, depth=99, seen=None):
obj_info = self._get_obj_info(val, depth - 1, seen)
if obj_info != ():
d[attr] = obj_info
except Exception, e:
except Exception as e:
pass
return d
elif isinstance(obj, SudsObject):
Expand Down Expand Up @@ -207,7 +207,7 @@ def _get_host_info(self, host, prefix='vmware'):
host_info[k] = v
try:
host_info['ipAddress'] = host.config.network.vnic[0].spec.ip.ipAddress
except Exception, e:
except Exception as e:
print >> sys.stderr, e
host_info = self._flatten_dict(host_info, prefix)
if ('%s_ipAddress' % prefix) in host_info:
Expand Down
2 changes: 1 addition & 1 deletion contrib/inventory/zabbix.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def __init__(self):
try:
api = ZabbixAPI(server=self.zabbix_server)
api.login(user=self.zabbix_username, password=self.zabbix_password)
except BaseException, e:
except BaseException as e:
print >> sys.stderr, "Error: Could not login to Zabbix server. Check your zabbix.ini."
sys.exit(1)

Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/cli/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def run(self):
# this typically means we couldn't even parse the docstring, not just that the YAML is busted,
# probably a quoting issue.
raise AnsibleError("Parsing produced an empty object.")
except Exception, e:
except Exception as e:
self.display.vvv(traceback.print_exc())
raise AnsibleError("module %s missing documentation (or could not parse documentation): %s\n" % (module, str(e)))

Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/cli/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def run(self):
os.chdir('/')
try:
shutil.rmtree(self.options.dest)
except Exception, e:
except Exception as e:
self.display.error("Failed to remove %s: %s" % (self.options.dest, str(e)))

return rc
Expand Down
4 changes: 2 additions & 2 deletions lib/ansible/executor/process/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, tqm, main_q, rslt_q, loader):
if fileno is not None:
try:
self._new_stdin = os.fdopen(os.dup(fileno))
except OSError, e:
except OSError as e:
# couldn't dupe stdin, most likely because it's
# not a valid file descriptor, so we just rely on
# using the one that was passed in
Expand Down Expand Up @@ -137,7 +137,7 @@ def run(self):
except:
# FIXME: most likely an abort, catch those kinds of errors specifically
break
except Exception, e:
except Exception as e:
debug("WORKER EXCEPTION: %s" % e)
debug("WORKER EXCEPTION: %s" % traceback.format_exc())
try:
Expand Down
6 changes: 3 additions & 3 deletions lib/ansible/executor/task_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ def run(self):
result = json.dumps(res)
debug("done dumping result, returning")
return result
except AnsibleError, e:
except AnsibleError as e:
return dict(failed=True, msg=to_unicode(e, nonstring='simplerepr'))
finally:
try:
self._connection.close()
except AttributeError:
pass
except Exception, e:
except Exception as e:
debug("error closing connection: %s" % to_unicode(e))

def _get_loop_items(self):
Expand Down Expand Up @@ -187,7 +187,7 @@ def _run_loop(self, items):

try:
tmp_task = self._task.copy()
except AnsibleParserError, e:
except AnsibleParserError as e:
results.append(dict(failed=True, msg=str(e)))
continue

Expand Down
4 changes: 2 additions & 2 deletions lib/ansible/inventory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def _match(self, str, pattern_str):
return re.search(pattern_str[1:], str)
else:
return fnmatch.fnmatch(str, pattern_str)
except Exception, e:
except Exception as e:
raise AnsibleError('invalid host pattern: %s' % pattern_str)

def _match_list(self, items, item_attr, pattern_str):
Expand All @@ -153,7 +153,7 @@ def _match_list(self, items, item_attr, pattern_str):
pattern = re.compile(fnmatch.translate(pattern_str))
else:
pattern = re.compile(pattern_str[1:])
except Exception, e:
except Exception as e:
raise AnsibleError('invalid host pattern: %s' % pattern_str)

for item in items:
Expand Down
4 changes: 2 additions & 2 deletions lib/ansible/inventory/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, loader, filename=C.DEFAULT_HOST_LIST):
cmd = [ self.filename, "--list" ]
try:
sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError, e:
except OSError as e:
raise AnsibleError("problem running %s (%s)" % (' '.join(cmd), e))
(stdout, stderr) = sp.communicate()

Expand Down Expand Up @@ -153,7 +153,7 @@ def get_host_variables(self, host):
cmd = [self.filename, "--host", host.name]
try:
sp = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError, e:
except OSError as e:
raise AnsibleError("problem running %s (%s)" % (' '.join(cmd), e))
(out, err) = sp.communicate()
if out.strip() == '':
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/playbook/conditional.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def evaluate_conditional(self, templar, all_vars):
for conditional in self.when:
if not self._check_conditional(conditional, templar, all_vars):
return False
except Exception, e:
except Exception as e:
raise AnsibleError("The conditional check '%s' failed. The error was: %s" % (conditional, e), obj=ds)

return True
Expand Down
2 changes: 0 additions & 2 deletions lib/ansible/playbook/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import os

from types import NoneType

from ansible.errors import AnsibleParserError
from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject, AnsibleSequence

Expand Down
15 changes: 7 additions & 8 deletions lib/ansible/playbook/role/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import os

from hashlib import sha1
from types import NoneType

from ansible.errors import AnsibleError, AnsibleParserError
from ansible.parsing import DataLoader
Expand Down Expand Up @@ -184,16 +183,16 @@ def _load_role_data(self, role_include, parent_role=None):

# vars and default vars are regular dictionaries
self._role_vars = self._load_role_yaml('vars')
if not isinstance(self._role_vars, (dict, NoneType)):
raise AnsibleParserError("The vars/main.yml file for role '%s' must contain a dictionary of variables" % self._role_name)
elif self._role_vars is None:
if self._role_vars is None:
self._role_vars = dict()
elif not isinstance(self._role_vars, dict):
raise AnsibleParserError("The vars/main.yml file for role '%s' must contain a dictionary of variables" % self._role_name)

self._default_vars = self._load_role_yaml('defaults')
if not isinstance(self._default_vars, (dict, NoneType)):
raise AnsibleParserError("The default/main.yml file for role '%s' must contain a dictionary of variables" % self._role_name)
elif self._default_vars is None:
if self._default_vars is None:
self._default_vars = dict()
elif not isinstance(self._default_vars, dict):
raise AnsibleParserError("The default/main.yml file for role '%s' must contain a dictionary of variables" % self._role_name)

def _load_role_yaml(self, subdir):
file_path = os.path.join(self._role_path, subdir)
Expand Down Expand Up @@ -370,7 +369,7 @@ def serialize(self, include_deps=True):
def deserialize(self, data, include_deps=True):
self._role_name = data.get('_role_name', '')
self._role_path = data.get('_role_path', '')
self._role_vars = data.get('_role_vars', dict())
self._role_vars = data.get('_role_vars', dict())
self._role_params = data.get('_role_params', dict())
self._default_vars = data.get('_default_vars', dict())
self._had_task_run = data.get('_had_task_run', dict())
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/plugins/action/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _make_tmp_path(self):

tmp_mode = None
if self._play_context.remote_user != 'root' or self._play_context.become and self._play_context.become_user != 'root':
tmp_mode = 0755
tmp_mode = 0o755

cmd = self._connection._shell.mkdtemp(basefile, use_system_tmp, tmp_mode)
self._display.debug("executing _low_level_execute_command to create the tmp path")
Expand Down
2 changes: 0 additions & 2 deletions lib/ansible/plugins/action/include_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import os

from types import NoneType

from ansible.errors import AnsibleError
from ansible.parsing import DataLoader
from ansible.plugins.action import ActionBase
Expand Down
Loading

0 comments on commit 6907166

Please sign in to comment.