1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
3
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
4
|
|
|
# this work for additional information regarding copyright ownership. |
5
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
6
|
|
|
# (the "License"); you may not use this file except in compliance with |
7
|
|
|
# the License. You may obtain a copy of the License at |
8
|
|
|
# |
9
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
# |
11
|
|
|
# Unless required by applicable law or agreed to in writing, software |
12
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
13
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14
|
|
|
# See the License for the specific language governing permissions and |
15
|
|
|
# limitations under the License. |
16
|
|
|
|
17
|
|
|
""" |
18
|
|
|
A utility script which sends test messages to a queue. |
19
|
|
|
""" |
20
|
|
|
|
21
|
|
|
import argparse |
22
|
|
|
import fnmatch |
23
|
|
|
try: |
24
|
|
|
import simplejson as json |
25
|
|
|
except ImportError: |
26
|
|
|
import json |
27
|
|
|
import os |
28
|
|
|
import pprint |
29
|
|
|
import subprocess |
30
|
|
|
import traceback |
31
|
|
|
import yaml |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
PRINT = pprint.pprint |
35
|
|
|
YAML_HEADER = '---' |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def get_files_matching_pattern(dir_, pattern): |
39
|
|
|
files = [] |
40
|
|
|
for root, _, filenames in os.walk(dir_): |
41
|
|
|
for filename in fnmatch.filter(filenames, pattern): |
42
|
|
|
files.append(os.path.join(root, filename)) |
43
|
|
|
return files |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def json_2_yaml_convert(filename): |
47
|
|
|
data = None |
48
|
|
|
try: |
49
|
|
|
with open(filename, 'r') as json_file: |
50
|
|
|
data = json.load(json_file) |
51
|
|
|
except: |
52
|
|
|
PRINT('Failed on {}'.format(filename)) |
53
|
|
|
traceback.print_exc() |
54
|
|
|
return (filename, '') |
55
|
|
|
new_filename = os.path.splitext(filename)[0] + '.yaml' |
56
|
|
|
with open(new_filename, 'w') as yaml_file: |
57
|
|
|
yaml_file.write(YAML_HEADER + '\n') |
58
|
|
|
yaml_file.write(yaml.safe_dump(data, default_flow_style=False)) |
59
|
|
|
return (filename, new_filename) |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
def git_rm(filename): |
63
|
|
|
try: |
64
|
|
|
subprocess.check_call(['git', 'rm', filename]) |
65
|
|
|
except subprocess.CalledProcessError: |
66
|
|
|
PRINT('Failed to git rm {}'.format(filename)) |
67
|
|
|
traceback.print_exc() |
68
|
|
|
return (False, filename) |
69
|
|
|
return (True, filename) |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def main(dir_, skip_convert): |
73
|
|
|
files = get_files_matching_pattern(dir_, '*.json') |
74
|
|
|
if skip_convert: |
75
|
|
|
PRINT(files) |
76
|
|
|
return |
77
|
|
|
results = [json_2_yaml_convert(filename) for filename in files] |
78
|
|
|
PRINT('*** conversion done ***') |
79
|
|
|
PRINT(['converted {} to {}'.format(result[0], result[1]) for result in results]) |
80
|
|
|
results = [git_rm(filename) for filename, new_filename in results if new_filename] |
81
|
|
|
PRINT('*** git rm done ***') |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
if __name__ == '__main__': |
85
|
|
|
parser = argparse.ArgumentParser(description='json2yaml converter.') |
86
|
|
|
parser.add_argument('--dir', '-d', required=True, |
87
|
|
|
help='The dir to look for json.') |
88
|
|
|
parser.add_argument('--skipconvert', '-s', action='store_true', |
89
|
|
|
help='Skip conversion') |
90
|
|
|
args = parser.parse_args() |
91
|
|
|
|
92
|
|
|
main(dir_=args.dir, skip_convert=args.skipconvert) |
93
|
|
|
|