mirror of
https://github.com/mail-in-a-box/mailinabox.git
synced 2026-03-31 21:17:23 +02:00
initial commit of some preliminary notes
This commit is contained in:
37
tools/editconf.py
Executable file
37
tools/editconf.py
Executable file
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import sys, re
|
||||
|
||||
# sanity check
|
||||
if len(sys.argv) < 3:
|
||||
print("usage: python3 editconf.py /etc/file.conf NAME=VAL [NAME=VAL ...]")
|
||||
sys.exit(1)
|
||||
|
||||
# parse command line arguments
|
||||
filename = sys.argv[1]
|
||||
settings = sys.argv[2:]
|
||||
|
||||
# create the new config file in memory
|
||||
found = set()
|
||||
buf = ""
|
||||
for line in open(filename):
|
||||
for i in range(len(settings)):
|
||||
name, val = settings[i].split("=", 1)
|
||||
if re.match("\s*" + re.escape(name) + "\s*=", line):
|
||||
buf += "#" + line
|
||||
if i in found: break # we've already set the directive
|
||||
buf += name + "=" + val + "\n"
|
||||
found.add(i)
|
||||
break
|
||||
else:
|
||||
# did not match any setting name
|
||||
buf += line
|
||||
|
||||
for i in range(len(settings)):
|
||||
if i not in found:
|
||||
buf += settings[i] + "\n"
|
||||
|
||||
# Write out the new file.
|
||||
with open(filename, "w") as f:
|
||||
f.write(buf)
|
||||
|
||||
Reference in New Issue
Block a user