Repository navigation
Add scripts for OpenAPI generation and post-generation and more convenience #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c9886ef
Add cross-platform regeneration script and optional Makefile
antalszava b185379
Replace perl post-generation hooks with cross-platform Python script
antalszava 28de2e8
chore: contributing
antalszava c20e942
chore: formatting
antalszava 42f135a
Merge branch 'main' into makefile
antalszava b84ff80
Merge branch 'main' into makefile
antalszava b5921ea
chore: addressing PR comments
antalszava da519df
Merge branch 'main' into makefile
splch edd0dfe
Merge branch 'main' into makefile
splch c4daf33
Switch local setup-uv refs to `$/`
splch 0857134
run CI again
antalszava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Optional convenience wrappers; scripts/regenerate_models.py is the canonical | ||
| # regeneration workflow and works on any OS. | ||
|
|
||
| PYTEST_ARGS ?= | ||
|
|
||
| .PHONY: lint format typecheck test integration regen sync-spec check-generated | ||
|
|
||
| lint: | ||
| uv run ruff check | ||
| uv run ruff format --check | ||
|
|
||
| format: | ||
| uv run ruff format | ||
|
|
||
| typecheck: | ||
| uv run ty check ionq_core/ | ||
|
|
||
| test: | ||
| uv run pytest $(PYTEST_ARGS) | ||
|
|
||
| integration: | ||
| uv run pytest -m integration --no-cov $(PYTEST_ARGS) | ||
|
|
||
| regen: | ||
| uv run --group regen python scripts/regenerate_models.py | ||
|
|
||
| sync-spec: | ||
| uv run --group regen python scripts/regenerate_models.py --sync-spec | ||
|
|
||
| check-generated: regen | ||
| @if [ -n "$$(git status --porcelain ionq_core/)" ]; then \ | ||
| echo "Generated code is out of date: run 'make regen' and commit the results."; \ | ||
| git diff ionq_core/; \ | ||
| exit 1; \ | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| """Post-generation hooks for openapi-python-client (cross-platform). | ||
|
|
||
| Invoked via post_hooks in openapi-python-client-config.yaml. Hides | ||
| AuthenticatedClient.token from repr and prepends SPDX/@generated headers. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import datetime | ||
| import re | ||
| from pathlib import Path | ||
|
|
||
| PACKAGE_DIR = Path(__file__).resolve().parent.parent / "ionq_core" | ||
|
|
||
|
|
||
| def main() -> None: | ||
| client_file = PACKAGE_DIR / "client.py" | ||
| client_file.write_text( | ||
|
antalszava marked this conversation as resolved.
|
||
| re.sub( | ||
| r"(token: str)$", | ||
| r"\1 = field(repr=False)", | ||
| client_file.read_text(encoding="utf-8"), | ||
| flags=re.MULTILINE, | ||
| ), | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| year = datetime.datetime.now(datetime.UTC).year | ||
| header = f"# SPDX-FileCopyrightText: {year} IonQ, Inc.\n# SPDX-License-Identifier: Apache-2.0\n# @generated\n\n" | ||
|
antalszava marked this conversation as resolved.
Outdated
|
||
| for path in PACKAGE_DIR.rglob("*.py"): | ||
| text = path.read_text(encoding="utf-8") | ||
| if not text.startswith("# SPDX-FileCopyrightText"): | ||
| path.write_text(header + text, encoding="utf-8") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| """Regenerate the generated client packages from the vendored OpenAPI spec. | ||
|
|
||
| Canonical invocation: | ||
|
|
||
| uv run --group regen python scripts/regenerate_models.py [--sync-spec] | ||
|
|
||
| Invoking this script as such ensures that the commands in here don't need to | ||
| be re-run via uv run. | ||
|
|
||
| Applies openapi-overlay.yaml (when present) to openapi.json, then runs | ||
| openapi-python-client with the repo's config and custom templates. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import urllib.request | ||
| from pathlib import Path | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parent.parent | ||
| SPEC_URL = "https://api.ionq.co/v0.4/api-docs" | ||
| SPEC_FILE = REPO_ROOT / "openapi.json" | ||
| OVERLAY_FILE = REPO_ROOT / "openapi-overlay.yaml" | ||
|
|
||
|
|
||
| def _get_tool_path(name: str) -> str: | ||
| path = shutil.which(name) | ||
| if path is None: | ||
| invocation_cmd = "uv run --group regen python scripts/regenerate_models.py" | ||
| sys.exit(f"error: {name!r} not found on PATH; run via '{invocation_cmd}'") | ||
| return path | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "--sync-spec", | ||
| action="store_true", | ||
| help=f"download the latest spec from {SPEC_URL} before regenerating", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| if args.sync_spec: | ||
| with urllib.request.urlopen(SPEC_URL, timeout=60) as response: | ||
| SPEC_FILE.write_bytes(response.read()) | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmp_dir: | ||
| patched_spec = Path(tmp_dir) / "patched-spec.json" | ||
| if OVERLAY_FILE.exists(): | ||
| cmd = [_get_tool_path("oas-patch"), "overlay", str(SPEC_FILE), str(OVERLAY_FILE), "-o", str(patched_spec)] | ||
| subprocess.run(cmd, check=True, cwd=REPO_ROOT) | ||
| else: | ||
| shutil.copyfile(SPEC_FILE, patched_spec) | ||
|
|
||
| # fmt: off | ||
| cmd = [ | ||
| _get_tool_path("openapi-python-client"), "generate", | ||
| "--path", str(patched_spec), | ||
| "--meta", "none", | ||
| "--config", "openapi-python-client-config.yaml", | ||
| "--custom-template-path", "custom-templates", | ||
| "--output-path", "ionq_core", | ||
| "--overwrite", | ||
| ] | ||
| # fmt: on | ||
| subprocess.run(cmd, check=True, cwd=REPO_ROOT) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.