Skip to content

Commit

Permalink
Remove locals() from various places.
Browse files Browse the repository at this point in the history
fixes bug 1171936

Remove usage of locals() for string formatting

Change-Id: Ib05538095086ddefdb486c84da506af662ec5c9b
  • Loading branch information
eugene64 committed Jul 17, 2013
1 parent 536f379 commit 20eac6c
Show file tree
Hide file tree
Showing 24 changed files with 82 additions and 67 deletions.
2 changes: 1 addition & 1 deletion nova/api/openstack/compute/contrib/quotas.py
Expand Up @@ -121,7 +121,7 @@ def update(self, req, id, body):
value = int(value)
except (ValueError, TypeError):
msg = _("Quota '%(value)s' for %(key)s should be "
"integer.") % locals()
"integer.") % {'value': value, 'key': key}
LOG.warn(msg)
raise webob.exc.HTTPBadRequest(explanation=msg)
self._validate_quota_limit(value)
Expand Down
2 changes: 1 addition & 1 deletion nova/api/openstack/compute/plugins/v3/quota_sets.py
Expand Up @@ -111,7 +111,7 @@ def update(self, req, id, body):
value = int(value)
except (ValueError, TypeError):
msg = _("Quota '%(value)s' for %(key)s should be "
"integer.") % locals()
"integer.") % {'value': value, 'key': key}
LOG.warn(msg)
raise webob.exc.HTTPBadRequest(explanation=msg)
self._validate_quota_limit(value)
Expand Down
3 changes: 2 additions & 1 deletion nova/cells/weights/mute_child.py
Expand Up @@ -63,7 +63,8 @@ def _weigh_object(self, cell, weight_properties):
if timeutils.is_older_than(last_seen, secs):
# yep, that's a mute child; recommend highly that it be skipped!
LOG.warn(_("%(cell)s has not been seen since %(last_seen)s and is "
"being treated as mute.") % locals())
"being treated as mute."),
{'cell': cell, 'last_seen': last_seen})
return CONF.cells.mute_weight_value
else:
return 0
3 changes: 2 additions & 1 deletion nova/cmd/baremetal_deploy_helper.py
Expand Up @@ -242,7 +242,8 @@ def run(self):
else:
# Requests comes here from BareMetalDeploy.post()
LOG.info(_('start deployment for node %(node_id)s, '
'params %(params)s') % locals())
'params %(params)s'),
{'node_id': node_id, 'params': params})
context = nova_context.get_admin_context()
try:
db.bm_node_update(context, node_id,
Expand Down
17 changes: 11 additions & 6 deletions nova/cmd/manage.py
Expand Up @@ -690,7 +690,8 @@ def enable(self, host, service):
except exception.NotFound as ex:
print _("error: %s") % ex
return(2)
print _("Service %(service)s on host %(host)s enabled.") % locals()
print (_("Service %(service)s on host %(host)s enabled.") %
{'service': service, 'host': host})

@args('--host', metavar='<host>', help='Host')
@args('--service', metavar='<service>', help='Nova service')
Expand All @@ -703,7 +704,8 @@ def disable(self, host, service):
except exception.NotFound as ex:
print _("error: %s") % ex
return(2)
print _("Service %(service)s on host %(host)s disabled.") % locals()
print (_("Service %(service)s on host %(host)s disabled.") %
{'service': service, 'host': host})

def _show_host_resources(self, context, host):
"""Shows the physical/usage resource given by hosts.
Expand Down Expand Up @@ -968,8 +970,9 @@ def set_key(self, name, key, value=None):
ctxt,
inst_type["flavorid"],
ext_spec)
print _("Key %(key)s set to %(value)s on instance"
" type %(name)s") % locals()
print (_("Key %(key)s set to %(value)s on instance "
"type %(name)s") %
{'key': key, 'value': value, 'name': name})
except db_exc.DBError as e:
_db_error(e)

Expand All @@ -990,7 +993,8 @@ def unset_key(self, name, key):
inst_type["flavorid"],
key)

print _("Key %(key)s on instance type %(name)s unset") % locals()
print (_("Key %(key)s on instance type %(name)s unset") %
{'key': key, 'name': name})
except db_exc.DBError as e:
_db_error(e)

Expand Down Expand Up @@ -1099,7 +1103,8 @@ def errors(self):
print log_file + ":-"
print_name = 1
linenum = len(lines) - index
print _('Line %(linenum)d : %(line)s') % locals()
print (_('Line %(linenum)d : %(line)s') %
{'linenum': linenum, 'line': line})
if error_found == 0:
print _('No errors in logfiles!')

Expand Down
3 changes: 2 additions & 1 deletion nova/conductor/manager.py
Expand Up @@ -120,7 +120,8 @@ def instance_update(self, context, instance_uuid,
for key, value in updates.iteritems():
if key not in allowed_updates:
LOG.error(_("Instance update attempted for "
"'%(key)s' on %(instance_uuid)s") % locals())
"'%(key)s' on %(instance_uuid)s"),
{'key': key, 'instance_uuid': instance_uuid})
raise KeyError("unexpected update keyword '%s'" % key)
if key in datetime_fields and isinstance(value, basestring):
updates[key] = timeutils.parse_strtime(value)
Expand Down
6 changes: 4 additions & 2 deletions nova/consoleauth/manager.py
Expand Up @@ -84,7 +84,8 @@ def authorize_console(self, context, token, console_type, host, port,
self.mc.set(instance_uuid.encode('UTF-8'),
jsonutils.dumps(tokens))

LOG.audit(_("Received Token: %(token)s, %(token_dict)s)"), locals())
LOG.audit(_("Received Token: %(token)s, %(token_dict)s"),
{'token': token, 'token_dict': token_dict})

def _validate_token(self, context, token):
instance_uuid = token['instance_uuid']
Expand All @@ -108,7 +109,8 @@ def _validate_token(self, context, token):
def check_token(self, context, token):
token_str = self.mc.get(token.encode('UTF-8'))
token_valid = (token_str is not None)
LOG.audit(_("Checking Token: %(token)s, %(token_valid)s)"), locals())
LOG.audit(_("Checking Token: %(token)s, %(token_valid)s"),
{'token': token, 'token_valid': token_valid})
if token_valid:
token = jsonutils.loads(token_str)
if self._validate_token(context, token):
Expand Down
2 changes: 1 addition & 1 deletion nova/db/sqlalchemy/api.py
Expand Up @@ -2953,7 +2953,7 @@ def quota_reserve(context, resources, quotas, deltas, expire,

if unders:
LOG.warning(_("Change will make usage less than 0 for the following "
"resources: %(unders)s") % locals())
"resources: %s"), unders)
if overs:
usages = dict((k, dict(in_use=v['in_use'], reserved=v['reserved']))
for k, v in usages.items())
Expand Down
6 changes: 4 additions & 2 deletions nova/hooks.py
Expand Up @@ -66,7 +66,8 @@ def run_pre(self, name, args, kwargs, f=None):
obj = e.obj
pre = getattr(obj, 'pre', None)
if pre:
LOG.debug(_("Running %(name)s pre-hook: %(obj)s") % locals())
LOG.debug(_("Running %(name)s pre-hook: %(obj)s"),
{'name': name, 'obj': obj})
if f:
pre(f, *args, **kwargs)
else:
Expand All @@ -77,7 +78,8 @@ def run_post(self, name, rv, args, kwargs, f=None):
obj = e.obj
post = getattr(obj, 'post', None)
if post:
LOG.debug(_("Running %(name)s post-hook: %(obj)s") % locals())
LOG.debug(_("Running %(name)s post-hook: %(obj)s"),
{'name': name, 'obj': obj})
if f:
post(f, rv, *args, **kwargs)
else:
Expand Down
11 changes: 7 additions & 4 deletions nova/image/glance.py
Expand Up @@ -184,14 +184,17 @@ def call(self, context, version, method, *args, **kwargs):
host = self.host
port = self.port
extra = "retrying"
error_msg = _("Error contacting glance server "
"'%(host)s:%(port)s' for '%(method)s', %(extra)s.")
error_msg = (_("Error contacting glance server "
"'%(host)s:%(port)s' for '%(method)s', "
"%(extra)s.") %
{'host': host, 'port': port,
'method': method, 'extra': extra})
if attempt == num_attempts:
extra = 'done trying'
LOG.exception(error_msg, locals())
LOG.exception(error_msg)
raise exception.GlanceConnectionFailed(
host=host, port=port, reason=str(e))
LOG.exception(error_msg, locals())
LOG.exception(error_msg)
time.sleep(1)


Expand Down
29 changes: 15 additions & 14 deletions nova/network/floating_ips.py
Expand Up @@ -85,7 +85,7 @@ def init_host_floating_ips(self):
fixed_ip_id,
get_network=True)
except exception.FixedIpNotFound:
msg = _('Fixed ip %(fixed_ip_id)s not found') % locals()
msg = _('Fixed ip %s not found') % fixed_ip_id
LOG.debug(msg)
continue
interface = CONF.public_interface or floating_ip['interface']
Expand All @@ -95,7 +95,7 @@ def init_host_floating_ips(self):
interface,
fixed_ip['network'])
except processutils.ProcessExecutionError:
LOG.debug(_('Interface %(interface)s not found'), locals())
LOG.debug(_('Interface %s not found'), interface)
raise exception.NoFloatingIpInterface(interface=interface)

def allocate_for_instance(self, context, **kwargs):
Expand All @@ -120,7 +120,7 @@ def allocate_for_instance(self, context, **kwargs):
floating_address = self.allocate_floating_ip(context, project_id,
True)
LOG.debug(_("floating IP allocation for instance "
"|%(floating_address)s|") % locals(),
"|%s|"), floating_address,
instance_uuid=instance_uuid, context=context)
# set auto_assigned column to true for the floating ip
self.db.floating_ip_set_auto_assigned(context, floating_address)
Expand Down Expand Up @@ -217,9 +217,8 @@ def allocate_floating_ip(self, context, project_id, auto_assigned=False,
if use_quota:
reservations = QUOTAS.reserve(context, floating_ips=1)
except exception.OverQuota:
pid = context.project_id
LOG.warn(_("Quota exceeded for %(pid)s, tried to allocate "
"floating IP") % locals())
LOG.warn(_("Quota exceeded for %s, tried to allocate "
"floating IP"), context.project_id)
raise exception.FloatingIpLimitExceeded()

try:
Expand Down Expand Up @@ -371,7 +370,7 @@ def do_associate():
except processutils.ProcessExecutionError as e:
self.db.floating_ip_disassociate(context, floating_address)
if "Cannot find device" in str(e):
LOG.error(_('Interface %(interface)s not found'), locals())
LOG.error(_('Interface %s not found'), interface)
raise exception.NoFloatingIpInterface(interface=interface)
raise

Expand Down Expand Up @@ -529,16 +528,17 @@ def migrate_instance_start(self, context, instance_uuid,
if not floating_addresses or (source and source == dest):
return

LOG.info(_("Starting migration network for instance"
" %(instance_uuid)s"), locals())
LOG.info(_("Starting migration network for instance %s"),
instance_uuid)
for address in floating_addresses:
floating_ip = self.db.floating_ip_get_by_address(context,
address)

if self._is_stale_floating_ip_address(context, floating_ip):
LOG.warn(_("Floating ip address |%(address)s| no longer "
"belongs to instance %(instance_uuid)s. Will not"
"migrate it "), locals())
"belongs to instance %(instance_uuid)s. Will not "
"migrate it "),
{'address': address, 'instance_uuid': instance_uuid})
continue

interface = CONF.public_interface or floating_ip['interface']
Expand Down Expand Up @@ -571,8 +571,8 @@ def migrate_instance_finish(self, context, instance_uuid,
if not floating_addresses or (source and source == dest):
return

LOG.info(_("Finishing migration network for instance"
" %(instance_uuid)s"), locals())
LOG.info(_("Finishing migration network for instance %s"),
instance_uuid)

for address in floating_addresses:
floating_ip = self.db.floating_ip_get_by_address(context,
Expand All @@ -581,7 +581,8 @@ def migrate_instance_finish(self, context, instance_uuid,
if self._is_stale_floating_ip_address(context, floating_ip):
LOG.warn(_("Floating ip address |%(address)s| no longer "
"belongs to instance %(instance_uuid)s. Will not"
"setup it."), locals())
"setup it."),
{'address': address, 'instance_uuid': instance_uuid})
continue

self.db.floating_ip_update(context,
Expand Down
2 changes: 1 addition & 1 deletion nova/network/linux_net.py
Expand Up @@ -1474,7 +1474,7 @@ def ensure_bridge(_self, bridge, interface, net_attrs=None, gateway=True,

if interface:
msg = _('Adding interface %(interface)s to bridge %(bridge)s')
LOG.debug(msg % locals())
LOG.debug(msg, {'interface': interface, 'bridge': bridge})
out, err = _execute('brctl', 'addif', bridge, interface,
check_exit_code=False, run_as_root=True)

Expand Down
13 changes: 6 additions & 7 deletions nova/network/manager.py
Expand Up @@ -485,8 +485,8 @@ def allocate_for_instance(self, context, **kwargs):
requested_networks=requested_networks)
networks_list = [self._get_network_dict(network)
for network in networks]
LOG.debug(_('networks retrieved for instance: |%(networks_list)s|'),
locals(), context=context, instance_uuid=instance_uuid)
LOG.debug(_('networks retrieved for instance: |%s|'),
networks_list, context=context, instance_uuid=instance_uuid)

try:
self._allocate_mac_addresses(context, instance_uuid, networks,
Expand Down Expand Up @@ -824,9 +824,8 @@ def allocate_fixed_ip(self, context, instance_id, network, **kwargs):
try:
reservations = QUOTAS.reserve(context, fixed_ips=1)
except exception.OverQuota:
pid = context.project_id
LOG.warn(_("Quota exceeded for %(pid)s, tried to allocate "
"fixed IP") % locals())
LOG.warn(_("Quota exceeded for %s, tried to allocate "
"fixed IP"), context.project_id)
raise exception.FixedIpLimitExceeded()

try:
Expand Down Expand Up @@ -948,7 +947,7 @@ def deallocate_fixed_ip(self, context, address, host=None, teardown=True):

def lease_fixed_ip(self, context, address):
"""Called by dhcp-bridge when ip is leased."""
LOG.debug(_('Leased IP |%(address)s|'), locals(), context=context)
LOG.debug(_('Leased IP |%s|'), address, context=context)
fixed_ip = self.db.fixed_ip_get_by_address(context, address)

if fixed_ip['instance_uuid'] is None:
Expand All @@ -966,7 +965,7 @@ def lease_fixed_ip(self, context, address):

def release_fixed_ip(self, context, address):
"""Called by dhcp-bridge when ip is released."""
LOG.debug(_('Released IP |%(address)s|'), locals(), context=context)
LOG.debug(_('Released IP |%s|'), address, context=context)
fixed_ip = self.db.fixed_ip_get_by_address(context, address)

if fixed_ip['instance_uuid'] is None:
Expand Down
4 changes: 2 additions & 2 deletions nova/network/neutronv2/api.py
Expand Up @@ -383,8 +383,8 @@ def deallocate_port_for_instance(self, context, instance, port_id,
try:
neutronv2.get_client(context).delete_port(port_id)
except Exception as ex:
LOG.exception(_("Failed to delete neutron port %(port_id)s ") %
locals())
LOG.exception(_("Failed to delete neutron port %s") %
port_id)

return self._get_instance_nw_info(context, instance)

Expand Down
2 changes: 1 addition & 1 deletion nova/objects/base.py
Expand Up @@ -302,7 +302,7 @@ def obj_load_attr(self, attrname):
be useful for future load operations.
"""
raise NotImplementedError(
_("Cannot load '%(attrname)s' in the base class") % locals())
_("Cannot load '%s' in the base class") % attrname)

def save(self, context):
"""Save the changed fields back to the store.
Expand Down
13 changes: 6 additions & 7 deletions nova/quota.py
Expand Up @@ -969,7 +969,7 @@ def reserve(self, context, expire=None, project_id=None, **deltas):
expire=expire,
project_id=project_id)

LOG.debug(_("Created reservations %(reservations)s") % locals())
LOG.debug(_("Created reservations %s"), reservations)

return reservations

Expand All @@ -991,10 +991,9 @@ def commit(self, context, reservations, project_id=None):
# usage resynchronization and the reservation expiration
# mechanisms will resolve the issue. The exception is
# logged, however, because this is less than optimal.
LOG.exception(_("Failed to commit reservations "
"%(reservations)s") % locals())
LOG.exception(_("Failed to commit reservations %s"), reservations)
return
LOG.debug(_("Committed reservations %(reservations)s") % locals())
LOG.debug(_("Committed reservations %s"), reservations)

def rollback(self, context, reservations, project_id=None):
"""Roll back reservations.
Expand All @@ -1014,10 +1013,10 @@ def rollback(self, context, reservations, project_id=None):
# usage resynchronization and the reservation expiration
# mechanisms will resolve the issue. The exception is
# logged, however, because this is less than optimal.
LOG.exception(_("Failed to roll back reservations "
"%(reservations)s") % locals())
LOG.exception(_("Failed to roll back reservations %s"),
reservations)
return
LOG.debug(_("Rolled back reservations %(reservations)s") % locals())
LOG.debug(_("Rolled back reservations %s"), reservations)

def usage_reset(self, context, resources):
"""
Expand Down
3 changes: 1 addition & 2 deletions nova/scheduler/filters/core_filter.py
Expand Up @@ -97,8 +97,7 @@ def _get_cpu_allocation_ratio(self, host_state, filter_properties):
try:
ratio = float(min(aggregate_vals))
except ValueError as e:
LOG.warning(_("Could not decode cpu_allocation_ratio: "
"'%(e)s'") % locals())
LOG.warning(_("Could not decode cpu_allocation_ratio: '%s'"), e)
ratio = CONF.cpu_allocation_ratio

return ratio
3 changes: 1 addition & 2 deletions nova/scheduler/filters/ram_filter.py
Expand Up @@ -98,8 +98,7 @@ def _get_ram_allocation_ratio(self, host_state, filter_properties):
try:
ratio = float(min(aggregate_vals))
except ValueError as e:
LOG.warning(_("Could not decode ram_allocation_ratio: "
"'%(e)s'") % locals())
LOG.warning(_("Could not decode ram_allocation_ratio: '%s'"), e)
ratio = CONF.ram_allocation_ratio

return ratio

0 comments on commit 20eac6c

Please sign in to comment.