From 43cf9864b9091319612926a01b07045806cfce49 Mon Sep 17 00:00:00 2001 From: Changbin Liu Date: Wed, 19 Jun 2013 12:09:28 -0400 Subject: [PATCH] Remove usage of locals() for formatting from nova.scheduler.* 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 --- nova/scheduler/driver.py | 8 +++++--- .../scheduler/filters/aggregate_multitenancy_isolation.py | 3 +-- nova/scheduler/host_manager.py | 8 +++++--- nova/scheduler/manager.py | 7 ++++--- nova/scheduler/scheduler_options.py | 6 +++--- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/nova/scheduler/driver.py b/nova/scheduler/driver.py index 5fed1e3979f..d5a1eedeac5 100644 --- a/nova/scheduler/driver.py +++ b/nova/scheduler/driver.py @@ -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, @@ -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 diff --git a/nova/scheduler/filters/aggregate_multitenancy_isolation.py b/nova/scheduler/filters/aggregate_multitenancy_isolation.py index d9192af52bb..1a9f6bf58d1 100644 --- a/nova/scheduler/filters/aggregate_multitenancy_isolation.py +++ b/nova/scheduler/filters/aggregate_multitenancy_isolation.py @@ -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 diff --git a/nova/scheduler/host_manager.py b/nova/scheduler/host_manager.py index b5fbb85947e..d5081ace50d 100644 --- a/nova/scheduler/host_manager.py +++ b/nova/scheduler/host_manager.py @@ -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 @@ -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() diff --git a/nova/scheduler/manager.py b/nova/scheduler/manager.py index 9429a0662ca..5c99feb0314 100644 --- a/nova/scheduler/manager.py +++ b/nova/scheduler/manager.py @@ -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', {}) @@ -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( diff --git a/nova/scheduler/scheduler_options.py b/nova/scheduler/scheduler_options.py index bae470d1b18..5ba2117cc6e 100644 --- a/nova/scheduler/scheduler_options.py +++ b/nova/scheduler/scheduler_options.py @@ -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):