Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitmodules
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
25 changes: 7 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ test-unit:
@echo "Running tests..."
poetry run pytest --cov=pinecone --timeout=120 tests/unit

test-integration:
@echo "Running integration tests..."
PINECONE_ENVIRONMENT="us-east4-gcp" SPEC='{"serverless": {"cloud": "aws", "region": "us-east-1" }}' DIMENSION=2 METRIC='cosine' GITHUB_BUILD_NUMBER='local' poetry run pytest tests/integration

test-grpc-unit:
@echo "Running tests..."
poetry run pytest --cov=pinecone --timeout=120 tests/unit_grpc

make type-check:
poetry run mypy pinecone --exclude pinecone/core

make generate-oas:
./codegen/build-oas.sh "2024-07"

version:
poetry version

Expand All @@ -28,21 +35,3 @@ package:

upload:
poetry publish --verbose --username ${PYPI_USERNAME} --password ${PYPI_PASSWORD}

upload-spruce:
# Configure Poetry for publishing to testpypi
poetry config repositories.test-pypi https://test.pypi.org/legacy/
poetry publish --verbose -r test-pypi --username ${PYPI_USERNAME} --password ${PYPI_PASSWORD}

license:
# Add license header using https://github.com/google/addlicense.
# If the license header already exists in a file, re-running this command has no effect.
pushd ${mkfile_path}/pinecone && \
docker run --rm -it -v ${mkfile_path}/pinecone:/src ghcr.io/google/addlicense:latest -f ./license_header.txt *.py */*.py */*/*.py */*/*/*.py */*/*/*/*.py */*/*/*/*/*.py */*/*/*/*/*/*.py; \
popd

set-production:
echo "production" > pinecone/__environment__

set-development:
echo "" > pinecone/__environment__
1 change: 1 addition & 0 deletions codegen/apis
Submodule apis added at fbd9d8
148 changes: 148 additions & 0 deletions codegen/build-oas.sh
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
Comment on lines +11 to +13

Copy link
Copy Markdown
Member

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-templates submodule is also up to date? I doubt it would have as much churn as the APIs repo, but just in case.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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}"
4 changes: 4 additions & 0 deletions codegen/openapi-config.control.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"packageName": "pinecone.core.openapi.control",
"pythonAttrNoneIfUnset": true
}
4 changes: 4 additions & 0 deletions codegen/openapi-config.data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"packageName": "pinecone.core.openapi.data",
"pythonAttrNoneIfUnset": true
}
1 change: 1 addition & 0 deletions codegen/python-oas-templates
Submodule python-oas-templates added at b72bd5
2 changes: 1 addition & 1 deletion pinecone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .data import *
from .models import *

from .core.client.models import (
from .core.openapi.control.models import (
IndexModel,
)

Expand Down
25 changes: 20 additions & 5 deletions pinecone/config/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import NamedTuple, Optional, Dict
import os

from pinecone.exceptions import PineconeConfigurationError
from pinecone.exceptions.exceptions import PineconeConfigurationError
from pinecone.config.openapi import OpenApiConfigFactory
from pinecone.core.client.configuration import Configuration as OpenApiConfiguration
from pinecone.core.openapi.shared.configuration import (
Configuration as OpenApiConfiguration,
)
from pinecone.utils import normalize_host
from pinecone.utils.constants import SOURCE_TAG

Expand Down Expand Up @@ -57,15 +59,28 @@ def build(
if not host:
raise PineconeConfigurationError("You haven't specified a host.")

return Config(api_key, host, proxy_url, proxy_headers, ssl_ca_certs, ssl_verify, additional_headers, source_tag)
return Config(
api_key,
host,
proxy_url,
proxy_headers,
ssl_ca_certs,
ssl_verify,
additional_headers,
source_tag,
)

@staticmethod
def build_openapi_config(
config: Config, openapi_config: Optional[OpenApiConfiguration] = None, **kwargs
config: Config,
openapi_config: Optional[OpenApiConfiguration] = None,
**kwargs,
) -> OpenApiConfiguration:
if openapi_config:
openapi_config = OpenApiConfigFactory.copy(
openapi_config=openapi_config, api_key=config.api_key, host=config.host
openapi_config=openapi_config,
api_key=config.api_key,
host=config.host,
)
elif openapi_config is None:
openapi_config = OpenApiConfigFactory.build(api_key=config.api_key, host=config.host)
Expand Down
12 changes: 10 additions & 2 deletions pinecone/config/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

from urllib3.connection import HTTPConnection

from pinecone.core.client.configuration import Configuration as OpenApiConfiguration
from pinecone.core.openapi.shared.configuration import (
Configuration as OpenApiConfiguration,
)

TCP_KEEPINTVL = 60 # Sec
TCP_KEEPIDLE = 300 # Sec
Expand Down Expand Up @@ -84,7 +86,13 @@ def _get_socket_options(
and hasattr(socket, "TCP_KEEPCNT")
):
socket_params += [(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, keep_alive_idle_sec)]
socket_params += [(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, keep_alive_interval_sec)]
socket_params += [
(
socket.IPPROTO_TCP,
socket.TCP_KEEPINTVL,
keep_alive_interval_sec,
)
]
socket_params += [(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, keep_alive_tries)]

# TCP Keep Alive Probes for Windows OS
Expand Down
7 changes: 6 additions & 1 deletion pinecone/config/pinecone_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ def build(
except Exception as e:
logger.warn(f"Ignoring PINECONE_ADDITIONAL_HEADERS: {e}")

return ConfigBuilder.build(api_key=api_key, host=host, additional_headers=additional_headers, **kwargs)
return ConfigBuilder.build(
api_key=api_key,
host=host,
additional_headers=additional_headers,
**kwargs,
)
6 changes: 4 additions & 2 deletions pinecone/control/index_host_store.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Dict
from pinecone.config import Config
from pinecone.core.client.api.manage_indexes_api import ManageIndexesApi as IndexOperationsApi
from pinecone.core.client.exceptions import PineconeException
from pinecone.core.openapi.control.api.manage_indexes_api import (
ManageIndexesApi as IndexOperationsApi,
)
from pinecone.core.openapi.shared.exceptions import PineconeException
from pinecone.utils import normalize_host


Expand Down
Loading