-
Notifications
You must be signed in to change notification settings - Fork 0
/
yamlthingy.py
156 lines (128 loc) · 5 KB
/
yamlthingy.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
YAML Templating Thingy
-- Tim Dousset 2015-04-28
-- Shaun Smekel 2015-05-14
--- Added dynamic environment file imports
"""
import re
import os
import sys
import argparse
import yaml
import time
import datetime
"""
Handles splicing YAML files
"""
class YAMLThingy(object):
def __init__(self, templatefile, marker, environmentPath):
# Create timestamp to inject for template identifier
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
outputfile = '%s.tmp' % templatefile
self._tf = file(templatefile, 'r')
try:
yaml.load(self._tf)
except yaml.YAMLError, exc:
print "Could not parse templatefile YAML"
if hasattr(exc, 'problem_mark'):
mark = exc.problem_mark
print "Error position: (%s:%s)" % (mark.line+1, mark.column+1)
sys.exit(1)
print "templatefile %s is validated." % (templatefile, )
# Init the output file
self._of = file(outputfile, 'w')
# Verify that the pattern occurs.
found = False
self._tf.seek(0)
for line in self._tf.readlines():
self._of.write(line)
# Check for any environmental files
envMatch = re.match('.*%s.*' % (marker, ), line)
if envMatch is not None:
found = True
print "Match is %s" % envMatch.group(1)
if envMatch.group(1) == 'timestamp':
print "Attaching timestamp to property"
# Match indents for valid YAML file
indent = ''
indentmatch = re.match('(\s+)', line)
if indentmatch is not None:
indent = indentmatch.group(0)
newline = '%s "%s"\n' % (indent, timestamp)
self._of.write(newline)
else:
# Check for environment file specified
environmentFilename = '%s%s.yaml' % (environmentPath, envMatch.group(1))
try:
self._cf = file(environmentFilename, 'r')
except IOError, exc:
print "Could not find environment file %s" % environmentFilename
continue
try:
yaml.load(self._cf)
except yaml.YAMLError, exc:
print "Could not parse environment file %s" % environmentFilename
if hasattr(exc, 'problem_mark'):
mark = exc.problem_mark
print "Error position: (%s:%s)" % (mark.line + 1, mark.column + 1)
sys.exit(1)
print "environment file %s is validated." % environmentFilename
# Match indents for valid YAML file
indent = ''
indentmatch = re.match('(\s+)', line)
if indentmatch is not None:
indent = indentmatch.group(0)
self._cf.seek(0)
for contentline in self._cf.readlines():
newline = '%s %s' % (indent, contentline)
self._of.write(newline)
self._of.close()
# Move the temp output file to override the original template
os.rename(outputfile, templatefile)
# if not found:
# print "Did not find the marker '%s' within the template file!" % (marker, )
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--templatefile', dest='templatefile', default=None,
help='Template YAML file.')
parser.add_argument('--marker', dest='marker', default='([a-zA-Z0-9-_]+):\\s#\\1#',
help='Marker identify where to inject environment file contents. \
The default ([a-zA-Z0-9-_]+):\\s#\\1# will match string: #string# \
and will import the contents of string.yaml from the environment directory.')
parser.add_argument('--envdir', dest='envdir', default=None,
help='File containing YAML data to inject.')
# parser.add_argument('--outputfile', dest='outputfile', default=None,
# help='File to write final product out to.')
args = parser.parse_args()
if args.templatefile is not None:
if not os.path.isfile(args.templatefile):
print "Could not find the templatefile you specified."
print args.templatefile
sys.exit(1)
else:
print "You must supply a templatefile (--templatefile)"
parser.print_help()
sys.exit(1)
if args.marker is None or len(args.marker) == 0:
print "You must supply a marker included in the template to show where contentfile should be injected."
sys.exit(1)
if args.envdir is not None:
if not os.path.isdir(args.envdir):
print "Could not find the envdir you specified."
print args.envdir
sys.exit(1)
else:
print "You must supply an envdir (--envdir)"
parser.print_help()
sys.exit(1)
# if args.outputfile is None:
# print "You must supply a outputfile (--outputfile)"
# parser.print_help()
# sys.exit(1)
try:
# thingy = YAMLThingy(args.templatefile, args.marker, args.envdir, args.outputfile)
thingy = YAMLThingy(args.templatefile, args.marker, args.envdir)
except KeyboardInterrupt:
print 'Interrupted. Bailing...'
sys.exit(1)