+ def __update_config_file(self, filename, newvars):
+
+ # read old file, apply changes
+ f = open(filename, "r")
+ data = f.read()
+ f.close()
+ todo = newvars.copy()
+ changed = False
+ for m in self.config_var_re.finditer(data):
+ (key, val) = m.groups()
+ newval = todo.pop(key, None)
+ if newval != None:
+ data = data[:m.start(2)] + str(newval) + data[m.end(2):]
+ changed = True
+ for (newkey, newval) in todo.items():
+ data += "%s=%s\n" % (newkey, newval)
+ changed = True
+
+ if not changed:
+ return
+
+ # write new file
+ newfile = filename + ".new"
+ f = open(newfile, "w")
+ f.write(data)
+ f.close()
+
+ # 'copy' original file, rename new to original
+ backup = filename + ".old"
+ try:
+ os.unlink(backup)
+ except OSError, ex:
+ if ex.errno != errno.ENOENT:
+ raise
+ os.link(filename, backup)
+ os.rename(newfile, filename)
+