[Freeipa-devel] [PATCH] don't append values multiple times to configuration files

Rob Crittenden rcritten at redhat.com
Thu Oct 4 13:27:49 UTC 2007


William Jon McCann wrote:
> Hi,
> 
> After playing with the install (repeatedly) I ended up with a lot of
> duplicate values in:
> /etc/sysconfig/dirsrv
> /etc/sysconfig/ipa-kpasswd
> 
> Here is a patch that should fix this.  It modifies the file "in-place"
> and removes lines that matching the key (or commented key) and then
> appends the new key=value.
> 
> Jon

Cool, I've wanted to fix this for a while (and recently aborted a switch 
from open with "a" to "w").

What happens if the file doesn't exist yet? Do we need to wrap the 
fileinput loop in either a try/except or just look to see if the file 
exists first (my vote)?

Something like:

def update_key_val_in_file(filename, key, val):
     if os.path.exists(filename):
         pattern = "^[\s#]*%s\s*=" % re.escape(key)
         p = re.compile(pattern)
         for line in fileinput.input(filename, inplace=1):
             if not p.search(line):
                 sys.stdout.write(line)
         fileinput.close()
     f = open(filename, "a")
     f.write("%s=%s\n" % (key, val))
     f.close()

rob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3245 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://listman.redhat.com/archives/freeipa-devel/attachments/20071004/c33e8dd9/attachment.bin>


More information about the Freeipa-devel mailing list