-
Notifications
You must be signed in to change notification settings - Fork 4
/
mkcfnuserdata.py
executable file
·70 lines (69 loc) · 2.17 KB
/
mkcfnuserdata.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
#!/usr/bin/env python
#
# mkcfnuserdata.py
#
# ATonns Wed May 1 16:51:58 EDT 2013
#
# Copyright 2013 Corsis
# http://www.corsis.com/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
import json
import re
progname = "mkcfnuserdata.py"
if __name__ == '__main__':
# setup arguments
parser = argparse.ArgumentParser(
prog=progname,
description="JSON encodes a file as list of strings "
"and parses it for CloudFormation Ref objects"
)
parser.add_argument(
"filenames",
help="files to encode", nargs='*'
)
args = parser.parse_args()
# prep regular expressions
shebang = re.compile('^#\!.*$')
commentLine = re.compile('^\s*#.*')
blankLine = re.compile('^$')
# iterate on args
for filename in args.filenames:
lines = []
with open(filename, 'r') as f:
done = False
while not done:
# get a line
line = f.readline()
# if null, we're done
if line == '':
break
# skip lines that are not the first line
# and either comment or blank
s = shebang.match(line)
c = commentLine.match(line)
b = blankLine.match(line)
if not s and (c or b):
continue
# add the line to the array
lines.append(line)
# get the json
j = json.dumps(lines, indent=1)
# do a little templating for CloudFormation Refs
print re.sub(
r'CFNREF_([a-zA-Z_0-9:]+)',
r'", { "Ref": "\1" }, "',
j
)