-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_all.py
More file actions
66 lines (54 loc) · 2.26 KB
/
generate_all.py
File metadata and controls
66 lines (54 loc) · 2.26 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
import sys
import subprocess
from pathlib import Path
SPECS_DIR = Path("openapi_specs")
PACKAGE_DIR = Path("src/osdu_python_client")
def generate_all():
PACKAGE_DIR.mkdir(parents=True, exist_ok=True)
(PACKAGE_DIR / "__init__.py").touch(exist_ok=True)
specs = list(SPECS_DIR.glob("*.json"))
print(f"Found {len(specs)} OpenAPI specs.")
for spec_path in specs:
service_name = spec_path.stem.lower().replace(" ", "_").replace("-", "_")
output_path = PACKAGE_DIR / service_name
print(f"Generating client for {service_name}...")
# OSDU Specs sometimes miss the 'version' field which is required by the generator
import json
with open(spec_path, "r") as f:
spec_data = json.load(f)
needs_version_patch = False
if "info" in spec_data and "version" not in spec_data["info"]:
spec_data["info"]["version"] = "1.0.0"
needs_version_patch = True
print(f" - Patching missing version for {service_name}")
temp_spec_path = spec_path
if needs_version_patch:
temp_spec_path = spec_path.with_suffix(".temp.json")
with open(temp_spec_path, "w") as f:
json.dump(spec_data, f)
# Ensure output directory is clean for the generator
if output_path.exists():
import shutil
shutil.rmtree(output_path)
output_path.mkdir(parents=True)
cmd = [
sys.executable, "-m", "openapi_python_client", "generate",
"--path", str(temp_spec_path),
"--meta", "none",
"--output-path", str(output_path),
"--overwrite"
]
try:
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
print(f"Successfully generated {service_name}")
else:
print(f"Failed to generate {service_name}")
print(result.stderr)
except Exception as e:
print(f"An error occurred while generating {service_name}: {e}")
finally:
if needs_version_patch and temp_spec_path.exists():
temp_spec_path.unlink()
if __name__ == "__main__":
generate_all()