From 4e7ab1018de181bdab881450e9bde04e3f8324dd Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Tue, 9 Jan 2024 18:38:34 -0800 Subject: [PATCH 1/3] Warn user when trying to `spin run test.py` They are probably looking for `spin run python test.py` Closes #147 --- .github/workflows/test.sh | 12 ++++++++++++ spin/cmds/meson.py | 21 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.sh b/.github/workflows/test.sh index e0dd112..ca9c41a 100644 --- a/.github/workflows/test.sh +++ b/.github/workflows/test.sh @@ -22,12 +22,14 @@ if [[ ${SPIN_PYTHONPATH} == "\$PYTHONPATH" ]]; then echo "Expected Python path, but got $SPIN_PYTHONPATH instead" exit 1 fi + echo -e "${MAGENTA}Does \$PYTHONPATH contains site-packages?${NORMAL}" if [[ ${SPIN_PYTHONPATH} == *"site-packages" ]]; then echo "Yes" else echo "No; it is $SPIN_PYTHONPATH" fi + echo -e "${MAGENTA}Does \`spin run\` redirect only command output to stdout?${NORMAL}" # Once we're on Python >3.11, can replace syspath manipulation below with -P flag to Python VERSION=$(spin run python -c 'import sys; del sys.path[0]; import example_pkg; print(example_pkg.__version__)') @@ -38,6 +40,16 @@ else exit 1 fi +echo -e "${MAGENTA}Does \`spin run foo.py\` warn that \`spin run python foo.py\` is correct?${NORMAL}" +OUT=$( touch __foo.py && spin run __foo.py || true ) +rm __foo.py +if [[ $OUT == *"Did you mean to call"* ]]; then + echo "Yes" +else + echo "No, output is: $OUT" + exit 1 +fi + prun spin test echo -e "${MAGENTA}Running \`spin test\`, but with PYTHONPATH set${NORMAL}" PYTHONPATH=./tmp spin test diff --git a/spin/cmds/meson.py b/spin/cmds/meson.py index 588a18b..e956c95 100644 --- a/spin/cmds/meson.py +++ b/spin/cmds/meson.py @@ -1,4 +1,5 @@ import contextlib +import copy import json import os import shutil @@ -496,15 +497,29 @@ def run(ctx, args): is_posix = sys.platform in ("linux", "darwin") shell = len(args) == 1 + cmd_args = copy.copy(args) if shell: - args = args[0] + cmd_args = args[0] if shell and not is_posix: # On Windows, we're going to try to use bash - args = ["bash", "-c", args] + cmd_args = ["bash", "-c", args] _set_pythonpath(quiet=True) - _run(args, echo=False, shell=shell) + p = _run(cmd_args, echo=False, shell=shell, sys_exit=False) + + # Is the user trying to run a Python script, without calling the Python interpreter? + executable = args[0] + if ( + (p.returncode != 0) + and args[0].endswith(".py") + and os.path.exists(executable) + and (not os.access(executable, os.X_OK)) + ): + click.secho( + f"Did you mean to call `spin run python {' '.join(args)}`?", fg="bright_red" + ) + sys.exit(p.returncode) @click.command() From f318f7979335dd4fa75a36a1f7afeb5f0ed53076 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 10 Jan 2024 14:06:08 -0800 Subject: [PATCH 2/3] Fix shell invocation on Windows --- spin/cmds/meson.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/spin/cmds/meson.py b/spin/cmds/meson.py index e956c95..7e51ff4 100644 --- a/spin/cmds/meson.py +++ b/spin/cmds/meson.py @@ -500,10 +500,9 @@ def run(ctx, args): cmd_args = copy.copy(args) if shell: cmd_args = args[0] - - if shell and not is_posix: - # On Windows, we're going to try to use bash - cmd_args = ["bash", "-c", args] + if not is_posix: + # On Windows, we're going to try to use bash + cmd_args = ["bash", "-c", cmd_args] _set_pythonpath(quiet=True) p = _run(cmd_args, echo=False, shell=shell, sys_exit=False) From 4d13ef97575b08fe8660e6876924ddc485320d90 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 10 Jan 2024 14:51:48 -0800 Subject: [PATCH 3/3] Skip test on Windows --- .github/workflows/test.sh | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.sh b/.github/workflows/test.sh index ca9c41a..4a8b6c4 100644 --- a/.github/workflows/test.sh +++ b/.github/workflows/test.sh @@ -40,14 +40,18 @@ else exit 1 fi -echo -e "${MAGENTA}Does \`spin run foo.py\` warn that \`spin run python foo.py\` is correct?${NORMAL}" -OUT=$( touch __foo.py && spin run __foo.py || true ) -rm __foo.py -if [[ $OUT == *"Did you mean to call"* ]]; then - echo "Yes" -else - echo "No, output is: $OUT" - exit 1 +if [[ $PLATFORM == linux || $PLATFORM == darwin ]]; then + # Detecting whether a file is executable is not that easy on Windows, + # as it seems to take into consideration whether that file is associated as an executable. + echo -e "${MAGENTA}Does \`spin run foo.py\` warn that \`spin run python foo.py\` is correct?${NORMAL}" + OUT=$( touch __foo.py && spin run __foo.py || true ) + rm __foo.py + if [[ $OUT == *"Did you mean to call"* ]]; then + echo "Yes" + else + echo "No, output is: $OUT" + exit 1 + fi fi prun spin test