Repository navigation
REST code generation for 2024-07 prerelease #361
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
035248d
Add codegen script
jhamon e248dda
Update codegen script to refresh apis submodule
jhamon b55b072
Experiment with generating two packages
jhamon 15aa28a
Experiment with generating two packages pinecone.core.data and pineco…
jhamon f84cce5
WIP on fixing import paths and tests
jhamon e13d34c
Do post-generation surgery to build pinecone.core.shared
jhamon e3a3c03
Refactoring in codegen script
jhamon 416a0c6
More cleanup
jhamon 1424c8d
Adjust package structure
jhamon 5ab73f9
Fix lint errors
jhamon 3712a96
Add build command to Makefile
jhamon 7f3805a
Add more tests for passing index spec as dict
jhamon 5a72002
Automatically pull updates to template repo as well
jhamon 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [submodule "codegen/apis"] | ||
| path = codegen/apis | ||
| url = git@github.com:pinecone-io/apis.git | ||
| [submodule "codegen/python-oas-templates"] | ||
| path = codegen/python-oas-templates | ||
| url = git@github.com:pinecone-io/python-oas-templates.git |
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,148 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -eux -o pipefail | ||
|
|
||
| version=$1 # e.g. 2024-07 | ||
| modules=("control" "data") | ||
|
|
||
| destination="pinecone/core/openapi" | ||
| build_dir="build" | ||
|
|
||
| update_apis_repo() { | ||
| echo "Updating apis repo" | ||
| pushd codegen/apis | ||
| git fetch | ||
| git pull | ||
| just build | ||
| popd | ||
| } | ||
|
|
||
| update_templates_repo() { | ||
| echo "Updating templates repo" | ||
| pushd codegen/python-oas-templates | ||
| git fetch | ||
| git pull | ||
| popd | ||
| } | ||
|
|
||
| verify_spec_version() { | ||
| local version=$1 | ||
| echo "Verifying spec version $version exists in apis repo" | ||
| if [ -z "$version" ]; then | ||
| echo "Version is required" | ||
| exit 1 | ||
| fi | ||
|
|
||
| verify_directory_exists "codegen/apis/_build/${version}" | ||
| } | ||
|
|
||
| verify_file_exists() { | ||
| local filename=$1 | ||
| if [ ! -f "$filename" ]; then | ||
| echo "File does not exist at $filename" | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| verify_directory_exists() { | ||
| local directory=$1 | ||
| if [ ! -d "$directory" ]; then | ||
| echo "Directory does not exist at $directory" | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| generate_client() { | ||
| local module_name=$1 | ||
|
|
||
| oas_file="codegen/apis/_build/${version}/${module_name}_${version}.oas.yaml" | ||
| openapi_generator_config="codegen/openapi-config.${module_name}.json" | ||
| template_dir="codegen/python-oas-templates/templates5.2.0" | ||
|
|
||
| verify_file_exists $oas_file | ||
| verify_file_exists $openapi_generator_config | ||
| verify_directory_exists $template_dir | ||
|
|
||
| # Cleanup previous build files | ||
| echo "Cleaning up previous build files" | ||
| rm -rf "${build_dir}" | ||
|
|
||
| # Generate client module | ||
| docker run --rm -v $(pwd):/workspace openapitools/openapi-generator-cli:v5.2.0 generate \ | ||
| --input-spec "/workspace/$oas_file" \ | ||
| --generator-name python \ | ||
| --config "/workspace/$openapi_generator_config" \ | ||
| --output "/workspace/${build_dir}" \ | ||
| --template-dir "/workspace/$template_dir" | ||
|
|
||
| # Copy the generated module to the correct location | ||
| rm -rf "${destination}/${module_name}" | ||
| cp -r "build/pinecone/core/openapi/${module_name}" "${destination}/${module_name}" | ||
| } | ||
|
|
||
| extract_shared_classes() { | ||
| target_directory="${destination}/shared" | ||
| mkdir -p "$target_directory" | ||
|
|
||
| # Define the list of shared source files | ||
| sharedFiles=( | ||
| "api_client" | ||
| "configuration" | ||
| "exceptions" | ||
| "model_utils" | ||
| "rest" | ||
| ) | ||
|
|
||
| source_directory="${destination}/${modules[0]}" | ||
|
|
||
| # Loop through each file we want to share and copy it to the target directory | ||
| for file in "${sharedFiles[@]}"; do | ||
| cp "${source_directory}/${file}.py" "$target_directory" | ||
| done | ||
|
|
||
| # Cleanup shared files in each module | ||
| for module in "${modules[@]}"; do | ||
| source_directory="${destination}/${module}" | ||
| for file in "${sharedFiles[@]}"; do | ||
| rm "${source_directory}/${file}.py" | ||
| done | ||
| done | ||
|
|
||
| # Remove the docstring headers that aren't really correct in the | ||
| # context of this new shared package structure | ||
| find "$target_directory" -name "*.py" -print0 | xargs -0 -I {} sh -c 'sed -i "" "/^\"\"\"/,/^\"\"\"/d" "{}"' | ||
|
Comment on lines
+111
to
+113
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice cleanup! 👍 |
||
|
|
||
| echo "All shared files have been copied to $target_directory." | ||
|
|
||
| # Adjust import paths in every file | ||
| find "${destination}" -name "*.py" | while IFS= read -r file; do | ||
|
Comment on lines
+117
to
+118
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also cool, very clever piece of scripting here, nice work. |
||
| sed -i '' 's/from \.\.model_utils/from pinecone\.core\.openapi\.shared\.model_utils/g' "$file" | ||
|
|
||
| for module in "${modules[@]}"; do | ||
| sed -i '' "s/from pinecone\.core\.openapi\.$module import rest/from pinecone\.core\.openapi\.shared import rest/g" "$file" | ||
|
|
||
| for sharedFile in "${sharedFiles[@]}"; do | ||
| sed -i '' "s/from pinecone\.core\.openapi\.$module\.$sharedFile/from pinecone\.core\.openapi\.shared\.$sharedFile/g" "$file" | ||
| done | ||
| done | ||
| done | ||
| } | ||
|
|
||
| update_apis_repo | ||
| update_templates_repo | ||
| verify_spec_version $version | ||
|
|
||
| rm -rf "${destination}" | ||
| mkdir -p "${destination}" | ||
|
|
||
| for module in "${modules[@]}"; do | ||
| generate_client $module | ||
| done | ||
|
|
||
| # Even though we want to generate multiple packages, we | ||
| # don't want to duplicate every exception and utility class. | ||
| # So we do a bit of surgery to combine the shared files. | ||
| extract_shared_classes | ||
|
|
||
| # Format generated files | ||
| poetry run black "${destination}" | ||
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,4 @@ | ||
| { | ||
| "packageName": "pinecone.core.openapi.control", | ||
| "pythonAttrNoneIfUnset": true | ||
| } |
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,4 @@ | ||
| { | ||
| "packageName": "pinecone.core.openapi.data", | ||
| "pythonAttrNoneIfUnset": true | ||
| } |
Submodule python-oas-templates
added at
b72bd5
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be useful to have something similar for making sure the
codegen/python-oas-templatessubmodule is also up to date? I doubt it would have as much churn as the APIs repo, but just in case.