Skip to content

Commit

Permalink
[SCons] Prevent aggressive pip uninstallation of existing package
Browse files Browse the repository at this point in the history
Fixes #1230
  • Loading branch information
speth committed Apr 6, 2022
1 parent 01b1a4b commit 2a74ad9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
26 changes: 26 additions & 0 deletions interfaces/cython/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ script = """\
from sysconfig import *
import numpy
import json
import site
vars = get_config_vars()
vars["plat"] = get_platform()
vars["numpy_include"] = numpy.get_include()
vars["site_packages"] = site.getsitepackages()
vars["user_site_packages"] = site.getusersitepackages()
print(json.dumps(vars))
"""
info = json.loads(get_command_output(localenv["python_cmd"], "-c", script))
Expand All @@ -50,6 +53,8 @@ py_version_short = parse_version(info["py_version_short"])
py_version_full = parse_version(info["py_version"])
py_version_nodot = info["py_version_nodot"]
numpy_include = info["numpy_include"]
site_packages = info["site_packages"]
user_site_packages = info["user_site_packages"]
localenv.Prepend(CPPPATH=[Dir('#include'), inc, numpy_include])
localenv.Prepend(LIBS=localenv['cantera_libs'])

Expand Down Expand Up @@ -137,6 +142,27 @@ elif not env["default_prefix"]:
install_cmd.append(f"--prefix={env['prefix']}")
python_prefix = env["prefix"]

# Check for existing Python module installation. Allow pip to remove an existing
# installation only 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",
ignore_errors=True)

if user_install:
test_prefix = Path(user_site_packages).parents[2]
elif python_prefix is None:
test_prefix = Path(site_packages).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
8 changes: 6 additions & 2 deletions site_scons/buildutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,20 +1188,24 @@ def our_spawn(sh: str, escape: str, cmd: str, args: str, environ: "Dict[str, str
return our_spawn


def get_command_output(cmd: str, *args: str):
def get_command_output(cmd: str, *args: str, ignore_errors=False):
"""
Run a command with arguments and return its output.
"""
environ = dict(os.environ)
if "PYTHONHOME" in environ:
# Can cause problems when trying to run a different Python interpreter
del environ["PYTHONHOME"]
kwargs = {}
if ignore_errors:
kwargs["stderr"] = subprocess.DEVNULL
data = subprocess.run(
[cmd] + list(args),
env=environ,
stdout=subprocess.PIPE,
universal_newlines=True,
check=True,
check=not ignore_errors,
**kwargs,
)
return data.stdout.strip()

Expand Down

0 comments on commit 2a74ad9

Please sign in to comment.