Adds Python 3.x compatible exception handling

Fixes exception handling syntax for Python 3.x compatibility

Changes generated with:

    2to3 -f except -w -n cloudbaseinit
This commit is contained in:
Alessandro Pilotti 2014-09-07 21:03:01 +03:00
parent 2a9be0e16d
commit b59a89e5e5
13 changed files with 14 additions and 14 deletions

View File

@ -70,7 +70,7 @@ class InitManager(object):
self._set_plugin_status(osutils, instance_id, plugin_name, self._set_plugin_status(osutils, instance_id, plugin_name,
status) status)
return reboot_required return reboot_required
except Exception, ex: except Exception as ex:
LOG.error('plugin \'%(plugin_name)s\' failed with error ' LOG.error('plugin \'%(plugin_name)s\' failed with error '
'\'%(ex)s\'', {'plugin_name': plugin_name, 'ex': ex}) '\'%(ex)s\'', {'plugin_name': plugin_name, 'ex': ex})
LOG.exception(ex) LOG.exception(ex)
@ -124,7 +124,7 @@ class InitManager(object):
if reboot_required and CONF.allow_reboot: if reboot_required and CONF.allow_reboot:
try: try:
osutils.reboot() osutils.reboot()
except Exception, ex: except Exception as ex:
LOG.error('reboot failed with error \'%s\'' % ex) LOG.error('reboot failed with error \'%s\'' % ex)
elif CONF.stop_service_on_exit: elif CONF.stop_service_on_exit:
osutils.terminate() osutils.terminate()

View File

@ -47,7 +47,7 @@ def get_metadata_service():
try: try:
if service.load(): if service.load():
return service return service
except Exception, ex: except Exception as ex:
LOG.error("Failed to load metadata service '%s'" % class_path) LOG.error("Failed to load metadata service '%s'" % class_path)
LOG.exception(ex) LOG.exception(ex)
raise Exception("No available service found") raise Exception("No available service found")

View File

@ -51,7 +51,7 @@ class EC2Service(base.BaseMetadataService):
try: try:
self.get_host_name() self.get_host_name()
return True return True
except Exception, ex: except Exception as ex:
LOG.exception(ex) LOG.exception(ex)
LOG.debug('Metadata not found at URL \'%s\'' % LOG.debug('Metadata not found at URL \'%s\'' %
CONF.ec2_metadata_base_url) CONF.ec2_metadata_base_url)

View File

@ -59,7 +59,7 @@ class MaaSHttpService(base.BaseMetadataService):
try: try:
self._get_data('%s/meta-data/' % self._metadata_version) self._get_data('%s/meta-data/' % self._metadata_version)
return True return True
except Exception, ex: except Exception as ex:
LOG.exception(ex) LOG.exception(ex)
LOG.debug('Metadata not found at URL \'%s\'' % LOG.debug('Metadata not found at URL \'%s\'' %
CONF.maas_metadata_url) CONF.maas_metadata_url)

View File

@ -593,7 +593,7 @@ class WindowsUtils(base.BaseOSUtils):
time.sleep(1) time.sleep(1)
LOG.info('Waiting for sysprep completion. ' LOG.info('Waiting for sysprep completion. '
'GeneralizationState: %d' % gen_state) 'GeneralizationState: %d' % gen_state)
except WindowsError, ex: except WindowsError as ex:
if ex.winerror == 2: if ex.winerror == 2:
LOG.debug('Sysprep data not found in the registry, ' LOG.debug('Sysprep data not found in the registry, '
'skipping sysprep completion check.') 'skipping sysprep completion check.')

View File

@ -57,5 +57,5 @@ def exec_file(file_path):
LOG.debug('User_data stderr:\n%s' % err) LOG.debug('User_data stderr:\n%s' % err)
return ret_val return ret_val
except Exception, ex: except Exception as ex:
LOG.warning('An error occurred during file execution: \'%s\'' % ex) LOG.warning('An error occurred during file execution: \'%s\'' % ex)

View File

@ -106,7 +106,7 @@ class UserDataPlugin(base.BasePlugin):
new_user_handlers) new_user_handlers)
else: else:
ret_val = user_data_plugin.process(part) ret_val = user_data_plugin.process(part)
except Exception, ex: except Exception as ex:
LOG.error('Exception during multipart part handling: ' LOG.error('Exception during multipart part handling: '
'%(content_type)s, %(filename)s' % '%(content_type)s, %(filename)s' %
{'content_type': part.get_content_type(), {'content_type': part.get_content_type(),

View File

@ -36,7 +36,7 @@ class ShellScriptPlugin(base.BaseUserDataPlugin):
f.write(part.get_payload()) f.write(part.get_payload())
return fileexecutils.exec_file(target_path) return fileexecutils.exec_file(target_path)
except Exception, ex: except Exception as ex:
LOG.warning('An error occurred during user_data execution: \'%s\'' LOG.warning('An error occurred during user_data execution: \'%s\''
% ex) % ex)
finally: finally:

View File

@ -69,7 +69,7 @@ def execute_user_data_script(user_data):
LOG.debug('User_data stderr:\n%s' % err) LOG.debug('User_data stderr:\n%s' % err)
return ret_val return ret_val
except Exception, ex: except Exception as ex:
LOG.warning('An error occurred during user_data execution: \'%s\'' LOG.warning('An error occurred during user_data execution: \'%s\''
% ex) % ex)
finally: finally:

View File

@ -108,7 +108,7 @@ def _bind_dhcp_client_socket(s, max_bind_attempts, bind_retry_interval):
try: try:
s.bind(('', 68)) s.bind(('', 68))
break break
except socket.error, ex: except socket.error as ex:
if (bind_attempts >= max_bind_attempts or if (bind_attempts >= max_bind_attempts or
ex.errno not in [48, 10048]): ex.errno not in [48, 10048]):
raise raise

View File

@ -60,6 +60,6 @@ def check_metadata_ip_route(metadata_url):
gateway, gateway,
interface_index, interface_index,
10) 10)
except Exception, ex: except Exception as ex:
# Ignore it # Ignore it
LOG.exception(ex) LOG.exception(ex)

View File

@ -58,7 +58,7 @@ def _get_registry_dhcp_server(adapter_name):
if dhcp_server == "255.255.255.255": if dhcp_server == "255.255.255.255":
dhcp_server = None dhcp_server = None
return dhcp_server return dhcp_server
except Exception, ex: except Exception as ex:
# Not found # Not found
if ex.errno != 2: if ex.errno != 2:
raise raise

View File

@ -103,7 +103,7 @@ class WinRMConfig(object):
session = self._get_wsman_session() session = self._get_wsman_session()
try: try:
return session.Get(resource_uri) return session.Get(resource_uri)
except pywintypes.com_error, ex: except pywintypes.com_error as ex:
if len(ex.excepinfo) > 5 and ex.excepinfo[5] == -2144108544: if len(ex.excepinfo) > 5 and ex.excepinfo[5] == -2144108544:
return None return None
else: else: