-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.py
143 lines (126 loc) · 4.24 KB
/
scripts.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import sys
import subprocess
import shutil
import os
import yaml
def download_python_template():
"""
Download the Python client library template from the OpenAPI Generator.
"""
try:
subprocess.run([
"openapi-generator-cli",
"author",
"template",
"-g",
"python",
"--library",
"urllib3",
])
except Exception as e:
print("Exception when generating package: %s\n" % e)
def generate_package():
"""
Generate the Python client library package using the OpenAPI Generator.
"""
try:
subprocess.run([
"openapi-generator-cli",
"generate",
"-g",
"python",
"--library",
"urllib3",
"-t",
"lib_template",
"-o",
"src",
"-i",
"openapi_filtered.yaml",
"-c",
"config.json"
])
except Exception as e:
print("Exception when generating package: %s\n" % e)
def build_distro_package():
"""
Build the distribution package for the Notehub Py client library.
"""
try:
os.chdir("src/")
# Check if the 'dist/' folder exists
if os.path.exists("dist"):
# If it exists, delete it and its contents
shutil.rmtree("dist")
# Upgrade the 'build' module
subprocess.run([
"python3",
"-m",
"pip",
"install",
"--upgrade",
"build"
])
# Generate a new 'dist/' folder
subprocess.run([
"python3",
"-m",
"build"
])
except Exception as e:
print("Exception when building distro package: %s\n" % e)
def remove_deprecated_parameters(input_file: str, output_file: str):
"""
Load an OpenAPI YAML file, remove deprecated parameters, and save to a new file.
"""
with open(input_file, 'r') as f:
openapi_spec = yaml.safe_load(f)
# Traverse paths and operations to remove deprecated parameters
for path, methods in openapi_spec.get('paths', {}).items():
for method, operation in methods.items():
if isinstance(operation, dict):
# Remove deprecated parameters
if 'parameters' in operation:
operation['parameters'] = [
param for param in operation['parameters']
if not param.get('deprecated', False)
]
# Remove deprecated requestBody properties if applicable
if 'requestBody' in operation:
content = operation['requestBody'].get('content', {})
for content_type, schema in content.items():
properties = schema.get('schema', {}).get(
'properties', {}
)
schema['schema']['properties'] = {
k: v for k, v in properties.items()
if not v.get('deprecated', False)
}
# Save the modified spec to a new file
with open(output_file, 'w') as f:
yaml.dump(openapi_spec, f, sort_keys=False)
print(f"Filtered OpenAPI spec saved to: {output_file}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print(
"Usage: python3 scripts.py [download_python_template | "
"generate_package | build_distro_package | "
"remove_deprecated_parameters]"
)
sys.exit(1)
script_to_run = sys.argv[1]
if script_to_run == "download_python_template":
download_python_template()
elif script_to_run == "generate_package":
generate_package()
elif script_to_run == "build_distro_package":
build_distro_package()
elif script_to_run == "remove_deprecated_parameters":
remove_deprecated_parameters("openapi.yaml", "openapi_filtered.yaml")
else:
print(
"Invalid script name. Use one of: download_python_template, "
"generate_package, build_distro_package, "
"remove_deprecated_parameters"
)
sys.exit(1)