Loading...
Searching...
No Matches
generate_region_config.py
Go to the documentation of this file.
1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements. See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership. The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with 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,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied. See the License for the
15# specific language governing permissions and limitations
16# under the License.
17
18import os
19import json
20import sys
21from platform.proto.replica_info_pb2 import ResConfigData,ReplicaInfo
22from google.protobuf.json_format import MessageToJson
23from google.protobuf.json_format import Parse, ParseDict
24
25
26def GenerateJsonConfig(file_name, output_file, template_file):
27 config_data=ResConfigData()
28 tmp_config={}
29 with open(file_name) as f:
30 for line in f.readlines():
31 if len(line.split()) == 0:
32 continue
33 info={}
34 data = line.split()
35 info["id"] = data[0]
36 info["ip"] = data[1]
37 info["port"] = data[2]
38 region_id = 0
39 if len(data) > 3:
40 region_id = data[3]
41 if region_id not in tmp_config:
42 tmp_config[region_id] = []
43
44 tmp_config[region_id].append({"replica_info":info})
45
46 config = []
47 for region_data in tmp_config.values():
48 region = config_data.region.add()
49 for rep in region_data:
50 replica = Parse(json.dumps(rep["replica_info"]), ReplicaInfo())
51 region.replica_info.append(replica)
52
53 json_obj = MessageToJson(config_data)
54 with open(output_file,"w") as f:
55 f.write(json_obj)
56
57 if template_file:
58 old_json = None
59 with open(output_file) as f:
60 lines=f.readlines()
61 for l in lines:
62 l=l.strip()
63 s=''.join(lines)
64 old_json=json.loads(s)
65
66 template_json = {}
67 with open(template_file) as f:
68 lines=f.readlines()
69 for l in lines:
70 l=l.strip()
71 s=''.join(lines)
72 template_json=json.loads(s)
73
74 for (k,v) in template_json.items():
75 old_json[k] = v
76
77 with open(output_file,"w") as f:
78 json.dump(old_json, f)
79
80if __name__ == "__main__":
81 template_config = None
82 if len(sys.argv)>3:
83 template_config = sys.argv[3]
84 GenerateJsonConfig(sys.argv[1], sys.argv[2], template_config)
GenerateJsonConfig(file_name, output_file, template_file)