Skip to content

Commit

Permalink
Remove gettext.install() from nova/__init__.py
Browse files Browse the repository at this point in the history
The gettext.install() function installs a builtin _() function which
translates a string in the translation domain supplied to the install()
function. If gettext.install() is called multiple times, it's the last
call to the function which wins and the last supplied translation domain
which is used e.g.

 >>> import os
 >>> os.environ['LANG'] = 'ja.UTF-8'
 >>> import gettext
 >>> gettext.install('keystone', unicode=1, localedir='/opt/stack/keystone/keystone/locale')
 >>> print _('Invalid syslog facility')
 無効な syslog ファシリティ
 >>> gettext.install('nova', unicode=1, localedir='/opt/stack/nova/nova/locale')
 >>> print _('Invalid syslog facility')
 Invalid syslog facility

Usually this function is called early on in a toplevel script and we
assume that no other code will call it and override the installed _().
However, in Nova, we have taken a shortcut to avoid having to call it
explicitly from each script and instead call it from nova/__init__.py.

This shortcut would be perfectly fine if we were absolutely sure that
nova modules would never be imported from another program. It's probably
quite incorrect for a program to use nova code (indeed, if we wanted to
support this, Nova code shouldn't use the default _() function) but
nevertheless there are some corner cases where it happens. For example,
the keystoneclient auth_token middleware tries to import cfg from
nova.openstack.common and this in turn causes gettext.install('nova')
in other projects like glance or quantum.

To avoid any doubt here, let's just rip out the shortcut and always
call gettext.install() from the top-level script.

Change-Id: If4125d6bcbde63df95de129ac5c83b4a6d6f130a
  • Loading branch information
markmc committed Apr 1, 2013
1 parent c51321c commit 9447e59
Show file tree
Hide file tree
Showing 20 changed files with 42 additions and 8 deletions.
2 changes: 2 additions & 0 deletions bin/nova-all
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ continue attempting to launch the rest of the services.
import eventlet import eventlet
eventlet.monkey_patch(os=False) eventlet.monkey_patch(os=False)


import gettext
import os import os
import sys import sys


Expand All @@ -40,6 +41,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(
if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")): if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)


from nova import config from nova import config
from nova.objectstore import s3server from nova.objectstore import s3server
Expand Down
2 changes: 2 additions & 0 deletions bin/nova-api
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Starts both the EC2 and OpenStack APIs in separate greenthreads.
import eventlet import eventlet
eventlet.monkey_patch(os=False) eventlet.monkey_patch(os=False)


import gettext
import os import os
import sys import sys


Expand All @@ -36,6 +37,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(
if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")): if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)


from nova import config from nova import config
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
Expand Down
2 changes: 2 additions & 0 deletions bin/nova-api-ec2
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import eventlet import eventlet
eventlet.monkey_patch(os=False) eventlet.monkey_patch(os=False)


import gettext
import os import os
import sys import sys


Expand All @@ -31,6 +32,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(
if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")): if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)


from nova import config from nova import config
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
Expand Down
2 changes: 2 additions & 0 deletions bin/nova-api-metadata
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import eventlet import eventlet
eventlet.monkey_patch(os=False) eventlet.monkey_patch(os=False)


import gettext
import os import os
import sys import sys


Expand All @@ -31,6 +32,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(
if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")): if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)


from nova import config from nova import config
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
Expand Down
2 changes: 2 additions & 0 deletions bin/nova-api-os-compute
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import eventlet import eventlet
eventlet.monkey_patch(os=False) eventlet.monkey_patch(os=False)


import gettext
import os import os
import sys import sys


Expand All @@ -31,6 +32,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(
if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")): if os.path.exists(os.path.join(possible_topdir, "nova", "__init__.py")):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)


from nova import config from nova import config
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
Expand Down
3 changes: 3 additions & 0 deletions bin/nova-baremetal-deploy-helper
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import eventlet
if __name__ == '__main__': if __name__ == '__main__':
eventlet.monkey_patch() eventlet.monkey_patch()


import gettext
import os import os
import sys import sys
import threading import threading
Expand All @@ -36,6 +37,8 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')): if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)

import cgi import cgi
import Queue import Queue
import re import re
Expand Down
3 changes: 3 additions & 0 deletions bin/nova-cells
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import eventlet import eventlet
eventlet.monkey_patch() eventlet.monkey_patch()


import gettext
import os import os
import sys import sys


Expand All @@ -34,6 +35,8 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')): if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)

from nova import config from nova import config
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
from nova import service from nova import service
Expand Down
2 changes: 2 additions & 0 deletions bin/nova-cert
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import eventlet import eventlet
eventlet.monkey_patch() eventlet.monkey_patch()


import gettext
import os import os
import sys import sys


Expand All @@ -33,6 +34,7 @@ POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'nova', '__init__.py')): if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'nova', '__init__.py')):
sys.path.insert(0, POSSIBLE_TOPDIR) sys.path.insert(0, POSSIBLE_TOPDIR)


gettext.install('nova', unicode=1)


from nova import config from nova import config
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
Expand Down
2 changes: 2 additions & 0 deletions bin/nova-compute
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ if os.name == 'nt':
else: else:
eventlet.monkey_patch() eventlet.monkey_patch()


import gettext
import os import os
import sys import sys
import traceback import traceback
Expand All @@ -43,6 +44,7 @@ POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'nova', '__init__.py')): if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'nova', '__init__.py')):
sys.path.insert(0, POSSIBLE_TOPDIR) sys.path.insert(0, POSSIBLE_TOPDIR)


gettext.install('nova', unicode=1)


from nova import config from nova import config
import nova.db.api import nova.db.api
Expand Down
2 changes: 2 additions & 0 deletions bin/nova-conductor
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import eventlet import eventlet
eventlet.monkey_patch() eventlet.monkey_patch()


import gettext
import os import os
import sys import sys


Expand All @@ -33,6 +34,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')): if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)


from nova import config from nova import config
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
Expand Down
2 changes: 2 additions & 0 deletions bin/nova-console
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import eventlet import eventlet
eventlet.monkey_patch() eventlet.monkey_patch()


import gettext
import os import os
import sys import sys


Expand All @@ -34,6 +35,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')): if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)


from nova import config from nova import config
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
Expand Down
2 changes: 2 additions & 0 deletions bin/nova-consoleauth
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import eventlet import eventlet
eventlet.monkey_patch() eventlet.monkey_patch()


import gettext
import os import os
import sys import sys


Expand All @@ -32,6 +33,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')): if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)


from nova import config from nova import config
from nova.consoleauth import manager from nova.consoleauth import manager
Expand Down
2 changes: 2 additions & 0 deletions bin/nova-network
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import eventlet import eventlet
eventlet.monkey_patch() eventlet.monkey_patch()


import gettext
import os import os
import sys import sys


Expand All @@ -35,6 +36,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')): if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)


from nova import config from nova import config
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
Expand Down
3 changes: 3 additions & 0 deletions bin/nova-novncproxy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ Websocket proxy that is compatible with OpenStack Nova
noVNC consoles. Leverages websockify.py by Joel Martin noVNC consoles. Leverages websockify.py by Joel Martin
""" """


import gettext
import os import os
import sys import sys


from oslo.config import cfg from oslo.config import cfg


gettext.install('nova', unicode=1)

from nova import config from nova import config
from nova.console import websocketproxy from nova.console import websocketproxy


Expand Down
2 changes: 2 additions & 0 deletions bin/nova-objectstore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import eventlet import eventlet
eventlet.monkey_patch() eventlet.monkey_patch()


import gettext
import os import os
import sys import sys


Expand All @@ -33,6 +34,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')): if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)


from nova import config from nova import config
from nova.objectstore import s3server from nova.objectstore import s3server
Expand Down
3 changes: 3 additions & 0 deletions bin/nova-spicehtml5proxy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ Websocket proxy that is compatible with OpenStack Nova
SPICE HTML5 consoles. Leverages websockify.py by Joel Martin SPICE HTML5 consoles. Leverages websockify.py by Joel Martin
""" """


import gettext
import os import os
import sys import sys


from oslo.config import cfg from oslo.config import cfg


gettext.install('nova', unicode=1)

from nova import config from nova import config
from nova.console import websocketproxy from nova.console import websocketproxy


Expand Down
2 changes: 2 additions & 0 deletions bin/nova-xvpvncproxy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import eventlet import eventlet
eventlet.monkey_patch() eventlet.monkey_patch()


import gettext
import os import os
import sys import sys


Expand All @@ -30,6 +31,7 @@ possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')): if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
sys.path.insert(0, possible_topdir) sys.path.insert(0, possible_topdir)


gettext.install('nova', unicode=1)


from nova import config from nova import config
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
Expand Down
5 changes: 2 additions & 3 deletions doc/source/devref/il8n.rst
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ The ``_()`` function is brought into the global scope by doing::
import gettext import gettext
gettext.install("nova", unicode=1) gettext.install("nova", unicode=1)


In general, you shouldn't need to add these to any nova files, since the lines These lines are needed in any toplevel script before any nova modules are
are present in ``nova/__init__.py``. However, if this code is missing, it may imported. If this code is missing, it may result in an error that looks like::
result in an error that looks like like::


NameError: name '_' is not defined NameError: name '_' is not defined
5 changes: 0 additions & 5 deletions nova/__init__.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,8 +24,3 @@
:platform: Unix :platform: Unix
:synopsis: Infrastructure-as-a-Service Cloud platform. :synopsis: Infrastructure-as-a-Service Cloud platform.
""" """

import gettext


gettext.install('nova', unicode=1)
2 changes: 2 additions & 0 deletions tools/hacking.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Built on top of pep8.py Built on top of pep8.py
""" """


import gettext
import imp import imp
import inspect import inspect
import logging import logging
Expand Down Expand Up @@ -643,6 +644,7 @@ def once_git_check_commit_title():
""" """


if __name__ == "__main__": if __name__ == "__main__":
gettext.install('nova', unicode=1)
#include nova path #include nova path
sys.path.append(os.getcwd()) sys.path.append(os.getcwd())
#Run once tests (not per line) #Run once tests (not per line)
Expand Down

0 comments on commit 9447e59

Please sign in to comment.