#!/usr/bin/python # -*- mode: Python; indent-tabs-mode: nil; -*- # # Small convenience helper to substitute arbitrary variables in # a Yum .repo definition file and print the result to stdout. # Author: Michael Schwendt / Licence: GPLv2+ import sys, errno from optparse import OptionParser import yum.parser usage = "Usage: %s [options] [file]" % sys.argv[0] parser = OptionParser(usage=usage) parser.add_option("-s", "--subst", default=[], action='append', help="a 'key:value' pair to substitute") (opts, args) = parser.parse_args() if not len(args): infile = sys.stdin elif len(args)>1: print "ERROR: invalid arguments" sys.exit(errno.EINVAL) else: infile = file( args[0] ) vars = {} for kv in opts.subst: key, val = kv.split(':') vars[key] = val for line in infile.readlines(): sys.stdout.write( yum.parser.varReplace(line,vars) ) infile.close() sys.exit(0)