diff options
author | YAMAMOTO Takashi <yamamoto@valinux.co.jp> | 2015-04-17 14:33:09 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-04-18 08:56:19 +0900 |
commit | cdd7084b941160f3b948d9c98fcc549784444b29 (patch) | |
tree | e14ab9812afb96eb103c79430a1505929520b6e9 | |
parent | d4e8026aa8bafc24feb2dd40a5eeae9e159da189 (diff) |
ryu.contrib: Be explicit about sys.path modification
Avoid the automatic modification of sys.path because it hurts
ryu-as-a-library use cases.
An example is the recent versions of neutron OVS-agent,
which optionally imports OVS python bindings, and ends up to
use a wrong copy in ryu.contrib.ovs.
Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/app/wsgi.py | 4 | ||||
-rwxr-xr-x | ryu/cmd/manager.py | 1 | ||||
-rwxr-xr-x | ryu/cmd/of_config_cli.py | 1 | ||||
-rwxr-xr-x | ryu/cmd/rpc_cli.py | 1 | ||||
-rw-r--r-- | ryu/cmd/ryu_base.py | 1 | ||||
-rw-r--r-- | ryu/contrib/__init__.py | 30 | ||||
-rw-r--r-- | ryu/lib/of_config/__init__.py | 4 |
7 files changed, 32 insertions, 10 deletions
diff --git a/ryu/app/wsgi.py b/ryu/app/wsgi.py index 94e67f6b..24baf053 100644 --- a/ryu/app/wsgi.py +++ b/ryu/app/wsgi.py @@ -23,12 +23,16 @@ from ryu import cfg from ryu.lib import hub from routes import Mapper from routes.util import URLGenerator + +import ryu.contrib +ryu.contrib.update_module_path() from tinyrpc.server import RPCServer from tinyrpc.dispatch import RPCDispatcher from tinyrpc.dispatch import public as rpc_public from tinyrpc.protocols.jsonrpc import JSONRPCProtocol from tinyrpc.transports import ServerTransport, ClientTransport from tinyrpc.client import RPCClient +ryu.contrib.restore_module_path() CONF = cfg.CONF CONF.register_cli_opts([ diff --git a/ryu/cmd/manager.py b/ryu/cmd/manager.py index 66d59b3b..f17d0eab 100755 --- a/ryu/cmd/manager.py +++ b/ryu/cmd/manager.py @@ -27,6 +27,7 @@ hub.patch(thread=False) # NOTE: this modifies sys.path and thus affects the following imports. # eg. oslo.config.cfg. import ryu.contrib +ryu.contrib.update_module_path() from ryu import cfg import logging diff --git a/ryu/cmd/of_config_cli.py b/ryu/cmd/of_config_cli.py index 6a6e49aa..ef1bb16a 100755 --- a/ryu/cmd/of_config_cli.py +++ b/ryu/cmd/of_config_cli.py @@ -24,6 +24,7 @@ # (Cmd) raw_get sw1 import ryu.contrib +ryu.contrib.update_module_path() from ryu import cfg diff --git a/ryu/cmd/rpc_cli.py b/ryu/cmd/rpc_cli.py index a2af9cb6..f71ec943 100755 --- a/ryu/cmd/rpc_cli.py +++ b/ryu/cmd/rpc_cli.py @@ -30,6 +30,7 @@ # (Cmd) import ryu.contrib +ryu.contrib.update_module_path() from ryu import cfg diff --git a/ryu/cmd/ryu_base.py b/ryu/cmd/ryu_base.py index 4a8fc417..b153dfce 100644 --- a/ryu/cmd/ryu_base.py +++ b/ryu/cmd/ryu_base.py @@ -15,6 +15,7 @@ # limitations under the License. import ryu.contrib +ryu.contrib.update_module_path() from ryu import cfg from ryu import utils diff --git a/ryu/contrib/__init__.py b/ryu/contrib/__init__.py index 7faed780..b79831ee 100644 --- a/ryu/contrib/__init__.py +++ b/ryu/contrib/__init__.py @@ -1,11 +1,23 @@ -# Adjust module loading path for third party libraries -import os import sys -for path in __path__: - if path in sys.path: - sys.path.remove(path) - path = os.path.abspath(path) - if path in sys.path: - sys.path.remove(path) - sys.path.insert(0, path) # prioritize our own copy than system's +_orig_sys_path = None + +def update_module_path(): + # Adjust module loading path for third party libraries + import os + global _orig_sys_path + + _orig_sys_path = sys.path[:] + for path in __path__: + if path in sys.path: + sys.path.remove(path) + path = os.path.abspath(path) + if path in sys.path: + sys.path.remove(path) + sys.path.insert(0, path) # prioritize our own copy than system's + +def restore_module_path(): + global _orig_sys_path + + sys.path = _orig_sys_path + _orig_sys_path = None diff --git a/ryu/lib/of_config/__init__.py b/ryu/lib/of_config/__init__.py index b2dbd05a..cd419c11 100644 --- a/ryu/lib/of_config/__init__.py +++ b/ryu/lib/of_config/__init__.py @@ -22,7 +22,9 @@ import glob import os.path import sys -import ryu.contrib # we require ncclient +# we require ncclient +import ryu.contrib +ryu.contrib.update_module_path() SCHEMA_DIR = os.path.dirname(__file__) |