diff --git a/contrib/inventory/abiquo.py b/contrib/inventory/abiquo.py index a6030c58b8..ee8373c1cd 100755 --- a/contrib/inventory/abiquo.py +++ b/contrib/inventory/abiquo.py @@ -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 @@ -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 @@ -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']) @@ -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': {} } } @@ -214,7 +214,7 @@ if __name__ == '__main__': 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): diff --git a/contrib/inventory/cloudstack.py b/contrib/inventory/cloudstack.py index 426cf163fd..1f10b3da33 100755 --- a/contrib/inventory/cloudstack.py +++ b/contrib/inventory/cloudstack.py @@ -98,7 +98,7 @@ class CloudStackInventory(object): 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 = '' diff --git a/contrib/inventory/consul_io.py b/contrib/inventory/consul_io.py index 4e40f96873..52bc05a13c 100755 --- a/contrib/inventory/consul_io.py +++ b/contrib/inventory/consul_io.py @@ -136,7 +136,7 @@ except ImportError: 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) diff --git a/contrib/inventory/digital_ocean.py b/contrib/inventory/digital_ocean.py index 1927f09fdf..48f76cb762 100755 --- a/contrib/inventory/digital_ocean.py +++ b/contrib/inventory/digital_ocean.py @@ -145,7 +145,7 @@ except ImportError: 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) diff --git a/contrib/inventory/gce.py b/contrib/inventory/gce.py index 740e112332..e420fd951b 100755 --- a/contrib/inventory/gce.py +++ b/contrib/inventory/gce.py @@ -237,7 +237,7 @@ class GceInventory(object): '''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): diff --git a/contrib/inventory/linode.py b/contrib/inventory/linode.py index cbce5f8a69..0577ba9509 100755 --- a/contrib/inventory/linode.py +++ b/contrib/inventory/linode.py @@ -101,7 +101,7 @@ except: 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() @@ -184,7 +184,7 @@ class LinodeInventory(object): 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 @@ -194,7 +194,7 @@ class LinodeInventory(object): """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 diff --git a/contrib/inventory/rax.py b/contrib/inventory/rax.py index a42bbfcfef..5892f2cbb9 100755 --- a/contrib/inventory/rax.py +++ b/contrib/inventory/rax.py @@ -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(): @@ -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) diff --git a/contrib/inventory/spacewalk.py b/contrib/inventory/spacewalk.py index b853ca18ba..cace56401e 100755 --- a/contrib/inventory/spacewalk.py +++ b/contrib/inventory/spacewalk.py @@ -132,7 +132,7 @@ try: 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) @@ -148,7 +148,7 @@ if options.list: 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) @@ -185,7 +185,7 @@ if options.list: 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) @@ -212,7 +212,7 @@ elif options.host: 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) diff --git a/contrib/inventory/vmware.py b/contrib/inventory/vmware.py index b708d59994..44896656f0 100755 --- a/contrib/inventory/vmware.py +++ b/contrib/inventory/vmware.py @@ -164,7 +164,7 @@ class VMwareInventory(object): 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): @@ -207,7 +207,7 @@ class VMwareInventory(object): 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: diff --git a/contrib/inventory/zabbix.py b/contrib/inventory/zabbix.py index 2bc1e2e1cc..e23a7ac9f7 100755 --- a/contrib/inventory/zabbix.py +++ b/contrib/inventory/zabbix.py @@ -109,7 +109,7 @@ class ZabbixInventory(object): 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) diff --git a/lib/ansible/cli/doc.py b/lib/ansible/cli/doc.py index 4a56e68e1f..e27d85d799 100644 --- a/lib/ansible/cli/doc.py +++ b/lib/ansible/cli/doc.py @@ -120,7 +120,7 @@ class DocCLI(CLI): # 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))) diff --git a/lib/ansible/cli/pull.py b/lib/ansible/cli/pull.py index 3b61c473c3..0c33568d3d 100644 --- a/lib/ansible/cli/pull.py +++ b/lib/ansible/cli/pull.py @@ -196,7 +196,7 @@ class PullCLI(CLI): 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 diff --git a/lib/ansible/executor/process/worker.py b/lib/ansible/executor/process/worker.py index 5fb4d6250b..7968b41679 100644 --- a/lib/ansible/executor/process/worker.py +++ b/lib/ansible/executor/process/worker.py @@ -67,7 +67,7 @@ class WorkerProcess(multiprocessing.Process): 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 @@ -137,7 +137,7 @@ class WorkerProcess(multiprocessing.Process): 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: diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index 4b372cf4c7..62923d4317 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -125,14 +125,14 @@ class TaskExecutor: 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): @@ -187,7 +187,7 @@ class TaskExecutor: try: tmp_task = self._task.copy() - except AnsibleParserError, e: + except AnsibleParserError as e: results.append(dict(failed=True, msg=str(e))) continue diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index a3bb0bba47..1e6efeefdd 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -143,7 +143,7 @@ class Inventory(object): 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): @@ -153,7 +153,7 @@ class Inventory(object): 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: diff --git a/lib/ansible/inventory/script.py b/lib/ansible/inventory/script.py index 91549d78fb..6e64b0e894 100644 --- a/lib/ansible/inventory/script.py +++ b/lib/ansible/inventory/script.py @@ -46,7 +46,7 @@ class InventoryScript: 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() @@ -153,7 +153,7 @@ class InventoryScript: 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() == '': diff --git a/lib/ansible/playbook/conditional.py b/lib/ansible/playbook/conditional.py index f0acbbdb3f..4fa600734f 100644 --- a/lib/ansible/playbook/conditional.py +++ b/lib/ansible/playbook/conditional.py @@ -66,7 +66,7 @@ class Conditional: 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 diff --git a/lib/ansible/playbook/helpers.py b/lib/ansible/playbook/helpers.py index 98bef15e2a..98c9ffae30 100644 --- a/lib/ansible/playbook/helpers.py +++ b/lib/ansible/playbook/helpers.py @@ -20,8 +20,6 @@ __metaclass__ = type import os -from types import NoneType - from ansible.errors import AnsibleParserError from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject, AnsibleSequence diff --git a/lib/ansible/playbook/role/__init__.py b/lib/ansible/playbook/role/__init__.py index 1a6f99540c..c406bd9404 100644 --- a/lib/ansible/playbook/role/__init__.py +++ b/lib/ansible/playbook/role/__init__.py @@ -25,7 +25,6 @@ import inspect import os from hashlib import sha1 -from types import NoneType from ansible.errors import AnsibleError, AnsibleParserError from ansible.parsing import DataLoader @@ -184,16 +183,16 @@ class Role(Base, Become, Conditional, Taggable): # 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) @@ -370,7 +369,7 @@ class Role(Base, Become, Conditional, Taggable): 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()) diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index 6cea019ce1..6f63231b14 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -166,7 +166,7 @@ class ActionBase: 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") diff --git a/lib/ansible/plugins/action/include_vars.py b/lib/ansible/plugins/action/include_vars.py index 31d93e7acc..e0c1c088b8 100644 --- a/lib/ansible/plugins/action/include_vars.py +++ b/lib/ansible/plugins/action/include_vars.py @@ -19,8 +19,6 @@ __metaclass__ = type import os -from types import NoneType - from ansible.errors import AnsibleError from ansible.parsing import DataLoader from ansible.plugins.action import ActionBase diff --git a/lib/ansible/plugins/cache/jsonfile.py b/lib/ansible/plugins/cache/jsonfile.py index 04e05f9b0c..3fc3458fb9 100644 --- a/lib/ansible/plugins/cache/jsonfile.py +++ b/lib/ansible/plugins/cache/jsonfile.py @@ -45,7 +45,7 @@ class CacheModule(BaseCacheModule): if not os.path.exists(self._cache_dir): try: os.makedirs(self._cache_dir) - except (OSError,IOError), e: + except (OSError,IOError) as e: self._display.warning("error while trying to create cache dir %s : %s" % (self._cache_dir, str(e))) return None @@ -60,7 +60,7 @@ class CacheModule(BaseCacheModule): cachefile = "%s/%s" % (self._cache_dir, key) try: f = codecs.open(cachefile, 'r', encoding='utf-8') - except (OSError,IOError), e: + except (OSError,IOError) as e: self._display.warning("error while trying to read %s : %s" % (cachefile, str(e))) pass else: @@ -81,7 +81,7 @@ class CacheModule(BaseCacheModule): cachefile = "%s/%s" % (self._cache_dir, key) try: f = codecs.open(cachefile, 'w', encoding='utf-8') - except (OSError,IOError), e: + except (OSError,IOError) as e: self._display.warning("error while trying to write to %s : %s" % (cachefile, str(e))) pass else: @@ -94,7 +94,7 @@ class CacheModule(BaseCacheModule): cachefile = "%s/%s" % (self._cache_dir, key) try: st = os.stat(cachefile) - except (OSError,IOError), e: + except (OSError,IOError) as e: if e.errno == errno.ENOENT: return False else: @@ -126,7 +126,7 @@ class CacheModule(BaseCacheModule): try: st = os.stat(cachefile) return True - except (OSError,IOError), e: + except (OSError,IOError) as e: if e.errno == errno.ENOENT: return False else: @@ -137,7 +137,7 @@ class CacheModule(BaseCacheModule): del self._cache[key] try: os.remove("%s/%s" % (self._cache_dir, key)) - except (OSError,IOError), e: + except (OSError,IOError) as e: pass #TODO: only pass on non existing? def flush(self): diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index aa7464c323..39303af4b6 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -152,7 +152,7 @@ def version_compare(value, version, operator='eq', strict=False): try: method = getattr(py_operator, operator) return method(Version(str(value)), Version(str(version))) - except Exception, e: + except Exception as e: raise errors.AnsibleFilterError('Version comparison: %s' % e) def regex_escape(string): diff --git a/lib/ansible/plugins/filter/mathstuff.py b/lib/ansible/plugins/filter/mathstuff.py index 516ef1c677..341c2aa2d8 100644 --- a/lib/ansible/plugins/filter/mathstuff.py +++ b/lib/ansible/plugins/filter/mathstuff.py @@ -80,14 +80,14 @@ def logarithm(x, base=math.e): return math.log10(x) else: return math.log(x, base) - except TypeError, e: + except TypeError as e: raise errors.AnsibleFilterError('log() can only be used on numbers: %s' % str(e)) def power(x, y): try: return math.pow(x, y) - except TypeError, e: + except TypeError as e: raise errors.AnsibleFilterError('pow() can only be used on numbers: %s' % str(e)) @@ -97,7 +97,7 @@ def inversepower(x, base=2): return math.sqrt(x) else: return math.pow(x, 1.0/float(base)) - except TypeError, e: + except TypeError as e: raise errors.AnsibleFilterError('root() can only be used on numbers: %s' % str(e)) @@ -107,13 +107,13 @@ def human_readable(size, isbits=False, unit=None): suffix = '' ranges = ( - (1<<70L, 'Z'), - (1<<60L, 'E'), - (1<<50L, 'P'), - (1<<40L, 'T'), - (1<<30L, 'G'), - (1<<20L, 'M'), - (1<<10L, 'K'), + (1<<70, 'Z'), + (1<<60, 'E'), + (1<<50, 'P'), + (1<<40, 'T'), + (1<<30, 'G'), + (1<<20, 'M'), + (1<<10, 'K'), (1, base) ) diff --git a/lib/ansible/plugins/lookup/consul_kv.py b/lib/ansible/plugins/lookup/consul_kv.py index 5da1a5bef0..47eaa71bc8 100755 --- a/lib/ansible/plugins/lookup/consul_kv.py +++ b/lib/ansible/plugins/lookup/consul_kv.py @@ -67,7 +67,7 @@ except ImportError: try: import consul HAS_CONSUL = True -except ImportError, e: +except ImportError as e: HAS_CONSUL = False @@ -104,7 +104,7 @@ class LookupModule(LookupBase): values.append(r['Value']) else: values.append(results[1]['Value']) - except Exception, e: + except Exception as e: raise AnsibleError( "Error locating '%s' in kv store. Error was %s" % (term, e)) @@ -127,7 +127,7 @@ class LookupModule(LookupBase): name, value = param.split('=') assert name in paramvals, "% not a valid consul lookup parameter" % name paramvals[name] = value - except (ValueError, AssertionError), e: + except (ValueError, AssertionError) as e: raise AnsibleError(e) return paramvals diff --git a/lib/ansible/plugins/lookup/credstash.py b/lib/ansible/plugins/lookup/credstash.py index 9d548baea6..41cc6b894f 100644 --- a/lib/ansible/plugins/lookup/credstash.py +++ b/lib/ansible/plugins/lookup/credstash.py @@ -41,7 +41,7 @@ class LookupModule(LookupBase): val = credstash.getSecret(term, **kwargs) except credstash.ItemNotFound: raise AnsibleError('Key {0} not found'.format(term)) - except Exception, e: + except Exception as e: raise AnsibleError('Encountered exception while fetching {0}: {1}'.format(term, e.message)) ret.append(val) diff --git a/lib/ansible/plugins/lookup/dig.py b/lib/ansible/plugins/lookup/dig.py index acd73ddc19..a3ba43879b 100644 --- a/lib/ansible/plugins/lookup/dig.py +++ b/lib/ansible/plugins/lookup/dig.py @@ -141,7 +141,7 @@ class LookupModule(LookupBase): try: nsaddr = dns.resolver.query(ns)[0].address nameservers.append(nsaddr) - except Exception, e: + except Exception as e: raise AnsibleError("dns lookup NS: ", str(e)) myres.nameservers = nameservers continue @@ -176,7 +176,7 @@ class LookupModule(LookupBase): domain = n.to_text() except dns.exception.SyntaxError: pass - except Exception, e: + except Exception as e: raise AnsibleError("dns.reversename unhandled exception", str(e)) try: @@ -196,7 +196,7 @@ class LookupModule(LookupBase): rd['ttl'] = answers.rrset.ttl ret.append(rd) - except Exception, e: + except Exception as e: ret.append(str(e)) except dns.resolver.NXDOMAIN: @@ -205,7 +205,7 @@ class LookupModule(LookupBase): ret.append("") except dns.resolver.Timeout: ret.append('') - except dns.exception.DNSException, e: + except dns.exception.DNSException as e: raise AnsibleError("dns.resolver unhandled exception", e) return ret diff --git a/lib/ansible/plugins/lookup/ini.py b/lib/ansible/plugins/lookup/ini.py index 7ea8f92aaf..9c1adc90ca 100644 --- a/lib/ansible/plugins/lookup/ini.py +++ b/lib/ansible/plugins/lookup/ini.py @@ -47,7 +47,7 @@ class LookupModule(LookupBase): # Retrieve a single value try: value = self.cp.get(section, key) - except ConfigParser.NoOptionError, e: + except ConfigParser.NoOptionError as e: return dflt return value @@ -76,7 +76,7 @@ class LookupModule(LookupBase): name, value = param.split('=') assert(name in paramvals) paramvals[name] = value - except (ValueError, AssertionError), e: + except (ValueError, AssertionError) as e: raise errors.AnsibleError(e) path = self._loader.path_dwim_relative(basedir, 'files', paramvals['file']) diff --git a/lib/ansible/plugins/lookup/nested.py b/lib/ansible/plugins/lookup/nested.py index ff865c28ee..2593aa5d9f 100644 --- a/lib/ansible/plugins/lookup/nested.py +++ b/lib/ansible/plugins/lookup/nested.py @@ -32,7 +32,7 @@ class LookupModule(LookupBase): for x in terms: try: intermediate = listify_lookup_plugin_terms(x, templar=self._templar, loader=self._loader, fail_on_undefined=True) - except UndefinedError, e: + except UndefinedError as e: raise AnsibleUndefinedVariable("One of the nested variables was undefined. The error was: %s" % e) results.append(intermediate) return results diff --git a/lib/ansible/plugins/lookup/sequence.py b/lib/ansible/plugins/lookup/sequence.py index a6d133b3f9..319173d5a9 100644 --- a/lib/ansible/plugins/lookup/sequence.py +++ b/lib/ansible/plugins/lookup/sequence.py @@ -186,7 +186,7 @@ class LookupModule(LookupBase): try: if not self.parse_simple_args(term): self.parse_kv_args(parse_kv(term)) - except Exception, e: + except Exception as e: raise AnsibleError("unknown error parsing with_sequence arguments: %r. Error was: %s" % (term, e)) self.sanity_check() diff --git a/lib/ansible/plugins/lookup/shelvefile.py b/lib/ansible/plugins/lookup/shelvefile.py index 89e393694b..8883dc06b9 100644 --- a/lib/ansible/plugins/lookup/shelvefile.py +++ b/lib/ansible/plugins/lookup/shelvefile.py @@ -55,7 +55,7 @@ class LookupModule(LookupBase): assert(name in paramvals) paramvals[name] = value - except (ValueError, AssertionError), e: + except (ValueError, AssertionError) as e: # In case "file" or "key" are not present raise AnsibleError(e) diff --git a/lib/ansible/plugins/shell/sh.py b/lib/ansible/plugins/shell/sh.py index 1464fd09fa..249131a0fb 100644 --- a/lib/ansible/plugins/shell/sh.py +++ b/lib/ansible/plugins/shell/sh.py @@ -70,7 +70,7 @@ class ShellModule(object): # change the umask in a subshell to achieve the desired mode # also for directories created with `mkdir -p` if mode: - tmp_umask = 0777 & ~mode + tmp_umask = 0o777 & ~mode cmd = '(umask %o && %s)' % (tmp_umask, cmd) return cmd diff --git a/lib/ansible/plugins/strategies/__init__.py b/lib/ansible/plugins/strategies/__init__.py index f6bb3b03aa..834569dd8a 100644 --- a/lib/ansible/plugins/strategies/__init__.py +++ b/lib/ansible/plugins/strategies/__init__.py @@ -382,7 +382,7 @@ class StrategyBase: data = self._loader.load_from_file(included_file._filename) if data is None: return [] - except AnsibleError, e: + except AnsibleError as e: for host in included_file._hosts: tr = TaskResult(host=host, task=included_file._task, return_data=dict(failed=True, reason=str(e))) iterator.mark_host_failed(host) @@ -455,7 +455,7 @@ class StrategyBase: loader=self._loader, variable_manager=self._variable_manager ) - except AnsibleError, e: + except AnsibleError as e: return False if len(included_files) > 0: @@ -475,7 +475,7 @@ class StrategyBase: # and add the new blocks to the list of handler blocks handler_block.block.extend(block.block) #iterator._play.handlers.extend(new_blocks) - except AnsibleError, e: + except AnsibleError as e: for host in included_file._hosts: iterator.mark_host_failed(host) self._tqm._failed_hosts[host.name] = True diff --git a/lib/ansible/plugins/strategies/free.py b/lib/ansible/plugins/strategies/free.py index 5bc0d8db36..aed71caef6 100644 --- a/lib/ansible/plugins/strategies/free.py +++ b/lib/ansible/plugins/strategies/free.py @@ -144,7 +144,7 @@ class StrategyModule(StrategyBase): try: included_files = IncludedFile.process_include_results(host_results, self._tqm, iterator=iterator, loader=self._loader, variable_manager=self._variable_manager) - except AnsibleError, e: + except AnsibleError as e: return False if len(included_files) > 0: @@ -153,7 +153,7 @@ class StrategyModule(StrategyBase): # list of noop tasks, to make sure that they continue running in lock-step try: new_blocks = self._load_included_file(included_file, iterator=iterator) - except AnsibleError, e: + except AnsibleError as e: for host in included_file._hosts: iterator.mark_host_failed(host) self._display.warning(str(e)) diff --git a/lib/ansible/plugins/strategies/linear.py b/lib/ansible/plugins/strategies/linear.py index 2e6b3b2386..35c2cb5067 100644 --- a/lib/ansible/plugins/strategies/linear.py +++ b/lib/ansible/plugins/strategies/linear.py @@ -258,7 +258,7 @@ class StrategyModule(StrategyBase): try: included_files = IncludedFile.process_include_results(host_results, self._tqm, iterator=iterator, loader=self._loader, variable_manager=self._variable_manager) - except AnsibleError, e: + except AnsibleError as e: return False if len(included_files) > 0: @@ -273,7 +273,7 @@ class StrategyModule(StrategyBase): # list of noop tasks, to make sure that they continue running in lock-step try: new_blocks = self._load_included_file(included_file, iterator=iterator) - except AnsibleError, e: + except AnsibleError as e: for host in included_file._hosts: iterator.mark_host_failed(host) self._display.warning(str(e)) @@ -296,7 +296,7 @@ class StrategyModule(StrategyBase): iterator.add_tasks(host, all_blocks[host]) self._display.debug("results queue empty") - except (IOError, EOFError), e: + except (IOError, EOFError) as e: self._display.debug("got IOError/EOFError in task loop: %s" % e) # most likely an abort, return failed return False diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 1a1465139a..1793a2f43e 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -37,7 +37,6 @@ from ansible.template.vars import AnsibleJ2Vars from ansible.utils.debug import debug from numbers import Number -from types import NoneType __all__ = ['Templar'] @@ -188,7 +187,7 @@ class Templar: resolved_val = self._available_variables[var_name] if isinstance(resolved_val, NON_TEMPLATED_TYPES): return resolved_val - elif isinstance(resolved_val, NoneType): + elif resolved_val is None: return C.DEFAULT_NULL_REPRESENTATION result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, fail_on_undefined=fail_on_undefined, overrides=overrides) @@ -261,7 +260,7 @@ class Templar: ran = instance.run(loop_terms, variables=self._available_variables, **kwargs) except (AnsibleUndefinedVariable, UndefinedError) as e: raise AnsibleUndefinedVariable(e) - except Exception, e: + except Exception as e: if self._fail_on_lookup_errors: raise ran = None @@ -299,9 +298,9 @@ class Templar: try: t = myenv.from_string(data) - except TemplateSyntaxError, e: + except TemplateSyntaxError as e: raise AnsibleError("template error while templating string: %s" % str(e)) - except Exception, e: + except Exception as e: if 'recursion' in str(e): raise AnsibleError("recursive loop detected in template string: %s" % data) else: @@ -317,7 +316,7 @@ class Templar: try: res = j2_concat(rf) - except TypeError, te: + except TypeError as te: if 'StrictUndefined' in str(te): raise AnsibleUndefinedVariable( "Unable to look up a name or access an attribute in template string. " + \ @@ -338,7 +337,7 @@ class Templar: res += '\n' * (data_newlines - res_newlines) return res - except (UndefinedError, AnsibleUndefinedVariable), e: + except (UndefinedError, AnsibleUndefinedVariable) as e: if fail_on_undefined: raise AnsibleUndefinedVariable(e) else: diff --git a/lib/ansible/utils/module_docs.py b/lib/ansible/utils/module_docs.py old mode 100644 new mode 100755 diff --git a/lib/ansible/utils/path.py b/lib/ansible/utils/path.py index b271e7ed4b..0f9e641b74 100644 --- a/lib/ansible/utils/path.py +++ b/lib/ansible/utils/path.py @@ -45,6 +45,6 @@ def makedirs_safe(path, mode=None): os.makedirs(path, mode) else: os.makedirs(path) - except OSError, e: + except OSError as e: if e.errno != EEXIST: raise diff --git a/lib/ansible/utils/unicode.py b/lib/ansible/utils/unicode.py index 2cff2e5e45..a63c1960e1 100644 --- a/lib/ansible/utils/unicode.py +++ b/lib/ansible/utils/unicode.py @@ -215,7 +215,7 @@ def to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None): return obj elif nonstring == 'simplerepr': try: - simple = binary_type(obj) + simple = str(obj) except UnicodeError: try: simple = obj.__str__() diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index 27f7221252..4eeedb1b05 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -242,7 +242,7 @@ class VariableManager: break else: raise AnsibleError("vars file %s was not found" % vars_file_item) - except UndefinedError, e: + except UndefinedError as e: continue if not C.DEFAULT_PRIVATE_ROLE_VARS: diff --git a/test/integration/cleanup_ec2.py b/test/integration/cleanup_ec2.py index 1935f0bdc1..559a5c0ae1 100644 --- a/test/integration/cleanup_ec2.py +++ b/test/integration/cleanup_ec2.py @@ -57,7 +57,7 @@ def delete_aws_eips(get_func, attr, opts): try: eip_log = open(opts.eip_log, 'r').read().splitlines() except IOError: - print opts.eip_log, 'not found.' + print('%s not found.' % opts.eip_log) return for item in get_func(): @@ -175,5 +175,5 @@ if __name__ == '__main__': filters = {"tag:Name":opts.match_re.replace('^',''), "instance-state-name": ['running', 'pending', 'stopped' ]} delete_aws_instances(aws.get_all_instances(filters=filters), opts) - except KeyboardInterrupt, e: - print "\nExiting on user command." + except KeyboardInterrupt as e: + print("\nExiting on user command.") diff --git a/test/integration/cleanup_gce.py b/test/integration/cleanup_gce.py index e0cf0bc043..c807ebb81a 100644 --- a/test/integration/cleanup_gce.py +++ b/test/integration/cleanup_gce.py @@ -73,5 +73,5 @@ if __name__ == '__main__': delete_gce_resources(get_snapshots, 'name', opts) # Delete matching disks delete_gce_resources(gce.list_volumes, 'name', opts) - except KeyboardInterrupt, e: - print "\nExiting on user command." + except KeyboardInterrupt as e: + print("\nExiting on user command.") diff --git a/test/integration/cleanup_rax.py b/test/integration/cleanup_rax.py old mode 100644 new mode 100755 index f872e9458d..5c757f53c5 --- a/test/integration/cleanup_rax.py +++ b/test/integration/cleanup_rax.py @@ -54,8 +54,8 @@ def authenticate(): def prompt_and_delete(item, prompt, assumeyes): if not assumeyes: assumeyes = raw_input(prompt).lower() == 'y' - assert (hasattr(item, 'delete') or hasattr(item, 'terminate'), - "Class <%s> has no delete or terminate attribute" % item.__class__) + assert hasattr(item, 'delete') or hasattr(item, 'terminate'), \ + "Class <%s> has no delete or terminate attribute" % item.__class__ if assumeyes: if hasattr(item, 'delete'): item.delete() diff --git a/test/integration/consul_running.py b/test/integration/consul_running.py index 9fdff9ef59..f64aaeecc3 100644 --- a/test/integration/consul_running.py +++ b/test/integration/consul_running.py @@ -6,6 +6,6 @@ if __name__ == '__main__': import consul consul = consul.Consul(host='0.0.0.0', port=8500) consul.catalog.nodes() - print "True" + print("True") except: pass diff --git a/test/integration/roles/test_service/files/ansible_test_service b/test/integration/roles/test_service/files/ansible_test_service index 66c3a3a2d4..5e8691f2f1 100755 --- a/test/integration/roles/test_service/files/ansible_test_service +++ b/test/integration/roles/test_service/files/ansible_test_service @@ -20,7 +20,7 @@ else: def createDaemon(): try: pid = os.fork() - except OSError, e: + except OSError as e: raise Exception, "%s [%d]" % (e.strerror, e.errno) if (pid == 0): @@ -28,7 +28,7 @@ def createDaemon(): try: pid = os.fork() - except OSError, e: + except OSError as e: raise Exception, "%s [%d]" % (e.strerror, e.errno) if (pid == 0): diff --git a/test/integration/setup_gce.py b/test/integration/setup_gce.py index 0248d7684d..8aa8babb2d 100644 --- a/test/integration/setup_gce.py +++ b/test/integration/setup_gce.py @@ -38,5 +38,5 @@ if __name__ == '__main__': gce.create_volume_snapshot(base_volume, name=prefix+'-snapshot') gce.create_volume( size=10, name=prefix+'-extra', location='us-central1-a') - except KeyboardInterrupt, e: - print "\nExiting on user command." + except KeyboardInterrupt as e: + print("\nExiting on user command.") diff --git a/test/units/module_utils/test_basic.py b/test/units/module_utils/test_basic.py index e1e3399b93..1a2fbefd43 100644 --- a/test/units/module_utils/test_basic.py +++ b/test/units/module_utils/test_basic.py @@ -314,7 +314,7 @@ class TestModuleUtilsBasic(unittest.TestCase): base_params = dict( path = '/path/to/file', - mode = 0600, + mode = 0o600, owner = 'root', group = 'root', seuser = '_default', @@ -711,9 +711,9 @@ class TestModuleUtilsBasic(unittest.TestCase): ) mock_stat1 = MagicMock() - mock_stat1.st_mode = 0444 + mock_stat1.st_mode = 0o444 mock_stat2 = MagicMock() - mock_stat2.st_mode = 0660 + mock_stat2.st_mode = 0o660 with patch('os.lstat', side_effect=[mock_stat1]): self.assertEqual(am.set_mode_if_different('/path/to/file', None, True), True) @@ -723,13 +723,13 @@ class TestModuleUtilsBasic(unittest.TestCase): with patch('os.lstat') as m: with patch('os.lchmod', return_value=None, create=True) as m_os: m.side_effect = [mock_stat1, mock_stat2, mock_stat2] - self.assertEqual(am.set_mode_if_different('/path/to/file', 0660, False), True) - m_os.assert_called_with('/path/to/file', 0660) + self.assertEqual(am.set_mode_if_different('/path/to/file', 0o660, False), True) + m_os.assert_called_with('/path/to/file', 0o660) m.side_effect = [mock_stat1, mock_stat2, mock_stat2] - am._symbolic_mode_to_octal = MagicMock(return_value=0660) + am._symbolic_mode_to_octal = MagicMock(return_value=0o660) self.assertEqual(am.set_mode_if_different('/path/to/file', 'o+w,g+w,a-r', False), True) - m_os.assert_called_with('/path/to/file', 0660) + m_os.assert_called_with('/path/to/file', 0o660) m.side_effect = [mock_stat1, mock_stat2, mock_stat2] am._symbolic_mode_to_octal = MagicMock(side_effect=Exception) @@ -737,7 +737,7 @@ class TestModuleUtilsBasic(unittest.TestCase): m.side_effect = [mock_stat1, mock_stat2, mock_stat2] am.check_mode = True - self.assertEqual(am.set_mode_if_different('/path/to/file', 0660, False), True) + self.assertEqual(am.set_mode_if_different('/path/to/file', 0o660, False), True) am.check_mode = False # FIXME: this isn't working yet @@ -746,11 +746,11 @@ class TestModuleUtilsBasic(unittest.TestCase): # del m_os.lchmod # with patch('os.path.islink', return_value=False): # with patch('os.chmod', return_value=None) as m_chmod: - # self.assertEqual(am.set_mode_if_different('/path/to/file/no_lchmod', 0660, False), True) - # m_chmod.assert_called_with('/path/to/file', 0660) + # self.assertEqual(am.set_mode_if_different('/path/to/file/no_lchmod', 0o660, False), True) + # m_chmod.assert_called_with('/path/to/file', 0o660) # with patch('os.path.islink', return_value=True): # with patch('os.chmod', return_value=None) as m_chmod: # with patch('os.stat', return_value=mock_stat2): - # self.assertEqual(am.set_mode_if_different('/path/to/file', 0660, False), True) - # m_chmod.assert_called_with('/path/to/file', 0660) + # self.assertEqual(am.set_mode_if_different('/path/to/file', 0o660, False), True) + # m_chmod.assert_called_with('/path/to/file', 0o660) diff --git a/tox.ini b/tox.ini index 1031d051e9..643ab11dac 100644 --- a/tox.ini +++ b/tox.ini @@ -26,7 +26,7 @@ whitelist_externals = make [testenv:py34] commands = python --version - python -m compileall -fq -x 'lib/ansible/module_utils' lib test contrib + python -m compileall -fq -x 'lib/ansible/module_utils|lib/ansible/modules' lib test make tests deps = -r{toxinidir}/test-requirements.txt whitelist_externals = make