From 5b79f0d04a766a7df459dff51ccff93a6edaf8ec Mon Sep 17 00:00:00 2001 From: "Adam J. Stewart" Date: Mon, 1 Aug 2016 15:35:02 -0500 Subject: Fix backup=True for filter_file --- lib/spack/llnl/util/filesystem.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'lib') 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) -- cgit v1.2.3-60-g2f50