Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 35 additions & 33 deletions python/lib/cloudutils/configFileOps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
import re
import os
import tempfile
import shutil
from .utilities import bash
Expand Down Expand Up @@ -59,39 +60,42 @@ def getEntry(self, name, separator="="):
return ""

def save(self):
fp = open(self.fileName, "r")
newLines = []
for line in fp.readlines():
matched = False
for entry in self.entries:
if entry.op == "add":
if entry.separator == "=":
matchString = "^\ *" + entry.name + ".*"
elif entry.separator == " ":
matchString = "^\ *" + entry.name + "\ *" + entry.value
else:
if entry.separator == "=":
matchString = "^\ *" + entry.name + "\ *=\ *" + entry.value
if os.path.exists(self.fileName) and os.path.isfile(self.fileName):
Comment thread
weizhouapache marked this conversation as resolved.
fp = open(self.fileName, "r")
for line in fp.readlines():
matched = False
for entry in self.entries:
if entry.op == "add":
if entry.separator == "=":
matchString = "^\ *" + entry.name + ".*"
elif entry.separator == " ":
matchString = "^\ *" + entry.name + "\ *" + entry.value
else:
matchString = "^\ *" + entry.name + "\ *" + entry.value

match = re.match(matchString, line)
if match is not None:
if entry.op == "add" and entry.separator == "=":
newline = "\n" + entry.name + "=" + entry.value + "\n"
entry.setState("set")
newLines.append(newline)
self.backups.append([line, newline])
matched = True
break
elif entry.op == "rm":
entry.setState("set")
self.backups.append([line, None])
matched = True
break

if not matched:
newLines.append(line)
if entry.separator == "=":
matchString = "^\ *" + entry.name + "\ *=\ *" + entry.value
else:
matchString = "^\ *" + entry.name + "\ *" + entry.value

match = re.match(matchString, line)
if match is not None:
if entry.op == "add" and entry.separator == "=":
newline = "\n" + entry.name + "=" + entry.value + "\n"
entry.setState("set")
newLines.append(newline)
self.backups.append([line, newline])
matched = True
break
elif entry.op == "rm":
entry.setState("set")
self.backups.append([line, None])
matched = True
break

if not matched:
newLines.append(line)

fp.close()

for entry in self.entries:
if entry.getState() != "set":
Expand All @@ -101,8 +105,6 @@ def save(self):
self.backups.append([None, newline])
entry.setState("set")

fp.close()

open(self.fileName, "w").writelines(newLines)

def replace_line(self, startswith,stanza,always_add=False):
Expand Down