Skip to content

Commit e9bc73c

Browse files
committed
2 parents 436d734 + 14d861a commit e9bc73c

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

lib/spack/llnl/util/filesystem.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
import stat
3030
import errno
3131
import getpass
32-
from contextlib import contextmanager, closing
32+
from contextlib import contextmanager
3333
import subprocess
34+
import fileinput
3435

3536
import llnl.util.tty as tty
3637

@@ -85,13 +86,14 @@ def groupid_to_group(x):
8586
if ignore_absent and not os.path.exists(filename):
8687
continue
8788

88-
shutil.copy(filename, backup_filename)
89+
# Create backup file. Don't overwrite an existing backup
90+
# file in case this file is being filtered multiple times.
91+
if not os.path.exists(backup_filename):
92+
shutil.copy(filename, backup_filename)
93+
8994
try:
90-
with closing(open(backup_filename)) as infile:
91-
with closing(open(filename, 'w')) as outfile:
92-
for line in infile:
93-
foo = re.sub(regex, repl, line)
94-
outfile.write(foo)
95+
for line in fileinput.input(filename, inplace=True):
96+
print(re.sub(regex, repl, line.rstrip()))
9597
except:
9698
# clean up the original file on failure.
9799
shutil.move(backup_filename, filename)

0 commit comments

Comments
 (0)