Esempio n. 1
0
    def test_shebang_handling(self):
        filter_shebangs_in_directory(self.tempdir)

        # Make sure this is untouched
        with open(self.short_shebang, 'r') as f:
            self.assertEqual(f.readline(), short_line)
            self.assertEqual(f.readline(), last_line)

        # Make sure this got patched.
        with open(self.long_shebang, 'r') as f:
            self.assertEqual(f.readline(), sbang_line)
            self.assertEqual(f.readline(), long_line)
            self.assertEqual(f.readline(), last_line)

        # Make sure this got patched.
        with open(self.lua_shebang, 'r') as f:
            self.assertEqual(f.readline(), sbang_line)
            self.assertEqual(f.readline(), lua_line_patched)
            self.assertEqual(f.readline(), last_line)

        # Make sure this is untouched
        with open(self.has_shebang, 'r') as f:
            self.assertEqual(f.readline(), sbang_line)
            self.assertEqual(f.readline(), long_line)
            self.assertEqual(f.readline(), last_line)
Esempio n. 2
0
def test_read_unicode(tmpdir, working_env):
    script_name = 'print_unicode.py'
    # read the unicode back in and see whether things work
    if is_windows:
        script = ex.Executable('%s %s' % (sys.executable, script_name))
    else:
        script = ex.Executable('./%s' % script_name)
    with tmpdir.as_cwd():
        os.environ['LD_LIBRARY_PATH'] = spack.main.spack_ld_library_path
        # make a script that prints some unicode
        with open(script_name, 'w') as f:
            f.write('''#!{0}
from __future__ import print_function
import sys
if sys.version_info < (3, 0, 0):
    reload(sys)
    sys.setdefaultencoding('utf8')
print(u'\\xc3')
'''.format(sys.executable))

        # make it executable
        fs.set_executable(script_name)
        filter_shebangs_in_directory('.', [script_name])

        assert u'\xc3' == script(output=str).strip()
Esempio n. 3
0
    def test_shebang_handling(self):
        filter_shebangs_in_directory(self.tempdir)

        # Make sure this is untouched
        with open(self.short_shebang, 'r') as f:
            self.assertEqual(f.readline(), short_line)
            self.assertEqual(f.readline(), last_line)

        # Make sure this got patched.
        with open(self.long_shebang, 'r') as f:
            self.assertEqual(f.readline(), sbang_line)
            self.assertEqual(f.readline(), long_line)
            self.assertEqual(f.readline(), last_line)

        # Make sure this got patched.
        with open(self.lua_shebang, 'r') as f:
            self.assertEqual(f.readline(), sbang_line)
            self.assertEqual(f.readline(), lua_line_patched)
            self.assertEqual(f.readline(), last_line)

        # Make sure this is untouched
        with open(self.has_shebang, 'r') as f:
            self.assertEqual(f.readline(), sbang_line)
            self.assertEqual(f.readline(), long_line)
            self.assertEqual(f.readline(), last_line)
Esempio n. 4
0
def test_shebang_handling(script_dir, sbang_line):
    sbang.filter_shebangs_in_directory(script_dir.tempdir)

    # Make sure this is untouched
    with open(script_dir.short_shebang, 'r') as f:
        assert f.readline() == short_line
        assert f.readline() == last_line

    # Make sure this got patched.
    with open(script_dir.long_shebang, 'r') as f:
        assert f.readline() == sbang_line
        assert f.readline() == long_line
        assert f.readline() == last_line

    # Make sure this is untouched
    with open(script_dir.nonexec_long_shebang, 'r') as f:
        assert f.readline() == long_line
        assert f.readline() == last_line

    # Make sure this got patched.
    with open(script_dir.lua_shebang, 'r') as f:
        assert f.readline() == sbang_line
        assert f.readline() == lua_line_patched
        assert f.readline() == last_line

    # Make sure this got patched.
    with open(script_dir.luajit_shebang, 'r') as f:
        assert f.readline() == sbang_line
        assert f.readline() == luajit_line_patched
        assert f.readline() == last_line

    # Make sure this got patched.
    with open(script_dir.node_shebang, 'r') as f:
        assert f.readline() == sbang_line
        assert f.readline() == node_line_patched
        assert f.readline() == last_line

    assert filecmp.cmp(script_dir.lua_textbang,
                       os.path.join(script_dir.tempdir, 'lua_in_text'))
    assert filecmp.cmp(script_dir.luajit_textbang,
                       os.path.join(script_dir.tempdir, 'luajit_in_text'))
    assert filecmp.cmp(script_dir.node_textbang,
                       os.path.join(script_dir.tempdir, 'node_in_text'))
    assert filecmp.cmp(script_dir.php_textbang,
                       os.path.join(script_dir.tempdir, 'php_in_text'))

    # Make sure this is untouched
    with open(script_dir.has_sbang, 'r') as f:
        assert f.readline() == sbang_line
        assert f.readline() == long_line
        assert f.readline() == last_line
Esempio n. 5
0
def test_sbang_hook_skips_nonexecutable_blobs(tmpdir):
    # Write a binary blob to non-executable.sh, with a long interpreter "path"
    # consisting of invalid UTF-8. The latter is technically not really necessary for
    # the test, but binary blobs accidentally starting with b'#!' usually do not contain
    # valid UTF-8, so we also ensure that Spack does not attempt to decode as UTF-8.
    contents = b'#!' + b'\x80' * sbang.system_shebang_limit
    file = str(tmpdir.join('non-executable.sh'))
    with open(file, 'wb') as f:
        f.write(contents)

    sbang.filter_shebangs_in_directory(str(tmpdir))

    # Make sure there is no sbang shebang.
    with open(file, 'rb') as f:
        assert b'sbang' not in f.readline()
Esempio n. 6
0
File: sbang.py Progetto: LLNL/spack
def test_shebang_handling(script_dir):
    assert shebang_too_long(script_dir.lua_shebang)
    assert shebang_too_long(script_dir.long_shebang)

    assert not shebang_too_long(script_dir.short_shebang)
    assert not shebang_too_long(script_dir.has_sbang)
    assert not shebang_too_long(script_dir.binary)
    assert not shebang_too_long(script_dir.directory)

    filter_shebangs_in_directory(script_dir.tempdir)

    # Make sure this is untouched
    with open(script_dir.short_shebang, 'r') as f:
        assert f.readline() == short_line
        assert f.readline() == last_line

    # Make sure this got patched.
    with open(script_dir.long_shebang, 'r') as f:
        assert f.readline() == sbang_line
        assert f.readline() == long_line
        assert f.readline() == last_line

    # Make sure this got patched.
    with open(script_dir.lua_shebang, 'r') as f:
        assert f.readline() == sbang_line
        assert f.readline() == lua_line_patched
        assert f.readline() == last_line

    # Make sure this got patched.
    with open(script_dir.node_shebang, 'r') as f:
        assert f.readline() == sbang_line
        assert f.readline() == node_line_patched
        assert f.readline() == last_line

    assert filecmp.cmp(script_dir.lua_textbang,
                       os.path.join(script_dir.tempdir, 'lua_in_text'))
    assert filecmp.cmp(script_dir.node_textbang,
                       os.path.join(script_dir.tempdir, 'node_in_text'))

    # Make sure this is untouched
    with open(script_dir.has_sbang, 'r') as f:
        assert f.readline() == sbang_line
        assert f.readline() == long_line
        assert f.readline() == last_line
Esempio n. 7
0
def test_read_unicode(tmpdir):
    script_name = 'print_unicode.py'

    with tmpdir.as_cwd():

        # make a script that prints some unicode
        with open(script_name, 'w') as f:
            f.write('''#!{0}
from __future__ import print_function
import sys
if sys.version_info < (3, 0, 0):
    reload(sys)
    sys.setdefaultencoding('utf8')
print(u'\\xc3')
'''.format(sys.executable))

        # make it executable
        fs.set_executable(script_name)
        filter_shebangs_in_directory('.', [script_name])

        # read the unicode back in and see whether things work
        script = ex.Executable('./%s' % script_name)
        assert u'\xc3' == script(output=str).strip()