Skip to content

Commit 0d29d6a

Browse files
spethischoegl
authored andcommitted
[SCons] Prevent aggressive pip uninstallation of existing package
Fixes #1230
1 parent 01b1a4b commit 0d29d6a

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

interfaces/cython/SConscript

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ script = """\
3636
from sysconfig import *
3737
import numpy
3838
import json
39+
import site
3940
vars = get_config_vars()
4041
vars["plat"] = get_platform()
4142
vars["numpy_include"] = numpy.get_include()
43+
vars["site_packages"] = site.getsitepackages()
44+
vars["user_site_packages"] = site.getusersitepackages()
4245
print(json.dumps(vars))
4346
"""
4447
info = json.loads(get_command_output(localenv["python_cmd"], "-c", script))
@@ -50,6 +53,8 @@ py_version_short = parse_version(info["py_version_short"])
5053
py_version_full = parse_version(info["py_version"])
5154
py_version_nodot = info["py_version_nodot"]
5255
numpy_include = info["numpy_include"]
56+
site_packages = info["site_packages"]
57+
user_site_packages = info["user_site_packages"]
5358
localenv.Prepend(CPPPATH=[Dir('#include'), inc, numpy_include])
5459
localenv.Prepend(LIBS=localenv['cantera_libs'])
5560

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

145+
# Check for existing Python module installation. Allow pip to remove an existing
146+
# installation only if we're installing to the same location. Also disable
147+
# uninstallation if we're installing to a staging directory.
148+
if env["stage_dir"]:
149+
install_cmd.append("--ignore-installed")
150+
else:
151+
info = get_command_output(localenv["python_cmd"], "-m", "pip", "show", "cantera",
152+
ignore_errors=True)
153+
154+
if user_install:
155+
test_prefix = Path(user_site_packages).parents[2]
156+
elif python_prefix is None:
157+
test_prefix = Path(site_packages[0]).parents[2]
158+
else:
159+
test_prefix = Path(python_prefix)
160+
161+
match = re.search(r"Location: (.*)\n", info, re.MULTILINE)
162+
existing_prefix = Path(match.group(1)).parents[2] if match else None
163+
if existing_prefix and existing_prefix != test_prefix:
164+
install_cmd.append("--ignore-installed")
165+
140166
if env["stage_dir"]:
141167
# Get the absolute path to the stage directory. If the stage directory is a relative
142168
# path, consider it to be relative to the root of the Cantera source directory.

site_scons/buildutils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,20 +1188,24 @@ def our_spawn(sh: str, escape: str, cmd: str, args: str, environ: "Dict[str, str
11881188
return our_spawn
11891189

11901190

1191-
def get_command_output(cmd: str, *args: str):
1191+
def get_command_output(cmd: str, *args: str, ignore_errors=False):
11921192
"""
11931193
Run a command with arguments and return its output.
11941194
"""
11951195
environ = dict(os.environ)
11961196
if "PYTHONHOME" in environ:
11971197
# Can cause problems when trying to run a different Python interpreter
11981198
del environ["PYTHONHOME"]
1199+
kwargs = {}
1200+
if ignore_errors:
1201+
kwargs["stderr"] = subprocess.DEVNULL
11991202
data = subprocess.run(
12001203
[cmd] + list(args),
12011204
env=environ,
12021205
stdout=subprocess.PIPE,
12031206
universal_newlines=True,
1204-
check=True,
1207+
check=not ignore_errors,
1208+
**kwargs,
12051209
)
12061210
return data.stdout.strip()
12071211

0 commit comments

Comments
 (0)