From d5f4e1d9fb54c1bdc892c23986eddd9b783b1fd2 Mon Sep 17 00:00:00 2001 From: Raqueebuddin Aziz Date: Fri, 1 Dec 2023 18:19:21 +0530 Subject: [PATCH] update install.py --- install.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++- vars.py | 3 +- 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/install.py b/install.py index 10bd7f5..826f9ff 100644 --- a/install.py +++ b/install.py @@ -1,8 +1,111 @@ +from pathlib import Path import shutil -from vars import module_js_directory, application_root_directory +import sys +import subprocess +import threading +import locale +import re +from vars import module_js_directory, application_root_directory, root_directory + +if "python_embeded" in sys.executable or "python_embedded" in sys.executable: + pip_install = [sys.executable, "-s", "-m", "pip", "install"] +else: + pip_install = [sys.executable, "-m", "pip", "install"] shutil.copytree( module_js_directory, application_root_directory / "web" / "extensions" / "vanilla.simple.wildcard", dirs_exist_ok=True, ) + + +def handle_stream(stream, is_stdout): + stream.reconfigure(encoding=locale.getpreferredencoding(), errors="replace") + + for msg in stream: + if is_stdout: + print(msg, end="", file=sys.stdout) + else: + print(msg, end="", file=sys.stderr) + + +def process_wrap(cmd_str, cwd=None, handler=None): + print(f"[Simple Wildcard] EXECUTE: {cmd_str} in '{cwd}'") + process = subprocess.Popen( + cmd_str, + cwd=cwd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + bufsize=1, + ) + + if handler is None: + handler = handle_stream + + stdout_thread = threading.Thread(target=handler, args=(process.stdout, True)) + stderr_thread = threading.Thread(target=handler, args=(process.stderr, False)) + + stdout_thread.start() + stderr_thread.start() + + stdout_thread.join() + stderr_thread.join() + + return process.wait() + + +# --- + + +pip_list = None + + +def get_installed_packages(): + global pip_list + + if pip_list is None: + try: + result = subprocess.check_output( + [sys.executable, "-m", "pip", "list"], universal_newlines=True + ) + pip_list = set( + [line.split()[0].lower() for line in result.split("\n") if line.strip()] + ) + except subprocess.CalledProcessError as e: + print( + f"[Simple Wildcard] Failed to retrieve the information of installed pip packages." + ) + return set() + + return pip_list + + +def is_installed(name): + name = name.strip() + pattern = r"([^<>!=]+)([<>!=]=?)" + match = re.search(pattern, name) + + if match: + name = match.group(1) + + result = name.lower() in get_installed_packages() + return result + + +def is_requirements_installed(file_path: Path): + print(f"req_path: {file_path.as_posix()}") + if file_path.exists(): + with open(file_path, "r") as file: + lines = file.readlines() + for line in lines: + if not is_installed(line): + return False + + return True + + +requirements_path = root_directory / "requirements.txt" + +if not is_requirements_installed(requirements_path): + process_wrap(pip_install + ["-r", requirements_path]) diff --git a/vars.py b/vars.py index 0ae775d..c7b892d 100644 --- a/vars.py +++ b/vars.py @@ -1,4 +1,5 @@ from pathlib import Path -module_js_directory = Path(__file__).parent / "js" +root_directory = Path(__file__).parent +module_js_directory = root_directory / "js" application_root_directory = Path(__file__).parent.parent.parent