From b7cfb3666569c098d8c18aa6e0877371bc0f14ff Mon Sep 17 00:00:00 2001 From: Chris Hennes Date: Sun, 2 Feb 2025 19:46:42 +0100 Subject: [PATCH] Addon Manager: Create utility function to get pip --- .../AddonManager/addonmanager_utilities.py | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Mod/AddonManager/addonmanager_utilities.py b/src/Mod/AddonManager/addonmanager_utilities.py index a82f29f6d7..c378390e78 100644 --- a/src/Mod/AddonManager/addonmanager_utilities.py +++ b/src/Mod/AddonManager/addonmanager_utilities.py @@ -25,18 +25,15 @@ """ Utilities to work across different platforms, providers and python versions """ from datetime import datetime -from typing import Optional, Any -import ctypes +from typing import Optional, Any, List import os import platform -import re import shutil import stat import subprocess import time import re import ctypes -from typing import Optional, Any from urllib.parse import urlparse @@ -50,6 +47,12 @@ except ImportError: import addonmanager_freecad_interface as fci +try: + from freecad.utils import get_python_exe +except ImportError: + def get_python_exe(): + return shutil.which("python") + if fci.FreeCADGui: # If the GUI is up, we can use the NetworkManager to handle our downloads. If there is no event @@ -495,3 +498,20 @@ def get_main_am_window(): return widget.centralWidget() # Why is this code even getting called? return None + + +def create_pip_call(args: List[str]) -> List[str]: + """Choose the correct mechanism for calling pip on each platform. It currently supports + either `python -m pip` (most environments) or `freecad.pip` (Snap packages). Returns a list + of arguments suitable for passing directly to subprocess.Popen and related functions.""" + snap_package = os.getenv("SNAP_REVISION") + if snap_package: + call_args = ["freecad.pip", "--disable-pip-version-check"] + call_args.extend(args) + else: + python_exe = get_python_exe() + if not python_exe: + raise (RuntimeError("Could not locate Python executable on this system")) + call_args = [python_exe, "-m", "pip", "--disable-pip-version-check"] + call_args.extend(args) + return call_args