Skip to content

Commit

Permalink
[SCons] Prevent aggressive pip uninstallation of cantera-minimal
Browse files Browse the repository at this point in the history
Fixes #1230
  • Loading branch information
speth committed Apr 10, 2022
1 parent 903c184 commit da3985a
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions interfaces/python_minimal/SConscript
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Minimal Python Module"""
from pathlib import Path
from buildutils import *
import json
import re

Import('env', 'build', 'install')

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit da3985a

Please sign in to comment.