Skip to content

Instantly share code, notes, and snippets.

@eubnara
Last active December 11, 2017 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eubnara/a976b8d66d4ca527668b86b795c80c57 to your computer and use it in GitHub Desktop.
Save eubnara/a976b8d66d4ca527668b86b795c80c57 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import re
import sys
if 2 != len(sys.argv):
print "Usage: %s <filename>" % sys.argv[0]
exit(1)
want_to_set = {'net.ipv4.ip_forward': '1'}
# get currently set variables
def get_custom_variables(filename):
custom_var = {}
f = open(filename)
while True:
line = f.readline()
if not line:
break
line = line.strip()
if re.match('^# eub', line) or re.match('^#.*', line):
continue
splitted = line.split('=')
if 2 == len(splitted):
(key, value) = [i.strip() for i in splitted]
custom_var[key] = value
f.close()
return custom_var
# add currently set variables
for key, value in get_custom_variables(sys.argv[1]).iteritems():
if key in want_to_set:
continue
else:
want_to_set[key] = value
f = open(sys.argv[1])
while True:
line = f.readline()
if not line:
break
line = line.strip()
# when there is a newline only
if 0 == len(line):
print
continue
if re.match('^# eub', line):
break
# do not modify comments
if re.match('^#.*', line):
print line
else:
splitted = line.split('=')
if 2 == len(splitted):
key = splitted[0].strip()
if key in want_to_set:
print "#%s" % line
else:
print line
f.close()
print "# eub"
for k, v in want_to_set.iteritems():
print "%s=%s" % (k, v)
@eubnara
Copy link
Author

eubnara commented Dec 11, 2017

overwrite default settings with custom settings which are defined after "# eub" comment

@eubnara
Copy link
Author

eubnara commented Dec 11, 2017

The reason why I don't read a file reversely is that the "# eub" comment(delimiter) may not exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment