summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTodd Gamblin <tgamblin@llnl.gov>2014-09-27 20:47:38 -0700
committerTodd Gamblin <tgamblin@llnl.gov>2014-09-27 21:36:42 -0700
commit3bd52678bec6bf7f923d8ebc3073a68d86da7282 (patch)
tree6fead05daf742350dc7f31dceb2c458a0318d043 /lib
parent5cc508393a49c9212092356b9d8bf59eb0b488e4 (diff)
downloadspack-3bd52678bec6bf7f923d8ebc3073a68d86da7282.tar.gz
spack-3bd52678bec6bf7f923d8ebc3073a68d86da7282.tar.bz2
spack-3bd52678bec6bf7f923d8ebc3073a68d86da7282.tar.xz
spack-3bd52678bec6bf7f923d8ebc3073a68d86da7282.zip
MPICH sets MPI compilers to use real compilers and not spack wrappers.
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/llnl/util/filesystem.py27
1 files changed, 23 insertions, 4 deletions
diff --git a/lib/spack/llnl/util/filesystem.py b/lib/spack/llnl/util/filesystem.py
index 3782aefcce..a70111b915 100644
--- a/lib/spack/llnl/util/filesystem.py
+++ b/lib/spack/llnl/util/filesystem.py
@@ -38,7 +38,7 @@ import llnl.util.tty as tty
from spack.util.compression import ALLOWED_ARCHIVE_TYPES
-def filter_file(regex, repl, *filenames):
+def filter_file(regex, repl, *filenames, **kwargs):
"""Like sed, but uses python regular expressions.
Filters every line of file through regex and replaces the file
@@ -49,16 +49,31 @@ def filter_file(regex, repl, *filenames):
return a suitable replacement string. If it is a string, it
can contain ``\1``, ``\2``, etc. to represent back-substitution
as sed would allow.
+
+ Keyword Options:
+ string[=False] If True, treat regex as a plain string.
+ backup[=True] Make a backup files suffixed with ~
+ ignore_absent[=False] Ignore any files that don't exist.
"""
- # Keep callables intact
- if not hasattr(repl, '__call__'):
- # Allow strings to use \1, \2, etc. for replacement, like sed
+ string = kwargs.get('string', False)
+ backup = kwargs.get('backup', True)
+ ignore_absent = kwargs.get('ignore_absent', False)
+
+ # Allow strings to use \1, \2, etc. for replacement, like sed
+ if not callable(repl):
unescaped = repl.replace(r'\\', '\\')
repl = lambda m: re.sub(
r'\\([0-9])', lambda x: m.group(int(x.group(1))), unescaped)
+ if string:
+ regex = re.escape(regex)
+
for filename in filenames:
backup = filename + "~"
+
+ if ignore_absent and not os.path.exists(filename):
+ continue
+
shutil.copy(filename, backup)
try:
with closing(open(backup)) as infile:
@@ -71,6 +86,10 @@ def filter_file(regex, repl, *filenames):
shutil.move(backup, filename)
raise
+ finally:
+ if not backup:
+ shutil.rmtree(backup, ignore_errors=True)
+
def change_sed_delimiter(old_delim, new_delim, *filenames):
"""Find all sed search/replace commands and change the delimiter.