zturner created this revision.
zturner added a reviewer: tfiala.
zturner added a subscriber: lldb-commits.

We have a lot of Python code in test and scripts that in theory could be reused 
amongst the two.  Until now there is no mechanism to support this because 
Python's module / packaging system imposes some limitations on how and where 
your code must be in order to be re-used.

This patch addresses this by creating a new module `lldb_shared` which searches 
up the tree until it finds an `lldb.root` file.  Then, it dynamically imports 
`lldb_shared_base` from that folder.  `lldb_shared_base` is a module that 
exists only as a means to modify `sys.path` to add all of the shared module 
locations ot the path.

In order to make use of this, you need to copy the `lldb_shared.py` file into 
the root of each project that you wish to make use of this shared code.

The end result of this is that if you have a directory structure like this:

    lldb
    \__ test
    \__ scripts
    \__ common
          \__ useful_utility.py
    \__ third_party
          \__ six
                \__ six.py

All you need to do is add `lldb_shared.py` to the root of `lldb/test` and 
`lldb/scripts` and then you can write 

    import lldb_shared
    import useful_utility
    import six

This patch is dependent on D13880

http://reviews.llvm.org/D13906

Files:
  lldb.root
  lldb_shared_base.py
  test/dotest.py
  test/lldb_shared.py

Index: test/lldb_shared.py
===================================================================
--- /dev/null
+++ test/lldb_shared.py
@@ -0,0 +1,21 @@
+import os
+import sys
+
+def find_lldb_root():
+    lldb_root = os.getcwd()
+    while True:
+        lldb_root = os.path.dirname(lldb_root)
+        if lldb_root is None:
+            return None
+
+        test_path = os.path.join(lldb_root, "lldb.root")
+        if os.path.isfile(test_path):
+            return lldb_root
+    return None
+
+lldb_root = find_lldb_root()
+if lldb_root is not None:
+    import imp
+    module = imp.find_module("lldb_shared_base", [lldb_root])
+    if module is not None:
+        imp.load_module("lldb_shared_base", *module)
\ No newline at end of file
Index: test/dotest.py
===================================================================
--- test/dotest.py
+++ test/dotest.py
@@ -22,6 +22,8 @@
 
 from __future__ import print_function
 
+import lldb_shared
+
 import atexit
 import commands
 import importlib
@@ -40,6 +42,8 @@
 import unittest2
 import lldbtest_config
 
+import six
+
 
 def is_exe(fpath):
     """Returns true if fpath is an executable."""
Index: lldb_shared_base.py
===================================================================
--- /dev/null
+++ lldb_shared_base.py
@@ -0,0 +1,15 @@
+import inspect
+import os
+import sys
+
+def add_third_party_module_dirs(lldb_root):
+    third_party_modules_dir = os.path.join(lldb_root, "third_party", "Python", 
"module")
+    if not os.path.isdir(third_party_modules_dir):
+        return
+
+    module_dirs = os.listdir(third_party_modules_dir)
+    for module_dir in module_dirs:
+        module_dir = os.path.join(third_party_modules_dir, module_dir)
+        sys.path.append(module_dir)
+lldb_root = os.path.dirname(inspect.getfile(inspect.currentframe()))
+add_third_party_module_dirs(lldb_root)


Index: test/lldb_shared.py
===================================================================
--- /dev/null
+++ test/lldb_shared.py
@@ -0,0 +1,21 @@
+import os
+import sys
+
+def find_lldb_root():
+    lldb_root = os.getcwd()
+    while True:
+        lldb_root = os.path.dirname(lldb_root)
+        if lldb_root is None:
+            return None
+
+        test_path = os.path.join(lldb_root, "lldb.root")
+        if os.path.isfile(test_path):
+            return lldb_root
+    return None
+
+lldb_root = find_lldb_root()
+if lldb_root is not None:
+    import imp
+    module = imp.find_module("lldb_shared_base", [lldb_root])
+    if module is not None:
+        imp.load_module("lldb_shared_base", *module)
\ No newline at end of file
Index: test/dotest.py
===================================================================
--- test/dotest.py
+++ test/dotest.py
@@ -22,6 +22,8 @@
 
 from __future__ import print_function
 
+import lldb_shared
+
 import atexit
 import commands
 import importlib
@@ -40,6 +42,8 @@
 import unittest2
 import lldbtest_config
 
+import six
+
 
 def is_exe(fpath):
     """Returns true if fpath is an executable."""
Index: lldb_shared_base.py
===================================================================
--- /dev/null
+++ lldb_shared_base.py
@@ -0,0 +1,15 @@
+import inspect
+import os
+import sys
+
+def add_third_party_module_dirs(lldb_root):
+    third_party_modules_dir = os.path.join(lldb_root, "third_party", "Python", "module")
+    if not os.path.isdir(third_party_modules_dir):
+        return
+
+    module_dirs = os.listdir(third_party_modules_dir)
+    for module_dir in module_dirs:
+        module_dir = os.path.join(third_party_modules_dir, module_dir)
+        sys.path.append(module_dir)
+lldb_root = os.path.dirname(inspect.getfile(inspect.currentframe()))
+add_third_party_module_dirs(lldb_root)
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to