diff options
author | Gregory Lee <lee218@llnl.gov> | 2019-11-01 03:39:45 -0700 |
---|---|---|
committer | Todd Gamblin <tgamblin@llnl.gov> | 2019-11-01 03:50:38 -0700 |
commit | 2a9d6b9fbf5a2694dccd77fd75bb6517f6430dc1 (patch) | |
tree | dcdd619d76f2f9f1b260275c1adc26e9741b6e7d | |
parent | eb286bb80f6e4422f373a6a16cffa6342f6d8043 (diff) | |
download | spack-2a9d6b9fbf5a2694dccd77fd75bb6517f6430dc1.tar.gz spack-2a9d6b9fbf5a2694dccd77fd75bb6517f6430dc1.tar.bz2 spack-2a9d6b9fbf5a2694dccd77fd75bb6517f6430dc1.tar.xz spack-2a9d6b9fbf5a2694dccd77fd75bb6517f6430dc1.zip |
sbang: use utf-8 for encoding when patching (#13490)
This fixes a UnicodeDecodeError in the sbang patching function.
-rw-r--r-- | lib/spack/spack/hooks/sbang.py | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/lib/spack/spack/hooks/sbang.py b/lib/spack/spack/hooks/sbang.py index 6987815bc8..422f240ce8 100644 --- a/lib/spack/spack/hooks/sbang.py +++ b/lib/spack/spack/hooks/sbang.py @@ -6,6 +6,7 @@ import os import stat import re +import sys import llnl.util.tty as tty @@ -33,8 +34,12 @@ def shebang_too_long(path): def filter_shebang(path): """Adds a second shebang line, using sbang, at the beginning of a file.""" - with open(path, 'r') as original_file: + with open(path, 'rb') as original_file: original = original_file.read() + if sys.version_info >= (2, 7): + original = original.decode(encoding='UTF-8') + else: + original = original.decode('UTF-8') # This line will be prepended to file new_sbang_line = '#!/bin/bash %s/bin/sbang\n' % spack.paths.prefix @@ -61,9 +66,13 @@ def filter_shebang(path): saved_mode = st.st_mode os.chmod(path, saved_mode | stat.S_IWRITE) - with open(path, 'w') as new_file: - new_file.write(new_sbang_line) - new_file.write(original) + with open(path, 'wb') as new_file: + if sys.version_info >= (2, 7): + new_file.write(new_sbang_line.encode(encoding='UTF-8')) + new_file.write(original.encode(encoding='UTF-8')) + else: + new_file.write(new_sbang_line.encode('UTF-8')) + new_file.write(original.encode('UTF-8')) # Restore original permissions. if saved_mode is not None: |