mirror of
https://github.com/kevingruesser/bootstrap-vz.git
synced 2025-08-24 07:26:29 +00:00
Simplify sed_i by using fileinput module
This also fixes the problem with the owner:group being set to root
This commit is contained in:
parent
2b05fe977d
commit
a60715e710
1 changed files with 3 additions and 13 deletions
|
@ -44,17 +44,7 @@ def log_call(command, stdin=None):
|
||||||
|
|
||||||
|
|
||||||
def sed_i(file_path, pattern, subst):
|
def sed_i(file_path, pattern, subst):
|
||||||
from tempfile import mkstemp
|
import fileinput
|
||||||
from shutil import move
|
|
||||||
from os import close
|
|
||||||
import os
|
|
||||||
import re
|
import re
|
||||||
temp_fd, temp_path = mkstemp()
|
for line in fileinput.input(files=file_path, inplace=True):
|
||||||
mode = os.stat(file_path).st_mode
|
print re.sub(pattern, subst, line),
|
||||||
with open(temp_path, 'w') as new_file:
|
|
||||||
with open(file_path) as old_file:
|
|
||||||
for line in old_file:
|
|
||||||
new_file.write(re.sub(pattern, subst, line))
|
|
||||||
close(temp_fd)
|
|
||||||
os.chmod(temp_path, mode)
|
|
||||||
move(temp_path, file_path)
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue