Commit 31328335 authored by Felix Krull's avatar Felix Krull

Ensure shebang lines are correctly quoted if sys.executable contains spaces.

Fixes issue #398. This only special-cases sys.executable; if the --executable
parameter is used, paths with spaces have to be quoted there explicitly. While
this change also applies to Unix platforms, if sys.executable contains spaces
on Unix, any shebang lines created with it aren't going to work either way,
whether they are quoted or not.
parent 6f79ca2b
...@@ -1985,9 +1985,18 @@ class CommandSpec(list): ...@@ -1985,9 +1985,18 @@ class CommandSpec(list):
def as_header(self): def as_header(self):
return self._render(self + list(self.options)) return self._render(self + list(self.options))
@staticmethod
def _strip_quotes(item):
_QUOTES = '"\''
for q in _QUOTES:
if item.startswith(q) and item.endswith(q):
return item[1:-1]
return item
@staticmethod @staticmethod
def _render(items): def _render(items):
cmdline = subprocess.list2cmdline(items) cmdline = subprocess.list2cmdline(
CommandSpec._strip_quotes(item.strip()) for item in items)
return '#!' + cmdline + '\n' return '#!' + cmdline + '\n'
# For pbr compat; will be removed in a future version. # For pbr compat; will be removed in a future version.
......
from distutils import log from distutils import log
import distutils.command.install_scripts as orig import distutils.command.install_scripts as orig
import os import os
import sys
from pkg_resources import Distribution, PathMetadata, ensure_directory from pkg_resources import Distribution, PathMetadata, ensure_directory
...@@ -37,6 +38,10 @@ class install_scripts(orig.install_scripts): ...@@ -37,6 +38,10 @@ class install_scripts(orig.install_scripts):
if is_wininst: if is_wininst:
exec_param = "python.exe" exec_param = "python.exe"
writer = ei.WindowsScriptWriter writer = ei.WindowsScriptWriter
if exec_param == sys.executable:
# In case the path to the Python executable contains a space, wrap
# it so it's not split up.
exec_param = [exec_param]
# resolve the writer to the environment # resolve the writer to the environment
writer = writer.best() writer = writer.best()
cmd = writer.command_spec_class.best().from_param(exec_param) cmd = writer.command_spec_class.best().from_param(exec_param)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment