diff options
author | becker33 <becker33@llnl.gov> | 2016-08-01 14:10:59 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-01 14:10:59 -0700 |
commit | 14d861a41c86dc53b8ba94a0edad545304e5121e (patch) | |
tree | 0efa89ef1cfbb061de832fbf2c15dc018d820571 /lib | |
parent | b892cebe8ae1b65e45a14c8b4ffb33357237fbb8 (diff) | |
parent | 5b79f0d04a766a7df459dff51ccff93a6edaf8ec (diff) | |
download | spack-14d861a41c86dc53b8ba94a0edad545304e5121e.tar.gz spack-14d861a41c86dc53b8ba94a0edad545304e5121e.tar.bz2 spack-14d861a41c86dc53b8ba94a0edad545304e5121e.tar.xz spack-14d861a41c86dc53b8ba94a0edad545304e5121e.zip |
Merge pull request #1414 from adamjstewart/fixes/filter_file
Fix backup=True for filter_file
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/llnl/util/filesystem.py | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/lib/spack/llnl/util/filesystem.py b/lib/spack/llnl/util/filesystem.py index 553ec1e4b5..2478f5c159 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) |