forked from StackStorm/st2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json2yaml.py
executable file
·92 lines (77 loc) · 2.93 KB
/
json2yaml.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
#!/usr/bin/env python
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
"""
A utility script which sends test messages to a queue.
"""
import argparse
import fnmatch
try:
import simplejson as json
except ImportError:
import json
import os
import pprint
import subprocess
import traceback
import yaml
PRINT = pprint.pprint
YAML_HEADER = '---'
def get_files_matching_pattern(dir_, pattern):
files = []
for root, _, filenames in os.walk(dir_):
for filename in fnmatch.filter(filenames, pattern):
files.append(os.path.join(root, filename))
return files
def json_2_yaml_convert(filename):
data = None
try:
with open(filename, 'r') as json_file:
data = json.load(json_file)
except:
PRINT('Failed on {}'.format(filename))
traceback.print_exc()
return (filename, '')
new_filename = os.path.splitext(filename)[0] + '.yaml'
with open(new_filename, 'w') as yaml_file:
yaml_file.write(YAML_HEADER + '\n')
yaml_file.write(yaml.safe_dump(data, default_flow_style=False))
return (filename, new_filename)
def git_rm(filename):
try:
subprocess.check_call(['git', 'rm', filename])
except subprocess.CalledProcessError:
PRINT('Failed to git rm {}'.format(filename))
traceback.print_exc()
return (False, filename)
return (True, filename)
def main(dir_, skip_convert):
files = get_files_matching_pattern(dir_, '*.json')
if skip_convert:
PRINT(files)
return
results = [json_2_yaml_convert(filename) for filename in files]
PRINT('*** conversion done ***')
PRINT(['converted {} to {}'.format(result[0], result[1]) for result in results])
results = [git_rm(filename) for filename, new_filename in results if new_filename]
PRINT('*** git rm done ***')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='json2yaml converter.')
parser.add_argument('--dir', '-d', required=True,
help='The dir to look for json.')
parser.add_argument('--skipconvert', '-s', action='store_true',
help='Skip conversion')
args = parser.parse_args()
main(dir_=args.dir, skip_convert=args.skipconvert)