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]>
---
 ryu/utils.py |   62 +++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 53 insertions(+), 9 deletions(-)

diff --git a/ryu/utils.py b/ryu/utils.py
index d5b4479..afca459 100644
--- a/ryu/utils.py
+++ b/ryu/utils.py
@@ -24,17 +24,61 @@ import re
 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]
-        __import__(name)
-        return sys.modules[name]
-    return sys.modules[modname]
+        return _import(modname)
+    except ImportError:
+        pass
+
+    if modname.endswith('.py'):
+        modname = modname[:-3]
+        try:
+            return _import(modname)
+        except ImportError:
+            pass
+
+    modname = os.path.abspath(modname)
+    dirname, name = _split_modname(modname)
+    if dirname not in [_abspath(path) for path in sys.path]:
+        sys.path.append(dirname)
+    return _import(name)
 
 
 def round_up(x, y):
-- 
1.7.10.4


------------------------------------------------------------------------------
Minimize network downtime and maximize team effectiveness.
Reduce network management and security costs.Learn how to hire 
the most talented Cisco Certified professionals. Visit the 
Employer Resources Portal
http://www.cisco.com/web/learning/employer_resources/index.html
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to