-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.py
More file actions
279 lines (241 loc) · 8.7 KB
/
scripts.py
File metadata and controls
279 lines (241 loc) · 8.7 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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}")
def run_prettier_on_docs():
"""
Run Prettier on the generated markdown documentation files in the src/docs/ directory.
This requires Prettier to be installed on the system.
"""
try:
docs_dir = "src/docs"
# Check if the docs directory exists
if not os.path.exists(docs_dir):
print(f"Documentation directory {docs_dir} not found. Skipping Prettier formatting.")
return
print(f"Running Prettier on markdown documentation in {docs_dir}...")
# Check if Prettier is installed
result = subprocess.run(
["npx", "--no-install", "prettier", "--version"],
capture_output=True,
text=True
)
if result.returncode != 0:
print("Prettier not found. Installing Prettier...")
subprocess.run(["npm", "install", "--global", "prettier"])
# Run Prettier on all markdown files in the src/docs/ directory and subdirectories
subprocess.run([
"npx",
"prettier",
"--write",
f"{docs_dir}/**/*.md"
])
print("Prettier formatting of documentation completed successfully.")
except Exception as e:
print(f"Exception when running Prettier on docs: {e}")
def run_black_on_python():
"""
Run Black formatter on all Python files in the src/ directory.
This requires Black to be installed on the system.
Uses python -m black to ensure we use the installed package.
"""
try:
src_dir = "src"
# Check if the src directory exists
if not os.path.exists(src_dir):
print(f"Source directory {src_dir} not found. Skipping Black formatting.")
return
print(f"Running Black on Python files in {src_dir}...")
# Install Black if needed (will be a no-op if already installed)
print("Ensuring Black is installed...")
subprocess.run([
"python3",
"-m",
"pip",
"install",
"black"
])
# Run Black using Python module approach to avoid path issues
subprocess.run([
"python3",
"-m",
"black",
src_dir
])
print("Black formatting of Python files completed successfully.")
except Exception as e:
print(f"Exception when running Black on Python files: {e}")
def run_blacken_docs():
"""
Run blacken-docs on the generated markdown documentation files in the src/docs/ directory.
This normalizes Python code blocks in markdown to use double quotes (Black style).
"""
try:
docs_dir = "src/docs"
if not os.path.exists(docs_dir):
print(f"Documentation directory {docs_dir} not found. Skipping blacken-docs.")
return
print(f"Running blacken-docs on markdown documentation in {docs_dir}...")
subprocess.run([
"python3", "-m", "pip", "install", "blacken-docs"
])
md_files = [
os.path.join(docs_dir, f)
for f in os.listdir(docs_dir)
if f.endswith(".md")
]
if md_files:
subprocess.run(["python3", "-m", "blacken_docs"] + md_files)
print("blacken-docs formatting completed successfully.")
except Exception as e:
print(f"Exception when running blacken-docs: {e}")
def format_code():
"""
Format both Python and Markdown files in the repository.
"""
run_black_on_python()
run_blacken_docs()
run_prettier_on_docs()
print("All code formatting completed.")
def generate_and_format():
"""
Convenience function to generate the package and run Prettier on it.
"""
remove_deprecated_parameters("openapi.yaml", "openapi_filtered.yaml")
generate_package()
format_code()
build_distro_package()
if __name__ == "__main__":
if len(sys.argv) != 2:
print(
"Usage: python3 scripts.py [download_python_template | "
"generate_package | build_distro_package | "
"remove_deprecated_parameters | run_prettier_on_docs | "
"run_black_on_python | run_blacken_docs | format_code | generate_and_format]"
)
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")
elif script_to_run == "run_prettier_on_docs":
run_prettier_on_docs()
elif script_to_run == "run_black_on_python":
run_black_on_python()
elif script_to_run == "run_blacken_docs":
run_blacken_docs()
elif script_to_run == "format_code":
format_code()
elif script_to_run == "generate_and_format":
generate_and_format()
else:
print(
"Invalid script name. Use one of: download_python_template, "
"generate_package, build_distro_package, "
"remove_deprecated_parameters, run_prettier_on_docs, "
"run_black_on_python, format_code, generate_and_format"
)
sys.exit(1)