xref: /llvm-project/lldb/examples/customization/import-python/importcmd.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1import sys
2import os
3import lldb
4
5
6def check_has_dir_in_path(dirname):
7    return sys.path.__contains__(dirname)
8
9
10def ensure_has_dir_in_path(dirname):
11    dirname = os.path.abspath(dirname)
12    if not (check_has_dir_in_path(dirname)):
13        sys.path.append(dirname)
14
15
16def do_import(debugger, modname):
17    if len(modname) > 4 and modname[-4:] == ".pyc":
18        modname = modname[:-4]
19    if len(modname) > 3 and modname[-3:] == ".py":
20        modname = modname[:-3]
21    debugger.HandleCommand("script import " + modname)
22
23
24def pyimport_cmd(debugger, args, result, dict):
25    """Import a Python module given its full path"""
26    print('WARNING: obsolete feature - use native command "command script import"')
27    if args == "":
28        return "no module path given"
29    if not (os.sep in args):
30        modname = args
31        ensure_has_dir_in_path(".")
32    else:
33        endofdir = args.rfind(os.sep)
34        modname = args[endofdir + 1 :]
35        args = args[0:endofdir]
36        ensure_has_dir_in_path(args)
37    do_import(debugger, modname)
38    return None
39