2013-08-21 02:27:32 +00:00
|
|
|
#!/usr/bin/python3
|
2014-06-08 21:23:06 +00:00
|
|
|
#
|
|
|
|
# This is a helper tool for editing configuration files during the setup
|
|
|
|
# process. The tool is given new values for settings as command-line
|
|
|
|
# arguments. It comments-out existing setting values in the configuration
|
|
|
|
# file and adds new values either after their former location or at the
|
|
|
|
# end.
|
|
|
|
#
|
|
|
|
# The configuration file has settings that look like:
|
|
|
|
#
|
|
|
|
# NAME=VALUE
|
|
|
|
#
|
|
|
|
# If the -s option is given, then space becomes the delimiter, i.e.:
|
|
|
|
#
|
|
|
|
# NAME VALUE
|
|
|
|
#
|
|
|
|
# If the -w option is given, then setting lines continue onto following
|
|
|
|
# lines while the lines start with whitespace, e.g.:
|
|
|
|
#
|
|
|
|
# NAME VAL
|
|
|
|
# UE
|
2013-08-21 02:27:32 +00:00
|
|
|
|
|
|
|
import sys, re
|
|
|
|
|
|
|
|
# sanity check
|
|
|
|
if len(sys.argv) < 3:
|
2014-06-08 21:23:06 +00:00
|
|
|
print("usage: python3 editconf.py /etc/file.conf [-s] [-w] [-t] NAME=VAL [NAME=VAL ...]")
|
2013-08-21 02:27:32 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# parse command line arguments
|
|
|
|
filename = sys.argv[1]
|
|
|
|
settings = sys.argv[2:]
|
|
|
|
|
2013-08-23 15:59:28 +00:00
|
|
|
delimiter = "="
|
|
|
|
delimiter_re = r"\s*=\s*"
|
2014-06-08 21:23:06 +00:00
|
|
|
folded_lines = False
|
|
|
|
testing = False
|
|
|
|
while settings[0][0] == "-" and settings[0] != "--":
|
|
|
|
opt = settings.pop(0)
|
|
|
|
if opt == "-s":
|
|
|
|
# Space is the delimiter
|
|
|
|
delimiter = " "
|
|
|
|
delimiter_re = r"\s+"
|
|
|
|
elif opt == "-w":
|
|
|
|
folded_lines = True
|
|
|
|
elif opt == "-t":
|
|
|
|
testing = True
|
|
|
|
else:
|
|
|
|
print("Invalid option.")
|
|
|
|
sys.exit(1)
|
2013-08-23 15:59:28 +00:00
|
|
|
|
2013-08-21 02:27:32 +00:00
|
|
|
# create the new config file in memory
|
2014-06-08 21:23:06 +00:00
|
|
|
|
2013-08-21 02:27:32 +00:00
|
|
|
found = set()
|
|
|
|
buf = ""
|
2014-06-08 21:23:06 +00:00
|
|
|
input_lines = list(open(filename))
|
|
|
|
|
|
|
|
while len(input_lines) > 0:
|
|
|
|
line = input_lines.pop(0)
|
|
|
|
|
|
|
|
# If this configuration file uses folded lines, append any folded lines
|
|
|
|
# into our input buffer.
|
|
|
|
if folded_lines and line[0] not in ("#", " ", ""):
|
|
|
|
while len(input_lines) > 0 and input_lines[0][0] in " \t":
|
|
|
|
line += input_lines.pop(0)
|
|
|
|
|
|
|
|
# See if this line is for any settings passed on the command line.
|
2013-08-21 02:27:32 +00:00
|
|
|
for i in range(len(settings)):
|
2014-06-08 21:23:06 +00:00
|
|
|
# Check that this line contain this setting from the command-line arguments.
|
2013-08-21 02:27:32 +00:00
|
|
|
name, val = settings[i].split("=", 1)
|
2014-07-07 11:23:31 +00:00
|
|
|
m = re.match("(\s*)(#\s*)?" + re.escape(name) + delimiter_re + "(.*?)\s*$", line, re.S)
|
2014-06-08 21:23:06 +00:00
|
|
|
if not m: continue
|
2014-07-07 11:23:31 +00:00
|
|
|
indent, is_comment, existing_val = m.groups()
|
2014-06-08 21:23:06 +00:00
|
|
|
|
|
|
|
# If this is already the setting, do nothing.
|
2014-07-07 11:23:31 +00:00
|
|
|
if is_comment is None and existing_val == val:
|
2014-06-08 21:23:06 +00:00
|
|
|
buf += line
|
2013-08-21 02:27:32 +00:00
|
|
|
found.add(i)
|
|
|
|
break
|
2014-06-08 21:23:06 +00:00
|
|
|
|
|
|
|
# comment-out the existing line (also comment any folded lines)
|
2014-07-07 11:23:31 +00:00
|
|
|
if is_comment is None:
|
|
|
|
buf += "#" + line.rstrip().replace("\n", "\n#") + "\n"
|
|
|
|
else:
|
|
|
|
# the line is already commented, pass it through
|
|
|
|
buf += line
|
2014-06-08 21:23:06 +00:00
|
|
|
|
|
|
|
# if this option oddly appears more than once, don't add the setting again
|
|
|
|
if i in found:
|
|
|
|
break
|
|
|
|
|
|
|
|
# add the new setting
|
2014-07-07 11:23:31 +00:00
|
|
|
buf += indent + name + delimiter + val + "\n"
|
2014-06-08 21:23:06 +00:00
|
|
|
|
|
|
|
# note that we've applied this option
|
|
|
|
found.add(i)
|
|
|
|
|
|
|
|
break
|
2013-08-21 02:27:32 +00:00
|
|
|
else:
|
2013-08-21 13:37:33 +00:00
|
|
|
# If did not match any setting names, pass this line through.
|
2013-08-21 02:27:32 +00:00
|
|
|
buf += line
|
|
|
|
|
2013-08-21 13:37:33 +00:00
|
|
|
# Put any settings we didn't see at the end of the file.
|
2013-08-21 02:27:32 +00:00
|
|
|
for i in range(len(settings)):
|
|
|
|
if i not in found:
|
2013-08-23 15:59:28 +00:00
|
|
|
name, val = settings[i].split("=", 1)
|
|
|
|
buf += name + delimiter + val + "\n"
|
2013-08-21 02:27:32 +00:00
|
|
|
|
2014-06-08 21:23:06 +00:00
|
|
|
if not testing:
|
|
|
|
# Write out the new file.
|
|
|
|
with open(filename, "w") as f:
|
|
|
|
f.write(buf)
|
|
|
|
else:
|
|
|
|
# Just print the new file to stdout.
|
|
|
|
print(buf)
|