Fix backup=True for filter_file
This commit is contained in:
parent
e470478dc1
commit
5b79f0d04a
@ -29,8 +29,9 @@
|
|||||||
import stat
|
import stat
|
||||||
import errno
|
import errno
|
||||||
import getpass
|
import getpass
|
||||||
from contextlib import contextmanager, closing
|
from contextlib import contextmanager
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import fileinput
|
||||||
|
|
||||||
import llnl.util.tty as tty
|
import llnl.util.tty as tty
|
||||||
|
|
||||||
@ -85,13 +86,14 @@ def groupid_to_group(x):
|
|||||||
if ignore_absent and not os.path.exists(filename):
|
if ignore_absent and not os.path.exists(filename):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# 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)
|
shutil.copy(filename, backup_filename)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with closing(open(backup_filename)) as infile:
|
for line in fileinput.input(filename, inplace=True):
|
||||||
with closing(open(filename, 'w')) as outfile:
|
print(re.sub(regex, repl, line.rstrip()))
|
||||||
for line in infile:
|
|
||||||
foo = re.sub(regex, repl, line)
|
|
||||||
outfile.write(foo)
|
|
||||||
except:
|
except:
|
||||||
# clean up the original file on failure.
|
# clean up the original file on failure.
|
||||||
shutil.move(backup_filename, filename)
|
shutil.move(backup_filename, filename)
|
||||||
|
Loading…
Reference in New Issue
Block a user