From d252320b497e6691d2b6db24cb7a77f8b8f41ad5 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Fri, 1 Mar 2024 10:59:00 +0100 Subject: [PATCH] ensures python site package dir exists --- scripts/install_api.py | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/scripts/install_api.py b/scripts/install_api.py index 5e93064ba0..9758400007 100755 --- a/scripts/install_api.py +++ b/scripts/install_api.py @@ -40,6 +40,21 @@ def check_virtual_environment() -> bool: return False +def getsitepackage() -> str: + """ + gets the site package and creates it if needed + returns empty if fails + """ + install_path = getsitepackages()[0] + if not os.path.exists(install_path): + try: + os.makedirs(install_path, exist_ok=True) + except OSError: + print_error(f"Root install specified but cannot create {install_path}") + return '' + return install_path + + def binaryninja_installed() -> bool: try: binaryninja = importlib.util.find_spec("binaryninja") @@ -121,20 +136,20 @@ def install(interactive=False, on_root=False, on_pyenv=False) -> bool: api_path = new_path if on_root: - install_path = getsitepackages()[0] - if not os.access(install_path, os.W_OK): - print_error(f"Root install specified but cannot write to {install_path}") + install_path = getsitepackage() + if not install_path or not os.access(install_path, os.W_OK): + print_error(f"Root install specified but cannot write to \"{install_path}\"") return False else: - print(f"Installing on root site: {install_path}") + print(f"Installing on root site: \"{install_path}\"") elif on_pyenv: - install_path = getsitepackages()[0] - print(f"Installing on pyenv site: {install_path}") + install_path = getsitepackage() + print(f"Installing on pyenv site: \"{install_path}") elif check_virtual_environment(): - install_path = getsitepackages()[0] - print(f"Installing on virtual environment site: {install_path}") + install_path = getsitepackage() + print(f"Installing on virtual environment site: \"{install_path}\"") else: if not check_enableusersite(): @@ -146,6 +161,9 @@ def install(interactive=False, on_root=False, on_pyenv=False) -> bool: os.makedirs(install_path) print(f"Installing on user site: {install_path}") + if not install_path: + print_error("empty site packages path") + return False 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"))