From 9ae66a7f5cf18498d7cd1860dba17e5c24dc35df Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Thu, 27 Aug 2015 21:58:51 +0300 Subject: [PATCH] Use 'except ... as' syntax in contrib/ and test/ too --- contrib/inventory/abiquo.py | 10 +++++----- contrib/inventory/cloudstack.py | 2 +- contrib/inventory/consul_io.py | 2 +- contrib/inventory/digital_ocean.py | 2 +- contrib/inventory/gce.py | 2 +- contrib/inventory/linode.py | 6 +++--- contrib/inventory/rax.py | 2 +- contrib/inventory/spacewalk.py | 8 ++++---- contrib/inventory/vmware.py | 4 ++-- contrib/inventory/zabbix.py | 2 +- test/integration/cleanup_ec2.py | 2 +- test/integration/cleanup_gce.py | 2 +- .../roles/test_service/files/ansible_test_service | 4 ++-- test/integration/setup_gce.py | 2 +- 14 files changed, 25 insertions(+), 25 deletions(-) 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 c6b25f596e..5892f2cbb9 100755 --- a/contrib/inventory/rax.py +++ b/contrib/inventory/rax.py @@ -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/test/integration/cleanup_ec2.py b/test/integration/cleanup_ec2.py index 1935f0bdc1..48db7572ce 100644 --- a/test/integration/cleanup_ec2.py +++ b/test/integration/cleanup_ec2.py @@ -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: + 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..0dc0555c45 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: + except KeyboardInterrupt as e: print "\nExiting on user command." 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..6ef4f0f001 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: + except KeyboardInterrupt as e: print "\nExiting on user command."