summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAdam J. Stewart <ajstewart426@gmail.com>2016-08-01 15:35:02 -0500
committerAdam J. Stewart <ajstewart426@gmail.com>2016-08-01 15:35:02 -0500
commit5b79f0d04a766a7df459dff51ccff93a6edaf8ec (patch)
treeabab20efaf3af3d8d65fd4138d264099e32e6ecc /lib
parente470478dc1c84b5343b069290d8808cb953d7dfe (diff)
downloadspack-5b79f0d04a766a7df459dff51ccff93a6edaf8ec.tar.gz
spack-5b79f0d04a766a7df459dff51ccff93a6edaf8ec.tar.bz2
spack-5b79f0d04a766a7df459dff51ccff93a6edaf8ec.tar.xz
spack-5b79f0d04a766a7df459dff51ccff93a6edaf8ec.zip
Fix backup=True for filter_file
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/llnl/util/filesystem.py16
1 files changed, 9 insertions, 7 deletions
diff --git a/lib/spack/llnl/util/filesystem.py b/lib/spack/llnl/util/filesystem.py
index 6e4cd338fe..f416fc6fd9 100644
--- a/lib/spack/llnl/util/filesystem.py
+++ b/lib/spack/llnl/util/filesystem.py
@@ -29,8 +29,9 @@ import shutil
import stat
import errno
import getpass
-from contextlib import contextmanager, closing
+from contextlib import contextmanager
import subprocess
+import fileinput
import llnl.util.tty as tty
@@ -85,13 +86,14 @@ def filter_file(regex, repl, *filenames, **kwargs):
if ignore_absent and not os.path.exists(filename):
continue
- shutil.copy(filename, backup_filename)
+ # Create backup file. Don't overwrite an existing backup
+ # file in case this file is being filtered multiple times.
+ if not os.path.exists(backup_filename):
+ shutil.copy(filename, backup_filename)
+
try:
- with closing(open(backup_filename)) as infile:
- with closing(open(filename, 'w')) as outfile:
- for line in infile:
- foo = re.sub(regex, repl, line)
- outfile.write(foo)
+ for line in fileinput.input(filename, inplace=True):
+ print(re.sub(regex, repl, line.rstrip()))
except:
# clean up the original file on failure.
shutil.move(backup_filename, filename)