def test_get_script_header(self): expected = '#!%s\n' % nt_quote_arg(os.path.normpath(sys.executable)) assert get_script_header('#!/usr/local/bin/python') == expected expected = '#!%s -x\n' % nt_quote_arg(os.path.normpath(sys.executable)) assert get_script_header('#!/usr/bin/python -x') == expected candidate = get_script_header('#!/usr/bin/python', executable=self.non_ascii_exe) assert candidate == '#!%s -x\n' % self.non_ascii_exe candidate = get_script_header('#!/usr/bin/python', executable=self.exe_with_spaces) assert candidate == '#!"%s"\n' % self.exe_with_spaces
def test_get_script_header(self): expected = '#!%s\n' % nt_quote_arg(os.path.normpath(sys.executable)) assert get_script_header('#!/usr/local/bin/python') == expected expected = '#!%s -x\n' % nt_quote_arg(os.path.normpath( sys.executable)) assert get_script_header('#!/usr/bin/python -x') == expected candidate = get_script_header('#!/usr/bin/python', executable=self.non_ascii_exe) assert candidate == '#!%s -x\n' % self.non_ascii_exe candidate = get_script_header('#!/usr/bin/python', executable=self.exe_with_spaces) assert candidate == '#!"%s"\n' % self.exe_with_spaces
def test_get_script_header(self): if not sys.platform.startswith('java') or not is_sh(sys.executable): # This test is for non-Jython platforms expected = '#!%s\n' % nt_quote_arg(os.path.normpath(sys.executable)) assert get_script_header('#!/usr/local/bin/python') == expected expected = '#!%s -x\n' % nt_quote_arg(os.path.normpath(sys.executable)) assert get_script_header('#!/usr/bin/python -x') == expected candidate = get_script_header('#!/usr/bin/python', executable=self.non_ascii_exe) assert candidate == '#!%s -x\n' % self.non_ascii_exe candidate = get_script_header('#!/usr/bin/python', executable=self.exe_with_spaces) assert candidate == '#!"%s"\n' % self.exe_with_spaces
def test_get_script_header(self): if not sys.platform.startswith('java') or not is_sh(sys.executable): # This test is for non-Jython platforms expected = '#!%s\n' % nt_quote_arg(os.path.normpath(sys.executable)) self.assertEqual(get_script_header('#!/usr/local/bin/python'), expected) expected = '#!%s -x\n' % nt_quote_arg(os.path.normpath(sys.executable)) self.assertEqual(get_script_header('#!/usr/bin/python -x'), expected) self.assertEqual(get_script_header('#!/usr/bin/python', executable=self.non_ascii_exe), '#!%s -x\n' % self.non_ascii_exe) candidate = get_script_header('#!/usr/bin/python', executable=self.exe_with_spaces) self.assertEqual(candidate, '#!"%s"\n' % self.exe_with_spaces)
def get_script_header(script_text, executable=easy_install.sys_executable, wininst=False): from distutils.command.build_scripts import first_line_re first, rest = (script_text+'\n').split('\n',1) match = first_line_re.match(first) options = '' if match: script_text = rest options = match.group(1) or '' if options: options = ' '+options if wininst: executable = "python.exe" else: executable = easy_install.nt_quote_arg(executable) if options.find('-S') == -1: options += ' -S' shbang = "#!%(executable)s%(options)s\n" % locals() shbang += ("import sys, os\n" "join, dirname, abspath = os.path.join, os.path.dirname, os.path.abspath\n" "site_dirs = [join(dirname(dirname(abspath(__file__))), 'lib', 'python%s.%s' % tuple(sys.version_info[:2])), join(dirname(dirname(abspath(__file__))), 'lib', 'python')]\n" "sys.path[0:0] = site_dirs\n" "import site\n" "[site.addsitedir(d) for d in site_dirs]\n") return shbang
def test_get_script_header(self): expected = '#!%s\n' % nt_quote_arg(os.path.normpath(sys.executable)) actual = ScriptWriter.get_script_header('#!/usr/local/bin/python') assert actual == expected expected = '#!%s -x\n' % nt_quote_arg(os.path.normpath(sys.executable)) actual = ScriptWriter.get_script_header('#!/usr/bin/python -x') assert actual == expected actual = ScriptWriter.get_script_header('#!/usr/bin/python', executable=self.non_ascii_exe) expected = '#!%s -x\n' % self.non_ascii_exe assert actual == expected actual = ScriptWriter.get_script_header('#!/usr/bin/python', executable='"'+self.exe_with_spaces+'"') expected = '#!"%s"\n' % self.exe_with_spaces assert actual == expected
def test_get_script_header_jython_workaround(self, tmpdir): # Create a mock sys.executable that uses a shebang line header = DALS(""" #!/usr/bin/python # -*- coding: utf-8 -*- """) exe = tmpdir / 'exe.py' with exe.open('w') as f: f.write(header) exe = ei.nt_quote_arg(os.path.normpath(str(exe))) # Make sure Windows paths are quoted properly before they're sent # through shlex.split by get_script_header executable = '"%s"' % exe if os.path.splitdrive(exe)[0] else exe header = ei.ScriptWriter.get_script_header('#!/usr/local/bin/python', executable=executable) assert header == '#!/usr/bin/env %s\n' % exe expect_out = 'stdout' if sys.version_info < (2,7) else 'stderr' with contexts.quiet() as (stdout, stderr): # When options are included, generate a broken shebang line # with a warning emitted candidate = ei.ScriptWriter.get_script_header('#!/usr/bin/python -x', executable=executable) assert candidate == '#!%s -x\n' % exe output = locals()[expect_out] assert 'Unable to adapt shebang line' in output.getvalue() with contexts.quiet() as (stdout, stderr): candidate = ei.ScriptWriter.get_script_header('#!/usr/bin/python', executable=self.non_ascii_exe) assert candidate == '#!%s -x\n' % self.non_ascii_exe output = locals()[expect_out] assert 'Unable to adapt shebang line' in output.getvalue()
def test_get_script_header_jython_workaround(self, tmpdir): # Create a mock sys.executable that uses a shebang line header = DALS(""" #!/usr/bin/python # -*- coding: utf-8 -*- """) exe = tmpdir / 'exe.py' with exe.open('w') as f: f.write(header) exe = ei.nt_quote_arg(os.path.normpath(str(exe))) # Make sure Windows paths are quoted properly before they're sent # through shlex.split by get_script_header executable = '"%s"' % exe if os.path.splitdrive(exe)[0] else exe header = ei.ScriptWriter.get_script_header('#!/usr/local/bin/python', executable=executable) assert header == '#!/usr/bin/env %s\n' % exe expect_out = 'stdout' if sys.version_info < (2, 7) else 'stderr' with contexts.quiet() as (stdout, stderr): # When options are included, generate a broken shebang line # with a warning emitted candidate = ei.ScriptWriter.get_script_header( '#!/usr/bin/python -x', executable=executable) assert candidate == '#!%s -x\n' % exe output = locals()[expect_out] assert 'Unable to adapt shebang line' in output.getvalue() with contexts.quiet() as (stdout, stderr): candidate = ei.ScriptWriter.get_script_header( '#!/usr/bin/python', executable=self.non_ascii_exe) assert candidate == '#!%s -x\n' % self.non_ascii_exe output = locals()[expect_out] assert 'Unable to adapt shebang line' in output.getvalue()
def test_get_script_header_args(self): expected = '#!%s -x\n' % ei.nt_quote_arg( os.path.normpath(sys.executable)) actual = ei.ScriptWriter.get_header('#!/usr/bin/python -x') assert actual == expected
def test_get_script_header_args(self): expected = '#!%s -x\n' % ei.nt_quote_arg(os.path.normpath (sys.executable)) actual = ei.ScriptWriter.get_script_header('#!/usr/bin/python -x') assert actual == expected
def as_requirement(self): return 'spec' WANTED = DALS(""" #!%s # EASY-INSTALL-ENTRY-SCRIPT: 'spec','console_scripts','name' __requires__ = 'spec' import sys from pkg_resources import load_entry_point if __name__ == '__main__': sys.exit( load_entry_point('spec', 'console_scripts', 'name')() ) """) % nt_quote_arg(fix_jython_executable(sys.executable, "")) SETUP_PY = DALS(""" from setuptools import setup setup(name='foo') """) class TestEasyInstallTest: def test_install_site_py(self): dist = Distribution() cmd = easy_install(dist) cmd.sitepy_installed = False cmd.install_dir = tempfile.mkdtemp() try:
def as_requirement(self): return 'spec' WANTED = """\ #!%s # EASY-INSTALL-ENTRY-SCRIPT: 'spec','console_scripts','name' __requires__ = 'spec' import sys from pkg_resources import load_entry_point if __name__ == '__main__': sys.exit( load_entry_point('spec', 'console_scripts', 'name')() ) """ % nt_quote_arg(fix_jython_executable(sys.executable, "")) SETUP_PY = """\ from setuptools import setup setup(name='foo') """ class TestEasyInstallTest(unittest.TestCase): def test_install_site_py(self): dist = Distribution() cmd = easy_install(dist) cmd.sitepy_installed = False cmd.install_dir = tempfile.mkdtemp() try:
def prep_script(cls, template): python_exe = nt_quote_arg(sys.executable) return template % locals()