This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_config_json.py
executable file
·129 lines (89 loc) · 3.36 KB
/
update_config_json.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
"""
A simple script to ensure that the ordering of the "service" section in config.json is standardized.
NOTE: This overwrites the files.
Usage: from the `dcos-commons` root:
$ python tools/update_config_json.py
"""
import collections
import json
import logging
import sys
import difflib
logging.basicConfig(
format="[%(asctime)s|%(name)s|%(levelname)s]: %(message)s", level="INFO", stream=sys.stdout
)
LOG = logging.getLogger(__name__)
def read_file(file_path: str) -> str:
LOG.info("Reading from %s", file_path)
with open(file_path, "r") as handle:
return handle.read()
def read_json_file(file_path: str) -> collections.OrderedDict:
return json.loads(read_file(file_path), object_pairs_hook=collections.OrderedDict)
def write_file(file_path: str, content: str) -> str:
LOG.info("Writing to %s", file_path)
with open(file_path, "w") as handle:
handle.write(content)
if not content.endswith("\n"):
handle.write("\n")
def write_json_file(file_path: str, content: collections.OrderedDict):
write_file(file_path, json.dumps(content, indent=2))
def reorder(
original: collections.OrderedDict, head: list = [], tail: list = [], mapper=lambda x: x
) -> collections.OrderedDict:
remaining = []
if not isinstance(original, dict):
return original
for p in original.keys():
if p in tail:
continue
if p in head:
continue
remaining.append(p)
reordered = collections.OrderedDict()
for p in head:
if p in original:
reordered[p] = mapper(original[p])
for p in remaining:
reordered[p] = mapper(original[p])
for p in tail:
if p in original:
reordered[p] = mapper(original[p])
return reordered
def reorder_property(schema: collections.OrderedDict) -> collections.OrderedDict:
return reorder(schema, head=["description", "type", "enum", "default"], tail=["properties"])
def reorder_service(service_properties: collections.OrderedDict) -> collections.OrderedDict:
expected_order_head = [
"name",
"user",
"service_account",
"service_account_secret",
"virtual_network_enabled",
"virtual_network_name",
"virtual_network_plugin_labels",
"mesos_api_version",
"log_level",
]
expected_order_tail = ["security"]
return reorder(service_properties, expected_order_head, expected_order_tail, reorder_property)
def print_diff(original: collections.OrderedDict, new: collections.OrderedDict):
o = json.dumps(original, indent=2)
c = json.dumps(new, indent=2)
diff = difflib.unified_diff(o.split("\n"), c.split("\n"))
LOG.info("\n".join(diff))
def process(filename: str):
contents = read_json_file(filename)
original = read_json_file(filename)
reordered = reorder_service(contents["properties"]["service"]["properties"])
contents["properties"]["service"]["properties"] = reordered
print_diff(original, contents)
write_json_file(filename, contents)
if __name__ == "__main__":
files = [
"frameworks/cassandra/universe/config.json",
"frameworks/elastic/universe-kibana/config.json",
"frameworks/elastic/universe/config.json",
"frameworks/hdfs/universe/config.json",
"frameworks/helloworld/universe/config.json",
]
for f in files:
process(f)