[Lldb-commits] [lldb] r250858 - Introduce a mechanism for reusing Python modules out of tree.

Zachary Turner via lldb-commits lldb-commits at lists.llvm.org
Tue Oct 20 14:05:57 PDT 2015


Author: zturner
Date: Tue Oct 20 16:05:57 2015
New Revision: 250858

URL: http://llvm.org/viewvc/llvm-project?rev=250858&view=rev
Log:
Introduce a mechanism for reusing Python modules out of tree.

Right now our Python code does not all share a common root.  Tests and
scripts both contain python code that cannot take advantage of reusability
since they are unrelated siblings of each other.

In particular, this presents a problem for wanting to use third party
packages from both sides, since it does not make sense to copy the module
into both places.

This patch solves this by introducing a script lldb_shared.py which is a
very lightweight script that just searches up the tree until it finds a
root, and then imports a module from there.  That module knows how to
find all of the shared code that LLDB uses, and adjusts sys.path
accordingly to make them all visible.

Added:
    lldb/trunk/lldb.root
    lldb/trunk/lldb_shared_base.py
    lldb/trunk/test/lldb_shared.py
Modified:
    lldb/trunk/test/dotest.py

Added: lldb/trunk/lldb.root
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lldb.root?rev=250858&view=auto
==============================================================================
    (empty)

Added: lldb/trunk/lldb_shared_base.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/lldb_shared_base.py?rev=250858&view=auto
==============================================================================
--- lldb/trunk/lldb_shared_base.py (added)
+++ lldb/trunk/lldb_shared_base.py Tue Oct 20 16:05:57 2015
@@ -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)

Modified: lldb/trunk/test/dotest.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/dotest.py?rev=250858&r1=250857&r2=250858&view=diff
==============================================================================
--- lldb/trunk/test/dotest.py (original)
+++ lldb/trunk/test/dotest.py Tue Oct 20 16:05:57 2015
@@ -22,6 +22,8 @@ for available options.
 
 from __future__ import print_function
 
+import lldb_shared
+
 import atexit
 import commands
 import importlib
@@ -40,6 +42,8 @@ import inspect
 import unittest2
 import lldbtest_config
 
+import six
+
 
 def is_exe(fpath):
     """Returns true if fpath is an executable."""

Added: lldb/trunk/test/lldb_shared.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/lldb_shared.py?rev=250858&view=auto
==============================================================================
--- lldb/trunk/test/lldb_shared.py (added)
+++ lldb/trunk/test/lldb_shared.py Tue Oct 20 16:05:57 2015
@@ -0,0 +1,22 @@
+import inspect
+import os
+import sys
+
+def find_lldb_root():
+    lldb_root = os.path.dirname(inspect.getfile(inspect.currentframe()))
+    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




More information about the lldb-commits mailing list