Home | History | Annotate | Download | only in python-vim-lldb
      1 
      2 # Locate and load the lldb python module
      3 
      4 import os, sys
      5 
      6 def import_lldb():
      7   """ Find and import the lldb modules. This function tries to find the lldb module by:
      8       1. Simply by doing "import lldb" in case the system python installation is aware of lldb. If that fails,
      9       2. Executes the lldb executable pointed to by the LLDB environment variable (or if unset, the first lldb
     10          on PATH") with the -P flag to determine the PYTHONPATH to set. If the lldb executable returns a valid
     11          path, it is added to sys.path and the import is attempted again. If that fails, 3. On Mac OS X the
     12          default Xcode 4.5 installation path.
     13   """
     14 
     15   # Try simple 'import lldb', in case of a system-wide install or a pre-configured PYTHONPATH
     16   try:
     17     import lldb
     18     return True
     19   except ImportError:
     20     pass
     21 
     22   # Allow overriding default path to lldb executable with the LLDB environment variable
     23   lldb_executable = 'lldb'
     24   if 'LLDB' in os.environ and os.path.exists(os.environ['LLDB']):
     25     lldb_executable = os.environ['LLDB']
     26 
     27   # Try using builtin module location support ('lldb -P')
     28   from subprocess import check_output, CalledProcessError
     29   try:
     30     with open(os.devnull, 'w') as fnull:
     31       lldb_minus_p_path = check_output("%s -P" % lldb_executable, shell=True, stderr=fnull).strip()
     32     if not os.path.exists(lldb_minus_p_path):
     33       #lldb -P returned invalid path, probably too old
     34       pass
     35     else:
     36       sys.path.append(lldb_minus_p_path)
     37       import lldb
     38       return True
     39   except CalledProcessError:
     40     # Cannot run 'lldb -P' to determine location of lldb python module
     41     pass
     42   except ImportError:
     43     # Unable to import lldb module from path returned by `lldb -P`
     44     pass
     45 
     46   # On Mac OS X, use the try the default path to XCode lldb module
     47   if "darwin" in sys.platform:
     48     xcode_python_path = "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/Current/Resources/Python/"
     49     sys.path.append(xcode_python_path)
     50     try:
     51       import lldb
     52       return True
     53     except ImportError:
     54       # Unable to import lldb module from default Xcode python path
     55       pass
     56 
     57   return False
     58 
     59 if not import_lldb():
     60   import vim
     61   vim.command('redraw | echo "%s"' % " Error loading lldb module; vim-lldb will be disabled. Check LLDB installation or set LLDB environment variable.")
     62