summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGregory Lee <lee218@llnl.gov>2019-11-01 03:39:45 -0700
committerTodd Gamblin <tgamblin@llnl.gov>2019-11-01 03:39:45 -0700
commit3bdab6f6868ce60eb596406db02c1d60be491786 (patch)
treeebc08115b52fe714533959af03f05ae1fc35f5e6 /lib
parent390ffb80e74bb474e9b590548a1ccc1cc92d7537 (diff)
downloadspack-3bdab6f6868ce60eb596406db02c1d60be491786.tar.gz
spack-3bdab6f6868ce60eb596406db02c1d60be491786.tar.bz2
spack-3bdab6f6868ce60eb596406db02c1d60be491786.tar.xz
spack-3bdab6f6868ce60eb596406db02c1d60be491786.zip
sbang: use utf-8 for encoding when patching (#13490)
This fixes a UnicodeDecodeError in the sbang patching function.
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/hooks/sbang.py17
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: