]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Utility to convert JSON template files to YAML
authorSteve Baker <sbaker@redhat.com>
Thu, 22 Nov 2012 19:15:15 +0000 (08:15 +1300)
committerSteve Baker <sbaker@redhat.com>
Mon, 26 Nov 2012 02:11:04 +0000 (15:11 +1300)
Accepts a directory or file path.

Change-Id: Icd51db5484283372a2b5fb8c681b0e88c7a9c1d5

tools/cfn-json2yaml [new file with mode: 0644]

diff --git a/tools/cfn-json2yaml b/tools/cfn-json2yaml
new file mode 100644 (file)
index 0000000..19c6861
--- /dev/null
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+import sys
+import os
+import yaml
+import json
+import re
+from heat.engine import format
+
+def main():
+    path = sys.argv[1]
+    if os.path.isdir(path):
+        convert_directory(path)
+    elif os.path.isfile(path):
+        convert_file()
+    else:
+        print 'File or directory not valid: %s' % path
+
+def convert_file(path):
+    f = open(path, 'r')
+    print json2yaml(f)
+
+def convert_directory(dirpath):
+    for path in os.listdir(dirpath):
+        if not path.endswith('.template') and not path.endswith('.json'):
+            continue
+        yamlpath = re.sub('\..*$', '.yaml', path)
+        print 'Writing to %s' % yamlpath
+        f = open(os.path.join(dirpath, path), 'r')
+        out = open(os.path.join(dirpath, yamlpath), 'w')
+        yml = format.convert_json_to_yaml(f.read())
+        out.write(yml)
+        out.close()
+
+if __name__ == '__main__':
+    main()
\ No newline at end of file