Skip to content

Commit c89d824

Browse files
authored
Detect when a conflicting editable install exists (#150)
* Detect when a conflicting editable install exists Closes #143 * Correctly handle non-editable package install * Add test for editable install warning
1 parent 9e504d6 commit c89d824

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

.github/workflows/test.sh

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,34 @@ RED="\033[31;1m"
66
MAGENTA="\033[35m"
77
NORMAL="\033[0m"
88

9-
prun() { echo -e "\n$RED\$ $@ $NORMAL\n" ; "$@" ; }
9+
ptest() { echo -e "\n${MAGENTA}[TEST] $@${NORMAL}\n" ; }
10+
prun() { echo -e "$RED\$ $@ $NORMAL" ; "$@" ; }
1011

1112
prun cd example_pkg
1213

14+
ptest version command runs
1315
prun spin --version
1416

17+
ptest build command runs
1518
pip install meson-python ninja
1619
prun spin build
1720

18-
# Test spin run
21+
ptest Does spin expand \$PYTHONPATH?
1922
SPIN_PYTHONPATH=$(spin run 'echo $PYTHONPATH')
2023
echo spin sees PYTHONPATH=\"${SPIN_PYTHONPATH}\"
2124
if [[ ${SPIN_PYTHONPATH} == "\$PYTHONPATH" ]]; then
2225
echo "Expected Python path, but got $SPIN_PYTHONPATH instead"
2326
exit 1
2427
fi
2528

26-
echo -e "${MAGENTA}Does \$PYTHONPATH contains site-packages?${NORMAL}"
29+
ptest Does \$PYTHONPATH contains site-packages?
2730
if [[ ${SPIN_PYTHONPATH} == *"site-packages" ]]; then
2831
echo "Yes"
2932
else
3033
echo "No; it is $SPIN_PYTHONPATH"
3134
fi
3235

33-
echo -e "${MAGENTA}Does \`spin run\` redirect only command output to stdout?${NORMAL}"
36+
ptest Does \`spin run\` redirect only command output to stdout?
3437
# Once we're on Python >3.11, can replace syspath manipulation below with -P flag to Python
3538
VERSION=$(spin run python -c 'import sys; del sys.path[0]; import example_pkg; print(example_pkg.__version__)')
3639
if [[ $VERSION == "0.0.0dev0" ]]; then
@@ -40,10 +43,21 @@ else
4043
exit 1
4144
fi
4245

46+
ptest Does spin detect conflict with editable install?
47+
prun pip install --quiet -e .
48+
OUT=$(spin run ls)
49+
if [[ $OUT == *"Warning! An editable installation"* ]]; then
50+
echo "Yes"
51+
else
52+
echo "No"
53+
exit 1
54+
fi
55+
prun pip uninstall --quiet -y example_pkg
56+
4357
if [[ $PLATFORM == linux || $PLATFORM == darwin ]]; then
4458
# Detecting whether a file is executable is not that easy on Windows,
4559
# as it seems to take into consideration whether that file is associated as an executable.
46-
echo -e "${MAGENTA}Does \`spin run foo.py\` warn that \`spin run python foo.py\` is correct?${NORMAL}"
60+
ptest Does \`spin run foo.py\` warn that \`spin run python foo.py\` is likely intended?
4761
OUT=$( touch __foo.py && spin run __foo.py || true )
4862
rm __foo.py
4963
if [[ $OUT == *"Did you mean to call"* ]]; then
@@ -54,28 +68,34 @@ if [[ $PLATFORM == linux || $PLATFORM == darwin ]]; then
5468
fi
5569
fi
5670

71+
ptest test command runs
5772
prun spin test
58-
echo -e "${MAGENTA}Running \`spin test\`, but with PYTHONPATH set${NORMAL}"
73+
74+
ptest Does \`spin test\` work when PYTHONPATH is set?
5975
PYTHONPATH=./tmp spin test
6076

77+
ptest sdist command runs
6178
prun spin sdist
79+
80+
ptest example command runs
6281
prun spin example
6382

64-
pip install sphinx
83+
ptest docs command runs
84+
pip install --quiet sphinx
6585
prun spin docs
6686

87+
ptest install command works
88+
prun spin install
89+
(cd /tmp ; [[ $(python -c 'import example_pkg; print(example_pkg.__version__)') == "0.0.0dev0" ]])
90+
prun pip uninstall -y --quiet example_pkg
6791

6892
## Platform specialized tests
6993

7094
if [[ $PLATFORM == linux ]]; then
95+
ptest gdb command runs on linux
7196
prun spin gdb -c 'import example_pkg; example_pkg.echo("hi")' -- --eval "run" --batch
7297
fi
7398

7499
# if [[ $PLATFORM == darwin ]]; then
75100

76101
# if [[ $PLATFORM =~ ^win.* ]]; then
77-
78-
79-
prun spin install
80-
cd /tmp
81-
python -c 'import example_pkg; print(example_pkg.__version__)'

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ classifiers = [
2525
dependencies = [
2626
"click",
2727
"tomli; python_version < '3.11'",
28-
"colorama; platform_system == 'Windows'"
28+
"colorama; platform_system == 'Windows'",
29+
"importlib_metadata >= 7"
2930
]
3031
dynamic = ['version']
3132

spin/cmds/meson.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ def _set_pythonpath(quiet=False):
3636
site_packages = _get_site_packages()
3737
env = os.environ
3838

39+
cfg = get_config()
40+
package = cfg.get("tool.spin.package", None)
41+
if package:
42+
import importlib_metadata
43+
44+
try:
45+
dist = importlib_metadata.Distribution.from_name(package)
46+
if getattr(dist.origin.dir_info, "editable", False):
47+
click.secho(
48+
f"Warning! An editable installation of `{package}` was detected.",
49+
fg="bright_red",
50+
)
51+
click.secho("Spin commands will pick up that version.", fg="bright_red")
52+
click.secho(
53+
f"Try removing the other installation with `pip uninstall {package}`.",
54+
fg="bright_red",
55+
)
56+
except importlib_metadata.PackageNotFoundError:
57+
pass
58+
3959
if "PYTHONPATH" in env:
4060
env["PYTHONPATH"] = f"{site_packages}{os.pathsep}{env['PYTHONPATH']}"
4161
else:

0 commit comments

Comments
 (0)