# Copyright (C) 2003 Konstantin Korikov
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Process templates.
# This script needed for building chestnut-dialer.
#
import sys
import re
import getopt
import commands
def print_usage(exit_code = 1):
print ("Usage: %s [-o outputfile] [inputfile] " +
"[ PARAM=VALUE | \"PARAM<file\" \"PARAM|command\" ] ...") % sys.argv[0]
sys.exit(exit_code)
infile = None
outfile = None
try: opts, args = getopt.getopt(sys.argv[1:], "o:?h")
except getopt.GetoptError: print_usage()
params = {}
for a in args:
m = re.match(r'^(\w+)=(.*)$', a)
if m:
params.update({m.group(1):m.group(2)})
continue
m = re.match(r'^(\w+)<(.*)$', a)
if m:
try: params.update({m.group(1):open(m.group(2)).read()})
except IOError:
sys.stderr.write("Cannot open input file: %s\n" % m.group(2))
sys.exit(1)
continue
m = re.match(r'^(\w+)\|(.*)$', a)
if m:
params.update({m.group(1):commands.getoutput(m.group(2))})
continue
if infile == None:
infile = a
continue
print_usage()
for o, v in opts:
if o == "-o": outfile = v
elif o in ("-h", "-?"): print_usage(0)
inf = sys.stdin
outf = sys.stdout
if infile != None:
try: inf = open(infile)
except IOError:
sys.stderr.write("Cannot open input file\n")
sys.exit(1)
if outfile != None:
try: outf = open(outfile, 'w')
except IOError:
sys.stderr.write("Cannot open output file\n")
sys.exit(1)
text = inf.read()
for p in params.keys():
text = text.replace("$(%s)" % p, params[p])
outf.write(text)
|