From a60715e710314bd62f20fde2515b4366b2727869 Mon Sep 17 00:00:00 2001 From: Anders Ingemann Date: Thu, 21 Nov 2013 16:45:29 +0100 Subject: [PATCH] Simplify sed_i by using fileinput module This also fixes the problem with the owner:group being set to root --- common/tools.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/common/tools.py b/common/tools.py index b6b2547..47a14b0 100644 --- a/common/tools.py +++ b/common/tools.py @@ -44,17 +44,7 @@ def log_call(command, stdin=None): def sed_i(file_path, pattern, subst): - from tempfile import mkstemp - from shutil import move - from os import close - import os + import fileinput import re - temp_fd, temp_path = mkstemp() - mode = os.stat(file_path).st_mode - 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) + for line in fileinput.input(files=file_path, inplace=True): + print re.sub(pattern, subst, line),