From 64959e2d6daa0a81952ed1c3acc28ec01209f0d1 Mon Sep 17 00:00:00 2001 From: Oliver Nagy Date: Thu, 6 Sep 2018 02:33:48 +1000 Subject: [PATCH] Added generic Python asyncio generator script. (#68) * Added generic Python asyncio generator script. * Merged 'python-asyncio.sh' and 'python-async-generic.sh' * Refactored 'python-asyncio.sh' to remove duplicate code. * Changed 'kubernetes_client' to 'client'. * Code patches are now client specific. --- openapi/python-asyncio.sh | 71 +++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/openapi/python-asyncio.sh b/openapi/python-asyncio.sh index 9a35228..15caa86 100755 --- a/openapi/python-asyncio.sh +++ b/openapi/python-asyncio.sh @@ -44,34 +44,53 @@ popd > /dev/null source "${SCRIPT_ROOT}/client-generator.sh" source "${SETTING_FILE}" -SWAGGER_CODEGEN_COMMIT=f9b2839a3076f26db1b8fc61655a26662f2552ee; \ -CLIENT_LANGUAGE=python-asyncio; \ -CLEANUP_DIRS=(client/apis client/models docs test); \ +# Client specific Swagger branch to use. +if [ ${PACKAGE_NAME} == "client" ]; then + SWAGGER_CODEGEN_COMMIT=f9b2839a3076f26db1b8fc61655a26662f2552ee +else + SWAGGER_CODEGEN_COMMIT=v2.3.1 +fi + +# Build the client library in a Docker container. +CLIENT_LANGUAGE=python-asyncio +CLEANUP_DIRS=(client/apis client/models docs test) kubeclient::generator::generate_client "${OUTPUT_DIR}" +# Generic patches to the generated Python code, most notably renaming the library. echo "--- Patching generated code..." -find "${OUTPUT_DIR}/test" -type f -name \*.py -exec sed -i 's/\bclient/kubernetes_asyncio.client/g' {} + -find "${OUTPUT_DIR}" -path "${OUTPUT_DIR}/base" -prune -o -type f -a -name \*.md -exec sed -i 's/\bclient/kubernetes_asyncio.client/g' {} + -find "${OUTPUT_DIR}" -path "${OUTPUT_DIR}/base" -prune -o -type f -a -name \*.md -exec sed -i 's/kubernetes_asyncio.client-python/client-python/g' {} + - -# workaround https://github.com/swagger-api/swagger-codegen/pull/7905 -find "${OUTPUT_DIR}/client" -type f -name \*.py ! -name '__init__.py' -exec sed -i '/^from .*models.*/d' {} \; - -# workaround https://github.com/swagger-api/swagger-codegen/pull/8204 -# + closing session -# + support application/strategic-merge-patch+json -patch "${OUTPUT_DIR}/client/rest.py" "${SCRIPT_ROOT}/python-asyncio-rest.py.patch" - -# workaround https://github.com/swagger-api/swagger-codegen/pull/8401 -find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/async=/async_req=/g' {} + -find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/async bool/async_req bool/g' {} + -find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i "s/'async'/'async_req'/g" {} + -find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i "s/async parameter/async_req parameter/g" {} + -find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i "s/if not async/if not async_req/g" {} + - -# fix imports -find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/import client\./import kubernetes_asyncio.client./g' {} + -find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/from client/from kubernetes_asyncio.client/g' {} + -find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/getattr(client\.models/getattr(kubernetes_asyncio.client.models/g' {} + + +# Post-processing of the generated Python wrapper. +if [ ${PACKAGE_NAME} == "client" ]; then + find "${OUTPUT_DIR}/test" -type f -name \*.py -exec sed -i 's/\bclient/kubernetes_asyncio.client/g' {} + + find "${OUTPUT_DIR}" -path "${OUTPUT_DIR}/base" -prune -o -type f -a -name \*.md -exec sed -i 's/\bclient/kubernetes_asyncio.client/g' {} + + find "${OUTPUT_DIR}" -path "${OUTPUT_DIR}/base" -prune -o -type f -a -name \*.md -exec sed -i 's/kubernetes_asyncio.client-python/client-python/g' {} + + + # workaround https://github.com/swagger-api/swagger-codegen/pull/7905 + find "${OUTPUT_DIR}/client" -type f -name \*.py ! -name '__init__.py' -exec sed -i '/^from .*models.*/d' {} \; + + # workaround https://github.com/swagger-api/swagger-codegen/pull/8204 + # + closing session + # + support application/strategic-merge-patch+json + patch "${OUTPUT_DIR}/client/rest.py" "${SCRIPT_ROOT}/python-asyncio-rest.py.patch" + + # workaround https://github.com/swagger-api/swagger-codegen/pull/8401 + find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/async=/async_req=/g' {} + + find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/async bool/async_req bool/g' {} + + find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i "s/'async'/'async_req'/g" {} + + find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i "s/async parameter/async_req parameter/g" {} + + find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i "s/if not async/if not async_req/g" {} + + + # fix imports + find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/import client\./import kubernetes_asyncio.client./g' {} + + find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/from client/from kubernetes_asyncio.client/g' {} + + find "${OUTPUT_DIR}/client/" -type f -name \*.py -exec sed -i 's/getattr(client\.models/getattr(kubernetes_asyncio.client.models/g' {} + +else + find "${OUTPUT_DIR}/test" -type f -name \*.py -exec sed -i "s/\\bclient/${PACKAGE_NAME}.client/g" {} + + find "${OUTPUT_DIR}" -path "${OUTPUT_DIR}/base" -prune -o -type f -a -name \*.md -exec sed -i "s/\\bclient/${PACKAGE_NAME}.client/g" {} + + find "${OUTPUT_DIR}" -path "${OUTPUT_DIR}/base" -prune -o -type f -a -name \*.md -exec sed -i "s/${PACKAGE_NAME}.client-python/client-python/g" {} + + + # Remove circular imports in `v1beta1_json_schema_props.py`. + sed -i "/^from ${PACKAGE_NAME}\.models.*/d" "${PACKAGE_NAME}/${PACKAGE_NAME}/models/v1beta1_json_schema_props.py" +fi echo "---Done."