If a man speaks in a forest, and his wife's not there, is he still wrong?

python code to manipulate ini-style config files

I wanted to be able to enable/disable yum repo files from a kickstart script. They are in "ini" file format, e.g.:

[epel]
name=Extra Packages for Enterprise Linux 5 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/5/$basearch
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-5&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
priority=0

I have written some perl code to do this that uses the Config::IniFiles module. But, this is not included in perl core and so would require installation before it could be used.

python includes ini-style file manipulation with the ConfigParser module, so I've written a python script to use it.

Sample usage:

initool --file /etc/yum.repos.d/epel.repo --section epel --option enabled=0
initool --file /etc/yum.repos.d/epel.repo --section epel --del priority

Here's the code:

#!/usr/bin/env python
import ConfigParser
from optparse import OptionParser

def main():
    version = "1.0"
    usage = "usage: %prog [options]"
    parser = OptionParser(usage=usage, version=version )
    parser.add_option("-f", "--file", dest="filename",
                      help="use file FILE", metavar="FILE")
    parser.add_option("-s", "--section", dest="section",
                      help="use section SECTION", metavar="SECTION")
    parser.add_option("-o", "--option", dest="set_options", action="append",
                      help="set OPTION=VALUE", metavar="OPTION=VALUE")
    parser.add_option("-d", "--del", dest="del_options", action="append",
                      help="delete option OPTION", metavar="OPTION")
    (opts, args) = parser.parse_args()

    if opts.set_options is None and opts.del_options is None:
        parser.error("no action specified")
    if opts.filename is None:
        parser.error("no file specified")
    if opts.section is None:
        parser.error("no section specified")

    cfg = ConfigParser.ConfigParser()
    cfg.read(opts.filename)

    if opts.set_options is not None:
        for opt in opts.set_options:
             # opt should be in format NAME=VALUE;
             name, value = opt.split('=')
             cfg.set(opts.section, name, value)

    if opts.del_options is not None:
        for opt in opts.del_options:
             # opt is just NAME
             cfg.remove_option(opts.section, opt)

    cfgfile = open(opts.filename, 'wb')
    cfg.write(cfgfile)
    cfgfile.close()

if __name__ == "__main__":
    main()

Story Options

python code to manipulate ini-style config files | 0 comments
The following comments are owned by whomever posted them. This site is not responsible for what they say.

Topics

  • Home
  • Misc (6/0)
  • Audio (5/0)
  • Linux (21/0)
  • Family (1/0)
  • Fishing Diary (1/0)
  • OpenSolaris (7/0)
  • Computing (11/0)
  • General News (7/0)
  • Chloe (1/0)
  • Emily (2/0)
  • Twins (5/0)
  • Classifieds (2/0)
  • GeekLog (2/0)
  • Project Management (1/0)
  • User Functions






    Lost your password?