-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_protoc.py
More file actions
executable file
·74 lines (58 loc) · 2.21 KB
/
merge_protoc.py
File metadata and controls
executable file
·74 lines (58 loc) · 2.21 KB
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
#!/usr/bin/env python3
# Call this from the ./scripts/protoc_swagger_openapi_gen.sh script
# merged protoc definitions together into 1 JSON file without duplicate keys
# this is done AFTER swagger-merger has been run, merging the multiple name-#.json files into 1.
import json
import os
import random
import string
current_dir = os.path.dirname(os.path.realpath(__file__))
project_root = os.path.dirname(current_dir)
all_dir = os.path.join(project_root, "tmp-swagger-gen", "_all")
# get the go.mod file Version
version = ""
with open(os.path.join(project_root, "go.mod"), "r") as f:
for line in f.readlines():
if line.startswith("module"):
version = line.split("/")[-1].strip()
break
if not version:
print("Could not find version in go.mod")
exit(1)
# What we will save when all combined
output: dict
output = {
"swagger": "2.0",
"info": {"title": "Juno network", "version": version},
"consumes": ["application/json"],
"produces": ["application/json"],
"paths": {},
"definitions": {},
}
# Combine all individual files calls into 1 massive file.
for file in os.listdir(all_dir):
if not file.endswith(".json"):
continue
# read file all_dir / file
with open(os.path.join(all_dir, file), "r") as f:
data = json.load(f)
for key in data["paths"]:
output["paths"][key] = data["paths"][key]
for key in data["definitions"]:
output["definitions"][key] = data["definitions"][key]
# loop through all paths, then alter any keys which are "operationId" to be a random string of 20 characters
# this is done to avoid duplicate keys in the final output (which opens 2 tabs in swagger-ui)
# current-random
for path in output["paths"]:
for method in output["paths"][path]:
if "operationId" in output["paths"][path][method]:
output["paths"][path][method][
"operationId"
] = f'{output["paths"][path][method]["operationId"]}_' + "".join(
random.choices(string.ascii_uppercase + string.digits, k=5)
)
# save output into 1 big json file
with open(
os.path.join(project_root, "tmp-swagger-gen", "_all", "FINAL.json"), "w"
) as f:
json.dump(output, f, indent=2)