mirror of
https://github.com/explosion/spaCy.git
synced 2025-08-08 06:04:57 +03:00
Attempt to handle command rewriting on Windows
This handles command rewriting on Windows to ensure the same Python executable is re-used by using the `executable` argument to subprocess.run. This has the advantage that it avoids any need to escape the path of sys.executable. Still needs testing.
This commit is contained in:
parent
e2265f0864
commit
30c90febf1
|
@ -167,12 +167,28 @@ def run_commands(
|
||||||
"""
|
"""
|
||||||
for c in commands:
|
for c in commands:
|
||||||
command: Union[str, List[str]]
|
command: Union[str, List[str]]
|
||||||
|
exe: Optional[str] = None
|
||||||
if is_windows:
|
if is_windows:
|
||||||
# On Windows we don't rewrite the command because there's no
|
|
||||||
# reliable way to split and reassemble it
|
|
||||||
command = c
|
command = c
|
||||||
|
|
||||||
|
# Correct Windows splitting is hard, so this only checks for simple
|
||||||
|
# cases. It will work in ordinary cases, but will miss cases where
|
||||||
|
# the command is like:
|
||||||
|
# "C:\My Programs\python.exe" script.py
|
||||||
|
head, _, tail = c.partition(" ")
|
||||||
|
|
||||||
|
# This doesn't rewrite the command to include sys.executable
|
||||||
|
# because sys.executable might include spaces, quotes, or
|
||||||
|
# something, and need quoting itself. Instead the exe param is used
|
||||||
|
# to directly specify the binary to call.
|
||||||
|
if head in ("python", "python3"):
|
||||||
|
exe = sys.executable
|
||||||
|
if head in ("pip", "pip3"):
|
||||||
|
exe = sys.executable
|
||||||
|
command = "python -m pip " + tail
|
||||||
|
|
||||||
if not silent:
|
if not silent:
|
||||||
print(f"Running command: {c}")
|
print(f"Running command: {command}")
|
||||||
else:
|
else:
|
||||||
command = shlex.split(c, posix=True)
|
command = shlex.split(c, posix=True)
|
||||||
# Not sure if this is needed or a good idea. Motivation: users may often
|
# Not sure if this is needed or a good idea. Motivation: users may often
|
||||||
|
@ -190,7 +206,7 @@ def run_commands(
|
||||||
print(f"Running command: {c}")
|
print(f"Running command: {c}")
|
||||||
|
|
||||||
if not dry:
|
if not dry:
|
||||||
run_command(command, capture=capture)
|
run_command(command, capture=capture, exe=exe)
|
||||||
|
|
||||||
|
|
||||||
def validate_subcommand(
|
def validate_subcommand(
|
||||||
|
|
|
@ -941,6 +941,7 @@ def run_command(
|
||||||
*,
|
*,
|
||||||
stdin: Optional[Any] = None,
|
stdin: Optional[Any] = None,
|
||||||
capture: bool = False,
|
capture: bool = False,
|
||||||
|
exe: str = None,
|
||||||
) -> subprocess.CompletedProcess:
|
) -> subprocess.CompletedProcess:
|
||||||
"""Run a command on the command line as a subprocess. If the subprocess
|
"""Run a command on the command line as a subprocess. If the subprocess
|
||||||
returns a non-zero exit code, a system exit is performed.
|
returns a non-zero exit code, a system exit is performed.
|
||||||
|
@ -952,6 +953,8 @@ def run_command(
|
||||||
sys.exit will be called with the return code. You should use capture=False
|
sys.exit will be called with the return code. You should use capture=False
|
||||||
when you want to turn over execution to the command, and capture=True
|
when you want to turn over execution to the command, and capture=True
|
||||||
when you want to run the command more like a function.
|
when you want to run the command more like a function.
|
||||||
|
exe (Optional[str]): Path to Python executable to use. Passed separately
|
||||||
|
from the command on Windows to avoid quoting issues.
|
||||||
RETURNS (Optional[CompletedProcess]): The process object.
|
RETURNS (Optional[CompletedProcess]): The process object.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -990,6 +993,7 @@ def run_command(
|
||||||
input=stdin,
|
input=stdin,
|
||||||
encoding="utf8",
|
encoding="utf8",
|
||||||
check=False,
|
check=False,
|
||||||
|
executable=exe,
|
||||||
stdout=subprocess.PIPE if capture else None,
|
stdout=subprocess.PIPE if capture else None,
|
||||||
stderr=subprocess.STDOUT if capture else None,
|
stderr=subprocess.STDOUT if capture else None,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user