From: Isaku Yamahata <[email protected]>

ryu-manager ryu/topology/dumper.py ryu/services/vrrp/dumper.py
results in loading ryu.topology.dumper twice.
ryu/services/vrrp/dumper.py is not loaded.
And also don't populate sys.path multiple times with same path.

Signed-off-by: Isaku Yamahata <[email protected]>
Signed-off-by: YAMAMOTO Takashi <[email protected]>
---
 ryu/utils.py | 47 +++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 43 insertions(+), 4 deletions(-)

diff --git a/ryu/utils.py b/ryu/utils.py
index 66fc922..689b8a4 100644
--- a/ryu/utils.py
+++ b/ryu/utils.py
@@ -40,14 +40,53 @@ from oslo.config import cfg
 LOG = logging.getLogger('ryu.utils')
 
 
+def _abspath(path):
+    if path == '':
+        path = '.'
+    return os.path.abspath(path)
+
+
+def _split_modname(modpath):
+    sys_path = [_abspath(path) for path in sys.path]
+    modname = os.path.basename(modpath)
+    dirname = os.path.dirname(modpath)
+    while True:
+        if dirname in sys_path:
+            break
+        if not os.path.exists(os.path.join(dirname, '__init__.py')):
+            break
+
+        basename = os.path.basename(dirname)
+        if basename:
+            old_dirname = dirname
+            dirname = os.path.dirname(dirname)
+            if old_dirname == dirname:
+                break
+            if modname:
+                modname = basename + '.' + modname
+            else:
+                modname = basename
+        else:
+            break
+
+    return dirname, modname
+
+
+def _import(modname):
+    __import__(modname)
+    return sys.modules[modname]
+
+
 def import_module(modname):
     try:
         __import__(modname)
     except:
-        sys.path.append(os.path.dirname(os.path.abspath(modname)))
-        name = os.path.basename(modname)
-        if name.endswith('.py'):
-            name = name[:-3]
+        modname = os.path.abspath(modname)
+        if modname.endswith('.py'):
+            modname = modname[:-3]
+        dirname, name = _split_modname(modname)
+        if dirname not in [_abspath(path) for path in sys.path]:
+            sys.path.append(dirname)
         try:
             __import__(name)
         except cfg.ArgsAlreadyParsedError:
-- 
1.8.3.1


------------------------------------------------------------------------------
LIMITED TIME SALE - Full Year of Microsoft Training For Just $49.99!
1,500+ hours of tutorials including VisualStudio 2012, Windows 8, SharePoint
2013, SQL 2012, MVC 4, more. BEST VALUE: New Multi-Library Power Pack includes
Mobile, Cloud, Java, and UX Design. Lowest price ever! Ends 9/20/13. 
http://pubads.g.doubleclick.net/gampad/clk?id=58041151&iu=/4140/ostg.clktrk
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to