From da3985aceb02b6cf42b88a5d4913f70fc0aff101 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Sun, 10 Apr 2022 11:15:10 -0400 Subject: [PATCH] [SCons] Prevent aggressive pip uninstallation of cantera-minimal Fixes #1230 --- interfaces/python_minimal/SConscript | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/interfaces/python_minimal/SConscript b/interfaces/python_minimal/SConscript index 1ae8a19370..3a2a5fd353 100644 --- a/interfaces/python_minimal/SConscript +++ b/interfaces/python_minimal/SConscript @@ -1,6 +1,8 @@ """Minimal Python Module""" from pathlib import Path from buildutils import * +import json +import re Import('env', 'build', 'install') @@ -46,6 +48,43 @@ if localenv["PYTHON_INSTALLER"] == "direct": install_cmd.append(f"--prefix={env['prefix']}") python_prefix = env["prefix"] +# Get information about installation paths +script = """\ +import json +import site +vars = { + "site_packages": site.getsitepackages(), + "user_site_packages": site.getusersitepackages(), +} +print(json.dumps(vars)) +""" +info = json.loads(get_command_output(localenv["python_cmd"], "-c", script)) +site_packages = info["site_packages"] +user_site_packages = info["user_site_packages"] + +# Check for existing Python module installation. Allow pip to remove an existing +# installation only if we're installing to the same location. Also disable +# uninstallation if we're installing to a staging directory. +if env["stage_dir"]: + install_cmd.append("--ignore-installed") +else: + info = get_command_output( + localenv["python_cmd"], "-m", "pip", "show", "cantera-minimal", + ignore_errors=True + ) + + if user_install: + test_prefix = Path(user_site_packages).parents[2] + elif python_prefix is None: + test_prefix = Path(site_packages[0]).parents[2] + else: + test_prefix = Path(python_prefix) + + match = re.search(r"Location: (.*)\n", info, re.MULTILINE) + existing_prefix = Path(match.group(1)).parents[2] if match else None + if existing_prefix and existing_prefix != test_prefix: + install_cmd.append("--ignore-installed") + if env["stage_dir"]: # Get the absolute path to the stage directory. If the stage directory is a relative # path, consider it to be relative to the root of the Cantera source directory.