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 <[email protected]>
---
 ryu/app/wsgi.py               |  4 ++++
 ryu/cmd/manager.py            |  1 +
 ryu/cmd/of_config_cli.py      |  1 +
 ryu/cmd/rpc_cli.py            |  1 +
 ryu/cmd/ryu_base.py           |  1 +
 ryu/contrib/__init__.py       | 30 +++++++++++++++++++++---------
 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 94e67f6..24baf05 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 66d59b3..f17d0ea 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 6a6e49a..ef1bb16 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 a2af9cb..f71ec94 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 4a8fc41..b153dfc 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 7faed78..b79831e 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 b2dbd05..cd419c1 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__)
 
-- 
2.1.0


------------------------------------------------------------------------------
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM through live exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual- event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to