Commit a361ef49 authored by Jason R. Coombs's avatar Jason R. Coombs

Extract _use_header method

parent fb2860e6
...@@ -20,6 +20,7 @@ from distutils.errors import DistutilsArgError, DistutilsOptionError, \ ...@@ -20,6 +20,7 @@ from distutils.errors import DistutilsArgError, DistutilsOptionError, \
from distutils.command.install import INSTALL_SCHEMES, SCHEME_KEYS from distutils.command.install import INSTALL_SCHEMES, SCHEME_KEYS
from distutils import log, dir_util from distutils import log, dir_util
from distutils.command.build_scripts import first_line_re from distutils.command.build_scripts import first_line_re
from distutils.spawn import find_executable
import sys import sys
import os import os
import zipimport import zipimport
...@@ -2126,8 +2127,8 @@ class WindowsScriptWriter(ScriptWriter): ...@@ -2126,8 +2127,8 @@ class WindowsScriptWriter(ScriptWriter):
blockers = [name + x for x in old] blockers = [name + x for x in old]
yield name + ext, header + script_text, 't', blockers yield name + ext, header + script_text, 't', blockers
@staticmethod @classmethod
def _adjust_header(type_, orig_header): def _adjust_header(cls, type_, orig_header):
""" """
Make sure 'pythonw' is used for gui and and 'python' is used for Make sure 'pythonw' is used for gui and and 'python' is used for
console (regardless of what sys.executable is). console (regardless of what sys.executable is).
...@@ -2138,14 +2139,19 @@ class WindowsScriptWriter(ScriptWriter): ...@@ -2138,14 +2139,19 @@ class WindowsScriptWriter(ScriptWriter):
pattern, repl = repl, pattern pattern, repl = repl, pattern
pattern_ob = re.compile(re.escape(pattern), re.IGNORECASE) pattern_ob = re.compile(re.escape(pattern), re.IGNORECASE)
new_header = pattern_ob.sub(string=orig_header, repl=repl) new_header = pattern_ob.sub(string=orig_header, repl=repl)
if sys.platform == 'win32': return new_header if cls._use_header(new_header) else orig_header
from distutils.spawn import find_executable
@staticmethod
clean_header = new_header[2:-1].strip('"') def _use_header(new_header):
if not find_executable(clean_header): """
# the adjusted version doesn't exist, so return the original Should _adjust_header use the replaced header?
return orig_header
return new_header On non-windows systems, always use. On
Windows systems, only use the replaced header if it resolves
to an executable on the system.
"""
clean_header = new_header[2:-1].strip('"')
return sys.platform != 'win32' or find_executable(clean_header)
class WindowsExecutableLauncherWriter(WindowsScriptWriter): class WindowsExecutableLauncherWriter(WindowsScriptWriter):
......
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