Skip to content

Commit

Permalink
Remove usage of locals() for formatting from nova.scheduler.*
Browse files Browse the repository at this point in the history
Using of locals() for formatting string is a nasty thing because:
1) It is not so clear as using explicit dicts
2) It could produce hidden errors during refactoring
3) Changing name of variable causes change in message
4) Creating a lot of unused variables

fixes bug #1171936

Change-Id: I7639631846a9145c3a18f2a704b71184ec781f45
  • Loading branch information
changbl committed Jun 19, 2013
1 parent 6808e05 commit 43cf986
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
8 changes: 5 additions & 3 deletions nova/scheduler/driver.py
Expand Up @@ -60,8 +60,8 @@ def handle_schedule_error(context, ex, instance_uuid, request_spec):
if not isinstance(ex, exception.NoValidHost):
LOG.exception(_("Exception during scheduler.run_instance"))
state = vm_states.ERROR.upper()
LOG.warning(_('Setting instance to %(state)s state.'),
locals(), instance_uuid=instance_uuid)
LOG.warning(_('Setting instance to %s state.'), state,
instance_uuid=instance_uuid)

# update instance state and notify on the transition
(old_ref, new_ref) = db.instance_update_and_get_original(context,
Expand Down Expand Up @@ -339,7 +339,9 @@ def _assert_compute_node_has_enough_memory(self, context,
reason = _("Unable to migrate %(instance_uuid)s to %(dest)s: "
"Lack of memory(host:%(avail)s <= "
"instance:%(mem_inst)s)")
raise exception.MigrationPreCheckError(reason=reason % locals())
raise exception.MigrationPreCheckError(reason=reason %
{'instance_uuid': instance_uuid, 'dest': dest, 'avail': avail,
'mem_inst': mem_inst})

def _get_compute_info(self, context, host):
"""get compute node's information specified by key
Expand Down
3 changes: 1 addition & 2 deletions nova/scheduler/filters/aggregate_multitenancy_isolation.py
Expand Up @@ -41,7 +41,6 @@ def host_passes(self, host_state, filter_properties):

if metadata != {}:
if tenant_id not in metadata["filter_tenant_id"]:
LOG.debug(_("%(host_state)s fails tenant id on "
"aggregate"), locals())
LOG.debug(_("%s fails tenant id on aggregate"), host_state)
return False
return True
8 changes: 5 additions & 3 deletions nova/scheduler/host_manager.py
Expand Up @@ -377,12 +377,14 @@ def update_service_capabilities(self, service_name, host, capabilities):

if service_name != 'compute':
LOG.debug(_('Ignoring %(service_name)s service update '
'from %(host)s'), locals())
'from %(host)s'), {'service_name': service_name,
'host': host})
return

state_key = (host, capabilities.get('hypervisor_hostname'))
LOG.debug(_("Received %(service_name)s service update from "
"%(state_key)s.") % locals())
"%(state_key)s."), {'service_name': service_name,
'state_key': state_key})
# Copy the capabilities, so we don't modify the original dict
capab_copy = dict(capabilities)
capab_copy["timestamp"] = timeutils.utcnow() # Reported time
Expand Down Expand Up @@ -423,7 +425,7 @@ def get_all_host_states(self, context):
for state_key in dead_nodes:
host, node = state_key
LOG.info(_("Removing dead compute node %(host)s:%(node)s "
"from scheduler") % locals())
"from scheduler") % {'host': host, 'node': node})
del self.host_state_map[state_key]

return self.host_state_map.itervalues()
7 changes: 4 additions & 3 deletions nova/scheduler/manager.py
Expand Up @@ -208,7 +208,8 @@ def _set_vm_state_and_notify(self, method, updates, context, ex,
# The refactoring could go further but trying to minimize changes
# for essex timeframe

LOG.warning(_("Failed to schedule_%(method)s: %(ex)s") % locals())
LOG.warning(_("Failed to schedule_%(method)s: %(ex)s"),
{'method': method, 'ex': ex})

vm_state = updates['vm_state']
properties = request_spec.get('instance_properties', {})
Expand All @@ -222,8 +223,8 @@ def _set_vm_state_and_notify(self, method, updates, context, ex,
for instance_uuid in request_spec.get('instance_uuids') or uuids:
if instance_uuid:
state = vm_state.upper()
LOG.warning(_('Setting instance to %(state)s state.'),
locals(), instance_uuid=instance_uuid)
LOG.warning(_('Setting instance to %s state.'), state,
instance_uuid=instance_uuid)

# update instance state and notify on the transition
(old_ref, new_ref) = self.db.instance_update_and_get_original(
Expand Down
6 changes: 3 additions & 3 deletions nova/scheduler/scheduler_options.py
Expand Up @@ -69,15 +69,15 @@ def _get_file_timestamp(self, filename):
except os.error as e:
with excutils.save_and_reraise_exception():
LOG.exception(_("Could not stat scheduler options file "
"%(filename)s: '%(e)s'"), locals())
"%(filename)s: '%(e)s'"),
{'filename': filename, 'e': e})

def _load_file(self, handle):
"""Decode the JSON file. Broken out for testing."""
try:
return json.load(handle)
except ValueError as e:
LOG.exception(_("Could not decode scheduler options: "
"'%(e)s'") % locals())
LOG.exception(_("Could not decode scheduler options: '%s'"), e)
return {}

def _get_time_now(self):
Expand Down

0 comments on commit 43cf986

Please sign in to comment.