forked from Spantree/docker-sugarcrm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envtemplate.py
executable file
·48 lines (39 loc) · 1.09 KB
/
envtemplate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/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 'test.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
# Read all environment variables in and populate a case-insensitive
# dictionary for replacement
values = dict()
for k in os.environ:
v = os.environ.get(k)
values[k] = v
values[k.lower()] = v
# Read the template
ifile = open(inputfile)
templatestr = open(inputfile).read()
ifile.close()
out = Template(templatestr).substitute(values)
print "Writing output to {}".format(outputfile)
print "==============================="
print out
ofile = open(outputfile, "w")
ofile.write(out)
ofile.close()
if __name__ == "__main__":
main(sys.argv[1:])