summaryrefslogtreecommitdiff
path: root/scripts/install_api.py
blob: 3c10f7897f4d45c36ca9b7ba2715d8fe3db2b8d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
#
#    Thanks to @withzombies for letting us adapt his script
#

import sys
import os
from site import check_enableusersite

# Handle both normal environments and virtualenvs
try: 
    from site import getusersitepackages, getsitepackages
except ImportError:
    from sysconfig import get_path
    getsitepackages = lambda: get_path('purelib')
    getusersitepackages = getsitepackages

# Hacky command line parsing to accept a silent-install -s flag like linux-setup.sh:
INTERACTIVE = True
if '-s' in sys.argv[1:]:
    INTERACTIVE = False

INSTALL_VENV = False
if '-v' in sys.argv[1:]:
    if os.environ.get('VIRTUAL_ENV'):
        INSTALL_VENV = True
    else:
        print("Error: venv installation requested without an active python3 venv.")
        sys.exit(1)
    
try:
    import binaryninja
    import binaryninjaui #To better detect if migrating from a version without UI plugin support
    print("Binary Ninja API already in the path")
    sys.exit(1)
except ImportError:
    pass

dir_name = os.path.dirname(os.path.abspath(__file__))
api_path = os.path.abspath(os.path.join(dir_name, "..", "python"))

if (os.path.isdir(api_path)):
    print("Found install folder of {}".format(api_path))
else:
    print("Failed to find installed python expected at {}".format(api_path))
    sys.exit(1)


def validate_path(path):
    try:
        os.stat(path)
    except OSError:
        return False

    old_path = sys.path
    sys.path.append(path)

    try:
        from binaryninja import core_version
        print("Found Binary Ninja core version: {}".format(core_version()))
    except ImportError:
        sys.path = old_path
        return False

    return True


while not validate_path(api_path):
    print("\nBinary Ninja not found.")
    if not INTERACTIVE:
        print("Non-interactive mode selected, failing.")
        sys.exit(-1)

    print("Please provide the path to Binary Ninja's install directory: \n [{}] : ".format(api_path))
    new_path = sys.stdin.readline().strip()
    if len(new_path) == 0:
        print("\nInvalid path")
        continue

    if not new_path.endswith('python'):
        new_path = os.path.join(new_path, 'python')

    api_path = new_path

if ( len(sys.argv) > 1 and sys.argv[1].lower() == "root" ):
    #write to root site
    install_path = getsitepackages()[0]
    if not os.access(install_path, os.W_OK):
        print("Root install specified but cannot write to {}".format(install_path))
        sys.exit(1)
elif INSTALL_VENV:
    install_path = getsitepackages()[0]
else:
    if check_enableusersite():
        install_path = getusersitepackages()
        if not os.path.exists(install_path):
            os.makedirs(install_path)
    else:
        print("Warning, trying to write to user site packages, but check_enableusersite fails.")
        sys.exit(1)

binaryninja_pth_path = os.path.join(install_path, 'binaryninja.pth')
with open(binaryninja_pth_path, 'wb') as pth_file:
    pth_file.write((api_path+"\n").encode('charmap'))
    if sys.version_info.major < 3 or (sys.version_info.major == 3 and sys.version_info.minor < 9):
        pth_file.write((api_path+sys.version[0]+"\n").encode('charmap')) #support for python2/3 QT bindings

print("Binary Ninja API installed using {}".format(binaryninja_pth_path))