#!/usr/bin/env python import os, sys, getopt from string import Template def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print 'envtemplate.py -i -o ' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'envtemplate.py -i -o ' sys.exit() elif opt in ("-i", "--ifile"): inputfile = arg elif opt in ("-o", "--ofile"): outputfile = arg # Populate case-insensitive env var dictionary values = dict() for k in os.environ: v = os.environ.get(k) values[k] = v values[k.lower()] = v # Read template and substitute with open(inputfile) as f: templatestr = f.read() out = Template(templatestr).substitute(values) print "Writing output to {}".format(outputfile) with open(outputfile, "w") as f: f.write(out) if __name__ == "__main__": main(sys.argv[1:])